added full time limit functionality for tests
This commit is contained in:
parent
40aa77386b
commit
7d160134b8
7 changed files with 61 additions and 19 deletions
app/Http/Controllers
|
@ -15,7 +15,7 @@ class TestController extends Controller
|
|||
{
|
||||
public function startTest(Test $test)
|
||||
{
|
||||
if (null !== session("start_time") && session("start_time")+3600 > time()) {
|
||||
if (null !== session("start_time") && session("time_limit") !== 0 && session("start_time")+session("time_limit") > time()) {
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
|
@ -23,12 +23,16 @@ class TestController extends Controller
|
|||
//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, 'start_time' => time()]);
|
||||
session(['questions' => $test->randomizeQuestions(), 'question_counter' => 1, 'test' => $test, 'correct_answers' => 0, 'wrong_answers' => 0, 'is_correct' => false, 'has_failed' => false, 'last_question' => false, 'start_time' => time(), 'time_limit' => $test->time_limit]);
|
||||
return redirect()->action('TestController@showQuestion');
|
||||
}
|
||||
|
||||
public function showQuestion()
|
||||
{
|
||||
if (null === session("start_time") || session("time_limit") !== 0 && session("start_time")+session("time_limit") < time()) {
|
||||
return redirect()->action('HomeController@index');
|
||||
}
|
||||
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
$options = $question->options->shuffle();
|
||||
session(['options' => $options]);
|
||||
|
@ -56,34 +60,47 @@ class TestController extends Controller
|
|||
}
|
||||
if ($user_answers_correct == $correct_answers) {
|
||||
session(['is_correct' => true]);
|
||||
session(['correct_answers' => session('correct_answers')+1]);
|
||||
} else {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$question->multiple_answers_question) {
|
||||
if ($answers[0]->correct_answer) {
|
||||
session(['is_correct' => true]);
|
||||
session(['correct_answers' => session('correct_answers')+1]);
|
||||
} else {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (session('test')->hasFailed(session('wrong_answers'))) {
|
||||
session(['has_failed' => true]);
|
||||
}
|
||||
|
||||
if (session('test')->lastQuestion(session('question_counter'))) {
|
||||
session(['last_question' => true]);
|
||||
}
|
||||
|
||||
if (session('test')->timePassed(session('start_time'))) {
|
||||
session(['time_passed' => true]);
|
||||
session(["wrong_answers" => session("test")->question_count-session("correct_answers")]);
|
||||
}
|
||||
|
||||
if (session('test')->hasFailed(session('wrong_answers'))) {
|
||||
session(['has_failed' => true]);
|
||||
}
|
||||
session(['question_counter' => session('question_counter')+1]);
|
||||
return redirect()->action('TestController@showAnswer');
|
||||
}
|
||||
|
||||
public function showAnswer()
|
||||
{
|
||||
if (!session("time_passed")) {
|
||||
if (null === session("start_time") || session("time_limit") !== 0 && session("start_time")+session("time_limit") < time()) {
|
||||
return redirect()->action('HomeController@index');
|
||||
}
|
||||
}
|
||||
|
||||
$question = session('questions')->get(session('question_counter')-2);
|
||||
$options = session('options');
|
||||
return view('tests.answer', compact("question"), compact("options"));
|
||||
|
|
Reference in a new issue