diff --git a/group/group_form.php b/group/group_form.php index 72e6849fa40..a7f15564fca 100644 --- a/group/group_form.php +++ b/group/group_form.php @@ -111,11 +111,19 @@ class group_form extends moodleform { } } - if (!empty($CFG->groupenrolmentkeypolicy) and $data['enrolmentkey'] != '' and $group->enrolmentkey !== $data['enrolmentkey']) { - // enforce password policy only if changing password + if ($data['enrolmentkey'] != '') { $errmsg = ''; - if (!check_password_policy($data['enrolmentkey'], $errmsg)) { + if (!empty($CFG->groupenrolmentkeypolicy) && $group->enrolmentkey !== $data['enrolmentkey'] + && !check_password_policy($data['enrolmentkey'], $errmsg)) { + // Enforce password policy when the password is changed. $errors['enrolmentkey'] = $errmsg; + } else { + // Prevent twice the same enrolment key in course groups. + $sql = "SELECT id FROM {groups} WHERE id <> :groupid AND courseid = :courseid AND enrolmentkey = :key"; + $params = array('groupid' => $data['id'], 'courseid' => $COURSE->id, 'key' => $data['enrolmentkey']); + if ($DB->record_exists_sql($sql, $params)) { + $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group'); + } } } @@ -123,6 +131,11 @@ class group_form extends moodleform { $errors['name'] = get_string('groupnameexists', 'group', $name); } else if (!empty($idnumber) && groups_get_group_by_idnumber($COURSE->id, $idnumber)) { $errors['idnumber']= get_string('idnumbertaken'); + } else if ($data['enrolmentkey'] != '') { + // Prevent the same enrolment key from being used multiple times in course groups. + if ($DB->record_exists('groups', array('courseid' => $COURSE->id, 'enrolmentkey' => $data['enrolmentkey']))) { + $errors['enrolmentkey'] = get_string('enrolmentkeyalreadyinuse', 'group'); + } } return $errors; diff --git a/group/tests/behat/create_groups.feature b/group/tests/behat/create_groups.feature index 51970edc5ab..505b59748a9 100644 --- a/group/tests/behat/create_groups.feature +++ b/group/tests/behat/create_groups.feature @@ -89,3 +89,37 @@ Feature: Organize students into groups | Grouping name | Not the greatest grouping, but it's ok! | And I press "Save changes" And I should see "Not the greatest grouping, but it's ok!" + + Scenario: Create groups with enrolment key + Given the following "courses" exist: + | fullname | shortname | category | groupmode | + | Course 1 | C1 | 0 | 1 | + | Course 2 | C2 | 0 | 1 | + And I log in as "admin" + And I follow "Course 1" + And I expand "Users" node + And I follow "Groups" + When I press "Create group" + And I set the following fields to these values: + | Group name | Group A | + | Enrolment key | Abcdef-1 | + And I press "Save changes" + And I press "Create group" + And I set the following fields to these values: + | Group name | Group B | + | Enrolment key | Abcdef-1 | + And I press "Save changes" + Then I should see "This enrolment key is already used for another group." + And I set the following fields to these values: + | Enrolment key | Abcdef-2 | + And I press "Save changes" + And the "groups" select box should contain "Group B (0)" + And I am on homepage + And I follow "Course 2" + And I expand "Users" node + And I follow "Groups" + And I press "Create group" + And I set the following fields to these values: + | Group name | Group A | + | Enrolment key | Abcdef-1 | + And I should not see "This enrolment key is already used for another group." \ No newline at end of file diff --git a/group/tests/behat/update_groups.feature b/group/tests/behat/update_groups.feature index a363d2bb0a8..66e19206768 100644 --- a/group/tests/behat/update_groups.feature +++ b/group/tests/behat/update_groups.feature @@ -100,3 +100,54 @@ Feature: Automatic updating of groups and groupings And the "idnumber" "field" should be readonly And the field "idnumber" matches value "An ID" + @javascript + Scenario: Update groups with enrolment key + Given the following "courses" exist: + | fullname | shortname | + | Course 2 | C2 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C2 | editingteacher | + And I log out + And I log in as "teacher1" + And I follow "Course 1" + And I expand "Users" node + And I follow "Groups" + And I set the field "groups" to "Group (with ID)" + And I press "Edit group settings" + And I set the following fields to these values: + | Enrolment key | badpasswd | + When I press "Save changes" + Then I should see "Passwords must have at least 1 digit(s)" + And I set the following fields to these values: + | Enrolment key | Abcdef-1 | + And I press "Save changes" + And I set the field "groups" to "Group (with ID)" + And I press "Edit group settings" + And I press "Save changes" + And I should not see "This enrolment key is already used for another group." + And I set the field "groups" to "Group (without ID)" + And I press "Edit group settings" + And I set the following fields to these values: + | Enrolment key | Abcdef-1 | + And I press "Save changes" + And I should see "This enrolment key is already used for another group." + And I set the following fields to these values: + | Enrolment key | Abcdef-2 | + And I press "Save changes" + And I should not see "This enrolment key is already used for another group." + And I am on homepage + And I follow "Course 2" + And I expand "Users" node + And I follow "Groups" + And I press "Create group" + And I set the following fields to these values: + | Group name | Group A | + And I press "Save changes" + And I should not see "This enrolment key is already used for another group." + And I set the field "groups" to "Group A" + And I press "Edit group settings" + And I set the following fields to these values: + | Enrolment key | Abcdef-1 | + And I press "Save changes" + And I should not see "This enrolment key is already used for another group." \ No newline at end of file diff --git a/lang/en/group.php b/lang/en/group.php index 7fee0f78d63..ac1316a1f9d 100644 --- a/lang/en/group.php +++ b/lang/en/group.php @@ -61,6 +61,7 @@ $string['enrolmentkey'] = 'Enrolment key'; $string['enrolmentkey_help'] = 'An enrolment key enables access to the course to be restricted to only those who know the key. If a group enrolment key is specified, then not only will entering that key let the user into the course, but it will also automatically make them a member of this group. Note: Group enrolment keys must be enabled in the self enrolment settings and an enrolment key for the course must also be specified.'; +$string['enrolmentkeyalreadyinuse'] = 'This enrolment key is already used for another group.'; $string['erroraddremoveuser'] = 'Error adding/removing user {$a} to group'; $string['erroreditgroup'] = 'Error creating/updating group {$a}'; $string['erroreditgrouping'] = 'Error creating/updating grouping {$a}';