228 lines
7.4 KiB
PHP
228 lines
7.4 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;
|
|
use App\Http\Requests\StoreTest;
|
|
use App\Http\Requests\StoreQuestion;
|
|
|
|
class TestController extends Controller
|
|
{
|
|
public function startTest(Test $test)
|
|
{
|
|
if (null !== session("start_time") && session("time_limit") !== 0 && session("start_time")+session("time_limit") > time()) {
|
|
return redirect()->action('TestController@showQuestion');
|
|
}
|
|
|
|
if (Auth::user()->testTaken($test->id)) {
|
|
//insert session flash
|
|
return redirect('/home');
|
|
}
|
|
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]);
|
|
return view('tests.index', compact('question'), compact('options'));
|
|
}
|
|
|
|
public function answerQuestion(Request $request)
|
|
{
|
|
$question = session('questions')->get(session('question_counter')-1);
|
|
$options = session('options');
|
|
$answers = collect([]);
|
|
foreach ($options as $option) {
|
|
if (array_key_exists("answer{$option->id}", $request->all())) {
|
|
$answers->push($option);
|
|
}
|
|
}
|
|
|
|
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]);
|
|
session(['correct_answers' => session('correct_answers')+1]);
|
|
} else {
|
|
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(['is_correct' => false]);
|
|
session(['wrong_answers' => session('wrong_answers')+1]);
|
|
}
|
|
}
|
|
|
|
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(['answers' => $answers]);
|
|
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');
|
|
$answers = session('answers');
|
|
return view('tests.answer', compact("question"), compact(["options", "answers"]));
|
|
}
|
|
|
|
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(StoreTest $request)
|
|
{
|
|
$test = new Test();
|
|
$test->createTest($request->all());
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
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, StoreTest $request)
|
|
{
|
|
$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'));
|
|
}
|
|
|
|
public function addQuestion(Test $test, StoreQuestion $request)
|
|
{
|
|
$question = new Question;
|
|
$question->addQuestion($test, $request);
|
|
foreach ($request["options"] as $optionData) {
|
|
$option = new Option;
|
|
$option->addOption($question, $optionData);
|
|
}
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
public function updateQuestion(Question $question, StoreQuestion $request)
|
|
{
|
|
$test = $question->test;
|
|
$question->updateQuestion($request);
|
|
$options = $question->options;
|
|
$optionsData = $request["options"];
|
|
foreach ($options as $key => $option) {
|
|
$option->updateOption($optionsData[$key+1]);
|
|
}
|
|
return redirect("/admin/tests/$test->id");
|
|
}
|
|
|
|
}
|