如何使用用户在laravel5.1中输入的数据继续进行测验 [英] How to continue a quiz with a collection of data entered by the user in laravel5.1

查看:71
本文介绍了如何使用用户在laravel5.1中输入的数据继续进行测验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为此直接前进.此问题与我通过的问题相关联,如果您解决了此问题,我将在3天之内为您提供赏金.

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.

我想要的

用户回答完测验后,可以保存它而无需提交,因此以后可以编辑/继续.现在,在他们保存之后,如何在测验的当前问题循环中遍历用户的答案?以便用户可以编辑或继续测验.

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.

我有什么

数据库:

quiz_result_of_user 具有9个字段,['id', 'quiz_id', 'user_id', 'question_id', 'answer', 'question_type', 'attempt_number', 'marks', 'grades']

  • 此表保存了所有用户的答案,因此可用作用户测验的历史记录表.

quizattempt_user 具有3字段,['id', 'quiz_id', 'user_id']

  • 此表保存了所有用户的尝试,因此我可以在 quiz_result_of_user 表中引用用户whereid=attempt_number的所有答案. li>
  • This table saves all the user's attempt, so I can reference all the answer of the user where the id = attempt_number in the quiz_result_of_user table.

控制器-更新

$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');

问题

我现在可以遍历用户的问题和答案,但是我不知道该如何收集用户的答案,因为它也是数据的集合. 注意:每个测验可以包含不同类型的问题,分别为multiple choicetrue or falseshort answer/fill in the blank.

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.

推荐答案

根据我对表格结构的了解,我们可以创建由问题ID键控的用户答案字典,因此我们可以轻松查找回答每个问题.这是我们可以扩展的最小实现:

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:

$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; 
}

我不确定页面是否使用Blade生成结果.为了清楚起见,这是用普通的PHP编写的,但是对于Blade模板来说,应该很容易重写它.

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.

这篇关于如何使用用户在laravel5.1中输入的数据继续进行测验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆