Permalink
Browse files

MDL-13853 automatic fixing of incorrect grade category paths

  • Loading branch information...
1 parent 5ce3727 commit 52d6376cb9b94609b35342544d2b5425f65b32eb skodak committed Mar 8, 2008
Showing with 23 additions and 3 deletions.
  1. +23 −3 lib/grade/grade_category.php
@@ -224,7 +224,6 @@ function update($source=null) {
if ($result and $updatechildren) {
if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
foreach ($children as $child) {
- echo "updating $child->id ";
$child->path = null;
$child->depth = 0;
$child->update($source);
@@ -869,7 +868,28 @@ function get_children($include_category_items=false) {
// now find the requested category and connect categories as children
$category = false;
foreach ($cats as $catid=>$cat) {
- if (!empty($cat->parent)) {
+ if (empty($cat->parent)) {
+ if ($cat->path !== '/'.$cat->id.'/') {
+ $grade_category = new grade_category($cat, false);
+ $grade_category->path = '/'.$cat->id.'/';
+ $grade_category->depth = 1;
+ $grade_category->update('system');
+ return $this->get_children($include_category_items);
+ }
+ } else {
+ if (empty($cat->path) or !preg_match('|/'.$cat->parent.'/'.$cat->id.'/$|', $cat->path)) {
+ //fix paths and depts
+ static $recursioncounter = 0; // prevents infinite recursion
+ $recursioncounter++;
+ if ($recursioncounter < 5) {
+ // fix paths and depths!
+ $grade_category = new grade_category($cat, false);
+ $grade_category->depth = 0;
+ $grade_category->path = null;
+ $grade_category->update('system');
+ return $this->get_children($include_category_items);
+ }
+ }
// prevent problems with duplicate sortorders in db
$sortorder = $cat->sortorder;
while(array_key_exists($sortorder, $cats[$cat->parent]->children)) {
@@ -1044,7 +1064,7 @@ function set_parent($parentid, $source=null) {
$this->parent = $parent_category->id;
$this->parent_category =& $parent_category;
$this->path = null; // remove old path and depth - will be recalculated in update()
- $this->depth = null; // remove old path and depth - will be recalculated in update()
+ $this->depth = 0; // remove old path and depth - will be recalculated in update()
$this->update($source);
return $this->update($source);

0 comments on commit 52d6376

Please sign in to comment.