]> git.mjollnir.org Git - moodle.git/commitdiff
random questions: MDL-17160 Adding random questions, maximum option.
authortjhunt <tjhunt>
Fri, 7 Nov 2008 05:50:02 +0000 (05:50 +0000)
committertjhunt <tjhunt>
Fri, 7 Nov 2008 05:50:02 +0000 (05:50 +0000)
When counting how many options to put in the add NN random questions dropdown, we should only count questions of types that are usable by random.

mod/quiz/edit.php
question/type/random/questiontype.php

index 9fb1693105682a4c33bb5edef152a91cb34a5ef6..063756e65d2c813f7f49fb2112777b23a83cc677 100644 (file)
      * Callback function called from question_list() function (which is called from showbank())
      */
     function module_specific_controls($totalnumber, $recurse, $category, $cmid){
+        global $QTYPES;
+        $out = '';
         $catcontext = get_context_instance_by_id($category->contextid);
         if (has_capability('moodle/question:useall', $catcontext)){
-            for ($i = 1;$i <= min(10, $totalnumber); $i++) {
-                $randomcount[$i] = $i;
-            }
-            for ($i = 20;$i <= min(100, $totalnumber); $i += 10) {
-                $randomcount[$i] = $i;
+            $randomusablequestions = $QTYPES['random']->get_usable_questions_from_category(
+                    $category->id, $recurse, '0');
+            $maxrand = count($randomusablequestions);
+            if ($maxrand > 0) {
+                for ($i = 1;$i <= min(10, $maxrand); $i++) {
+                    $randomcount[$i] = $i;
+                }
+                for ($i = 20;$i <= min(100, $maxrand); $i += 10) {
+                    $randomcount[$i] = $i;
+                }
+                $out .= '<br />';
+                $out .= get_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
+                $out .= '<input type="hidden" name="recurse" value="'.$recurse.'" />';
+                $out .= '<input type="hidden" name="categoryid" value="'.$category->id.'" />';
+                $out .= ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
+                $out .= helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, '', true);
             }
-            $out = '<br />';
-            $out .= get_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true));
-            $out .= '<input type="hidden" name="recurse" value="'.$recurse.'" />';
-            $out .= "<input type=\"hidden\" name=\"categoryid\" value=\"$category->id\" />";
-            $out .= ' <input type="submit" name="addrandom" value="'. get_string('add') .'" />';
-            $out .= helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, '', true);
-        } else {
-            $out = '';
         }
         return $out;
     }
index 88fd27adb68313d76449fc49390a14729ddfd7d3..3cc08e0f5c99c88d6507976437087555e140faa1 100644 (file)
@@ -72,9 +72,34 @@ class random_qtype extends default_questiontype {
         return ($DB->set_field('question', 'parent', $question->id, array('id' => $question->id)) ? true : false);
     }
 
+    /**
+     * Get all the usable questions from a particular question category.
+     *
+     * @param integer $categoryid the id of a question category.
+     * @param boolean whether to include questions from subcategories.
+     * @param string $questionsinuse comma-separated list of question ids to exclude from consideration.
+     * @return array of question records.
+     */
+    function get_usable_questions_from_category($categoryid, $subcategories, $questionsinuse) {
+        global $QTYPE_EXCLUDE_FROM_RANDOM, $DB;
+        if ($subcategories) {
+            $categorylist = question_categorylist($categoryid);
+        } else {
+            $categorylist = $categoryid;
+        }
+        if (!$catrandoms = $DB->get_records_select('question',
+                "category IN ($categorylist)
+                     AND parent = '0'
+                     AND hidden = '0'
+                     AND id NOT IN ($questionsinuse)
+                     AND qtype NOT IN ($QTYPE_EXCLUDE_FROM_RANDOM)", null, '', 'id')) {
+            $catrandoms = array();
+        }
+        return $catrandoms;
+    }
+
     function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
-        global $DB;
-        global $QTYPE_EXCLUDE_FROM_RANDOM;
+        global $QTYPES, $DB;
         // Choose a random question from the category:
         // We need to make sure that no question is used more than once in the
         // quiz. Therfore the following need to be excluded:
@@ -87,49 +112,32 @@ class random_qtype extends default_questiontype {
         }
 
         if (!isset($this->catrandoms[$question->category][$question->questiontext])) {
-            // Need to fetch random questions from category $question->category"
-            // (Note: $this refers to the questiontype, not the question.)
-            global $CFG;
-            if ($question->questiontext == "1") {
-                // recurse into subcategories
-                $categorylist = question_categorylist($question->category);
-            } else {
-                $categorylist = $question->category;
-            }
-            if ($catrandoms = $DB->get_records_select('question',
-                    "category IN ($categorylist)
-                         AND parent = '0'
-                         AND hidden = '0'
-                         AND id NOT IN ($cmoptions->questionsinuse)
-                         AND qtype NOT IN ($QTYPE_EXCLUDE_FROM_RANDOM)", array(), '', 'id')) {
-                $this->catrandoms[$question->category][$question->questiontext] =
-                        draw_rand_array($catrandoms, count($catrandoms));
-            } else {
-                $this->catrandoms[$question->category][$question->questiontext] = array();
-            }
+            $this->catrandoms[$question->category][$question->questiontext] =
+                    $this->get_usable_questions_from_category($question->category,
+                    $question->questiontext == "1", $cmoptions->questionsinuse);
         }
 
-        while ($wrappedquestion =
-                array_pop($this->catrandoms[$question->category][$question->questiontext])) {
+        while ($wrappedquestion = array_pop(
+                $this->catrandoms[$question->category][$question->questiontext])) {
             if (!ereg("(^|,)$wrappedquestion->id(,|$)", $cmoptions->questionsinuse)) {
                 /// $randomquestion is not in use and will therefore be used
                 /// as the randomquestion here...
                 $wrappedquestion = $DB->get_record('question', array('id' => $wrappedquestion->id));
                 global $QTYPES;
                 $QTYPES[$wrappedquestion->qtype]
-                 ->get_question_options($wrappedquestion);
+                        ->get_question_options($wrappedquestion);
                 $QTYPES[$wrappedquestion->qtype]
-                 ->create_session_and_responses($wrappedquestion,
-                 $state, $cmoptions, $attempt);
+                        ->create_session_and_responses($wrappedquestion,
+                        $state, $cmoptions, $attempt);
                 $wrappedquestion->name_prefix = $question->name_prefix;
-                $wrappedquestion->maxgrade    = $question->maxgrade;
+                $wrappedquestion->maxgrade = $question->maxgrade;
                 $cmoptions->questionsinuse .= ",$wrappedquestion->id";
                 $state->options->question = &$wrappedquestion;
                 return true;
             }
         }
         $question->questiontext = '<span class="notifyproblem">'.
-         get_string('toomanyrandom', 'quiz'). '</span>';
+                get_string('toomanyrandom', 'quiz'). '</span>';
         $question->qtype = 'description';
         $state->responses = array('' => '');
         return true;