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.
This commit is contained in:
gustav_delius
2006-02-05 21:50:37 +00:00
parent e034804630
commit 00aad1aee6
@@ -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);