* 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;
}
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:
}
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;