From 8b1877d1aa42bc09d9c33d2e32fb5728d53acf13 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sat, 25 Nov 2023 20:25:21 +0200 Subject: [PATCH] add assigning of tests --- .../Controllers/AssignedTestsController.php | 93 ++++++++++ app/Http/Controllers/TestController.php | 20 +++ app/Models/AssignedTests.php | 16 ++ app/Models/TestQuestionResult.php | 11 ++ app/Models/TestResult.php | 11 ++ ...11_25_154609_create_test_results_table.php | 30 ++++ ...637_create_test_question_results_table.php | 30 ++++ ..._25_155528_create_assigned_tests_table.php | 30 ++++ resources/views/tests/edit.blade.php | 170 +++++++++++++----- routes/web.php | 5 + 10 files changed, 368 insertions(+), 48 deletions(-) create mode 100644 app/Http/Controllers/AssignedTestsController.php create mode 100644 app/Models/AssignedTests.php create mode 100644 app/Models/TestQuestionResult.php create mode 100644 app/Models/TestResult.php create mode 100644 database/migrations/2023_11_25_154609_create_test_results_table.php create mode 100644 database/migrations/2023_11_25_154637_create_test_question_results_table.php create mode 100644 database/migrations/2023_11_25_155528_create_assigned_tests_table.php diff --git a/app/Http/Controllers/AssignedTestsController.php b/app/Http/Controllers/AssignedTestsController.php new file mode 100644 index 0000000..e4c2bf0 --- /dev/null +++ b/app/Http/Controllers/AssignedTestsController.php @@ -0,0 +1,93 @@ +validate([ + "test_id" => "required|integer", + "student_id" => "required|integer" + ]); + + AssignedTests::create($validated); + + return redirect(route("tests.edit", [ + "test" => Test::where("id", $validated["test_id"])->first() + ])); + } + + /** + * Display the specified resource. + */ + public function show($assigned_student) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit($assigned_student) + { + $obj = AssignedTests::where("id", $assigned_student); + $obj->update([ + "allowed" => "1" + ]); + $test_id = $obj->first()->test_id; + + return redirect(route("tests.edit", [ + "test" => Test::where("id", $test_id)->first() + ])); + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, AssignedTests $assigned_student) + { + // $test->update($validated); + } + + /** + * Remove the specified resource from storage. + */ + public function destroy($assigned_student) + { + $obj = AssignedTests::where("id", $assigned_student); + $test_id = $obj->first()->test_id; + $obj->delete(); + + return redirect(route("tests.edit", [ + "test" => Test::where("id", $test_id)->first() + ])); + } +} diff --git a/app/Http/Controllers/TestController.php b/app/Http/Controllers/TestController.php index cc6c07f..2a3a380 100644 --- a/app/Http/Controllers/TestController.php +++ b/app/Http/Controllers/TestController.php @@ -2,10 +2,12 @@ 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 @@ -83,9 +85,27 @@ class TestController extends Controller } } + $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, ]); } diff --git a/app/Models/AssignedTests.php b/app/Models/AssignedTests.php new file mode 100644 index 0000000..8be3ec0 --- /dev/null +++ b/app/Models/AssignedTests.php @@ -0,0 +1,16 @@ +id(); + $table->foreignId("student_id")->references("id")->on("users")->constrained()->cascadeOnDelete(); + $table->foreignId("test_id")->references("id")->on("tests")->constrained()->cascadeOnDelete(); + $table->float("points")->default(0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('test_results'); + } +}; diff --git a/database/migrations/2023_11_25_154637_create_test_question_results_table.php b/database/migrations/2023_11_25_154637_create_test_question_results_table.php new file mode 100644 index 0000000..3317c4b --- /dev/null +++ b/database/migrations/2023_11_25_154637_create_test_question_results_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId("test_results_id")->references("id")->on("test_results")->constrained()->cascadeOnDelete(); + $table->foreignId("question_id")->references("id")->on("questions")->constrained()->cascadeOnDelete(); + $table->float("points")->default(0); + $table->string("answer"); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('test_question_results'); + } +}; diff --git a/database/migrations/2023_11_25_155528_create_assigned_tests_table.php b/database/migrations/2023_11_25_155528_create_assigned_tests_table.php new file mode 100644 index 0000000..4828228 --- /dev/null +++ b/database/migrations/2023_11_25_155528_create_assigned_tests_table.php @@ -0,0 +1,30 @@ +id(); + $table->foreignId("student_id")->references("id")->on("users")->constrained()->cascadeOnDelete(); + $table->foreignId("test_id")->references("id")->on("tests")->constrained()->cascadeOnDelete(); + $table->boolean("allowed")->default(true); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('assigned_tests'); + } +}; diff --git a/resources/views/tests/edit.blade.php b/resources/views/tests/edit.blade.php index 5d0c92d..8f802d4 100644 --- a/resources/views/tests/edit.blade.php +++ b/resources/views/tests/edit.blade.php @@ -16,58 +16,132 @@
+
+

{{ __("Questions") }}

-
- @csrf -
- -
+ + @csrf +
+ +
-
- {{ __("Add question") }} -
-
+
+ {{ __("Add question") }} +
+ - - - - - - - +
{{ __("Question") }}{{ __("Answer") }}{{ __("Points") }}
+ + + + + + - @foreach ($questions as $question) - - - - - + + + + - - @endforeach -
{{ __("Question") }}{{ __("Answer") }}{{ __("Points") }}
{{ $question->question }}{{ $question->answer }}{{ $question->points }} - - - - - - - {{ __("Edit") }} - -
- @csrf - @method("delete") - - {{ __("Delete") }} + @foreach ($questions as $question) +
{{ $question->question }}{{ $question->answer }}{{ $question->points }} + + + + + + + {{ __("Edit") }} - - - -
+
+ @csrf + @method("delete") + + {{ __("Delete") }} + +
+ + + + + @endforeach + +
+ +
+
+

{{ __("Assigned students") }}

+ +
+ @csrf +
+ + + {{ __("Student") }} + + @if (sizeof($students) == 0) + + @endif + @foreach ($students as $student) + + @endforeach + +
+ +
+ {{ __("Add student") }} +
+
+ + + + + + + + + @foreach ($assigned_students as $student) + + + + + + @endforeach +
{{ __("Student") }}{{ __("Can take test") }}
{{ $student->name }}{{ __($student->allowed ? "Yes" : "No") }} + + + + + + @if (!$student->allowed) + + {{ __("Allow to take test") }} + + @endif +
+ @csrf + @method("delete") + + {{ __("Delete") }} + +
+
+
+
+
+ +
+
+

{{ __("Test results") }}

+
diff --git a/routes/web.php b/routes/web.php index 128c2c2..1cf1a5d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,5 +1,6 @@ only(["index", "store", "edit", "update", "destroy"]) ->middleware(["auth", "verified"]); +Route::resource("assigned_tests", AssignedTestsController::class) + ->only(["index", "store", "edit", "update", "destroy"]) + ->middleware(["auth", "verified"]); + Route::get('/take-tests', function () { return view('take-tests'); })->middleware(['auth', 'verified'])->name('take-tests');