206 lines
6.2 KiB
PHP
206 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Http\Requests;
|
|
use App\Test;
|
|
use App\Testdetail;
|
|
use App\Question;
|
|
use App\Option;
|
|
use App\Group;
|
|
|
|
class TestController extends Controller
|
|
{
|
|
public function startTest(Test $test)
|
|
{
|
|
if (Auth::user()->testTaken(2)) {
|
|
//insert session flash
|
|
return redirect('/home');
|
|
}
|
|
session(['questions' => $test->randomizeQuestions(), 'question_counter' => 0, 'test' => $test, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false]);
|
|
return redirect()->action('TestController@showQuestion');
|
|
}
|
|
|
|
public function showQuestion()
|
|
{
|
|
session(['question_counter' => session('question_counter')+1]);
|
|
$question = session('questions')->get(session('question_counter')-1);
|
|
$options = $question->options->shuffle();
|
|
session(['options' => $options]);
|
|
return view('tests.index', compact('question'), compact('options'));
|
|
}
|
|
|
|
public function answerQuestion()
|
|
{
|
|
$question = session('questions')->get(session('question_counter')-1);
|
|
$question_id = $question->id;
|
|
$answer_id = request()->get('answer');
|
|
if (!empty(session('answers'))) {
|
|
$answers = session('answers');
|
|
}
|
|
$answers["$question_id"] = $answer_id;
|
|
session(['answers' => $answers]);
|
|
|
|
if (!$question->isCorrect(session('answers')["$question->id"])) {
|
|
session(['wrong_answers' => session('wrong_answers')+1]);
|
|
session(['is_correct' => false]);
|
|
} else {
|
|
session(['is_correct' => true]);
|
|
}
|
|
|
|
if (session('test')->hasFailed(session('wrong_answers'))) {
|
|
session(['has_failed' => true]);
|
|
}
|
|
|
|
if (session('test')->lastQuestion(session('question_counter'))) {
|
|
session(['last_question' => true]);
|
|
}
|
|
return redirect()->action('TestController@showAnswer');
|
|
}
|
|
|
|
public function showAnswer()
|
|
{
|
|
$question = session('questions')->get(session('question_counter')-1);
|
|
$options = session('options');
|
|
return view('tests.answer', compact("question"), compact("options"));
|
|
}
|
|
|
|
public function testRetry(Test $test)
|
|
{
|
|
Auth::user()->storeTestdetails($test, session("has_failed"));
|
|
$test->sessionPurge();
|
|
return redirect("/test/$test->id");
|
|
}
|
|
|
|
public function testEnd(Test $test)
|
|
{
|
|
Auth::user()->storeTestdetails($test, session("has_failed"));
|
|
$test->sessionPurge();
|
|
return redirect("/home");
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Common controller functions between moderators and administrators for handling tests and associated questions
|
|
*
|
|
*/
|
|
|
|
public function addTest()
|
|
{
|
|
$test = new Test();
|
|
$test->createTest(request()->all());
|
|
return redirect('/admin/tests');
|
|
}
|
|
|
|
public function confirmDeleteTest(Test $test)
|
|
{
|
|
return view('tests.delete', compact('test'));
|
|
}
|
|
|
|
public function confirmDeleteQuestion(Question $question)
|
|
{
|
|
return view('tests.question.delete', compact('question'));
|
|
}
|
|
|
|
public function deleteQuestion(Question $question)
|
|
{
|
|
$test = $question->test;
|
|
$question->deleteQuestion();
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
public function deleteTest(Test $test)
|
|
{
|
|
$test->deleteTest();
|
|
return redirect('/admin/tests');
|
|
}
|
|
|
|
public function editQuestion(Question $question)
|
|
{
|
|
$options = $question->options;
|
|
return view('tests.question.edit', compact('question'), compact('options'));
|
|
}
|
|
|
|
public function newQuestion(Test $test)
|
|
{
|
|
$question_number = $test->nextQuestionNumber();
|
|
return view('tests.question.new', compact('test'), compact('question_number'));
|
|
}
|
|
|
|
public function updateTest(Test $test)
|
|
{
|
|
$test->updateTest(request()->all());
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
public function showTest(Test $test)
|
|
{
|
|
$questions = $test->questions;
|
|
return view('tests.show', compact('test'), compact('questions'));
|
|
}
|
|
|
|
public function newTest()
|
|
{
|
|
if (Auth::user()->isAdministrator()) {
|
|
$groups = Group::all();
|
|
return view('tests.new', compact('groups'));
|
|
}
|
|
return view('tests.new');
|
|
}
|
|
|
|
public function editTest(Test $test)
|
|
{
|
|
if (Auth::user()->isAdministrator()) {
|
|
$groups = Group::all();
|
|
return view('tests.edit', compact('test'), compact('groups'));
|
|
}
|
|
return view('tests.edit', compact('test'));
|
|
}
|
|
|
|
/* REFACTOR */
|
|
public function addQuestion(Test $test)
|
|
{
|
|
$question = new Question;
|
|
$question->title = request()->title;
|
|
$question->question = request()->question;
|
|
if (empty(Option::all()->last()->id)) {
|
|
$question->answer_id = 1;
|
|
} else {
|
|
$question->answer_id = Option::all()->last()->id+1;
|
|
}
|
|
$test->questions()->save($question);
|
|
$option1 = new Option;
|
|
$option1->option = request()->option1;
|
|
$option2 = new Option;
|
|
$option2->option = request()->option2;
|
|
$option3 = new Option;
|
|
$option3->option = request()->option3;
|
|
$option4 = new Option;
|
|
$option4->option = request()->option4;
|
|
$question->options()->save($option1);
|
|
$question->options()->save($option2);
|
|
$question->options()->save($option3);
|
|
$question->options()->save($option4);
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
public function updateQuestion(Question $question)
|
|
{
|
|
$test = $question->test;
|
|
$question->question = request()->question;
|
|
$options = $question->options;
|
|
$options[0]->option = request()->option1;
|
|
$options[1]->option = request()->option2;
|
|
$options[2]->option = request()->option3;
|
|
$options[3]->option = request()->option4;
|
|
$question->update();
|
|
$options[0]->update();
|
|
$options[1]->update();
|
|
$options[2]->update();
|
|
$options[3]->update();
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
}
|