116 lines
3.1 KiB
PHP
116 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\MultipleChoiceAnswers;
|
|
use App\Models\SingleChoiceAnswer;
|
|
use App\Models\SingleQuestionAnswers;
|
|
use App\Models\Test;
|
|
use Illuminate\Http\Request;
|
|
|
|
class TestController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$tests = $request->user()->tests()->get();
|
|
return view("tests.index", [
|
|
"tests" => $tests
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
"name" => "required|string"
|
|
]);
|
|
|
|
$request->user()->tests()->create($validated);
|
|
|
|
return redirect(route("tests.index"));
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Test $test)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Test $test)
|
|
{
|
|
$questions = $test->questions()->get();
|
|
foreach ($questions as $question) {
|
|
if ($question->answer_type == "single_answer") {
|
|
$answer = SingleQuestionAnswers::where("question_id", $question->id)->first();
|
|
$question->answer = $answer->answer;
|
|
} else if ($question->answer_type == "single_choice") {
|
|
$answer = SingleChoiceAnswer::where("question_id", $question->id)->first();
|
|
$question->answer = $answer->correct_answer;
|
|
} else if ($question->answer_type == "multiple_choice") {
|
|
$answer = MultipleChoiceAnswers::where("question_id", $question->id)->first();
|
|
$valid_answer = array();
|
|
if ($answer->is_answer1_correct) {
|
|
array_push($valid_answer, $answer->answer1);
|
|
}
|
|
if ($answer->is_answer2_correct) {
|
|
array_push($valid_answer, $answer->answer2);
|
|
}
|
|
if ($answer->is_answer3_correct) {
|
|
array_push($valid_answer, $answer->answer3);
|
|
}
|
|
if ($answer->is_answer4_correct) {
|
|
array_push($valid_answer, $answer->answer4);
|
|
}
|
|
$question->answer = join(", ", $valid_answer);
|
|
}
|
|
}
|
|
|
|
return view("tests.edit", [
|
|
"test" => $test,
|
|
"questions" => $questions,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Test $test)
|
|
{
|
|
$validated = $request->validate([
|
|
"name" => "required|string"
|
|
]);
|
|
|
|
$test->update($validated);
|
|
|
|
return redirect(route("tests.index"));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Test $test)
|
|
{
|
|
$test->delete();
|
|
|
|
return redirect(route("tests.index"));
|
|
}
|
|
}
|