Commit c05e44b7 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Enforce max user limit on institution users page

parent 20f82907
......@@ -146,6 +146,19 @@ function institutionusers_submit(Pieform $form, $values) {
$SESSION->add_error_msg(get_string('errorupdatinginstitutionusers', 'admin'));
redirect($url);
}
$maxusers = $institution->maxuseraccounts;
if (!empty($maxusers)) {
$members = $institution->countMembers();
if ($values['update'] == 'addUserAsMember' && $members + count($values['users']) >= $maxusers) {
$SESSION->add_error_msg(get_string('institutionuserserrortoomanyusers', 'admin'));
redirect($url);
}
if ($values['update'] == 'inviteUser'
&& $members + $institution->countInvites() + count($values['users']) >= $maxusers) {
$SESSION->add_error_msg(get_string('institutionuserserrortoomanyinvites', 'admin'));
redirect($url);
}
}
db_begin();
foreach ($values['users'] as $id) {
$institution->{$values['update']}($id);
......
......@@ -319,7 +319,8 @@ $string['institutionusersupdatednonmembers'] = 'Invitations sent';
$string['maxuseraccounts'] = 'Maximum User Accounts Allowed';
$string['maxuseraccountsdescription'] = 'The maximum number of user accounts that can be associated with the institution. If there is no limit, this field should be left blank.';
$string['institutionuserserrortoomanyusers'] = 'The users were not added. The number of members cannot exceed the maximum allowed for the institution. You can add fewer users, remove some users from the institution, or ask the site administrator to increase the maximum number of users.';
$string['institutionuserserrortoomanyinvites'] = 'Your invitations were not sent. The number of existing members plus the number of outstanding invitations cannot exceed the institution\'s maximum number of users. You can invite fewer users, remove some users from the institution, or ask the site administrator to increase the maximum number of users.';
// general stuff
$string['notificationssaved'] = 'Notification settings saved';
......
......@@ -303,6 +303,19 @@ class Institution {
delete_records('usr_institution', 'usr', $userid, 'institution', $this->name);
handle_event('updateuser', $userid);
}
public function countMembers() {
return count_records_sql('
SELECT COUNT(*) FROM {usr} u INNER JOIN {usr_institution} i ON u.id = i.usr
WHERE i.institution = ? AND u.deleted = 0', array($this->name));
}
public function countInvites() {
return count_records_sql('
SELECT COUNT(*) FROM {usr} u INNER JOIN {usr_institution_request} r ON u.id = r.usr
WHERE r.institution = ? AND u.deleted = 0 AND r.confirmedinstitution = 1',
array($this->name));
}
}
function get_institution_selector() {
......
......@@ -1309,7 +1309,7 @@ function admin_nav() {
'weight' => 10,
),
array(
'path' => 'configusers/institutionmembers',
'path' => 'configusers/institutionusers',
'url' => 'admin/users/institutionusers.php',
'title' => get_string('institutionmembers', 'admin'),
'weight' => 20,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment