2016-09-01 11:37:49 +02:00
< ? php
namespace App\Http\Controllers ;
use Illuminate\Http\Request ;
2016-09-10 18:08:33 +02:00
use Illuminate\Support\Facades\Auth ;
2016-09-01 11:37:49 +02:00
use App\Http\Requests ;
use App\Test ;
2016-09-12 14:51:03 +02:00
use App\Testdetail ;
2016-09-16 17:37:31 +02:00
use App\Question ;
use App\Option ;
2016-09-21 12:39:29 +02:00
use App\Group ;
2016-10-22 13:57:56 +02:00
use App\Http\Requests\StoreTest ;
2016-10-22 17:22:26 +02:00
use App\Http\Requests\StoreQuestion ;
2016-09-01 11:37:49 +02:00
class TestController extends Controller
{
2016-10-25 20:30:05 +02:00
public function __construct ()
{
$this -> middleware ( 'auth' );
}
2016-11-01 13:03:39 +01:00
public function startTest ( Test $test , Request $request )
2016-09-01 11:37:49 +02:00
{
2016-11-01 13:03:39 +01:00
if ( null !== session ( " start_time " ) && session ( " time_limit " ) !== 0 && session ( " start_time " ) + session ( " time_limit " ) > time () && $test -> id === session ( 'test' ) -> id ) {
2016-09-26 17:34:05 +02:00
return redirect () -> action ( 'TestController@showQuestion' );
}
2016-09-22 15:57:39 +02:00
if ( Auth :: user () -> testTaken ( $test -> id )) {
2016-11-01 13:03:39 +01:00
$request -> session () -> flash ( 'status_failed' , 'You have already taken this test!' );
2016-09-10 15:17:31 +02:00
return redirect ( '/home' );
}
2016-09-28 11:28:12 +02:00
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 ]);
2016-09-07 18:26:58 +02:00
return redirect () -> action ( 'TestController@showQuestion' );
2016-09-01 11:37:49 +02:00
}
2016-09-07 18:26:58 +02:00
public function showQuestion ()
2016-09-01 11:37:49 +02:00
{
2016-09-28 11:28:12 +02:00
if ( null === session ( " start_time " ) || session ( " time_limit " ) !== 0 && session ( " start_time " ) + session ( " time_limit " ) < time ()) {
return redirect () -> action ( 'HomeController@index' );
}
2016-09-10 15:17:31 +02:00
$question = session ( 'questions' ) -> get ( session ( 'question_counter' ) - 1 );
$options = $question -> options -> shuffle ();
session ([ 'options' => $options ]);
return view ( 'tests.index' , compact ( 'question' ), compact ( 'options' ));
2016-09-07 18:26:58 +02:00
}
2016-10-22 17:22:26 +02:00
public function answerQuestion ( Request $request )
2016-09-07 18:26:58 +02:00
{
2016-09-10 15:17:31 +02:00
$question = session ( 'questions' ) -> get ( session ( 'question_counter' ) - 1 );
2016-09-22 15:57:39 +02:00
$options = session ( 'options' );
2016-09-29 20:53:57 +02:00
$answers = collect ([]);
2016-09-22 15:57:39 +02:00
foreach ( $options as $option ) {
2016-10-22 17:22:26 +02:00
if ( array_key_exists ( " answer { $option -> id } " , $request -> all ())) {
2016-09-29 20:53:57 +02:00
$answers -> push ( $option );
2016-09-22 15:57:39 +02:00
}
}
2016-09-09 12:19:49 +02:00
2016-09-22 15:57:39 +02:00
if ( $question -> multiple_answers_question ) {
$correct_answers = $question -> correct_answers ;
$user_answers_correct = 0 ;
foreach ( $answers as $answer ) {
if ( $answer -> correct_answer == 1 ) {
$user_answers_correct ++ ;
}
}
if ( $user_answers_correct == $correct_answers ) {
session ([ 'is_correct' => true ]);
2016-09-28 11:28:12 +02:00
session ([ 'correct_answers' => session ( 'correct_answers' ) + 1 ]);
2016-09-22 15:57:39 +02:00
} else {
session ([ 'is_correct' => false ]);
2016-09-28 11:28:12 +02:00
session ([ 'wrong_answers' => session ( 'wrong_answers' ) + 1 ]);
2016-09-22 15:57:39 +02:00
}
}
if ( ! $question -> multiple_answers_question ) {
if ( $answers [ 0 ] -> correct_answer ) {
session ([ 'is_correct' => true ]);
2016-09-28 11:28:12 +02:00
session ([ 'correct_answers' => session ( 'correct_answers' ) + 1 ]);
2016-09-22 15:57:39 +02:00
} else {
session ([ 'is_correct' => false ]);
2016-09-28 11:28:12 +02:00
session ([ 'wrong_answers' => session ( 'wrong_answers' ) + 1 ]);
2016-09-22 15:57:39 +02:00
}
2016-09-10 15:17:31 +02:00
}
2016-09-09 12:19:49 +02:00
2016-09-10 18:08:33 +02:00
if ( session ( 'test' ) -> lastQuestion ( session ( 'question_counter' ))) {
session ([ 'last_question' => true ]);
}
2016-09-28 11:28:12 +02:00
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 ]);
}
2016-09-29 20:53:57 +02:00
session ([ 'answers' => $answers ]);
2016-09-24 22:20:17 +02:00
session ([ 'question_counter' => session ( 'question_counter' ) + 1 ]);
2016-09-10 15:17:31 +02:00
return redirect () -> action ( 'TestController@showAnswer' );
2016-09-07 18:26:58 +02:00
}
public function showAnswer ()
{
2016-09-28 11:28:12 +02:00
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' );
}
}
2016-09-24 22:20:17 +02:00
$question = session ( 'questions' ) -> get ( session ( 'question_counter' ) - 2 );
2016-09-10 18:08:33 +02:00
$options = session ( 'options' );
2016-09-29 20:53:57 +02:00
$answers = session ( 'answers' );
return view ( 'tests.answer' , compact ( " question " ), compact ([ " options " , " answers " ]));
2016-09-01 11:37:49 +02:00
}
2016-09-12 14:51:03 +02:00
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 " );
}
2016-09-01 11:37:49 +02:00
}