Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-35547 Detect loops in question categories.

  • Loading branch information...
commit 2ade1b52f9f44ca80aff60da5ec8b325b2f8fc6f 1 parent 24d9d18
Tomasz Muras tmuras authored

Showing 1 changed file with 17 additions and 9 deletions. Show diff stats Hide diff stats

  1. +17 9 lib/questionlib.php
26 lib/questionlib.php
@@ -1179,16 +1179,24 @@ function question_add_tops($categories, $pcontexts) {
1179 1179 function question_categorylist($categoryid) {
1180 1180 global $DB;
1181 1181
1182   - $subcategories = $DB->get_records('question_categories',
1183   - array('parent' => $categoryid), 'sortorder ASC', 'id, 1');
1184   -
1185   - $categorylist = array($categoryid);
1186   - foreach ($subcategories as $subcategory) {
1187   - if($subcategory->id == $categoryid)
1188   - //prevent infinite loop
1189   - throw new coding_exception("Question category $categoryid points to itself as a parent.");
  1182 + //final list of category IDs
  1183 + $categorylist = array();
  1184 +
  1185 + //a list of category IDs to check for any sub-categories
  1186 + $templist = array($categoryid);
  1187 +
  1188 + while ($current = array_shift($templist)) {
  1189 + if (isset($categorylist[$current])) {
  1190 + throw new coding_exception("Category id=$current is already on the list - loop of categories detected.");
1190 1191 }
1191   - $categorylist = array_merge($categorylist, question_categorylist($subcategory->id));
  1192 +
  1193 + $subcategories = $DB->get_records('question_categories',
  1194 + array('parent' => $current), 'sortorder ASC', 'id, 1');
  1195 + foreach ($subcategories as $subcategory) {
  1196 + $templist[] = $subcategory->id;
  1197 + }
  1198 +
  1199 + $categorylist[$current] = $current;
1192 1200 }
1193 1201
1194 1202 return $categorylist;

0 comments on commit 2ade1b5

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