From 00aad1aee6812247c6a53fa69865a4b5d41c3340 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sun, 5 Feb 2006 21:50:37 +0000 Subject: [PATCH] Fixed a potential bug that would have hit if one quiz would have had two random questions for the same category, one of which was set to recurse into subcategories and the other not. At the same time this improves performance because no longer all the potential questions are loaded from the database but only their ids. --- mod/quiz/questiontypes/random/questiontype.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/mod/quiz/questiontypes/random/questiontype.php b/mod/quiz/questiontypes/random/questiontype.php index dfa8e23d4fd..c89f015dde1 100644 --- a/mod/quiz/questiontypes/random/questiontype.php +++ b/mod/quiz/questiontypes/random/questiontype.php @@ -42,7 +42,7 @@ class quiz_random_qtype extends quiz_default_questiontype { $quiz->questionsinuse = $attempt->layout; } - if (!isset($this->catrandoms[$question->category])) { + 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; @@ -53,23 +53,22 @@ class quiz_random_qtype extends quiz_default_questiontype { } else { $categorylist = $question->category; } - $this->catrandoms[$question->category] = get_records_sql - ("SELECT * FROM {$CFG->prefix}quiz_questions + $catrandoms = get_records_sql + ("SELECT id,id FROM {$CFG->prefix}quiz_questions WHERE category IN ($categorylist) AND parent = '0' AND id NOT IN ($quiz->questionsinuse) AND qtype NOT IN ($excludedtypes)"); - $this->catrandoms[$question->category] = - draw_rand_array($this->catrandoms[$question->category], - count($this->catrandoms[$question->category])); // from bug 1889 + $this->catrandoms[$question->category][$question->questiontext] = + draw_rand_array($catrandoms, count($catrandoms)); // from bug 1889 } while ($wrappedquestion = - array_pop($this->catrandoms[$question->category])) { + array_pop($this->catrandoms[$question->category][$question->questiontext])) { if (!ereg("(^|,)$wrappedquestion->id(,|$)", $quiz->questionsinuse)) { /// $randomquestion is not in use and will therefore be used /// as the randomquestion here... - + $wrappedquestion = get_record('quiz_questions', 'id', $wrappedquestion->id); global $QUIZ_QTYPES; $QUIZ_QTYPES[$wrappedquestion->qtype] ->get_question_options($wrappedquestion);