Skip to content

Commit

Permalink
MDL-30466 Backup: Fixed duplicate completion record check error
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Nov 29, 2011
1 parent 50ce778 commit b367f41
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -2237,8 +2237,6 @@ protected function process_availability($data) {
* - Activity includes completion info (file_exists) * - Activity includes completion info (file_exists)
*/ */
class restore_userscompletion_structure_step extends restore_structure_step { class restore_userscompletion_structure_step extends restore_structure_step {
private $done = array();

/** /**
* To conditionally decide if this step must be executed * To conditionally decide if this step must be executed
* Note the "settings" conditions are evaluated in the * Note the "settings" conditions are evaluated in the
Expand Down Expand Up @@ -2282,15 +2280,14 @@ protected function process_completion($data) {
$data->userid = $this->get_mappingid('user', $data->userid); $data->userid = $this->get_mappingid('user', $data->userid);
$data->timemodified = $this->apply_date_offset($data->timemodified); $data->timemodified = $this->apply_date_offset($data->timemodified);


// Find the existing record
$existing = $DB->get_record('course_modules_completion', array(
'coursemoduleid' => $data->coursemoduleid,
'userid' => $data->userid), 'id, timemodified');
// Check we didn't already insert one for this cmid and userid // Check we didn't already insert one for this cmid and userid
// (there aren't supposed to be duplicates in that field, but // (there aren't supposed to be duplicates in that field, but
// it was possible until MDL-28021 was fixed). // it was possible until MDL-28021 was fixed).
$key = $data->coursemoduleid . ',' . $data->userid; if ($existing) {
if (array_key_exists($key, $this->done)) {
// Find the existing record
$existing = $DB->get_record('course_modules_completion', array(
'coursemoduleid' => $data->coursemoduleid,
'userid' => $data->userid), 'id, timemodified');
// Update it to these new values, but only if the time is newer // Update it to these new values, but only if the time is newer
if ($existing->timemodified < $data->timemodified) { if ($existing->timemodified < $data->timemodified) {
$data->id = $existing->id; $data->id = $existing->id;
Expand All @@ -2299,16 +2296,13 @@ protected function process_completion($data) {
} else { } else {
// Normal entry where it doesn't exist already // Normal entry where it doesn't exist already
$DB->insert_record('course_modules_completion', $data); $DB->insert_record('course_modules_completion', $data);
// Remember this entry
$this->done[$key] = true;
} }
} }


protected function after_execute() { protected function after_execute() {
// This gets called once per activity (according to my testing). // This gets called once per activity (according to my testing).
// Clearing the array isn't strictly required, but avoids using // Clearing the array isn't strictly required, but avoids using
// unnecessary memory. // unnecessary memory.
$this->done = array();
} }
} }


Expand Down

0 comments on commit b367f41

Please sign in to comment.