diff --git a/app/Http/Controllers/QuestionController.php b/app/Http/Controllers/QuestionController.php index 7c51eb4..ea8684f 100644 --- a/app/Http/Controllers/QuestionController.php +++ b/app/Http/Controllers/QuestionController.php @@ -28,11 +28,7 @@ class QuestionController extends Controller // } - /** - * Store a newly created resource in storage. - */ - public function store(Request $request) - { + public function get_validation_rules($answer_type) { $validation_rules = [ "question" => "required|string", "points" => "required|integer|min:1", @@ -40,14 +36,14 @@ class QuestionController extends Controller "answer_type" => "required|string", ]; - if ($request->answer_type == "single_answer") { + if ($answer_type == "single_answer") { $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["incorrect_answer1"] = "required|string"; $validation_rules["incorrect_answer2"] = "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["answer2"] = "required|string"; $validation_rules["answer3"] = "nullable|string"; @@ -58,34 +54,31 @@ class QuestionController extends Controller $validation_rules["is_answer4_correct"] = "nullable|string"; } - $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([ - "question_id" => $question->id, + return $validation_rules; + } + + public function get_answer_payload($question_id, $validated) { + if ($validated["answer_type"] == "single_answer") { + return [ + "question_id" => $question_id, "answer" => $validated["answer"], - ]); - } else if ($request->answer_type == "single_choice") { - SingleChoiceAnswer::create([ - "question_id" => $question->id, + ]; + } else if ($validated["answer_type"] == "single_choice") { + return [ + "question_id" => $question_id, "correct_answer" => $validated["correct_answer"], "incorrect_answer1" => $validated["incorrect_answer1"], "incorrect_answer2" => $validated["incorrect_answer2"], "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_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_answer4_correct"] = !empty($validated["is_answer4_correct"]) && $validated["is_answer4_correct"] == "on"; - MultipleChoiceAnswers::create([ - "question_id" => $question->id, + return [ + "question_id" => $question_id, "answer1" => $validated["answer1"], "answer2" => $validated["answer2"], "answer3" => $validated["answer3"], @@ -94,7 +87,30 @@ class QuestionController extends Controller "is_answer2_correct" => $validated["is_answer2_correct"], "is_answer3_correct" => $validated["is_answer3_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", [ @@ -153,11 +169,48 @@ class QuestionController extends Controller "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", [ - "test" => $validated["test_id"] + $prev_answer_type = $question->answer_type; + $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)); } /** diff --git a/lang/lt.json b/lang/lt.json index c0893b6..8228979 100644 --- a/lang/lt.json +++ b/lang/lt.json @@ -26,5 +26,29 @@ "Question": "Klausimas", "Answer": "Atsakymas", "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 --" } diff --git a/lang/lt/validation.php b/lang/lt/validation.php index 316269e..35716d1 100644 --- a/lang/lt/validation.php +++ b/lang/lt/validation.php @@ -69,7 +69,7 @@ return [ 'image' => 'The :attribute field must be an image.', 'in' => 'The selected :attribute is invalid.', '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.', 'ipv4' => 'The :attribute field must be a valid IPv4 address.', 'ipv6' => 'The :attribute field must be a valid IPv6 address.', @@ -100,7 +100,7 @@ return [ 'min' => [ 'array' => 'The :attribute field must have at least :min items.', '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.', ], '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.', 'prohibits' => 'The :attribute field prohibits :other from being present.', '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_if' => 'The :attribute field is required when :other is :value.', '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" + ], ]; diff --git a/resources/views/take_tests/index.blade.php b/resources/views/take_tests/index.blade.php index 78b2f00..10b3f83 100644 --- a/resources/views/take_tests/index.blade.php +++ b/resources/views/take_tests/index.blade.php @@ -3,7 +3,7 @@
@foreach ($assigned_tests as $test)
- {{ __("Do test") }} {{ $test->name }} + {{ __("Do test:") }} {{ $test->name }}
@endforeach
diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index af62020..1f21276 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -20,9 +20,9 @@ @if (Route::has('login')) @endif