added functionality for multi answer questions and 2 different question types
This commit is contained in:
parent
a1af585133
commit
d4d634363d
9 changed files with 122 additions and 16 deletions
app/Http/Controllers
|
@ -34,14 +34,10 @@ class TestController extends Controller
|
|||
|
||||
public function answerQuestion()
|
||||
{
|
||||
dd(request()->all());
|
||||
$question = session('questions')->get(session('question_counter')-1);
|
||||
$question_id = $question->id;
|
||||
$answer_id = request()->get('answer');
|
||||
if (!empty(session('answers'))) {
|
||||
$answers = session('answers');
|
||||
}
|
||||
$answers["$question_id"] = $answer_id;
|
||||
session(['answers' => $answers]);
|
||||
|
||||
if (!$question->isCorrect(session('answers')["$question->id"])) {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
|
@ -165,20 +161,55 @@ class TestController extends Controller
|
|||
$question = new Question;
|
||||
$question->title = request()->title;
|
||||
$question->question = request()->question;
|
||||
if (empty(Option::all()->last()->id)) {
|
||||
$question->answer_id = 1;
|
||||
$question->question_type = request()->question_type;
|
||||
if (request()->multiple_answers_question == null) {
|
||||
$question->multiple_answers_question = 0;
|
||||
} else {
|
||||
$question->answer_id = Option::all()->last()->id+1;
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
}
|
||||
$correct_answers = 0;
|
||||
if (request()->correct_answer1 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer2 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer3 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer4 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
$question->correct_answers = $correct_answers;
|
||||
$test->questions()->save($question);
|
||||
$option1 = new Option;
|
||||
$option1->option = request()->option1;
|
||||
if (request()->correct_answer1 == null) {
|
||||
$option1->correct_answer = 0;
|
||||
} else {
|
||||
$option1->correct_answer = request()->correct_answer1;
|
||||
}
|
||||
$option2 = new Option;
|
||||
$option2->option = request()->option2;
|
||||
if (request()->correct_answer2 == null) {
|
||||
$option2->correct_answer = 0;
|
||||
} else {
|
||||
$option2->correct_answer = request()->correct_answer2;
|
||||
}
|
||||
$option3 = new Option;
|
||||
$option3->option = request()->option3;
|
||||
if (request()->correct_answer3 == null) {
|
||||
$option3->correct_answer = 0;
|
||||
} else {
|
||||
$option3->correct_answer = request()->correct_answer3;
|
||||
}
|
||||
$option4 = new Option;
|
||||
$option4->option = request()->option4;
|
||||
if (request()->correct_answer4 == null) {
|
||||
$option4->correct_answer = 0;
|
||||
} else {
|
||||
$option4->correct_answer = request()->correct_answer4;
|
||||
}
|
||||
$question->options()->save($option1);
|
||||
$question->options()->save($option2);
|
||||
$question->options()->save($option3);
|
||||
|
@ -189,12 +220,53 @@ class TestController extends Controller
|
|||
public function updateQuestion(Question $question)
|
||||
{
|
||||
$test = $question->test;
|
||||
$question->title = request()->title;
|
||||
$question->question = request()->question;
|
||||
$question->question_type = request()->question_type;
|
||||
if (request()->multiple_answers_question == null) {
|
||||
$question->multiple_answers_question = 0;
|
||||
} else {
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
}
|
||||
$correct_answers = 0;
|
||||
if (request()->correct_answer1 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer2 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer3 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
if (request()->correct_answer4 == 1) {
|
||||
$correct_answers++;
|
||||
}
|
||||
$question->correct_answers = $correct_answers;
|
||||
$options = $question->options;
|
||||
$options[0]->option = request()->option1;
|
||||
if (request()->correct_answer1 == null) {
|
||||
$options[0]->correct_answer = 0;
|
||||
} else {
|
||||
$options[0]->correct_answer = request()->correct_answer1;
|
||||
}
|
||||
$options[1]->option = request()->option2;
|
||||
if (request()->correct_answer2 == null) {
|
||||
$options[1]->correct_answer = 0;
|
||||
} else {
|
||||
$options[1]->correct_answer = request()->correct_answer2;
|
||||
}
|
||||
$options[2]->option = request()->option3;
|
||||
if (request()->correct_answer3 == null) {
|
||||
$options[2]->correct_answer = 0;
|
||||
} else {
|
||||
$options[2]->correct_answer = request()->correct_answer3;
|
||||
}
|
||||
$options[3]->option = request()->option4;
|
||||
if (request()->correct_answer4 == null) {
|
||||
$options[3]->correct_answer = 0;
|
||||
} else {
|
||||
$options[3]->correct_answer = request()->correct_answer4;
|
||||
}
|
||||
$question->update();
|
||||
$options[0]->update();
|
||||
$options[1]->update();
|
||||
|
|
Reference in a new issue