Skip to content

Commit

Permalink
MDL-45283 restore: Prevent failure when user course grade exists
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Frederic Massart committed Apr 30, 2014
1 parent 3f0a5f2 commit 98fed69
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,13 @@ protected function process_grade_grade($data) {
$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 {
$message = "Mapped user id not found for user id '{$olduserid}', grade item id '{$data->itemid}'";
$this->log($message, backup::LOG_DEBUG);
Expand Down

0 comments on commit 98fed69

Please sign in to comment.