From a60835c721145a13aeb23e58345afc0c561f4aa9 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 20 Jan 2012 15:53:05 +0000 Subject: [PATCH] MDL-31314 gradebook restore: fix category.depth on restore. Without this, restoring backups made with the OU's custom 'restore from 1.9' feature, and possibly other people's custom converstion code, does not work properly. Also, fix poor recordset code. --- backup/moodle2/restore_stepslib.php | 41 +++++++++++++---------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 14d130afa3b..2aebdcfbc1c 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -371,40 +371,35 @@ class restore_gradebook_structure_step extends restore_structure_step { } $rs->close(); - //need to correct the grade category path and parent + // Need to correct the grade category path and parent $conditions = array( 'courseid' => $this->get_courseid() ); - $grade_category = new stdclass(); $rs = $DB->get_recordset('grade_categories', $conditions); - if (!empty($rs)) { - //get all the parents correct first as grade_category::build_path() loads category parents from the DB - foreach($rs as $gc) { - if (!empty($gc->parent)) { - $grade_category->id = $gc->id; - $grade_category->parent = $this->get_mappingid('grade_category', $gc->parent); - $DB->update_record('grade_categories', $grade_category); - } - } - } - if (isset($grade_category->parent)) { - unset($grade_category->parent); - } - $rs->close(); - - $rs = $DB->get_recordset('grade_categories', $conditions); - if (!empty($rs)) { - //now we can rebuild all the paths - foreach($rs as $gc) { + // Get all the parents correct first as grade_category::build_path() loads category parents from the DB + foreach ($rs as $gc) { + if (!empty($gc->parent)) { + $grade_category = new stdClass(); $grade_category->id = $gc->id; - $grade_category->path = grade_category::build_path($gc); + $grade_category->parent = $this->get_mappingid('grade_category', $gc->parent); $DB->update_record('grade_categories', $grade_category); } } $rs->close(); - //Restore marks items as needing update. Update everything now. + // Now we can rebuild all the paths + $rs = $DB->get_recordset('grade_categories', $conditions); + foreach ($rs as $gc) { + $grade_category = new stdClass(); + $grade_category->id = $gc->id; + $grade_category->path = grade_category::build_path($gc); + $grade_category->depth = substr_count($grade_category->path, '/') - 1; + $DB->update_record('grade_categories', $grade_category); + } + $rs->close(); + + // Restore marks items as needing update. Update everything now. grade_regrade_final_grades($this->get_courseid()); } }