121 lines
3.3 KiB
PHP
121 lines
3.3 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\Question;
|
|
use App\Option;
|
|
use App\Group;
|
|
use App\Http\Requests\StoreTest;
|
|
use App\Http\Requests\StoreQuestion;
|
|
|
|
class AdministrativeTestController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* 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");
|
|
}
|
|
}
|