Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit 237897b9681e1d0a24b9a43837f92f479c08e659 1 parent 8c39dfb
@tmuras tmuras authored
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;
Please sign in to comment.
Something went wrong with that request. Please try again.