$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;
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;
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')) {