From c9d7e1d2a42c6d19cabe2c09a60e4a0da792b281 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 30 Apr 2014 12:09:13 +0800 Subject: [PATCH] MDL-45283 restore: Prevent failure when user course grade exists When we restore a course, the grade that a user has for the grade item of the course cannot be created twice. If we try to create it when it already exists, a database exception is raised because of index duplication. The safest way to proceed is certainly to ignore the grade information from the backup, otherwise we could potentially overwrite existing information. Though, this only applies to restore as merge, and it does not affect the update of the course grade. --- backup/moodle2/restore_stepslib.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 14c7ca7f7be..f1072a198b7 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -234,7 +234,13 @@ class restore_gradebook_structure_step extends restore_structure_step { $data->timecreated = $this->apply_date_offset($data->timecreated); $data->timemodified = $this->apply_date_offset($data->timemodified); - $newitemid = $DB->insert_record('grade_grades', $data); + $gradeexists = $DB->record_exists('grade_grades', array('userid' => $data->userid, 'itemid' => $data->itemid)); + if ($gradeexists) { + $message = "User id '{$data->userid}' already has a grade entry for grade item id '{$data->itemid}'"; + $this->log($message, backup::LOG_DEBUG); + } else { + $newitemid = $DB->insert_record('grade_grades', $data); + } } else { debugging("Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'"); }