diff --git a/backup/moodle2/restore_final_task.class.php b/backup/moodle2/restore_final_task.class.php index e355871a622..124208a48aa 100644 --- a/backup/moodle2/restore_final_task.class.php +++ b/backup/moodle2/restore_final_task.class.php @@ -61,11 +61,8 @@ class restore_final_task extends restore_task { $this->add_step(new restore_grade_history_structure_step('grade_history', 'grade_history.xml')); } - // Course completion, executed conditionally if restoring to new course - if ($this->get_target() !== backup::TARGET_CURRENT_ADDING && - $this->get_target() !== backup::TARGET_EXISTING_ADDING) { - $this->add_step(new restore_course_completion_structure_step('course_completion', 'completion.xml')); - } + // Course completion. + $this->add_step(new restore_course_completion_structure_step('course_completion', 'completion.xml')); // Conditionally restore course badges. if ($this->get_setting_value('badges')) { diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index d779cfacf5b..ca6610287bc 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -2453,11 +2453,12 @@ class restore_course_completion_structure_step extends restore_structure_step { * 2. The backup includes course completion information * 3. All modules are restorable * 4. All modules are marked for restore. + * 5. No completion criteria already exist for the course. * * @return bool True is safe to execute, false otherwise */ protected function execute_condition() { - global $CFG; + global $CFG, $DB; // First check course completion is enabled on this site if (empty($CFG->enablecompletion)) { @@ -2483,11 +2484,16 @@ class restore_course_completion_structure_step extends restore_structure_step { return false; } - // Finally check all modules within the backup are being restored. + // Check all modules within the backup are being restored. if ($this->task->is_excluding_activities()) { return false; } + // Check that no completion criteria is already set for the course. + if ($DB->record_exists('course_completion_criteria', array('course' => $this->get_courseid()))) { + return false; + } + return true; }