From 1f68fe635ff85f2d2d759abe67b8161070946252 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Wed, 26 Jul 2023 17:59:02 +0200 Subject: [PATCH] MDL-78530 core_completion: Use site default completion or none When creating a new activity we should use course level default completion and site level completion if there is no course level default completion. For non defined default completion values use COMPLETION_TRACKING_NONE instead of COMPLETION_TRACKING_MANUAL. --- completion/classes/form/form_trait.php | 12 +--- completion/classes/manager.php | 22 +++--- .../site_default_activity_completion.feature | 71 +++++++++++++++++++ 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/completion/classes/form/form_trait.php b/completion/classes/form/form_trait.php index 90e5eabf6e3..3986d68d3d6 100644 --- a/completion/classes/form/form_trait.php +++ b/completion/classes/form/form_trait.php @@ -115,7 +115,7 @@ trait form_trait { bool $defaultcompletion = true, ?int $courseid = null ): void { - global $CFG, $SITE; + global $SITE; $mform = $this->get_form(); if ($modname === null) { @@ -125,7 +125,6 @@ trait form_trait { $supportviews = plugin_supports('mod', $modname, FEATURE_COMPLETION_TRACKS_VIEWS, false); $supportgrades = plugin_supports('mod', $modname, FEATURE_GRADE_HAS_GRADE, false); $rating = $this->_features->rating; - $defaultcompletion = $CFG->completiondefault && $this->_features->defaultcompletion; } else { throw new \coding_exception('You must specify the modname parameter if you are not using a moodleform_mod.'); } @@ -139,15 +138,6 @@ trait form_trait { $mform->setType('completionunlocked', PARAM_INT); $trackingdefault = COMPLETION_TRACKING_NONE; - // If system and activity default completion is on, set it. - if ($defaultcompletion) { - $hasrules = plugin_supports('mod', $modname, FEATURE_COMPLETION_HAS_RULES, true); - if ($hasrules || $supportviews) { - $trackingdefault = COMPLETION_TRACKING_AUTOMATIC; - } else { - $trackingdefault = COMPLETION_TRACKING_MANUAL; - } - } // Get the sufix to add to the completion elements name. $suffix = $this->get_suffix(); diff --git a/completion/classes/manager.php b/completion/classes/manager.php index 64e5bb710cc..896fb4acf8a 100644 --- a/completion/classes/manager.php +++ b/completion/classes/manager.php @@ -559,9 +559,18 @@ class manager { * @return stdClass */ public static function get_default_completion($course, $module, $flatten = true, string $suffix = '') { - global $DB, $CFG; - if ($data = $DB->get_record('course_completion_defaults', ['course' => $course->id, 'module' => $module->id], - 'completion, completionview, completionexpected, completionusegrade, completionpassgrade, customrules')) { + global $DB, $CFG, $SITE; + + $fields = 'completion, completionview, completionexpected, completionusegrade, completionpassgrade, customrules'; + // Check course default completion values. + $params = ['course' => $course->id, 'module' => $module->id]; + $data = $DB->get_record('course_completion_defaults', $params, $fields); + if (!$data && $course->id != $SITE->id) { + // If there is no course default completion, check site level default completion values ($SITE->id). + $params['course'] = $SITE->id; + $data = $DB->get_record('course_completion_defaults', $params, $fields); + } + if ($data) { if ($data->customrules && ($customrules = @json_decode($data->customrules, true))) { // MDL-72375 This will override activity id for new mods. Skip this field, it is already exposed as courseid. unset($customrules['id']); @@ -578,13 +587,6 @@ class manager { } else { $data = new stdClass(); $data->completion = COMPLETION_TRACKING_NONE; - if ($CFG->completiondefault) { - $completion = new \completion_info(get_fast_modinfo($course->id)->get_course()); - if ($completion->is_enabled() && plugin_supports('mod', $module->name, FEATURE_MODEDIT_DEFAULT_COMPLETION, true)) { - $data->completion = COMPLETION_TRACKING_MANUAL; - $data->completionview = 1; - } - } } // If the suffix is not empty, the completion rules need to be renamed to avoid conflicts. diff --git a/completion/tests/behat/site_default_activity_completion.feature b/completion/tests/behat/site_default_activity_completion.feature index 21b6d9d231a..e78c56b70e9 100644 --- a/completion/tests/behat/site_default_activity_completion.feature +++ b/completion/tests/behat/site_default_activity_completion.feature @@ -4,6 +4,77 @@ Feature: Allow admins to edit the default activity completion rules at site leve As an admin I need to be able to edit the completion rules for a group of activities at site level. + Background: + Given the following "courses" exist: + | fullname | shortname | category | enablecompletion | + | Course 1 | C1 | 0 | 1 | + And I log in as "admin" + + @javascript + Scenario: Default activity completion rules with no site or course default completion + Given the following "activity" exists: + | activity | assign | + | course | C1 | + | name | Test assignment one | + | completion | 1 | + And I am on "Course 1" course homepage with editing mode on + When I add a "Assignment" to section "0" + And I expand all fieldsets + # Completion tracking 0 = Do not indicate activity completion. + Then the field "Completion tracking" matches value "0" + # Default values don't affect existing activities. + But I am on the "Test assignment one" "assign activity editing" page + And I navigate to "Settings" in current page administration + And I expand all fieldsets + And the field "Completion tracking" matches value "1" + + @javascript + Scenario: Default activity completion rules with site default completion but with no course default completion + Given the following "activity" exists: + | activity | assign | + | course | C1 | + | name | Test assignment one | + | completion | 0 | + And the following "core_completion > Course default" exist: + | course | module | completion | completionview | completionusegrade | completionsubmit | + | Acceptance test site | assign | 2 | 0 | 1 | 1 | + And I am on "Course 1" course homepage with editing mode on + When I add a "Assignment" to section "0" + And I expand all fieldsets + Then the field "Completion tracking" matches value "2" + And the field "completionview" matches value "0" + And the field "completionusegrade" matches value "1" + And the field "completionsubmit" matches value "1" + # Default values don't affect existing activities. + But I am on the "Test assignment one" "assign activity editing" page + And I navigate to "Settings" in current page administration + And I expand all fieldsets + And the field "Completion tracking" matches value "0" + + @javascript + Scenario: Default activity completion rules with site default completion and course default completion + Given the following "activity" exists: + | activity | assign | + | course | C1 | + | name | Test assignment one | + | completion | 0 | + And the following "core_completion > Course defaults" exist: + | course | module | completion | completionview | completionusegrade | completionsubmit | + | Acceptance test site | assign | 2 | 0 | 1 | 1 | + | C1 | assign | 2 | 1 | 0 | 1 | + And I am on "Course 1" course homepage with editing mode on + When I add a "Assignment" to section "0" + And I expand all fieldsets + Then the field "Completion tracking" matches value "2" + And the field "completionview" matches value "1" + And the field "completionusegrade" matches value "0" + And the field "completionsubmit" matches value "1" + # Default values don't affect existing activities. + But I am on the "Test assignment one" "assign activity editing" page + And I navigate to "Settings" in current page administration + And I expand all fieldsets + And the field "Completion tracking" matches value "0" + Scenario: Navigate to site default activity completion Given I navigate to "Courses > Default settings > Default activity completion" in site administration When I should see "Default activity completion"