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

Move institution membership functions into institution class; set expiry when joining

parent ab75da39
......@@ -73,6 +73,7 @@ if ($usertype == 'requesters') {
'righttitle' => get_string('userstobeadded', 'admin'),
'searchparams' => array('requested' => 1),
);
$update = 'addUserAsMember';
$submittext = get_string('addmembers', 'admin');
} else if ($usertype == 'members') {
// LHS shows institution members, RHS shows users to be removed
......@@ -83,6 +84,7 @@ if ($usertype == 'requesters') {
'righttitle' => get_string('userstoberemoved', 'admin'),
'searchparams' => array('member' => 1),
);
$update = 'removeMember';
$submittext = get_string('removeusers', 'admin');
} else { // $usertype == nonmembers
// Behaviour depends on whether we allow users to have > 1 institution
......@@ -95,6 +97,7 @@ if ($usertype == 'requesters') {
'righttitle' => get_string('userstobeinvited', 'admin'),
'searchparams' => array('member' => 0, 'invited' => 0, 'requested' => 0)
);
$update = 'inviteUser';
$submittext = get_string('inviteusers', 'admin');
}
......@@ -116,6 +119,11 @@ $userlistform = pieform(array(
'value' => $usertype,
'rules' => array('regex' => '/^[a-z]+$/')
),
'update' => array(
'type' => 'hidden',
'value' => $update,
'rules' => array('regex' => '/^[a-zA-Z]+$/')
),
'submit' => array(
'type' => 'submit',
'value' => $submittext
......@@ -126,42 +134,25 @@ $userlistform = pieform(array(
function institutionusers_submit(Pieform $form, $values) {
global $SESSION, $USER;
$url = '/admin/users/institutionusers.php?usertype=' . $values['usertype'];
$inst = $values['institution'];
if (empty($inst) || !$USER->is_institutional_admin($inst)) {
$SESSION->add_error_msg(get_string('notadminforinstitution', 'admin'));
redirect('/admin/users/institutionusers.php?usertype='.$values['usertype']);
redirect($url);
}
$update = 'update_' . $values['usertype'];
if ($update($values['users'], $values['institution'])) {
$SESSION->add_ok_msg(get_string('usersupdated', 'admin'));
} else {
$SESSION->add_error_msg(get_string('dberrorupdatingusers', 'admin'));
$institution = new Institution($values['institution']);
if (!in_array($values['update'], array('addUserAsMember', 'removeMember', 'inviteUser'))) {
$SESSION->add_error_msg(get_string('errorupdatinginstitutionusers', 'admin'));
redirect($url);
}
redirect('/admin/users/institutionusers.php?usertype='.$values['usertype']);
}
function update_nonmembers($userids, $institution) {
db_begin();
foreach ($userids as $id) {
invite_user_to_institution($id, $institution);
foreach ($values['users'] as $id) {
$institution->{$values['update']}($id);
}
db_commit();
return true;
}
function update_requesters($userids, $institution) {
db_begin();
foreach ($userids as $id) {
add_user_to_institution($id, $institution);
}
db_commit();
return true;
}
function update_members($userids, $institution) {
delete_records_select('usr_institution', 'usr IN (' . join(',', $userids) . ') AND institution = ' . db_quote($institution));
return true;
$SESSION->add_ok_msg(get_string('usersupdated', 'admin'));
redirect($url);
}
$wwwroot = get_config('wwwroot');
......
......@@ -30,6 +30,7 @@ define('MENUITEM', 'configusers/uploadcsv');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('uploadcsv', 'admin'));
require_once('pieforms/pieform.php');
require_once('institution.php');
safe_require('artefact', 'internal');
// Turn on autodetecting of line endings, so mac newlines (\r) will work
......@@ -269,7 +270,8 @@ function uploadcsv_submit(Pieform $form, $values) {
// Don't be tempted to use 'explode' here. There may be > 1 underscore.
$break = strpos($values['authinstance'], '_');
$authinstance = substr($values['authinstance'], 0, $break);
$institution = substr($values['authinstance'], $break+1);
$institution = substr($values['authinstance'], $break+1);
$institution = new Institution($institution);
foreach ($CSVDATA as $record) {
log_debug('adding user ' . $record[$formatkeylookup['username']]);
......@@ -288,14 +290,8 @@ function uploadcsv_submit(Pieform $form, $values) {
$user->passwordchange = 1;
$id = insert_record('usr', $user, 'id', true);
$user->id = $id;
if ($institution != 'mahara') {
$userinstitution = new StdClass;
$userinstitution->usr = $user->id;
$userinstitution->institution = $institution;
if (isset($formatkeylookup['studentid'])) {
$userinstitution->studentid = $record[$formatkeylookup['studentid']];
}
insert_record('usr_institution', $userinstitution);
if ($institution->name != 'mahara') {
$institution->addUserAsMember($user);
}
foreach ($FORMAT as $field) {
......
......@@ -348,7 +348,8 @@ class User {
public function join_institution($institution) {
if ($institution != 'mahara' && !$this->in_institution($institution)) {
require_once('institution.php');
add_user_to_institution($this->id, $institution);
$institution = new Institution($institution);
$institution->addUserAsMember($this);
}
}
......@@ -374,22 +375,9 @@ class User {
if (empty($institution) || $institution == 'mahara') {
return;
}
$request = get_record('usr_institution_request', 'usr', $this->get('id'), 'institution', $institution);
if (!$request) {
$request = (object) array(
'usr' => $this->get('id'),
'institution' => $institution,
'confirmedusr' => 1,
'studentid' => $this->get('studentid'),
'ctime' => db_format_timestamp(time())
);
insert_record('usr_institution_request', $request);
// Send request notification
} else if ($request->confirmedinstitution) {
$this->join_institution($institution);
delete_records('usr_institution_request', 'usr', $this->get('id'), 'institution', $institution);
// Send confirmation
}
require_once('institution.php');
$institution = new Institution($institution);
$institution->addRequestFromUser($this);
}
}
......
......@@ -509,6 +509,8 @@ function xmldb_core_upgrade($oldversion=0) {
// Add theme, default institution membership period to institution table
execute_sql('ALTER TABLE {institution} ADD COLUMN theme varchar(255)');
set_field('institution', 'theme', get_config('theme'));
execute_sql('ALTER TABLE {institution} ADD COLUMN defaultmembershipperiod bigint');
}
......
......@@ -51,7 +51,7 @@ class Institution {
return $this;
}
if ($this->findByName($name)) {
if (!$this->findByName($name)) {
throw new ParamOutOfRangeException('No such institution');
}
}
......@@ -214,6 +214,61 @@ class Institution {
$this->verifyReady();
}
public function addUserAsMember($user) {
$userinst = new StdClass;
$userinst->institution = $this->name;
if (is_object($user)) {
if (!empty($user->studentid)) {
$userinst->studentid = $user->studentid;
}
$userinst->usr = $user->id;
} else {
$userinst->usr = $user;
}
if (!empty($this->defaultmembershipperiod)) {
$userinst->expiry = db_format_timestamp(time() + $this->defaultmembershipperiod);
}
db_begin();
insert_record('usr_institution', $userinst);
delete_records('usr_institution_request', 'usr', $userinst->usr, 'institution', $this->name);
// Send confirmation
db_commit();
handle_event('updateuser', $userinst->usr);
}
public function addRequestFromUser($user) {
$request = get_record('usr_institution_request', 'usr', $user->id, 'institution', $this->name);
if (!$request) {
$request = (object) array(
'usr' => $user->id,
'institution' => $this->name,
'confirmedusr' => 1,
'studentid' => $user->studentid,
'ctime' => db_format_timestamp(time())
);
insert_record('usr_institution_request', $request);
// Send request notification
} else if ($request->confirmedinstitution) {
$this->addUserAsMember($user);
}
handle_event('updateuser', $user->id);
}
public function inviteUser($userid) {
insert_record('usr_institution_request', (object) array(
'usr' => $userid,
'institution' => $this->name,
'confirmedinstitution' => 1,
'ctime' => db_format_timestamp(time())
));
// Send notification
handle_event('updateuser', $userid);
}
public function removeMember($userid) {
delete_records('usr_institution', 'usr', $userid, 'institution', $this->name);
handle_event('updateuser', $userid);
}
}
function get_institution_selector() {
......@@ -253,26 +308,4 @@ function get_institution_selector() {
return $institutionelement;
}
function add_user_to_institution($id, $institution) {
// @todo: set expiry, studentid, ctime
db_begin();
insert_record('usr_institution', (object) array(
'usr' => $id,
'institution' => $institution
));
delete_records('usr_institution_request', 'usr', $id, 'institution', $institution);
// Send confirmation
db_commit();
}
function invite_user_to_institution($id, $institution) {
insert_record('usr_institution_request', (object) array(
'usr' => $id,
'institution' => $institution,
'confirmedinstitution' => 1,
'ctime' => db_format_timestamp(time())
));
// Send notification
}
?>
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