81 lines
2.5 KiB
PHP
81 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests;
|
|
use App\Test;
|
|
|
|
class TestController extends Controller
|
|
{
|
|
public function startTest(Test $test)
|
|
{
|
|
if (!$test->startTest(\Auth::user())) {
|
|
//session flash
|
|
return redirect('/home');
|
|
}
|
|
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]);
|
|
return redirect()->action('TestController@isCorrect');
|
|
}
|
|
|
|
public function isCorrect()
|
|
{
|
|
$question = session('questions')->get(session('question_counter')-1);
|
|
if (!$question->isCorrect(session('answers')["$question->id"])) {
|
|
session(['wrong_answers' => session('wrong_answers')+1]);
|
|
session(['is_correct' => false]);
|
|
} else {
|
|
session(['is_correct' => true]);
|
|
}
|
|
return redirect()->action('TestController@hasFailed');
|
|
}
|
|
|
|
public function hasFailed()
|
|
{
|
|
if (session('test')->hasFailed(session('wrong_answers'))) {
|
|
//Abort logic
|
|
return redirect('/home');
|
|
}
|
|
return redirect()->action('TestController@lastQuestion');
|
|
}
|
|
|
|
public function lastQuestion()
|
|
{
|
|
/*if (session('test')->lastQuestion(session('question_counter'))) {
|
|
|
|
}*/
|
|
return redirect()->action('TestController@showAnswer');
|
|
}
|
|
|
|
public function showAnswer()
|
|
{
|
|
$question = session('questions')->get(session('question_counter')-1);
|
|
if (session('answers')["$question->id"] == $question->answer_id) {
|
|
$question_status = "GZ, that was the correct answer!";
|
|
} else {
|
|
$question_status = "Sorry, wrong answer, pal!";
|
|
}
|
|
session(['question_counter' => session('question_counter')+1]);
|
|
return view('tests.answer');
|
|
}
|
|
}
|