From 166102e9b3b808311063584cc4ebd4b3f4070e90 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 10 Apr 2013 08:28:05 +0800 Subject: [PATCH] MDL-29877 core_grade:add some upgrade code to fix broken grade items introduced by bad restores --- lib/db/upgrade.php | 10 ++++++++++ lib/grade/grade_category.php | 8 ++++++-- version.php | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 528ef1426bb..0af3ca9fb0f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1673,5 +1673,15 @@ function xmldb_main_upgrade($oldversion) { // No save point needed for this change } + if ($oldversion < 2012120301.18) { + // 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, 2012120301.18); + } + return true; } diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 87c494ffe26..1597d697a6d 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -1154,13 +1154,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++; } diff --git a/version.php b/version.php index a6e9804bef9..119bf1a1476 100644 --- a/version.php +++ b/version.php @@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2012120301.17; // 20121203 = branching date YYYYMMDD - do not modify! +$version = 2012120301.18; // 20121203 = branching date YYYYMMDD - do not modify! // RR = release increments - 00 in DEV branches // .XX = incremental changes