diff --git a/app/Http/Controllers/TestTakingController.php b/app/Http/Controllers/TestTakingController.php index 2dc8a94..065297a 100644 --- a/app/Http/Controllers/TestTakingController.php +++ b/app/Http/Controllers/TestTakingController.php @@ -2,7 +2,14 @@ namespace App\Http\Controllers; +use App\Models\AssignedTests; +use App\Models\MultipleChoiceAnswers; +use App\Models\Question; +use App\Models\SingleChoiceAnswer; +use App\Models\SingleQuestionAnswers; use App\Models\Test; +use App\Models\TestQuestionResult; +use App\Models\TestResult; use Illuminate\Http\Request; class TestTakingController extends Controller @@ -12,7 +19,18 @@ class TestTakingController extends Controller */ public function index(Request $request) { - return view("test-taking"); + $user = $request->user(); + + $assigned_tests = []; + foreach (AssignedTests::where("student_id", $user->id)->get() as $assigned_test) { + if ($assigned_test->allowed) { + array_push($assigned_tests, Test::where("id", $assigned_test->test_id)->first()); + } + } + + return view("take_tests.index", [ + "assigned_tests" => $assigned_tests + ]); } /** @@ -28,7 +46,78 @@ class TestTakingController extends Controller */ public function store(Request $request) { - // + $test_id = $request->test_id; + $answers = $request->answer; + $questions = Question::where("test_id", $test_id)->get(); + $total_points = 0; + $max_points = 0; + $question_results = []; + + foreach ($questions as $idx => $question) { + $points = 0; + $student_answer = ""; + if (!empty($answers[$idx])) { + $student_answer = $answers[$idx]; + if ($question->answer_type == "single_answer") { + $answer = SingleQuestionAnswers::where("question_id", $question->id)->first(); + if ($student_answer == $answer->answer) { + $points = $question->points; + } + } else if ($question->answer_type == "single_choice") { + $answer = SingleChoiceAnswer::where("question_id", $question->id)->first(); + if ($student_answer == $answer->correct_answer) { + $points = $question->points; + } + } else if ($question["answer_type"] == "multiple_choice") { + $answer = MultipleChoiceAnswers::where("question_id", $question->id)->first(); + $correct_answers = []; + + if ($answer->is_answer1_correct) { + array_push($correct_answers, $answer->answer1); + } + if ($answer->is_answer2_correct) { + array_push($correct_answers, $answer->answer2); + } + if ($answer->is_answer3_correct) { + array_push($correct_answers, $answer->answer3); + } + if ($answer->is_answer4_correct) { + array_push($correct_answers, $answer->answer4); + } + + $points = count(array_intersect($correct_answers, $student_answer)) / count($correct_answers); + $student_answer = join(", ", $student_answer); + } + } + + $max_points += $question->points; + $total_points += $points; + array_push($question_results, [ + "question_id" => $question->id, + "points" => $points, + "answer" => $student_answer + ]); + } + + $user = $request->user(); + + $test_result = TestResult::create([ + "student_id" => $user->id, + "test_id" => $test_id, + "points" => $total_points, + "max_points" => $max_points + ]); + + foreach ($question_results as $question_result) { + $question_result["test_results_id"] = $test_result->id; + TestQuestionResult::create($question_result); + } + + AssignedTests::where("test_id", $test_id) + ->where("student_id", $user->id) + ->update([ "allowed" => false ]); + + return view('dashboard'); } /** @@ -42,9 +131,44 @@ class TestTakingController extends Controller /** * Show the form for editing the specified resource. */ - public function edit(Test $test) + public function edit($test_id) { - // + + $question_answers = []; + $questions = Question::where("test_id", $test_id)->get(); + foreach ($questions as $question) { + $question_answer = $question->toArray(); + if ($question->answer_type == "single_answer") { + $answer = SingleQuestionAnswers::where("question_id", $question->id)->first(); + $question_answer["answer"] = $answer->answer; + } else if ($question->answer_type == "single_choice") { + $answer = SingleChoiceAnswer::where("question_id", $question->id)->first(); + $question_answer["answers"] = [$answer->correct_answer, $answer->incorrect_answer1]; + if ($answer->incorrect_answer2) { + array_push($question_answer["answers"], $answer->incorrect_answer2); + } + if ($answer->incorrect_answer3) { + array_push($question_answer["answers"], $answer->incorrect_answer3); + } + shuffle($question_answer["answers"]); + } else if ($question["answer_type"] == "multiple_choice") { + $answer = MultipleChoiceAnswers::where("question_id", $question->id)->first(); + $question_answer["answers"] = [$answer->answer1, $answer->answer2]; + if ($answer->answer3) { + array_push($question_answer["answers"], $answer->answer3); + } + if ($answer->answer4) { + array_push($question_answer["answers"], $answer->answer4); + } + shuffle($question_answer["answers"]); + } + array_push($question_answers, $question_answer); + } + + return view("take_tests.edit", [ + "test_id" => $test_id, + "questions" => $question_answers + ]); } /** diff --git a/app/Models/TestQuestionResult.php b/app/Models/TestQuestionResult.php index f13f448..5b23ec5 100644 --- a/app/Models/TestQuestionResult.php +++ b/app/Models/TestQuestionResult.php @@ -8,4 +8,11 @@ use Illuminate\Database\Eloquent\Model; class TestQuestionResult extends Model { use HasFactory; + + protected $fillable = [ + "test_results_id", + "question_id", + "points", + "answer" + ]; } diff --git a/app/Models/TestResult.php b/app/Models/TestResult.php index 3120a65..0b0d69f 100644 --- a/app/Models/TestResult.php +++ b/app/Models/TestResult.php @@ -8,4 +8,11 @@ use Illuminate\Database\Eloquent\Model; class TestResult extends Model { use HasFactory; + + protected $fillable = [ + "student_id", + "test_id", + "points", + "max_points" + ]; } diff --git a/database/migrations/2023_11_25_154609_create_test_results_table.php b/database/migrations/2023_11_25_154609_create_test_results_table.php index c05392e..8e4787c 100644 --- a/database/migrations/2023_11_25_154609_create_test_results_table.php +++ b/database/migrations/2023_11_25_154609_create_test_results_table.php @@ -16,6 +16,7 @@ return new class extends Migration $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->float("max_points")->default(0); $table->timestamps(); }); } 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 index 3317c4b..f5b6a57 100644 --- 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 @@ -17,6 +17,7 @@ return new class extends Migration $table->foreignId("question_id")->references("id")->on("questions")->constrained()->cascadeOnDelete(); $table->float("points")->default(0); $table->string("answer"); + $table->timestamps(); }); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 8527747..b05b4cb 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -4,11 +4,14 @@ namespace Database\Seeders; // use Illuminate\Database\Console\Seeds\WithoutModelEvents; +use App\Models\AssignedTests; use App\Models\MultipleChoiceAnswers; use App\Models\Question; use App\Models\SingleChoiceAnswer; use App\Models\SingleQuestionAnswers; use App\Models\Test; +use App\Models\TestQuestionResult; +use App\Models\TestResult; use App\Models\User; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Hash; @@ -32,7 +35,7 @@ class DatabaseSeeder extends Seeder 'password' => Hash::make("bob"), 'role' => "teacher" ]); - User::create([ + $alice = User::create([ 'name' => "Alice student", 'email' => "alice@student", 'password' => Hash::make("alice"), @@ -44,43 +47,71 @@ class DatabaseSeeder extends Seeder "user_id" => $teacher->id ]); + $question_1 = Question::create([ + "test_id" => $test->id, + "question" => "1+1=?", + "answer_type" => "single_answer", + ]); + SingleQuestionAnswers::create([ + "question_id" => $question_1->id, + "answer" => "2" + ]); + + $question_2 = Question::create([ + "test_id" => $test->id, + "question" => "Gyvenimo prasme", + "answer_type" => "single_choice", + ]); + SingleChoiceAnswer::create([ + "question_id" => $question_2->id, + "correct_answer" => "42", + "incorrect_answer1" => "Kates", + "incorrect_answer3" => "Sunys", + ]); + + $question_3 = Question::create([ + "test_id" => $test->id, + "question" => "Siandienos orai", + "answer_type" => "multiple_choice", + ]); + MultipleChoiceAnswers::create([ + "question_id" => $question_3->id, + "answer1" => "Sninga", "is_answer1_correct" => true, + "answer2" => "Lija", "is_answer2_correct" => true, + "answer4" => "Idk, paziurek per langa", "is_answer4_correct" => false, + ]); + { - $question = Question::create([ + AssignedTests::create([ + "student_id" => $alice->id, "test_id" => $test->id, - "question" => "1+1=?", - "answer_type" => "single_answer", + "allowed" => false ]); - SingleQuestionAnswers::create([ - "question_id" => $question->id, + + $test_result = TestResult::create([ + "student_id" => $alice->id, + "test_id" => $test->id, + "points" => 2.5, + "max_points" => 3 + ]); + + TestQuestionResult::create([ + "test_results_id" => $test_result->id, + "question_id" => $question_1->id, + "points" => 1, "answer" => "2" ]); - } - - { - $question = Question::create([ - "test_id" => $test->id, - "question" => "Gyvenimo prasme", - "answer_type" => "single_choice", + TestQuestionResult::create([ + "test_results_id" => $test_result->id, + "question_id" => $question_2->id, + "points" => 1, + "answer" => "42" ]); - SingleChoiceAnswer::create([ - "question_id" => $question->id, - "correct_answer" => "42", - "incorrect_answer1" => "Kates", - "incorrect_answer3" => "Sunys", - ]); - } - - { - $question = Question::create([ - "test_id" => $test->id, - "question" => "Siandienos orai", - "answer_type" => "multiple_choice", - ]); - MultipleChoiceAnswers::create([ - "question_id" => $question->id, - "answer1" => "Sninga", "is_answer1_correct" => true, - "answer2" => "Lija", "is_answer2_correct" => true, - "answer4" => "Idk, paziurek per langa", "is_answer4_correct" => false, + TestQuestionResult::create([ + "test_results_id" => $test_result->id, + "question_id" => $question_3->id, + "points" => 0.5, + "answer" => "Lija" ]); } diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 096e91f..667bc2b 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -26,7 +26,7 @@ @endif @if (Auth::user()->role == "student") - + {{ __('Take tests') }} @endif @@ -96,7 +96,7 @@ @endif @if (Auth::user()->role == "student") - + {{ __('Take tests') }} @endif diff --git a/resources/views/take-tests.blade.php b/resources/views/take-tests.blade.php deleted file mode 100644 index a6ee4b0..0000000 --- a/resources/views/take-tests.blade.php +++ /dev/null @@ -1,4 +0,0 @@ - -
-
-
diff --git a/resources/views/take_tests/edit.blade.php b/resources/views/take_tests/edit.blade.php new file mode 100644 index 0000000..7b2a77b --- /dev/null +++ b/resources/views/take_tests/edit.blade.php @@ -0,0 +1,36 @@ + +
+
+ @csrf + +
+ @foreach ($questions as $idx => $question) +
+
+ {{ __("Question:") }} + {{ $question["question"] }} +
+ {{ __("Answer:") }} + @if ($question["answer_type"] == "single_answer") + + @elseif ($question["answer_type"] == "single_choice") + + @foreach ($question["answers"] as $answer) + + @endforeach + + @elseif ($question["answer_type"] == "multiple_choice") + @foreach ($question["answers"] as $answer) +
+ + {{ $answer }} +
+ @endforeach + @endif +
+ @endforeach +
+ {{ __("End test") }} +
+
+
diff --git a/resources/views/take_tests/index.blade.php b/resources/views/take_tests/index.blade.php new file mode 100644 index 0000000..78b2f00 --- /dev/null +++ b/resources/views/take_tests/index.blade.php @@ -0,0 +1,11 @@ + +
+
+ @foreach ($assigned_tests as $test) +
+ {{ __("Do test") }} {{ $test->name }} +
+ @endforeach +
+
+ diff --git a/routes/web.php b/routes/web.php index 1cf1a5d..275d0e9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,7 +4,7 @@ use App\Http\Controllers\AssignedTestsController; use App\Http\Controllers\ProfileController; use App\Http\Controllers\QuestionController; use App\Http\Controllers\TestController; -use App\Http\Controllers\UserController; +use App\Http\Controllers\TestTakingController; use Illuminate\Support\Facades\Route; /* @@ -48,9 +48,13 @@ 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'); +Route::resource("take_tests", TestTakingController::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'); // Route::resource("test-taking", TestTakingController::class) // ->only(["index"])