From e80c64ab9d444dca2d5181c8b29cd918b3760aed Mon Sep 17 00:00:00 2001 From: Tomasz Muras Date: Sun, 23 Sep 2012 19:25:30 +0200 Subject: [PATCH] MDL-35547 Fetch more sub-categories with one SQL query. --- lib/questionlib.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/questionlib.php b/lib/questionlib.php index 61a21185137..8d0fdb69176 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -1179,24 +1179,25 @@ function question_add_tops($categories, $pcontexts) { function question_categorylist($categoryid) { global $DB; - //final list of category IDs + // final list of category IDs $categorylist = array(); - //a list of category IDs to check for any sub-categories - $templist = array($categoryid); + // a list of category IDs to check for any sub-categories + $subcategories = array($categoryid); - while ($current = array_shift($templist)) { - if (isset($categorylist[$current])) { - throw new coding_exception("Category id=$current is already on the list - loop of categories detected."); - } - - $subcategories = $DB->get_records('question_categories', - array('parent' => $current), 'sortorder ASC', 'id, 1'); + while ($subcategories) { foreach ($subcategories as $subcategory) { - $templist[] = $subcategory->id; + // if anything from the temporary list was added already, then we have a loop + if (isset($categorylist[$subcategory])) { + throw new coding_exception("Category id=$subcategory is already on the list - loop of categories detected."); + } + $categorylist[$subcategory] = $subcategory; } - $categorylist[$current] = $current; + list ($in, $params) = $DB->get_in_or_equal($subcategories); + + $subcategories = $DB->get_records_select_menu('question_categories', + "parent $in", $params, NULL, 'id,id AS id2'); } return $categorylist;