1
0

add updating of answers and localization

This commit is contained in:
Rokas Puzonas 2023-11-26 01:12:01 +02:00
parent 2c352f98ab
commit c3f1715bbd
5 changed files with 123 additions and 38 deletions

View File

@ -28,11 +28,7 @@ class QuestionController extends Controller
// //
} }
/** public function get_validation_rules($answer_type) {
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validation_rules = [ $validation_rules = [
"question" => "required|string", "question" => "required|string",
"points" => "required|integer|min:1", "points" => "required|integer|min:1",
@ -40,14 +36,14 @@ class QuestionController extends Controller
"answer_type" => "required|string", "answer_type" => "required|string",
]; ];
if ($request->answer_type == "single_answer") { if ($answer_type == "single_answer") {
$validation_rules["answer"] = "required|string"; $validation_rules["answer"] = "required|string";
} else if ($request->answer_type == "single_choice") { } else if ($answer_type == "single_choice") {
$validation_rules["correct_answer"] = "required|string"; $validation_rules["correct_answer"] = "required|string";
$validation_rules["incorrect_answer1"] = "required|string"; $validation_rules["incorrect_answer1"] = "required|string";
$validation_rules["incorrect_answer2"] = "nullable|string"; $validation_rules["incorrect_answer2"] = "nullable|string";
$validation_rules["incorrect_answer3"] = "nullable|string"; $validation_rules["incorrect_answer3"] = "nullable|string";
} else if ($request->answer_type == "multiple_choice") { } else if ($answer_type == "multiple_choice") {
$validation_rules["answer1"] = "required|string"; $validation_rules["answer1"] = "required|string";
$validation_rules["answer2"] = "required|string"; $validation_rules["answer2"] = "required|string";
$validation_rules["answer3"] = "nullable|string"; $validation_rules["answer3"] = "nullable|string";
@ -58,34 +54,31 @@ class QuestionController extends Controller
$validation_rules["is_answer4_correct"] = "nullable|string"; $validation_rules["is_answer4_correct"] = "nullable|string";
} }
$validated = $request->validate($validation_rules); return $validation_rules;
$question = Question::create([ }
"question" => $validated["question"],
"points" => $validated["points"], public function get_answer_payload($question_id, $validated) {
"test_id" => $validated["test_id"], if ($validated["answer_type"] == "single_answer") {
"answer_type" => $validated["answer_type"], return [
]); "question_id" => $question_id,
if ($request->answer_type == "single_answer") {
SingleQuestionAnswers::create([
"question_id" => $question->id,
"answer" => $validated["answer"], "answer" => $validated["answer"],
]); ];
} else if ($request->answer_type == "single_choice") { } else if ($validated["answer_type"] == "single_choice") {
SingleChoiceAnswer::create([ return [
"question_id" => $question->id, "question_id" => $question_id,
"correct_answer" => $validated["correct_answer"], "correct_answer" => $validated["correct_answer"],
"incorrect_answer1" => $validated["incorrect_answer1"], "incorrect_answer1" => $validated["incorrect_answer1"],
"incorrect_answer2" => $validated["incorrect_answer2"], "incorrect_answer2" => $validated["incorrect_answer2"],
"incorrect_answer3" => $validated["incorrect_answer3"], "incorrect_answer3" => $validated["incorrect_answer3"],
]); ];
} else if ($request->answer_type == "multiple_choice") { } else if ($validated["answer_type"] == "multiple_choice") {
$validated["is_answer1_correct"] = !empty($validated["is_answer1_correct"]) && $validated["is_answer1_correct"] == "on"; $validated["is_answer1_correct"] = !empty($validated["is_answer1_correct"]) && $validated["is_answer1_correct"] == "on";
$validated["is_answer2_correct"] = !empty($validated["is_answer2_correct"]) && $validated["is_answer2_correct"] == "on"; $validated["is_answer2_correct"] = !empty($validated["is_answer2_correct"]) && $validated["is_answer2_correct"] == "on";
$validated["is_answer3_correct"] = !empty($validated["is_answer3_correct"]) && $validated["is_answer3_correct"] == "on"; $validated["is_answer3_correct"] = !empty($validated["is_answer3_correct"]) && $validated["is_answer3_correct"] == "on";
$validated["is_answer4_correct"] = !empty($validated["is_answer4_correct"]) && $validated["is_answer4_correct"] == "on"; $validated["is_answer4_correct"] = !empty($validated["is_answer4_correct"]) && $validated["is_answer4_correct"] == "on";
MultipleChoiceAnswers::create([ return [
"question_id" => $question->id, "question_id" => $question_id,
"answer1" => $validated["answer1"], "answer1" => $validated["answer1"],
"answer2" => $validated["answer2"], "answer2" => $validated["answer2"],
"answer3" => $validated["answer3"], "answer3" => $validated["answer3"],
@ -94,7 +87,30 @@ class QuestionController extends Controller
"is_answer2_correct" => $validated["is_answer2_correct"], "is_answer2_correct" => $validated["is_answer2_correct"],
"is_answer3_correct" => $validated["is_answer3_correct"], "is_answer3_correct" => $validated["is_answer3_correct"],
"is_answer4_correct" => $validated["is_answer4_correct"], "is_answer4_correct" => $validated["is_answer4_correct"],
]); ];
}
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validation_rules = $this->get_validation_rules($request->answer_type);
$validated = $request->validate($validation_rules);
$question = Question::create([
"question" => $validated["question"],
"points" => $validated["points"],
"test_id" => $validated["test_id"],
"answer_type" => $validated["answer_type"],
]);
if ($request->answer_type == "single_answer") {
SingleQuestionAnswers::create($this->get_answer_payload($question->id, $validated));
} else if ($request->answer_type == "single_choice") {
SingleChoiceAnswer::create($this->get_answer_payload($question->id, $validated));
} else if ($request->answer_type == "multiple_choice") {
MultipleChoiceAnswers::create($this->get_answer_payload($question->id, $validated));
} }
return redirect(route("tests.edit", [ return redirect(route("tests.edit", [
@ -153,11 +169,48 @@ class QuestionController extends Controller
"points" => "required|integer|min:1" "points" => "required|integer|min:1"
]); ]);
$question->update($validated); $validation_rules = $this->get_validation_rules($request->answer_type);
$validated = $request->validate($validation_rules);
return view("tests.edit", [ $prev_answer_type = $question->answer_type;
"test" => $validated["test_id"] $question->update([
"question" => $validated["question"],
"points" => $validated["points"],
"test_id" => $validated["test_id"],
"answer_type" => $validated["answer_type"],
]); ]);
if ($prev_answer_type != $validated["answer_type"]) {
if ($prev_answer_type == "single_answer") {
SingleQuestionAnswers::where("question_id", $question->id)->delete();
} else if ($prev_answer_type == "single_choice") {
SingleChoiceAnswer::where("question_id", $question->id)->delete();
} else if ($prev_answer_type == "multiple_choice") {
MultipleChoiceAnswers::where("question_id", $question->id)->delete();
}
if ($request->answer_type == "single_answer") {
SingleQuestionAnswers::create($this->get_answer_payload($question->id, $validated));
} else if ($request->answer_type == "single_choice") {
SingleChoiceAnswer::create($this->get_answer_payload($question->id, $validated));
} else if ($request->answer_type == "multiple_choice") {
MultipleChoiceAnswers::create($this->get_answer_payload($question->id, $validated));
}
} else {
if ($request->answer_type == "single_answer") {
SingleQuestionAnswers::where("question_id", $question->id)
->update($this->get_answer_payload($question->id, $validated));
} else if ($request->answer_type == "single_choice") {
SingleChoiceAnswer::where("question_id", $question->id)
->update($this->get_answer_payload($question->id, $validated));
} else if ($request->answer_type == "multiple_choice") {
MultipleChoiceAnswers::where("question_id", $question->id)
->update($this->get_answer_payload($question->id, $validated));
}
}
$test = Test::where("id", $request->test_id)->first();
return redirect(route("tests.edit", $test));
} }
/** /**

View File

@ -26,5 +26,29 @@
"Question": "Klausimas", "Question": "Klausimas",
"Answer": "Atsakymas", "Answer": "Atsakymas",
"Points": "Taškai", "Points": "Taškai",
"Add question": "Pridėti klausimą" "Add question": "Pridėti klausimą",
"Grade": "Pažymys",
"Can take test": "Gali laikyti testą",
"Allow to take test": "Leisti laikyti testą",
"Add student": "Pridėti studentą",
"Test results": "Testų rezultatai",
"Assigned students": "Priskirti studentai",
"Questions": "Klausimai",
"Single": "Vienas",
"Single choice": "Pasirinkti vieną",
"Multiple choice": "Pasirinkti kelis",
"Answer type": "Atsakymo tipas",
"Correct answer": "Teisingas atsakymas",
"Answer 1": "Atsakymas 1",
"Answer 2": "Atsakymas 2",
"Answer 3": "Atsakymas 3",
"Answer 4": "Atsakymas 4",
"Incorrect answer 1": "Neteisingas atsakymas 1",
"Incorrect Answer 2": "Neteisingas atsakymas 2",
"Incorrect answer 3": "Neteisingas atsakymas 3",
"Do test:": "Atlikti testą:",
"Question:": "Klausimas",
"Answer:": "Atsakymas",
"End test": "Pabaigti testą",
"-- Empty --": "-- Tuščia --"
} }

View File

@ -69,7 +69,7 @@ return [
'image' => 'The :attribute field must be an image.', 'image' => 'The :attribute field must be an image.',
'in' => 'The selected :attribute is invalid.', 'in' => 'The selected :attribute is invalid.',
'in_array' => 'The :attribute field must exist in :other.', 'in_array' => 'The :attribute field must exist in :other.',
'integer' => 'The :attribute field must be an integer.', 'integer' => 'Laukas \':attribute\' turi būti sveikasis skaičius',
'ip' => 'The :attribute field must be a valid IP address.', 'ip' => 'The :attribute field must be a valid IP address.',
'ipv4' => 'The :attribute field must be a valid IPv4 address.', 'ipv4' => 'The :attribute field must be a valid IPv4 address.',
'ipv6' => 'The :attribute field must be a valid IPv6 address.', 'ipv6' => 'The :attribute field must be a valid IPv6 address.',
@ -100,7 +100,7 @@ return [
'min' => [ 'min' => [
'array' => 'The :attribute field must have at least :min items.', 'array' => 'The :attribute field must have at least :min items.',
'file' => 'The :attribute field must be at least :min kilobytes.', 'file' => 'The :attribute field must be at least :min kilobytes.',
'numeric' => 'The :attribute field must be at least :min.', 'numeric' => 'Laukas \':attribute\' turi būti nors :min.',
'string' => 'The :attribute field must be at least :min characters.', 'string' => 'The :attribute field must be at least :min characters.',
], ],
'min_digits' => 'The :attribute field must have at least :min digits.', 'min_digits' => 'The :attribute field must have at least :min digits.',
@ -126,7 +126,7 @@ return [
'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.', 'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.',
'prohibits' => 'The :attribute field prohibits :other from being present.', 'prohibits' => 'The :attribute field prohibits :other from being present.',
'regex' => 'The :attribute field format is invalid.', 'regex' => 'The :attribute field format is invalid.',
'required' => 'Laukas \':attribute\' field yra butinas.', 'required' => 'Laukas \':attribute\' yra būtinas.',
'required_array_keys' => 'The :attribute field must contain entries for: :values.', 'required_array_keys' => 'The :attribute field must contain entries for: :values.',
'required_if' => 'The :attribute field is required when :other is :value.', 'required_if' => 'The :attribute field is required when :other is :value.',
'required_if_accepted' => 'The :attribute field is required when :other is accepted.', 'required_if_accepted' => 'The :attribute field is required when :other is accepted.',
@ -180,6 +180,14 @@ return [
| |
*/ */
'attributes' => [], 'attributes' => [
"question" => "klausimas",
"answer" => "atsakymas",
"answer1" => "atsakymas 1",
"answer2" => "atsakymas 2",
"correct_answer" => "teisingas atsakymas",
"incorrect_answer1" => "neteisingas atsakymas 1",
"points" => "taškai"
],
]; ];

View File

@ -3,7 +3,7 @@
<div class="flex flex-col"> <div class="flex flex-col">
@foreach ($assigned_tests as $test) @foreach ($assigned_tests as $test)
<form method="GET" action="{{ route("take_tests.edit", $test) }}"> <form method="GET" action="{{ route("take_tests.edit", $test) }}">
<x-primary-button>{{ __("Do test") }} {{ $test->name }}</x-primary-button> <x-primary-button>{{ __("Do test:") }} {{ $test->name }}</x-primary-button>
</form> </form>
@endforeach @endforeach
<div> <div>

View File

@ -20,9 +20,9 @@
@if (Route::has('login')) @if (Route::has('login'))
<div class="sm:fixed sm:top-0 sm:right-0 p-6 text-right z-10"> <div class="sm:fixed sm:top-0 sm:right-0 p-6 text-right z-10">
@auth @auth
<a href="{{ url('/dashboard') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Dashboard</a> <a href="{{ url('/dashboard') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">{{ __("Dashboard") }}</a>
@else @else
<a href="{{ route('login') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Log in</a> <a href="{{ route('login') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">{{ __("Log in") }}</a>
@endauth @endauth
</div> </div>
@endif @endif