Laravel 5测验项目 [英] Laravel 5 Quiz Project

查看:50
本文介绍了Laravel 5测验项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能更多是一个概念性问题,但我正在尝试寻找一种最佳方法来制作一个视图,该视图一次显示一个测验问题并检查答案.

This is probably more of a conceptual question but I am trying to find the best way to make a view that displays one quiz question at a time and checks the answer.

当前,我的MySQL表具有以下列:id,category_id,quiz_question,answer_one,answer_two,answer_three,correct_answer.

Currently my MySQL table has the following columns: id, category_id, quiz_question, answer_one, answer_two, answer_three, correct_answer.

我的控制器使用以下内容来获取测验问题:

My controller uses the following to get the quiz questions:

$quizzes = Quiz::where('category_id',$category_id)->simplePaginate(1);

在我看来,我经历了手动进行订购排序的过程

In my view, I go through the process of doing the asnwer ordering manually

{{$quiz->quiz_question }}

<a onclick="this.innerHTML='Wrong'">{{ $quiz->answer_one }}</a>
<a onclick="this.innerHTML='Right'">{{ $quiz->correct_answer }}</a>
<a onclick="this.innerHTML='Wrong'">{{ $quiz->answer_two }}</a>...etc.

,可能只是要使用JavaScript/JQuery来检查正确答案.

and was probably just going to use JavaScript/JQuery to check for the right answer.

从概念上讲,这感觉是一种非常糟糕的方法,并且我仍然不确定如何将答案的顺序随机化(如果它们来自同一张数据库表).总体而言,还有更好的方法(显示1个测验问题,并以随机顺序显示答案,无需DB查询即可检查)?预先感谢.

Conceptually this feels like a pretty bad way to do this and I am still not sure how to randomize the order of the answers if they are from the same DB table like this. Overall is there a better way to go about this (Display 1 quiz question, and display answers in random order, check without a DB query)? Thanks in advance.

推荐答案

请勿以任何形式将答案保留在客户端.

相反,只需提出问题并在客户端进行分页.如果您绝对需要逐一验证答案,请启动AJAX请求.

Instead, just get the questions and paginate it on the client side. If you absolutely need to validate answers one by one, then fire up AJAX requests.

或者,您也可以让该人员尝试整个测验,然后将问题与用户的答案一起发布并在服务器端对其进行验证.然后返回结果/分数/答案.

Or you can just let the person attempt the whole quiz and post the questions with users' answers and validate them on the server side. Then you return the result/score/answers.

关于不使用数据库查询进行检查的查询,您不应该这样做.如果绝对需要,则将答案保存在对象中,而不是DOM中.再次,它不再是不推荐"了.

Regarding your query about checking without DB query, you shouldn't do it. If you absolutely need it then save answers in an object, rather than in the DOM. Again, it can't be any more "not recommended".

您应该首先将其划分为这些表.没有第一个表的提示,就无法随机显示选项的显示顺序.

You should first divide it into these tables. You can't randomize the order of display of the options without a hack from the first table.

不要命名诸如answer_one,answer_two,answer_three,correct_answer之类的列.这样,通过列名 correct_answer 识别答案的唯一方法.列名不应提示正确的答案.它应该存储在其他地方.

And don't name the columns like answer_one, answer_two, answer_three, correct_answer. In this manner the only way to recognize the answer in by the column name correct_answer. Column name shouldn't give a hint about the correct answer. It should be stored somewhere else.

  1. 问题

id
question - the question body

  • 类别

  • categories

    id
    category - name of the category
    

  • question_categories(一个问题可能属于多个类别)

  • question_categories (one question may belong to multiple categories)

    id
    question_id
    category_id
    

  • 选项

  • options

    id
    question_id
    option - option text
    

  • 答案

  • answers

    id
    question_id
    option_id
    

  • 现在您要做的是将选项的顺序随机化

    Now what you do is you randomize the order of options

    $questions = Question::where('category_id', $category_id)->
                 with(['options' => function ($query) {
                   $query->orderBy(DB::raw('RAND()'));
                 }])
                 ->get();
    

    要随机订购选项,我们使用了 ORDER BY RAND().

    To randomly order the options we used ORDER BY RAND().

    现在,您只需发送 option_id 并可以检查它是否在服务器端是正确的答案.

    Now you just send the option_id and can check if it is the correct answer on the server side.

    您可以使用以下模型.

    1. 问题
    2. QuestionCategory
    3. 类别
    4. 选项
    5. 答案

    您可以使用以下关系.

    1. 问题属于QuestionCategory
    2. QuestionCategory属于类别
    3. 问题有很多选择
    4. 问题有一个答案

    但是创建一个QuestionCategory模型并不能很好地扩展.因此,如果您不想这样做,则可以使用多态关系.您可以使用类别表来存储基本上所有类型的类别,而不仅仅是问题类别.在这种情况下,您需要修改 categories 表以添加分类法类型.如果您不了解任何这种多态关系,请在Laravel官方文档中找到它

    But creating a QuestionCategory model doesn't really scale well. So if you don't want to do that you can use Polymorphic relationships. You can use category table to store basically all types of categories, not just question categories. In that case, you need to modify the categories tables to add the type of taxonomy. If you don't understand any of this polymorphic relationship thing, please find it in the official Laravel documentation https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations

    这篇关于Laravel 5测验项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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