149 lines
4.9 KiB
PHP
149 lines
4.9 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;
|
|
|
|
class QuestionController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
"question" => "required|string",
|
|
"points" => "required|integer|min:1",
|
|
"test_id" => "required|integer",
|
|
"answer_type" => "required|string",
|
|
]);
|
|
|
|
if ($request->answer_type == "single_answer") {
|
|
$validated_answer = $request->validate([
|
|
"answer" => "required|string",
|
|
]);
|
|
} else if ($request->answer_type == "single_choice") {
|
|
$validated_answer = $request->validate([
|
|
"correct_answer" => "required|string",
|
|
"incorrect_answer1" => "required|string",
|
|
"incorrect_answer2" => "nullable|string",
|
|
"incorrect_answer3" => "nullable|string",
|
|
]);
|
|
} else if ($request->answer_type == "multiple_choice") {
|
|
$validated_answer = $request->validate([
|
|
"answer1" => "required|string",
|
|
"answer2" => "required|string",
|
|
"answer3" => "nullable|string",
|
|
"answer4" => "nullable|string",
|
|
"is_answer1_correct" => "nullable|boolean",
|
|
"is_answer2_correct" => "nullable|boolean",
|
|
"is_answer3_correct" => "nullable|boolean",
|
|
"is_answer4_correct" => "nullable|boolean",
|
|
]);
|
|
}
|
|
|
|
$question = Question::create($validated);
|
|
$validated_answer["question_id"] = $question->id;
|
|
if ($request->answer_type == "single_answer") {
|
|
SingleQuestionAnswers::create($validated_answer);
|
|
} else if ($request->answer_type == "single_choice") {
|
|
SingleChoiceAnswer::create($validated_answer);
|
|
} else if ($request->answer_type == "multiple_choice") {
|
|
MultipleChoiceAnswers::create($validated_answer);
|
|
}
|
|
|
|
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"
|
|
]);
|
|
|
|
$question->update($validated);
|
|
|
|
return view("tests.edit", [
|
|
"test" => $validated["test_id"]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Question $question)
|
|
{
|
|
$question->delete();
|
|
|
|
return redirect(route("tests.edit", $question->test_id));
|
|
}
|
|
}
|