How to continue the quiz with the data set entered by the user in laravel5.1

I will go straight to that. This question is related to my past question , I will offer you a reward within 3 days if you solve this problem.

What I want

After the user answered the quiz, the user can save it without sending, so they can edit / continue later. Now, after they have been saved, how can I go through the user response in the current quiz cycle of questions? So that the user can edit or continue the quiz.

What i have

Database:

quiz_result_of_user has fields 9 , ['id', 'quiz_id', 'user_id', 'question_id', 'answer', 'question_type', 'attempt_number', 'marks', 'grades']

  • This table saves all user responses, so it serves as a history table for the user quiz.

quizattempt_user has fields 3 , ['id', 'quiz_id', 'user_id']

  • This table saves all user attempts, so I can refer to all user answers where id = attempt_number in the quiz_result_of_user table. li>

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

Now I can ask the questions and answers of the user, but I can’t understand how I can answer the user, because it is also a data set. Note. Each survey can have different types of questions, multiple choice , true or false and short answer/fill in the blank .

+2
source share
1 answer

Based on what I understand in the structure of the table, we can create a dictionary of user answers given by question identifiers, so we can easily find the answer to each question. Here is a minimal implementation that we can extend:

 $questions = ... // see question description $answers = QuizResults::where('quiz_id', $quiz->id) ->where('user_id', $userId) ->where('attempt_number', $attemptNumber) ->get() ->keyBy('question_id'); foreach ($questions as $question) { echo $question->question_number + ". " + $question->description; if ($question is MultipleChoice) { foreach ($question->answers as $choice) { echo $choice; ... } } echo 'Your answer: ' + $answers->get($question->id)->first()->answer; } 

I'm not sure if the Blade page is using to generate results. For clarity, this is written in plain PHP, but it should be easy to rewrite for the Blade template.

+1
source share

All Articles