226 lines
9.3 KiB
PHP
226 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\MultipleChoiceAnswers;
|
|
use App\Models\Question;
|
|
use App\Models\SingleChoiceAnswer;
|
|
use App\Models\SingleQuestionAnswers;
|
|
use App\Models\Test;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class QuestionController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function get_validation_rules($answer_type) {
|
|
$validation_rules = [
|
|
"question" => "required|string",
|
|
"points" => "required|integer|min:1",
|
|
"test_id" => "required|integer",
|
|
"answer_type" => "required|string",
|
|
];
|
|
|
|
if ($answer_type == "single_answer") {
|
|
$validation_rules["answer"] = "required|string";
|
|
} 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 ($answer_type == "multiple_choice") {
|
|
$validation_rules["answer1"] = "required|string";
|
|
$validation_rules["answer2"] = "required|string";
|
|
$validation_rules["answer3"] = "nullable|string";
|
|
$validation_rules["answer4"] = "nullable|string";
|
|
$validation_rules["is_answer1_correct"] = "nullable|string";
|
|
$validation_rules["is_answer2_correct"] = "nullable|string";
|
|
$validation_rules["is_answer3_correct"] = "nullable|string";
|
|
$validation_rules["is_answer4_correct"] = "nullable|string";
|
|
}
|
|
|
|
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 ($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 ($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";
|
|
|
|
return [
|
|
"question_id" => $question_id,
|
|
"answer1" => $validated["answer1"],
|
|
"answer2" => $validated["answer2"],
|
|
"answer3" => $validated["answer3"],
|
|
"answer4" => $validated["answer4"],
|
|
"is_answer1_correct" => $validated["is_answer1_correct"],
|
|
"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", [
|
|
"test" => Test::where("id", $validated["test_id"])->first()
|
|
]));
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Question $question)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Question $question)
|
|
{
|
|
$question_answer = $question;
|
|
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->correct_answer = $answer->correct_answer;
|
|
$question_answer->incorrect_answer1 = $answer->incorrect_answer1;
|
|
$question_answer->incorrect_answer2 = $answer->incorrect_answer2;
|
|
$question_answer->incorrect_answer3 = $answer->incorrect_answer3;
|
|
} else if ($question->answer_type == "multiple_choice") {
|
|
$answer = MultipleChoiceAnswers::where("question_id", $question->id)->first();
|
|
$question_answer->answer1 = $answer->answer1;
|
|
$question_answer->is_answer1_correct = $answer->is_answer1_correct;
|
|
$question_answer->answer2 = $answer->answer2;
|
|
$question_answer->is_answer2_correct = $answer->is_answer2_correct;
|
|
$question_answer->answer3 = $answer->answer3;
|
|
$question_answer->is_answer3_correct = $answer->is_answer3_correct;
|
|
$question_answer->answer4 = $answer->answer4;
|
|
$question_answer->is_answer4_correct = $answer->is_answer4_correct;
|
|
}
|
|
|
|
return view("questions.edit", [
|
|
"question" => $question_answer,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Question $question)
|
|
{
|
|
$validated = $request->validate([
|
|
"question" => "required|string",
|
|
"answer" => "required|string",
|
|
"points" => "required|integer|min:1"
|
|
]);
|
|
|
|
$validation_rules = $this->get_validation_rules($request->answer_type);
|
|
$validated = $request->validate($validation_rules);
|
|
|
|
$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));
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Question $question)
|
|
{
|
|
$question->delete();
|
|
|
|
return redirect(route("tests.edit", $question->test_id));
|
|
}
|
|
}
|