diff --git a/course/modlib.php b/course/modlib.php index 4eb9d0656e2..5fae8ab5b98 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -491,12 +491,17 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) { } $completion = new completion_info($course); - if ($completion->is_enabled() && !empty($moduleinfo->completionunlocked)) { - // Update completion settings. - $cm->completion = $moduleinfo->completion; - $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; - $cm->completionview = $moduleinfo->completionview; - $cm->completionexpected = $moduleinfo->completionexpected; + if ($completion->is_enabled()) { + // Completion settings that would affect users who have already completed + // the activity may be locked; if so, these should not be updated. + if (!empty($moduleinfo->completionunlocked)) { + $cm->completion = $moduleinfo->completion; + $cm->completiongradeitemnumber = $moduleinfo->completiongradeitemnumber; + $cm->completionview = $moduleinfo->completionview; + } + // The expected date does not affect users who have completed the activity, + // so it is safe to update it regardless of the lock status. + $cm->completionexpected = $moduleinfo->completionexpected; } if (!empty($CFG->enableavailability)) { // This code is used both when submitting the form, which uses a long diff --git a/course/tests/behat/activities_edit_completion.feature b/course/tests/behat/activities_edit_completion.feature new file mode 100644 index 00000000000..e099938ae52 --- /dev/null +++ b/course/tests/behat/activities_edit_completion.feature @@ -0,0 +1,57 @@ +@core @core_course +Feature: Edit completion settings of an activity + In order to edit completion settings without accidentally breaking user data + As a teacher + I need to edit the activity and use the unlock button if required + + Background: + Given I log in as "admin" + And I set the following administration settings values: + | Enable completion tracking | 1 | + And I log out + And the following "courses" exist: + | fullname | shortname | enablecompletion | + | Course 1 | C1 | 1 | + And the following "activities" exist: + | activity | course | idnumber | name | intro | content | completion | completionview | + | page | C1 | x | TestPage | x | x | 2 | 1 | + And I log in as "admin" + And I follow "Course 1" + + Scenario: Completion is not locked when the activity has not yet been viewed + Given I turn editing mode on + And I click on "Edit settings" "link" in the "TestPage" activity + When I expand all fieldsets + Then I should see "Completion tracking" + And I should not see "Completion options locked" + + Scenario: Completion is locked after the activity has been viewed + Given I follow "TestPage" + When I follow "Edit settings" + And I expand all fieldsets + Then I should see "Completion options locked" + + @javascript + Scenario: Pressing the unlock button allows the user to edit completion settings + Given I follow "TestPage" + When I follow "Edit settings" + And I expand all fieldsets + And I press "Unlock completion options" + Then I should see "Completion options unlocked" + And I set the field "Completion tracking" to "Students can manually mark the activity as completed" + And I press "Save and display" + And I follow "Edit settings" + And I expand all fieldsets + Then the field "Completion tracking" matches value "Students can manually mark the activity as completed" + + @javascript + Scenario: Even when completion is locked, the user can still set the date + Given I follow "TestPage" + And I follow "Edit settings" + And I expand all fieldsets + When I click on "id_completionexpected_enabled" "checkbox" + And I set the field "id_completionexpected_year" to "2013" + And I press "Save and display" + And I follow "Edit settings" + And I expand all fieldsets + Then the field "id_completionexpected_year" matches value "2013"