Skip to content

Commit

Permalink
Merge branch 'MDL-80599-main-2' of https://github.com/ilyatregubov/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed May 21, 2024
2 parents 1102b53 + 6be4f78 commit bf85928
Show file tree
Hide file tree
Showing 18 changed files with 707 additions and 101 deletions.
49 changes: 7 additions & 42 deletions admin/tool/uploadcourse/classes/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ protected function delete() {
* Log an error
*
* @param string $code error code.
* @param lang_string $message error message.
* @param string $message error message.
* @return void
*/
protected function error($code, lang_string $message) {
protected function error($code, string $message) {
if (array_key_exists($code, $this->errors)) {
throw new coding_exception('Error code already defined');
}
Expand Down Expand Up @@ -1129,34 +1129,7 @@ protected function process_enrolment_data($course) {
break;
}

// Now update values.
$modifiedinstance = $instance;

// Sort out the start, end and date.
$modifiedinstance->enrolstartdate = (isset($method['startdate']) ? strtotime($method['startdate']) : 0);
$modifiedinstance->enrolenddate = (isset($method['enddate']) ? strtotime($method['enddate']) : 0);

// Is the enrolment period set?
if (isset($method['enrolperiod']) && !empty($method['enrolperiod'])) {
if (preg_match('/^\d+$/', $method['enrolperiod'])) {
$method['enrolperiod'] = (int)$method['enrolperiod'];
} else {
// Try and convert period to seconds.
$method['enrolperiod'] = strtotime('1970-01-01 GMT + ' . $method['enrolperiod']);
}
$modifiedinstance->enrolperiod = $method['enrolperiod'];
}
if ($instance->enrolstartdate > 0 && isset($method['enrolperiod'])) {
$modifiedinstance->enrolenddate = $instance->enrolstartdate + $method['enrolperiod'];
}
if ($instance->enrolenddate > 0) {
$modifiedinstance->enrolperiod = $instance->enrolenddate - $instance->enrolstartdate;
}
if ($instance->enrolenddate < $instance->enrolstartdate) {
$modifiedinstance->enrolenddate = $instance->enrolstartdate;
}

// Sort out the given role.
// Validate role context again since course is created.
if (isset($method['role']) || isset($method['roleid'])) {
if (isset($method['role'])) {
$role = $method['role'];
Expand All @@ -1166,22 +1139,14 @@ protected function process_enrolment_data($course) {
$role = $DB->get_field('role', 'shortname', ['id' => $roleid], MUST_EXIST);
}
if (!$this->validate_role_context($course->id, $roleid)) {
$this->error('contextrolenotallowed',
new lang_string('contextrolenotallowed', 'core_role', $role));
$this->error('contextrolenotallowed', new lang_string('contextrolenotallowed', 'core_role', $role));
break;
}

$roleids = tool_uploadcourse_helper::get_role_ids();
if (in_array($roleid, $roleids)) {
$modifiedinstance->roleid = $roleid;
}
}

// Sort out custom instance name.
if (isset($method['name'])) {
$modifiedinstance->name = $method['name'];
}

// Now update values.
// Sort out plugin specific fields.
$modifiedinstance = $plugin->update_enrol_plugin_data($course->id, $method, $instance);
$plugin->update_instance($instance, $modifiedinstance);
} else {
foreach ($errors as $key => $message) {
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploadcourse/tests/behat/cohorts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Feature: An admin can create courses with cohort enrolments using a CSV file
| enrol_plugins_enabled | manual,guest,self |
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_cohort.csv" file to "File" filemanager
When I click on "Preview" "button"
Then I should see "Cohort sync plugin is disabled"
Then I should see "Cohort sync enrol plugin is disabled"

@javascript
Scenario: Validation of cohorts for uploaded courses
Expand Down
104 changes: 104 additions & 0 deletions admin/tool/uploadcourse/tests/behat/guest.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses with guest enrolments using a CSV file
In order to create courses using a CSV file with guest enrolment
As an admin
I need to be able to upload a CSV file and navigate through the import process

Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 0 | 0 | CAT0 |
| Cat 1 | CAT0 | CAT1 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | CAT1 |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"

@javascript
Scenario: Validation of password for uploaded courses with guest enrolments
# usepasswordpolicy is not set.
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Courses created: 2"
And I should see "Courses updated: 1"
And I should see "Courses errors: 0"
And I am on the "Course 1" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "test"
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"

# Policy is used, but password not required so it will not be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| usepasswordpolicy | 1 | enrol_guest |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Guest access" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Guest access" "table_row"
And I press "Continue"

# Policy is used and password not required so it will be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| requirepassword | 1 | enrol_guest |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Guest access" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" does not match value ""
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Guest access" "table_row"
And the field "Password" matches value "Test123@"
122 changes: 122 additions & 0 deletions admin/tool/uploadcourse/tests/behat/self.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
@tool @tool_uploadcourse @_file_upload
Feature: An admin can create courses with self enrolments using a CSV file
In order to create courses using a CSV file with self enrolment
As an admin
I need to be able to upload a CSV file and navigate through the import process

Background:
Given the following "categories" exist:
| name | category | idnumber |
| Cat 0 | 0 | CAT0 |
| Cat 1 | CAT0 | CAT1 |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | CAT1 |
And the following "groups" exist:
| name | course | idnumber | enrolmentkey |
| group1 | C1 | G1 | test |
And I log in as "admin"
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"

@javascript
Scenario: Validation of password for uploaded courses with self enrolments
# usepasswordpolicy is not set.
Given I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I click on "Upload courses" "button"
And I should see "Courses created: 2"
And I should see "Courses updated: 1"
And I should see "Courses errors: 0"
And I am on the "Course 1" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "test"
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"

# Policy is used, but password not required so it will not be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| usepasswordpolicy | 1 | enrol_self |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Self enrolment (Student)" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value ""
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "Test123@"
And I press "Cancel"
And I click on "Delete" "link" in the "Self enrolment" "table_row"
And I press "Continue"

# Policy is used and password not required so it will be generated if omitted.
And the following config values are set as admin:
| config | value | plugin |
| requirepassword | 1 | enrol_self |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "Passwords must be at least 8 characters long."
And I should see "Passwords must have at least 1 digit(s)."
And I should see "Passwords must have at least 1 upper case letter(s)."
And I should see "The password must have at least 1 special character(s) such as *, -, or #."
And I click on "Upload courses" "button"
And I should see "Courses created: 0"
And I should see "Courses updated: 2"
And I should see "Courses errors: 1"
And I am on the "Course 1" "enrolment methods" page
And "Self enrolment (Student)" "table_row" should not exist
And I am on the "Course 2" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" does not match value ""
And I am on the "Course 3" "enrolment methods" page
And I click on "Edit" "link" in the "Self enrolment" "table_row"
And the field "Enrolment key" matches value "Test123@"

# Use group enrolment keys is set to yes, so the password must be different from group enrolment key.
And I am on the "Course 1" "enrolment methods" page
And I add "Self enrolment" enrolment method in "Course 1" with:
| Enrolment key | Abcejfd12@ |
| Use group enrolment keys | Yes |
And the following config values are set as admin:
| config | value | plugin |
| usepasswordpolicy | 0 | enrol_self |
And I navigate to "Courses > Upload courses" in site administration
And I set the field "Upload mode" to "Create new courses, or update existing ones"
And I set the field "Update mode" to "Update with CSV data only"
And I upload "admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv" file to "File" filemanager
And I click on "Preview" "button"
And I should see "This enrolment key is already used as a group enrolment key."
4 changes: 4 additions & 0 deletions admin/tool/uploadcourse/tests/fixtures/enrolment_guest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_password
C1,Course 1,CAT1,guest,student,test
C2,Course 2,CAT1,guest,student,
C3,Course 3,CAT1,guest,student,Test123@
4 changes: 4 additions & 0 deletions admin/tool/uploadcourse/tests/fixtures/enrolment_self.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shortname,fullname,category_idnumber,enrolment_1,enrolment_1_role,enrolment_1_password
C1,Course 1,CAT1,self,student,test
C2,Course 2,CAT1,self,student,
C3,Course 3,CAT1,self,student,Test123@
1 change: 1 addition & 0 deletions enrol/cohort/lang/en/deprecated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugindisabled,enrol_cohort
4 changes: 3 additions & 1 deletion enrol/cohort/lang/en/enrol_cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
$string['defaultgroupnametext'] = '{$a->name} cohort {$a->increment}';
$string['enrolcohortsynctask'] = 'Cohort enrolment sync task';
$string['instanceexists'] = 'Cohort is already synchronised with selected role';
$string['plugindisabled'] = 'Cohort sync plugin is disabled';
$string['pluginname'] = 'Cohort sync';
$string['pluginname_desc'] = 'Cohort enrolment plugin synchronises cohort members with course participants.';
$string['status'] = 'Active';
$string['creategroup'] = 'Create new group';
$string['privacy:metadata:core_group'] = 'Enrol cohort plugin can create a new group or use an existing group to add all the members of the cohort.';

// Deprecated since Moodle 4.5.
$string['plugindisabled'] = 'Cohort sync plugin is disabled';
6 changes: 1 addition & 5 deletions enrol/cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,7 @@ public function edit_instance_validation($data, $files, $instance, $context) {
public function validate_enrol_plugin_data(array $enrolmentdata, ?int $courseid = null): array {
global $DB;

$errors = [];
if (!enrol_is_enabled('cohort')) {
$errors['plugindisabled'] =
new lang_string('plugindisabled', 'enrol_cohort');
}
$errors = parent::validate_enrol_plugin_data($enrolmentdata, $courseid);

if (isset($enrolmentdata['addtogroup'])) {
$addtogroup = $enrolmentdata['addtogroup'];
Expand Down
Loading

0 comments on commit bf85928

Please sign in to comment.