diff --git a/course/modedit.php b/course/modedit.php index 36dd26596d1..488a3afcc0a 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -290,13 +290,7 @@ if ($mform->is_cancelled()) { $completion = new completion_info($course); if ($completion->is_enabled()) { - // Handle completion settings. If necessary, wipe existing completion - // data first. - if (!empty($fromform->completionunlocked)) { - $completion = new completion_info($course); - $completion->reset_all_state($cm); - } - + // Update completion settings $cm->completion = $fromform->completion; $cm->completiongradeitemnumber = $fromform->completiongradeitemnumber; $cm->completionview = $fromform->completionview; @@ -340,6 +334,12 @@ if ($mform->is_cancelled()) { set_coursemodule_idnumber($fromform->coursemodule, $fromform->cmidnumber); } + // Now that module is fully updated, also update completion data if + // required (this will wipe all user completion data and recalculate it) + if ($completion->is_enabled() && !empty($fromform->completionunlocked)) { + $completion->reset_all_state($cm); + } + // Trigger mod_updated event with information about this module. $eventdata = new stdClass(); $eventdata->modulename = $fromform->modulename; diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index 468e66d806c..ffcc7fb2f0a 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -199,6 +199,10 @@ abstract class moodleform_mod extends moodleform { if ($mform->elementExists('unlockcompletion')) { $mform->removeElement('unlockcompletion'); } + // Automatically set to unlocked (note: this is necessary + // in order to make it recalculate completion once the option + // is changed, maybe someone has completed it now) + $mform->getElement('completionunlocked')->setValue(1); } else { // Has the element been unlocked? if ($mform->exportValue('unlockcompletion')) {