1
0
tinklu-it-projektas/app/Http/Controllers/TestTakingController.php

190 lines
6.3 KiB
PHP

<?php
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
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$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
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
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');
}
/**
* Display the specified resource.
*/
public function show(Test $test)
{
//
}
/**
* Show the form for editing the specified resource.
*/
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
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Test $test)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Test $test)
{
//
}
}