I'm gonna go straight forward for this. This question is connected with my passed question, I will offer you the bounty within 3 days from now if you solved this problem.
What I want
After the user answered a quiz, the user can save it without submitting, so they can edit/continue later. Now, after they saved, how can I loop through the user's answer in the current loop of questions in a quiz? So that the user can edit or continue the quiz.
What I have
database:
quiz_result_of_user
has 9
fields, ['id', 'quiz_id', 'user_id', 'question_id', 'answer', 'question_type', 'attempt_number', 'marks', 'grades']
- This table saves all the user's answer so this serves as the history table of the user's quiz.
quizattempt_user
has 3
fields, ['id', 'quiz_id', 'user_id']
- This table saves all the user's attempt, so I can reference all the answer of the user
where
theid
=
attempt_number
in thequiz_result_of_user
table.
Controller - Update
$quiz = Quiz::with('multiple_choices.answers', 'true_false', 'short_answer', 'descriptions')->findOrFail($id);
$questions = collect($quiz->multiple_choices);
$questions = $questions->merge(collect($quiz->true_false));
$questions = $questions->merge(collect($quiz->short_answer));
$questions = $questions->merge(collect($quiz->descriptions));
$questions = $questions->sortBy('question_number');
Problem
I can now loop through the questions, and the answers of the user, but I can't figure out how can I put the user's answer because it is also a collection of data. Note: every quiz can have different types of question, multiple choice
, true or false
, and short answer/fill in the blank
.
Based on what I understand of the table structure, we can create a dictionary of the user's answers keyed by the the question IDs, so we can easily look up the answer for each question. Here's a minimal implementation that we can expand on:
I'm not sure if the page uses Blade to generate the results. For clarity, this is written in plain PHP, but it should be easy to rewrite this for a Blade template.