diff --git a/completion/tests/behat/custom_completion_display_conditions.feature b/completion/tests/behat/custom_completion_display_conditions.feature index 783690b739c77..dd51b9a0d94e8 100644 --- a/completion/tests/behat/custom_completion_display_conditions.feature +++ b/completion/tests/behat/custom_completion_display_conditions.feature @@ -50,20 +50,32 @@ Feature: Allow teachers to edit the visibility of completion conditions in a cou And I follow "Test choice manual" And the manual completion button for "Test choice manual" should be disabled - Scenario: Default show completion conditions value in course form when default show completion conditions admin setting is set to No + Scenario Outline: Default showcompletionconditions value in course form on course creation Given I log in as "admin" And I navigate to "Courses > Course default settings" in site administration - When I set the following fields to these values: - | Show completion conditions | No | - And I click on "Save changes" "button" - And I navigate to "Courses > Add a new course" in site administration - Then the field "showcompletionconditions" matches value "No" + And I set the field "Show completion conditions" to "" + And I press "Save changes" + When I navigate to "Courses > Add a new course" in site administration + Then the field "showcompletionconditions" matches value "" - Scenario: Default show completion conditions value in course form when default show completion conditions admin setting is set to Yes + Examples: + | siteshowcompletion | expected | + | Yes | Yes | + | No | No | + + Scenario Outline: Default showcompletionconditions displayed when editing a course with disabled completion tracking Given I log in as "admin" And I navigate to "Courses > Course default settings" in site administration - When I set the following fields to these values: - | Show completion conditions | Yes | - And I click on "Save changes" "button" - And I navigate to "Courses > Add a new course" in site administration - Then the field "showcompletionconditions" matches value "Yes" + And I set the field "Show completion conditions" to "" + And I press "Save changes" + And I am on "Course 1" course homepage with editing mode on + And I navigate to "Edit settings" in current page administration + And I set the field "Enable completion tracking" to "No" + And I press "Save and display" + And I navigate to "Edit settings" in current page administration + Then the field "Show completion conditions" matches value "" + + Examples: + | siteshowcompletion | expected | + | Yes | Yes | + | No | No | diff --git a/course/edit_form.php b/course/edit_form.php index 3233a4d2f0474..73b5190c3bf59 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -322,7 +322,7 @@ function definition() { $mform->addElement('selectyesno', 'showcompletionconditions', get_string('showcompletionconditions', 'completion')); $mform->addHelpButton('showcompletionconditions', 'showcompletionconditions', 'completion'); $mform->setDefault('showcompletionconditions', $showcompletionconditions); - $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_HIDE_CONDITIONS); + $mform->hideIf('showcompletionconditions', 'enablecompletion', 'eq', COMPLETION_DISABLED); } else { $mform->addElement('hidden', 'enablecompletion'); $mform->setType('enablecompletion', PARAM_INT); diff --git a/course/lib.php b/course/lib.php index 6fdcff437ebd1..8e1582c8635a1 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2362,6 +2362,19 @@ function create_course($data, $editoroptions = NULL) { $data->summary_format = FORMAT_HTML; } + // Get default completion settings as a fallback in case the enablecompletion field is not set. + $courseconfig = get_config('moodlecourse'); + $defaultcompletion = !empty($CFG->enablecompletion) ? $courseconfig->enablecompletion : COMPLETION_DISABLED; + $enablecompletion = $data->enablecompletion ?? $defaultcompletion; + // Unset showcompletionconditions when completion tracking is not enabled for the course. + if ($enablecompletion == COMPLETION_DISABLED) { + unset($data->showcompletionconditions); + } else if (!isset($data->showcompletionconditions)) { + // Show completion conditions should have a default value when completion is enabled. Set it to the site defaults. + // This scenario can happen when a course is created through data generators or through a web service. + $data->showcompletionconditions = $courseconfig->showcompletionconditions; + } + if (!isset($data->visible)) { // data not from form, add missing visibility info $data->visible = $category->visible; @@ -2540,6 +2553,11 @@ function update_course($data, $editoroptions = NULL) { } } + // Set showcompletionconditions to null when completion tracking has been disabled for the course. + if (isset($data->enablecompletion) && $data->enablecompletion == COMPLETION_DISABLED) { + $data->showcompletionconditions = null; + } + // Update custom fields if there are any of them in the form. $handler = core_course\customfield\course_handler::create(); $handler->instance_form_save($data); diff --git a/lib/db/install.xml b/lib/db/install.xml index 3b3bde9eaacc5..7cc424e45ff53 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -104,7 +104,7 @@ - + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index d578aee1db553..0fc34f9793d3a 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2621,5 +2621,26 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021052500.85); } + if ($oldversion < 2021052500.87) { + // Changing the default of field showcompletionconditions on table course to 0. + $table = new xmldb_table('course'); + $field = new xmldb_field('showcompletionconditions', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'showactivitydates'); + + // Launch change of nullability for field showcompletionconditions. + $dbman->change_field_notnull($table, $field); + + // Launch change of default for field showcompletionconditions. + $dbman->change_field_default($table, $field); + + // Set showcompletionconditions to null for courses which don't track completion. + $sql = "UPDATE {course} + SET showcompletionconditions = null + WHERE enablecompletion <> 1"; + $DB->execute($sql); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021052500.87); + } + return true; } diff --git a/version.php b/version.php index e1ec1e9069bea..cfa6a9fab1ac3 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021052500.86; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021052500.87; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20210423)'; // Human-friendly version name