From 7a6ca49af5ae52ca82f328b37a86822db5ee10ff Mon Sep 17 00:00:00 2001 From: Joshua Johnston Date: Mon, 11 Aug 2014 17:16:30 -0400 Subject: [PATCH] MDL-46651 Backup: Handle duplicate course completions during restore Cron can cause duplicate completion records if triggered during a long running restore process. If this is the case, replace the new record with the record from the backup file --- backup/moodle2/restore_stepslib.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index bd0b08fde1b..1380f204e0a 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -2630,7 +2630,21 @@ class restore_course_completion_structure_step extends restore_structure_step { 'timecompleted' => $this->apply_date_offset($data->timecompleted), 'reaggregate' => $data->reaggregate ); - $DB->insert_record('course_completions', $params); + + $existing = $DB->get_record('course_completions', array( + 'userid' => $data->userid, + 'course' => $data->course + )); + + // MDL-46651 - If cron writes out a new record before we get to it + // then we should replace it with the Truth data from the backup. + // This may be obsolete after MDL-48518 is resolved + if ($existing) { + $params['id'] = $existing->id; + $DB->update_record('course_completions', $params); + } else { + $DB->insert_record('course_completions', $params); + } } }