Commit f06a39df authored by Richard Mansfield's avatar Richard Mansfield Committed by Gerrit Code Review
Browse files

Merge "Add institution bulk add/remove/invite/decline functions"

parents 73d613c1 8036393f
......@@ -201,15 +201,20 @@ function institutionusers_submit(Pieform $form, $values) {
redirect($url);
}
}
db_begin();
if ($action == 'removeMembers') {
$institution->removeMembers($values['users']);
} else {
foreach ($values['users'] as $id) {
$institution->{$action}($id);
}
}
db_commit();
else if ($action == 'addUserAsMember') {
$institution->add_members($values['users']);
}
else if ($action == 'inviteUser') {
$institution->invite_users($values['users']);
}
else if ($action == 'declineRequestFromUser') {
$institution->decline_requests($values['users']);
}
$SESSION->add_ok_msg(get_string('institutionusersupdated_'.$action, 'admin'));
if (!$USER->get('admin') && !$USER->is_institutional_admin()) {
redirect(get_config('wwwroot'));
......
......@@ -329,6 +329,21 @@ class User {
}
}
/**
* Convert from a row object from the usr table
*
* @param object $data The row data
*/
public function from_stdclass($data) {
foreach (array('expiry', 'lastlogin', 'lastlastlogin', 'lastaccess', 'suspendedctime', 'ctime') as $f) {
if (isset($data->$f) && !is_numeric($data->$f)) {
$data->$f = strtotime($data->$f);
}
}
$this->populate($data);
return $this;
}
/**
* Gets the user property keyed by $key.
*
......
......@@ -231,6 +231,47 @@ class Institution {
db_commit();
}
public function add_members($userids) {
global $USER;
if (empty($userids)) {
return;
}
if (!$USER->can_edit_institution($this->name)) {
throw new AccessDeniedException("Institution::add_members: access denied");
}
$values = array_map('intval', $userids);
array_unshift($values, $this->name);
$users = get_records_sql_array('
SELECT u.*, r.confirmedusr
FROM {usr} u LEFT JOIN {usr_institution_request} r ON u.id = r.usr AND r.institution = ?
WHERE u.id IN (' . join(',', array_fill(0, count($values) - 1, '?')) . ') AND u.deleted = 0',
$values
);
if (empty($users)) {
return;
}
db_begin();
foreach ($users as $user) {
// If the user hasn't requested membership, allow them to be added to
// the institution anyway so long as the logged-in user is a site admin
// or institutional admin for the user (in some other institution).
if (!$user->confirmedusr) {
$userobj = new User;
$userobj->from_stdclass($user);
if (!$USER->is_admin_for_user($userobj)) {
continue;
}
}
$this->addUserAsMember($user);
}
db_commit();
}
public function addRequestFromUser($user, $studentid = null) {
$request = get_record('usr_institution_request', 'usr', $user->id, 'institution', $this->name);
if (!$request) {
......@@ -277,6 +318,20 @@ class Institution {
db_commit();
}
public function decline_requests($userids) {
global $USER;
if (!$USER->can_edit_institution($this->name)) {
throw new AccessDeniedException("Institution::decline_requests: access denied");
}
db_begin();
foreach ($userids as $id) {
$this->declineRequestFromUser($id);
}
db_commit();
}
public function inviteUser($user) {
$userid = is_object($user) ? $user->id : $user;
db_begin();
......@@ -296,11 +351,31 @@ class Institution {
db_commit();
}
public function invite_users($userids) {
global $USER;
if (!$USER->can_edit_institution($this->name)) {
throw new AccessDeniedException("Institution::invite_users: access denied");
}
db_begin();
foreach ($userids as $id) {
$this->inviteUser($id);
}
db_commit();
}
public function removeMembers($userids) {
// Remove self last.
global $USER;
if (!$USER->can_edit_institution($this->name)) {
throw new AccessDeniedException("Institution::removeMembers: access denied");
}
$users = get_records_select_array('usr', 'id IN (' . join(',', array_map('intval', $userids)) . ')');
$removeself = false;
db_begin();
foreach ($users as $user) {
if ($user->id == $USER->id) {
$removeself = true;
......@@ -311,6 +386,7 @@ class Institution {
if ($removeself) {
$USER->leave_institution($this->name);
}
db_commit();
}
public function removeMember($user) {
......
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