150 lines
6.4 KiB
PHP
150 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Kreait\Firebase\Factory;
|
|
use Response;
|
|
use App\Http\Requests\LambdaRequest;
|
|
|
|
class QuizController extends Controller
|
|
{
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
|
|
protected $_options;
|
|
protected $_corrects;
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'form';
|
|
$this->_module = 'quiz';
|
|
$this->_viewname = 'quiz';
|
|
$this->_title = 'Quiz';
|
|
$this->_base_url = '/quiz';
|
|
}
|
|
|
|
protected function get_list_fields(){
|
|
return [
|
|
'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete', 'submission' => ['icon' => 'form-select', 'querystring' => 'id={ID}'] ] ],
|
|
'name' => ['title' => 'Name', 'type' => 'text'],
|
|
'show_score' => ['title' => 'Show Score', 'type' => 'text'],
|
|
'show_answers' => ['title' => 'Show Answers', 'type' => 'text'],
|
|
'number_attempts' => ['title' => 'No. of Attempts', 'type' => 'text'],
|
|
'start_time' => ['title' => 'Start Time', 'type' => 'text'],
|
|
'end_time' => ['title' => 'End Time', 'type' => 'text'],
|
|
'status' => ['title' => 'Status', 'type' => 'select'],
|
|
];
|
|
}
|
|
|
|
|
|
protected function get_form_fields($type, $values){
|
|
$metrics = $this->getlambda('get', 'metric', 'metric', 'get_all');
|
|
$metric_dd = [];
|
|
foreach($metrics->data->items as $metric){
|
|
$metric_dd[$metric->id] = $metric->name;
|
|
}
|
|
|
|
return [
|
|
'name' => ['title' => 'Name', 'type' => 'text', 'required' => true, 'fullwidth' => true],
|
|
'show_score' => ['title' => 'Show Score', 'type' => 'select', 'required' => true, 'values' => ['Yes' => 'Yes', 'No' => 'No' ]],
|
|
'show_answers' => ['title' => 'Show Answers', 'type' => 'select', 'required' => true, 'values' => ['Yes' => 'Yes', 'No' => 'No' ]],
|
|
'limit_attempts' => ['title' => 'Limit Attempts', 'type' => 'select', 'required' => true, 'values' => ['Yes' => 'Yes', 'No' => 'No' ]],
|
|
'number_attempts' => ['title' => 'No. of attempts', 'type' => 'number', 'required' => true],
|
|
'start_time' => ['title' => 'Start Time', 'type' => 'datetime', 'required' => true],
|
|
'end_time' => ['title' => 'End Time', 'type' => 'datetime', 'required' => true],
|
|
'quiz_type' => ['title' => 'Quiz Type', 'type' => 'select', 'required' => true, 'values' => ['quiz' => 'Quiz', 'poll' => 'Poll']],
|
|
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
|
|
'score_metric' => ['title' => 'Score Metric', 'type' => 'select', 'values' => $metric_dd, 'required' => true],
|
|
];
|
|
}
|
|
|
|
public function form($type, $id = null) {
|
|
$values = [];
|
|
$questions = [];
|
|
if($type == 'edit') {
|
|
$this->_request->session()->put('current_quiz', $id);
|
|
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_one', ['id' => $id]);
|
|
$values = $response->data->item;
|
|
|
|
$questions = LambdaRequest::signedrequest('get', $this->_service, 'quizquestion/get_all', ['entity_id' => $id]);
|
|
$questions = $questions->data->items;
|
|
}
|
|
$data = $this->_beforeform($type, $values);
|
|
$fields = $this->get_form_fields($type, $values);
|
|
|
|
return $this->view('form')->with([
|
|
'title' => $this->_title,
|
|
'mode' => $type,
|
|
'fields' => $fields,
|
|
'base_url' => $this->_base_url,
|
|
'item' => $values,
|
|
'questions' => $questions
|
|
]);
|
|
}
|
|
|
|
public function addquestion($request) {
|
|
$post = $request->input();
|
|
$quiz_id = $request->session()->get('current_quiz');
|
|
return $this->view('question-form')->with('quiz_id', $quiz_id)->with('edit', false);
|
|
}
|
|
public function editquestion($request) {
|
|
$post = $request->input();
|
|
$quiz_id = $request->session()->get('current_quiz');
|
|
$question = LambdaRequest::signedrequest('get', $this->_service, 'quizquestion/get_one', ['id' => $post['id']]);
|
|
$question = $question->data->item;
|
|
return $this->view('question-form')->with('quiz_id', $quiz_id)->with('edit', true)->with('question', $question);
|
|
}
|
|
|
|
public function postaddquestion($request) {
|
|
$post = $request->input();
|
|
$quiz_id = $request->session()->get('current_quiz');
|
|
$options = [];
|
|
foreach ($post['options'] as $index => $value) {
|
|
$options[] = ['option' => $value, 'is_correct' => ($index == $post['correct_ans']) ];
|
|
}
|
|
LambdaRequest::signedrequest('post', $this->_service, 'quizquestion/create', ['entity_id' => $quiz_id, 'question' => $post['question'], 'options' => $options]);
|
|
$message = 'Question added successfully!';
|
|
return redirect('/quiz/edit?id='.$quiz_id)->with('message', $message);
|
|
}
|
|
|
|
public function posteditquestion($request) {
|
|
$post = $request->input();
|
|
//echo '<pre>'; print_r($post); exit;
|
|
$quiz_id = $request->session()->get('current_quiz');
|
|
$options = [];
|
|
foreach ($post['options'] as $index => $value) {
|
|
$options[] = ['option' => $value, 'is_correct' => ($index == $post['correct_ans']) ];
|
|
}
|
|
LambdaRequest::signedrequest('post', $this->_service, 'quizquestion/update', ['id' => $post['id'], 'entity_id' => $quiz_id, 'question' => $post['question'], 'options' => $options]);
|
|
$message = 'Question added successfully!';
|
|
return redirect('/quiz/edit?id='.$quiz_id)->with('message', $message);
|
|
}
|
|
|
|
|
|
public function questions(Request $request) {
|
|
$entity_id = $request->input('id', 0);
|
|
if($entity_id === 0) {
|
|
return redirect('/quizs')->withError('Quiz not found!');
|
|
} else {
|
|
$request->session()->put('entity_id', $entity_id);
|
|
return redirect('/quizquestions');
|
|
}
|
|
}
|
|
|
|
public function submission(Request $request) {
|
|
$entity_id = $request->input('id', 0);
|
|
if($entity_id === 0) {
|
|
return redirect('/quizs')->withError('Quiz not found!');
|
|
} else {
|
|
$request->session()->put('entity_id', $entity_id);
|
|
return redirect('/quizsubmissions');
|
|
}
|
|
}
|
|
} |