1
0

add taking of tests by students

This commit is contained in:
Rokas Puzonas 2023-11-25 23:41:17 +02:00
parent 8b1877d1aa
commit 2ad0115405
11 changed files with 264 additions and 46 deletions

View File

@ -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
]);
}
/**

View File

@ -8,4 +8,11 @@ use Illuminate\Database\Eloquent\Model;
class TestQuestionResult extends Model
{
use HasFactory;
protected $fillable = [
"test_results_id",
"question_id",
"points",
"answer"
];
}

View File

@ -8,4 +8,11 @@ use Illuminate\Database\Eloquent\Model;
class TestResult extends Model
{
use HasFactory;
protected $fillable = [
"student_id",
"test_id",
"points",
"max_points"
];
}

View File

@ -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();
});
}

View File

@ -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();
});
}

View File

@ -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"
]);
}

View File

@ -26,7 +26,7 @@
</x-nav-link>
@endif
@if (Auth::user()->role == "student")
<x-nav-link :href="route('take-tests')" :active="request()->routeIs('take-tests')">
<x-nav-link :href="route('take_tests.index')" :active="request()->routeIs('take_tests.index')">
{{ __('Take tests') }}
</x-nav-link>
@endif
@ -96,7 +96,7 @@
</x-responsive-nav-link>
@endif
@if (Auth::user()->role == "student")
<x-responsive-nav-link :href="route('take-tests')" :active="request()->routeIs('take-tests')">
<x-responsive-nav-link :href="route('take_tests.index')" :active="request()->routeIs('take_tests.index')">
{{ __('Take tests') }}
</x-responsive-nav-link>
@endif

View File

@ -1,4 +0,0 @@
<x-app-layout>
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
</div>
</x-app-layout>

View File

@ -0,0 +1,36 @@
<x-app-layout>
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
<form method="POST" action="{{ route('take_tests.store') }}">
@csrf
<input type="hidden" value="{{ $test_id }}" name="test_id">
<div class="flex flex-col gap-5 mb-10">
@foreach ($questions as $idx => $question)
<div class="rounded overflow-hidden shadow-lg bg-slate-200 p-5">
<div class="mb-2">
{{ __("Question:") }}
{{ $question["question"] }}
</div>
{{ __("Answer:") }}
@if ($question["answer_type"] == "single_answer")
<x-text-input name="answer[]" placeholder="{{ __('Answer') }}" />
@elseif ($question["answer_type"] == "single_choice")
<x-select name="answer[]">
@foreach ($question["answers"] as $answer)
<option value="{{ $answer }}">{{ $answer }}</option>
@endforeach
</x-select>
@elseif ($question["answer_type"] == "multiple_choice")
@foreach ($question["answers"] as $answer)
<div>
<x-bool-input name="answer[{{$idx}}][]" value="{{$answer}}" />
{{ $answer }}
</div>
@endforeach
@endif
</div>
@endforeach
</div>
<x-primary-button>{{ __("End test") }}</x-primary-button>
</form>
</div>
</x-app-layout>

View File

@ -0,0 +1,11 @@
<x-app-layout>
<div class="max-w-2xl mx-auto p-4 sm:p-6 lg:p-8">
<div class="flex flex-col">
@foreach ($assigned_tests as $test)
<form method="GET" action="{{ route("take_tests.edit", $test) }}">
<x-primary-button>{{ __("Do test") }} {{ $test->name }}</x-primary-button>
</form>
@endforeach
<div>
</div>
</x-app-layout>

View File

@ -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"])