From 63709b0e162363d4d41d47e38af5c1db17fcdd0c Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 6 Apr 2017 08:54:00 +0800 Subject: [PATCH] MDL-23919 mod_data: fix bug when completion is reset --- mod/data/lib.php | 15 +++++++++------ .../completion_condition_entries.feature | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/mod/data/lib.php b/mod/data/lib.php index 8d0aa6cf5d2..dc6937e99e2 100644 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -4166,21 +4166,24 @@ function data_update_completion_state($data, $course, $cm) { * to the function name. This is why there are unused parameters. * * @since Moodle 3.3 - * @param object $course Course - * @param object $cm course-module + * @param stdClass $course Course + * @param cm_info|stdClass $cm course-module * @param int $userid User ID * @param bool $type Type of comparison (or/and; can be used as return value if no conditions) * @return bool True if completed, false if not, $type if conditions not set. */ function data_get_completion_state($course, $cm, $userid, $type) { - global $DB; + global $DB, $PAGE; $result = $type; // Default return value // Get data details. - $data = $DB->get_record('data', array('id' => $cm->instance), '*', MUST_EXIST); + if (isset($PAGE->cm->id) && $PAGE->cm->id == $cm->id) { + $data = $PAGE->activityrecord; + } else { + $data = $DB->get_record('data', array('id' => $cm->instance), '*', MUST_EXIST); + } // If completion option is enabled, evaluate it and return true/false. if ($data->completionentries) { - $data = $DB->get_record('data', array('id' => $cm->instance)); - $numentries = data_numentries($data); + $numentries = data_numentries($data, $userid); // Check the number of entries required against the number of entries already made. if ($numentries >= $data->completionentries) { $result = true; diff --git a/mod/data/tests/behat/completion_condition_entries.feature b/mod/data/tests/behat/completion_condition_entries.feature index c53a30c613b..a696806c991 100644 --- a/mod/data/tests/behat/completion_condition_entries.feature +++ b/mod/data/tests/behat/completion_condition_entries.feature @@ -4,14 +4,14 @@ Feature: Set entries required as a completion condition for a data item As a teacher I need to set entries required to mark the database activity as completed -Scenario: +Scenario: Two entries required to complete the activity Given the following "users" exist: | username | firstname | lastname | email | | student1 | Student | 1 | student1@example.com | | teacher1 | Teacher | 1 | teacher1@example.com | And the following "courses" exist: - | fullname | shortname | category | - | Course 1 | C1 | 0 | + | fullname | shortname | enablecompletion | + | Course 1 | C1 | 1 | And the following "course enrolments" exist: | user | course | role | | teacher1 | C1 | editingteacher | @@ -19,11 +19,6 @@ Scenario: And I log in as "teacher1" And I follow "C1" And I turn editing mode on - And I navigate to "Edit settings" in current page administration - And I set the following fields to these values: - | Enable completion tracking | Yes | - And I press "Save and display" - #Two entries required to complete the activity And I add a "Database" to section "1" and I fill the form with: | Name | Test database name | | Description | Test database description | @@ -56,4 +51,12 @@ Scenario: And I log in as "teacher1" And I follow "C1" Then "Student 1" user has completed "Test database name" activity + And I follow "Course 1" + And I follow "Test database name" + And I navigate to "Edit settings" in current page administration + And I press "Unlock completion" + And I set the field "completionentries" to "1" + And I press "Save and display" + And I follow "C1" + Then "Student 1" user has completed "Test database name" activity And I log out