Skip to content

Commit

Permalink
Merge branch 'MDL-78530-master' of https://github.com/aanabit/moodle
Browse files Browse the repository at this point in the history
…into MDL-78530-master-clr
  • Loading branch information
ferranrecio committed Aug 14, 2023
2 parents b4addeb + 07cd5d5 commit 8740488
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 30 deletions.
2 changes: 0 additions & 2 deletions admin/settings/subsystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
1 => get_string('completionactivitydefault', 'completion'),
0 => get_string('completion_none', 'completion')
);
$optionalsubsystems->add(new admin_setting_configselect('completiondefault', new lang_string('completiondefault', 'completion'),
new lang_string('configcompletiondefault', 'completion'), 1, $options));

$optionalsubsystems->add($checkbox = new admin_setting_configcheckbox('enableavailability',
new lang_string('enableavailability', 'availability'),
Expand Down
19 changes: 6 additions & 13 deletions completion/classes/form/form_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,20 @@ protected function get_cm(): ?\stdClass {
* @param bool $supportviews True if the module supports views and false otherwise.
* @param bool $supportgrades True if the module supports grades and false otherwise.
* @param bool $rating True if the rating feature is enabled and false otherwise.
* @param bool $defaultcompletion True if the default completion is enabled and false otherwise. To review in MDL-78531.
* @param null $unused This parameter has been deprecated since 4.3 and should not be used anymore.
* @throws \coding_exception If the form is not moodleform_mod and $modname is null.
*/
protected function add_completion_elements(
string $modname = null,
bool $supportviews = false,
bool $supportgrades = false,
bool $rating = false,
bool $defaultcompletion = true
$unused = null
): void {
global $CFG;

if ($unused !== null) {
debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
}

$mform = $this->get_form();
if ($modname === null) {
Expand All @@ -123,7 +126,6 @@ protected function add_completion_elements(
$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.');
}
Expand All @@ -137,15 +139,6 @@ protected function add_completion_elements(
$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();
Expand Down
22 changes: 12 additions & 10 deletions completion/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,18 @@ public function apply_default_completion($data, $updatecustomrules, string $suff
* @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']);
Expand All @@ -563,13 +572,6 @@ public static function get_default_completion($course, $module, $flatten = true,
} 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.
Expand Down
79 changes: 79 additions & 0 deletions completion/tests/behat/site_default_activity_completion.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@core @core_completion
Feature: Allow admins to edit the default activity completion rules at site level.
In order to set the activity completion defaults for new activities
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
And I press "Add an activity or resource"
When I click on "Add a new Assignment" "link" in the "Add an activity or resource" "dialogue"
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" Activity 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
And I press "Add an activity or resource"
When I click on "Add a new Assignment" "link" in the "Add an activity or resource" "dialogue"
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" Activity 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
And I press "Add an activity or resource"
When I click on "Add a new Assignment" "link" in the "Add an activity or resource" "dialogue"
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" Activity page
And I navigate to "Settings" in current page administration
And I expand all fieldsets
And the field "Completion tracking" matches value "0"
59 changes: 59 additions & 0 deletions completion/tests/generator/behat_core_completion_generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Completion test generator for Behat
*
* @package core_completion
* @copyright 2023 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_core_completion_generator extends behat_generator_base {

/**
* Get a list of the entities that can be created for completion
*
* @return array[]
*/
protected function get_creatable_entities(): array {
return [
'Course defaults' => [
'singular' => 'Course default',
'datagenerator' => 'default_completion',
'required' => [
'course',
'module',
],
'switchids' => [
'course' => 'course',
'module' => 'module',
],
],
];
}

/**
* Look up module ID from given name
*
* @param string $name
* @return int
*/
protected function get_module_id(string $name): int {
global $DB;

return (int) $DB->get_field('modules', 'id', ['name' => $name], MUST_EXIST);
}
}
50 changes: 50 additions & 0 deletions completion/tests/generator/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Completion test generator
*
* @package core_completion
* @copyright 2023 Amaia Anabitarte <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_completion_generator extends component_generator_base {

/**
* Create default completion
*
* @param array|stdClass $record
* @return stdClass
*/
public function create_default_completion($record): stdClass {
global $DB;

$record = (array) $record;

$record = (object) array_merge([
'completion' => 0,
'completionview' => 0,
'completionusegrade' => 0,
'completionpassgrade' => 0,
'completionexpected' => 0,
'customrules' => '',
], $record);

$record->id = $DB->insert_record('course_completion_defaults', $record);

return $record;
}
}
2 changes: 2 additions & 0 deletions completion/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ information provided here is intended especially for developers.
to be added to the name of the completion rules.
* The method manager::defaultcompletion() now has the $modules and $form parameters, to specify the modules that have been set
through the form and the current form that has been sent.
* $CFG->completiondefault setting has been removed.
* $defaultcompletion parameter has been deprecated for add_completion_elements() protected function.

=== 4.0 ===
* New method mark_course_completions_activity_criteria() has been added to mark course completions instantly. It is
Expand Down
4 changes: 2 additions & 2 deletions lang/en/completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
$string['completion_manual'] = 'Students can manually mark the activity as completed';
$string['completion_none'] = 'Do not indicate activity completion';
$string['completionactivitydefault'] = 'Use activity default';
$string['completiondefault'] = 'Default completion tracking';
$string['completiondisabled'] = 'Disabled, not shown in activity settings';
$string['completionenabled'] = 'Enabled, control via completion and activity settings';
$string['completionexpected'] = 'Expect completed on';
Expand Down Expand Up @@ -118,7 +117,6 @@
$string['completionupdated'] = 'Updated completion for activity <b>{$a}</b>';
$string['completionview'] = 'Require view';
$string['completionview_desc'] = 'Student must view this activity to complete it';
$string['configcompletiondefault'] = 'The default setting for completion tracking when creating new activities.';
$string['configenablecompletion'] = 'If enabled, course and activity completion conditions may be set. Setting activity completion conditions is recommended so that meaningful data is displayed for users in their course overview on the Dashboard.';
$string['confirmselfcompletion'] = 'Confirm self completion';
$string['courseaggregation'] = 'Condition requires';
Expand Down Expand Up @@ -261,3 +259,5 @@
// Deprecated since Moodle 4.0.
$string['yourprogress'] = 'Your progress';
$string['editcoursecompletionsettings'] = 'Edit course completion settings';
$string['completiondefault'] = 'Default completion tracking';
$string['configcompletiondefault'] = 'The default setting for completion tracking when creating new activities.';
2 changes: 2 additions & 0 deletions lang/en/deprecated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ backpackemail,core_badges
backpackemail_help,core_badges
editcoursecompletionsettings,core_completion
clicktochangeinbrackets,core
completiondefault,core_completion
configcompletiondefault,core_completion
7 changes: 7 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3370,5 +3370,12 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2023080100.00);
}

if ($oldversion < 2023081000.01) {
unset_config('completiondefault');

// Main savepoint reached.
upgrade_main_savepoint(true, 2023081000.01);
}

return true;
}
4 changes: 2 additions & 2 deletions lib/testing/tests/testing_generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function test_get_default_generator() {
*/
public function test_get_plugin_generator_no_component_dir() {
$this->expectException(\coding_exception::class);
$this->expectExceptionMessage('Component core_completion does not support generators yet. Missing tests/generator/lib.php.');
$generator = $this->getDataGenerator()->get_plugin_generator('core_completion');
$this->expectExceptionMessage('Component core_cohort does not support generators yet. Missing tests/generator/lib.php.');
$generator = $this->getDataGenerator()->get_plugin_generator('core_cohort');
}

public function test_create_user() {
Expand Down
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 = 2023081400.01; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023081400.02; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.3dev+ (Build: 20230814)'; // Human-friendly version name
Expand Down

0 comments on commit 8740488

Please sign in to comment.