From 7958fa72e1e128bb8c32f1ad076fa09ce11bf7d1 Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Mon, 23 Aug 2021 18:04:18 +0200 Subject: [PATCH] MDL-72356 core_course: auto enrol admins when adding a new course --- admin/settings/users.php | 3 +++ course/edit.php | 10 ++++++++- course/tests/behat/course_creation.feature | 24 ++++++++++++++++++++++ lang/en/admin.php | 2 ++ lib/db/upgrade.php | 8 ++++++++ version.php | 2 +- 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/admin/settings/users.php b/admin/settings/users.php index aa602290f6e..961bc366a56 100644 --- a/admin/settings/users.php +++ b/admin/settings/users.php @@ -186,6 +186,9 @@ if ($hassiteconfig unset($restorersnewrole); } + $temp->add(new admin_setting_configcheckbox('enroladminnewcourse', new lang_string('enroladminnewcourse', 'admin'), + new lang_string('enroladminnewcourse_help', 'admin'), 1)); + $temp->add(new admin_setting_configcheckbox('autologinguests', new lang_string('autologinguests', 'admin'), new lang_string('configautologinguests', 'admin'), 0)); $temp->add(new admin_setting_configmultiselect('hiddenuserfields', new lang_string('hiddenuserfields', 'admin'), diff --git a/course/edit.php b/course/edit.php index 976ddc35e57..70537bbced2 100644 --- a/course/edit.php +++ b/course/edit.php @@ -164,7 +164,15 @@ if ($editform->is_cancelled()) { // Get the context of the newly created course. $context = context_course::instance($course->id, MUST_EXIST); - if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) { + // Admins have all capabilities, so is_viewing is returning true for admins. + // We are checking 'enroladminnewcourse' setting to decide to enrol them or not. + if (is_siteadmin($USER->id)) { + $enroluser = $CFG->enroladminnewcourse; + } else { + $enroluser = !is_viewing($context, null, 'moodle/role:assign'); + } + + if (!empty($CFG->creatornewroleid) and $enroluser and !is_enrolled($context, null, 'moodle/role:assign')) { // Deal with course creators - enrol them internally with default role. // Note: This does not respect capabilities, the creator will be assigned the default role. // This is an expected behaviour. See MDL-66683 for further details. diff --git a/course/tests/behat/course_creation.feature b/course/tests/behat/course_creation.feature index 23d20c84635..73ec8538cb8 100644 --- a/course/tests/behat/course_creation.feature +++ b/course/tests/behat/course_creation.feature @@ -114,3 +114,27 @@ Feature: Managers can create courses And I press "Save and display" And I click on "Participants" "link" Then I should see "Non-editing teacher" in the "Kevin the" "table_row" + + @javascript + Scenario: Create a course as admin + Given I log in as "admin" + And the following config values are set as admin: + | enroladminnewcourse | 0 | + And I navigate to "Courses > Add a new course" in site administration + And I set the following fields to these values: + | Course full name | My first course | + | Course short name | myfirstcourse | + And I press "Save and display" + And I navigate to course participants + Then I should not see "Teacher" + And I should see "Nothing to display" + And the following config values are set as admin: + | enroladminnewcourse | 1 | + And I navigate to "Courses > Add a new course" in site administration + And I set the following fields to these values: + | Course full name | My second course | + | Course short name | mysecondcourse | + And I press "Save and display" + And I navigate to course participants + And I should see "Teacher" + And I should not see "Nothing to display" diff --git a/lang/en/admin.php b/lang/en/admin.php index 5723e87c1c6..b24cbc5bf91 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -576,6 +576,8 @@ $string['enablewebservices'] = 'Enable web services'; $string['enablewsdocumentation'] = 'Web services documentation'; $string['encryptedpassword_set'] = '(Set and encrypted)'; $string['encryptedpassword_edit'] = 'Enter new value'; +$string['enroladminnewcourse'] = 'Auto-enrol admin in new courses'; +$string['enroladminnewcourse_help'] = 'When an admin adds a new course, should they be automatically enrolled and assigned the creators\' role in new courses?'; $string['enrolinstancedefaults'] = 'Enrolment instance defaults'; $string['enrolinstancedefaults_desc'] = 'Default enrolment settings in new courses.'; $string['enrolmultipleusers'] = 'Enrol the users'; diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 96e0dead125..fb469443149 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2778,5 +2778,13 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2021091100.02); } + if ($oldversion < 2021091700.01) { + // Default 'off' for existing sites as this is the behaviour they had earlier. + set_config('enroladminnewcourse', false); + + // Main savepoint reached. + upgrade_main_savepoint(true, 2021091700.01); + } + return true; } diff --git a/version.php b/version.php index 4135a3e9e6b..502ae758dab 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2021091700.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2021091700.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.0dev (Build: 20210917)'; // Human-friendly version name