MDL-50373 questions: random Qs must not pick deleted or sub- questions
authorTim Hunt <T.J.Hunt@open.ac.uk>
Wed, 27 May 2015 18:50:05 +0000 (19:50 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Wed, 27 May 2015 19:32:47 +0000 (20:32 +0100)
This was a regression caused by MDL-6340. I missed the necessary
    AND parent = 0 AND hidden = 0
in one query of the question table.

question/engine/bank.php
question/tests/random_question_loader_test.php

index 04e598b..8a0250a 100644 (file)
@@ -536,7 +536,10 @@ class question_finder implements cache_data_source {
 
                       FROM {question} q
 
-                     WHERE q.category $qcsql $extraconditions
+                     WHERE q.category {$qcsql}
+                       AND q.parent = 0
+                       AND q.hidden = 0
+                      {$extraconditions}
 
                   ORDER BY previous_attempts
                 ", $qubaids->from_where_params() + $qcparams + $extraparams);
index 335afa4..b3a3389 100644 (file)
@@ -61,6 +61,45 @@ class random_question_loader_testcase extends advanced_testcase {
         $this->assertNull($loader->get_next_question_id($cat->id, 0));
     }
 
+    public function test_hidden_questions_not_returned() {
+        global $DB;
+        $this->resetAfterTest();
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
+
+        $cat = $generator->create_question_category();
+        $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
+        $DB->set_field('question', 'hidden', 1, array('id' => $question1->id));
+        $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
+
+        $this->assertNull($loader->get_next_question_id($cat->id, 0));
+    }
+
+    public function test_cloze_subquestions_not_returned() {
+        $this->resetAfterTest();
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
+
+        $cat = $generator->create_question_category();
+        $question1 = $generator->create_question('multianswer', null, array('category' => $cat->id));
+        $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
+
+        $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
+        $this->assertNull($loader->get_next_question_id($cat->id, 0));
+    }
+
+    public function test_random_questions_not_returned() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
+
+        $cat = $generator->create_question_category();
+        $course = $this->getDataGenerator()->create_course();
+        $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course));
+        quiz_add_random_questions($quiz, 1, $cat->id, 1, false);
+        $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
+
+        $this->assertNull($loader->get_next_question_id($cat->id, 0));
+    }
+
     public function test_one_question_category_returns_that_q_then_null() {
         $this->resetAfterTest();
         $generator = $this->getDataGenerator()->get_plugin_generator('core_question');