65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Http\Requests;
|
|
use App\Test;
|
|
|
|
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' => 1, 'test' => $test, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false]);
|
|
return redirect()->action('TestController@showQuestion');
|
|
}
|
|
|
|
public function showQuestion()
|
|
{
|
|
$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');
|
|
session(['question_counter' => session('question_counter')+1]);
|
|
return view('tests.answer', compact("question"), compact("options"));
|
|
}
|
|
}
|