douzhi3105 2009-12-21 03:25
浏览 21
已采纳

为CakePHP中的测验应用建议更好的流程

Here's the current flow of my quiz app:

  1. users/do_program
  2. quiz_contents/view
  3. quiz_questions/do_quiz

The checking if the user has to do another module after finishing the current ones are in the quiz_contents and quiz_questions controller which I think can be improved because of the duplicate code. So what would you guys suggest?

Some info:

  • A module belongs to a program and a program has many modules.
  • A module must have a quiz_content and it may or may not have a quiz_question
  • I got code that logs if a user finishes a quiz_content in quiz_contents and code that logs if a user finishes a quiz_question in quiz_questions

P.S. Also would you guys advise that I put the content/quiz to be rendered (the id in particular) in the session?

  • 写回答

3条回答 默认 最新

  • dongxie2749 2009-12-24 14:11
    关注

    If I understand you correctly then your models are set up like so:

    Program hasMany Module hasMany QuizContent hasMany QuizQuestion

    or more simply:

    Program hasMany Module hasMany Quiz hasMany Question

    There can be quite a lot to think about in these applications, so it is hard to tell you how we might structure this without knowing the answer to more questions:

    • Can users do a quiz more than once?
    • Can users return to questions they have already answered?
    • Are the answers saved individually?
    • Do the results get saved up and then stored at the end?
    • Do the results get stored in the database?
    • Are the results simply emailed off somewhere?

    Assuming you want to strictly control the question seen, not allowing users to go back using their browser controls, holding the state (incomplete quizzes) in sessions instead of the database, you might approach it like this:

    class QuizController extends AppController {
        # allow user to do a quiz
        function do($id) {
            # do something with each answer submitted
            if ($this->data) {
                // validate, save to database, or store in session until the end
                $questionNumber = $this->Session->read('Quiz.question');
                $this->Session->write('Quiz.question', $questionNumber + 1);
                $this->redirect(array('action' => 'do', $id));
            }
            # get the quiz (and questions)
            $quiz = $this->Quiz->find('first', array(
                'conditions' => array('Quiz.id' => $id),
                'contain' => array('Question'), // if using Containable
            ));
            # quiz doesn't exist
            if (!$quiz) {
                $this->cakeError('error404');
            }
            # get the question number
            $questionNumber = $this->Session->read('Quiz.question');
            # quiz hasn't been started
            if (!$questionNumber) {
                $questionNumber = 1;
                $this->Session->write('Quiz.question', $questionNumber);
            }
            # get the question
            $question = Set::extract("/Question[$questionNumber]", $quiz);
            # there are no more questions
            if (!$question) {
                // finalize quiz, save to database, redirect to obvious place
                $this->Session->setFlash('Quiz complete');
                $this->redirect(array('action' => 'index', $quiz['Quiz']['id']));
            }
            # set variables to the view
            $this->set(compact('quiz', 'question'));
        }
    }
    

    Note: In the example above I renamed the models to Quiz and Question to improve readabilty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题