From fca42002f7d2dbca4bdbe0714514f7e95541db85 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 22 Apr 2021 22:41:03 +0800 Subject: [PATCH] MDL-71370 course: Make showcompletionconditions nullable * When completion tracking is not enabled for the course, it does not make sense for the course's showcompletionconditions setting to be set according to the default value indicated by the "moodlecourse | showcompletionconditions" admin setting. Setting showcompletionconditions as enabled when completion tracking is disabled makes even less sense. So in such a case, we should not be setting a default value for showcompletionconditions and allow it to be null. * When the course is edited and completion tracking is enabled, this also would set the "Show completion conditions" field to default to the value set in the "moodlecourse | showcompletionconditions" admin setting. --- ...stom_completion_display_conditions.feature | 36 ++++++++++++------- course/edit_form.php | 2 +- course/lib.php | 18 ++++++++++ lib/db/install.xml | 4 +-- lib/db/upgrade.php | 21 +++++++++++ version.php | 2 +- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/completion/tests/behat/custom_completion_display_conditions.feature b/completion/tests/behat/custom_completion_display_conditions.feature index 783690b739c..dd51b9a0d94 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 3233a4d2f04..73b5190c3bf 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -322,7 +322,7 @@ class course_edit_form extends moodleform { $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 6fdcff437eb..8e1582c8635 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 3b3bde9eaac..7cc424e45ff 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 d578aee1db5..0fc34f9793d 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 e1ec1e9069b..cfa6a9fab1a 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