From 339ef4c24617239887c87034c57696842da3617e Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 7 Nov 2008 05:50:02 +0000 Subject: [PATCH] random questions: MDL-17160 Adding random questions, maximum option. 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 | 31 +++++++------ question/type/random/questiontype.php | 66 +++++++++++++++------------ 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/mod/quiz/edit.php b/mod/quiz/edit.php index 9fb1693105..063756e65d 100644 --- a/mod/quiz/edit.php +++ b/mod/quiz/edit.php @@ -65,22 +65,27 @@ * 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 .= '
'; + $out .= get_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true)); + $out .= ''; + $out .= ''; + $out .= ' '; + $out .= helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, '', true); } - $out = '
'; - $out .= get_string('addrandom', 'quiz', choose_from_menu($randomcount, 'randomcount', '1', '', '', '', true)); - $out .= ''; - $out .= "id\" />"; - $out .= ' '; - $out .= helpbutton('random', get_string('random', 'quiz'), 'quiz', true, false, '', true); - } else { - $out = ''; } return $out; } diff --git a/question/type/random/questiontype.php b/question/type/random/questiontype.php index 88fd27adb6..3cc08e0f5c 100644 --- a/question/type/random/questiontype.php +++ b/question/type/random/questiontype.php @@ -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 = ''. - get_string('toomanyrandom', 'quiz'). ''; + get_string('toomanyrandom', 'quiz'). ''; $question->qtype = 'description'; $state->responses = array('' => ''); return true; -- 2.39.5