Skip to content
Browse files

MDL-35547 Fetch more sub-categories with one SQL query.

  • Loading branch information...
1 parent 0f76ac4 commit e80c64ab9d444dca2d5181c8b29cd918b3760aed @tmuras tmuras committed Sep 23, 2012
Showing with 13 additions and 12 deletions.
  1. +13 −12 lib/questionlib.php
View
25 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;

0 comments on commit e80c64a

Please sign in to comment.
Something went wrong with that request. Please try again.