136 lines
3.8 KiB
PHP
136 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\AssignedTests;
|
|
use App\Models\MultipleChoiceAnswers;
|
|
use App\Models\SingleChoiceAnswer;
|
|
use App\Models\SingleQuestionAnswers;
|
|
use App\Models\Test;
|
|
use App\Models\User;
|
|
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);
|
|
}
|
|
}
|
|
|
|
$students = User::where("role", "student")->get()->toArray();
|
|
|
|
$assigned_students = [];
|
|
foreach (AssignedTests::where("test_id", $test->id)->get() as $assigned_student) {
|
|
$backing_student = User::where("id", $assigned_student->student_id)->first();
|
|
$assigned_student->name = $backing_student->name;
|
|
|
|
foreach ($students as $idx => $student) {
|
|
if ($student["id"] == $assigned_student->student_id) {
|
|
unset($students[$idx]);
|
|
}
|
|
}
|
|
|
|
array_push($assigned_students, $assigned_student);
|
|
}
|
|
|
|
return view("tests.edit", [
|
|
"test" => $test,
|
|
"questions" => $questions,
|
|
"students" => $students,
|
|
"assigned_students" => $assigned_students,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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"));
|
|
}
|
|
}
|