From af729c3f5e384002f3c3dea0b840d66e5ad0e67d Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 9 Dec 2014 12:42:14 +0000 Subject: [PATCH] MDL-48452 Completion: Expected date doesn't save when form is locked The completion fields are locked when a user has already completed the task, to prevent you accidentally causing it to recalculate the data. This lock doesn't apply to the expected date field, as this doesn't affect user completion. However, changes to the field in this situation were incorrectly not saved. --- course/modlib.php | 17 ++++-- .../behat/activities_edit_completion.feature | 57 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 course/tests/behat/activities_edit_completion.feature diff --git a/course/modlib.php b/course/modlib.php index fa1ac35ac3a..9a44714785e 100644 --- a/course/modlib.php +++ b/course/modlib.php @@ -483,12 +483,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"