Activity completion MDL-24746 If you turn on autocompletion for an activity after...
authorSam Marshall <s.marshall@open.ac.uk>
Mon, 8 Nov 2010 11:51:19 +0000 (11:51 +0000)
committerSam Marshall <s.marshall@open.ac.uk>
Mon, 8 Nov 2010 11:51:19 +0000 (11:51 +0000)
course/modedit.php
course/moodleform_mod.php

index 36dd265..488a3af 100644 (file)
@@ -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;
index 468e66d..ffcc7fb 100644 (file)
@@ -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')) {