Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-35547 Detect loops in question categories.

  • Loading branch information...
commit 2ade1b52f9f44ca80aff60da5ec8b325b2f8fc6f 1 parent 24d9d18
Tomasz Muras tmuras authored
Showing with 17 additions and 9 deletions.
  1. +17 −9 lib/questionlib.php
26 lib/questionlib.php
View
@@ -1179,16 +1179,24 @@ function question_add_tops($categories, $pcontexts) {
function question_categorylist($categoryid) {
global $DB;
- $subcategories = $DB->get_records('question_categories',
- array('parent' => $categoryid), 'sortorder ASC', 'id, 1');
-
- $categorylist = array($categoryid);
- foreach ($subcategories as $subcategory) {
- if($subcategory->id == $categoryid)
- //prevent infinite loop
- throw new coding_exception("Question category $categoryid points to itself as a parent.");
+ //final list of category IDs
+ $categorylist = array();
+
+ //a list of category IDs to check for any sub-categories
+ $templist = 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.");
}
- $categorylist = array_merge($categorylist, question_categorylist($subcategory->id));
+
+ $subcategories = $DB->get_records('question_categories',
+ array('parent' => $current), 'sortorder ASC', 'id, 1');
+ foreach ($subcategories as $subcategory) {
+ $templist[] = $subcategory->id;
+ }
+
+ $categorylist[$current] = $current;
}
return $categorylist;
Please sign in to comment.
Something went wrong with that request. Please try again.