fixed test taking logic to accommodate multi answer questions
This commit is contained in:
parent
d4d634363d
commit
f010bbf128
3 changed files with 67 additions and 55 deletions
|
@ -15,7 +15,7 @@ class TestController extends Controller
|
|||
{
|
||||
public function startTest(Test $test)
|
||||
{
|
||||
if (Auth::user()->testTaken(2)) {
|
||||
if (Auth::user()->testTaken($test->id)) {
|
||||
//insert session flash
|
||||
return redirect('/home');
|
||||
}
|
||||
|
@ -34,16 +34,38 @@ 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');
|
||||
$options = session('options');
|
||||
$answers = [];
|
||||
foreach ($options as $option) {
|
||||
if (array_key_exists("answer{$option->id}", request()->all())) {
|
||||
$answers[] = $option;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$question->isCorrect(session('answers')["$question->id"])) {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
} else {
|
||||
session(['is_correct' => true]);
|
||||
if ($question->multiple_answers_question) {
|
||||
$correct_answers = $question->correct_answers;
|
||||
$user_answers_correct = 0;
|
||||
foreach ($answers as $answer) {
|
||||
if ($answer->correct_answer == 1) {
|
||||
$user_answers_correct++;
|
||||
}
|
||||
}
|
||||
if ($user_answers_correct == $correct_answers) {
|
||||
session(['is_correct' => true]);
|
||||
} else {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$question->multiple_answers_question) {
|
||||
if ($answers[0]->correct_answer) {
|
||||
session(['is_correct' => true]);
|
||||
} else {
|
||||
session(['wrong_answers' => session('wrong_answers')+1]);
|
||||
session(['is_correct' => false]);
|
||||
}
|
||||
}
|
||||
|
||||
if (session('test')->hasFailed(session('wrong_answers'))) {
|
||||
|
@ -161,26 +183,28 @@ class TestController extends Controller
|
|||
$question = new Question;
|
||||
$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;
|
||||
$question->question_type = "radio";
|
||||
$question->correct_answers = 1;
|
||||
} else {
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
$question->question_type = "checkbox";
|
||||
$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;
|
||||
}
|
||||
$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;
|
||||
|
@ -222,26 +246,28 @@ class TestController extends Controller
|
|||
$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;
|
||||
$question->question_type = "radio";
|
||||
$question->correct_answers = 1;
|
||||
} else {
|
||||
$question->multiple_answers_question = request()->multiple_answers_question;
|
||||
$question->question_type = "checkbox";
|
||||
$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;
|
||||
}
|
||||
$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) {
|
||||
|
|
|
@ -13,17 +13,10 @@
|
|||
<label>Title</label>
|
||||
<input type="text" class="form-control" name="title" value="{{ $question->title }}">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question Type</label>
|
||||
<select name="question_type" class="form-control">
|
||||
<option value="radio">Radio Buttons</option>
|
||||
<option value="checkbox">Checkboxes</option>
|
||||
</select>
|
||||
<input type="checkbox" name="multiple_answers_question" value="1" @if ($question->correct_answer == 1) checked @endif> Multiple Correct Answers
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question</label>
|
||||
<input type="text" class="form-control" name="question" value="{{ $question->question }}">
|
||||
<input type="checkbox" name="multiple_answers_question" value="1" @if ($question->correct_answer == 1) checked @endif> Multiple Answers Required
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 1</label>
|
||||
|
|
|
@ -11,17 +11,10 @@
|
|||
<label>Title</label>
|
||||
<input type="text" class="form-control" name="title">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question Type</label>
|
||||
<select name="question_type" class="form-control">
|
||||
<option value="radio">Radio Buttons</option>
|
||||
<option value="checkbox">Checkboxes</option>
|
||||
</select>
|
||||
<input type="checkbox" name="multiple_answers_question" value="1"> Multiple Correct Answers
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Question</label>
|
||||
<input type="text" class="form-control" name="question">
|
||||
<input type="checkbox" name="multiple_answers_question" value="1"> Multiple Answers Required
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>Option 1</label>
|
||||
|
|
Reference in a new issue