Skip to content

Commit

Permalink
MDL-71370 course: Make showcompletionconditions nullable
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
junpataleta committed Apr 24, 2021
1 parent 146a38d commit fca4200
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 16 deletions.
36 changes: 24 additions & 12 deletions completion/tests/behat/custom_completion_display_conditions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<siteshowcompletion>"
And I press "Save changes"
When I navigate to "Courses > Add a new course" in site administration
Then the field "showcompletionconditions" matches value "<expected>"

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 "<siteshowcompletion>"
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 "<expected>"

Examples:
| siteshowcompletion | expected |
| Yes | Yes |
| No | No |
2 changes: 1 addition & 1 deletion course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20210415" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20210422" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -104,7 +104,7 @@
<FIELD NAME="cacherev" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Incrementing revision for validating the course content cache"/>
<FIELD NAME="originalcourseid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="the id of the source course when a new course originates from a restore of another course on the same site."/>
<FIELD NAME="showactivitydates" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Whether to display activity dates to user. 0 = do not display, 1 = display activity dates"/>
<FIELD NAME="showcompletionconditions" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="Whether to display completion conditions to user. 0 = do not display, 1 = display conditions"/>
<FIELD NAME="showcompletionconditions" TYPE="int" LENGTH="1" NOTNULL="false" SEQUENCE="false" COMMENT="Whether to display completion conditions to user. 0 = do not display, 1 = display conditions"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
21 changes: 21 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fca4200

Please sign in to comment.