MDL-29877 core_grade:add some upgrade code to fix broken grade items introduced by...
authorAndrew Davis <andrew@moodle.com>
Wed, 10 Apr 2013 00:34:04 +0000 (08:34 +0800)
committerAndrew Davis <andrew@moodle.com>
Wed, 10 Apr 2013 00:34:04 +0000 (08:34 +0800)
lib/db/upgrade.php
lib/grade/grade_category.php
version.php

index 6233a52..5ff29f7 100644 (file)
@@ -1142,5 +1142,15 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2012062506.02);
     }
 
+    if ($oldversion < 2012062506.04) {
+        // MDL-29877 Some bad restores created grade items with no category information.
+        $sql = "UPDATE mdl_grade_items
+                   SET categoryid = courseid
+                 WHERE itemtype <> 'course' and itemtype <> 'category'
+                       AND categoryid IS NULL";
+        $DB->execute($sql);
+        upgrade_main_savepoint(true, 2012062506.04);
+    }
+
     return true;
 }
index 6f3deff..183c564 100644 (file)
@@ -1152,13 +1152,17 @@ class grade_category extends grade_object {
 
             } else {
                 $categoryid = $item->categoryid;
+                if (empty($categoryid)) {
+                    debugging('Found a grade item that isnt in a category');
+                }
             }
 
             // prevent problems with duplicate sortorders in db
             $sortorder = $item->sortorder;
 
-            while (array_key_exists($sortorder, $cats[$categoryid]->children)) {
-                //debugging("$sortorder exists in item loop");
+            while (array_key_exists($categoryid, $cats) 
+                && array_key_exists($sortorder, $cats[$categoryid]->children)) {
+
                 $sortorder++;
             }
 
index d744a25..ffa1c18 100644 (file)
@@ -30,7 +30,7 @@
 defined('MOODLE_INTERNAL') || die();
 
 
-$version  = 2012062506.03;              // YYYYMMDD      = weekly release date of this DEV branch
+$version  = 2012062506.04;              // YYYYMMDD      = weekly release date of this DEV branch
                                         //         RR    = release increments - 00 in DEV branches
                                         //           .XX = incremental changes