Commit 625fdf36 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Allow reactivation of expired users from suspended users page (bug #914072)



Modifies the suspended users page so that the admin can browse expired
users too, and unexpire multiple users at once.

Change-Id: I55489776998a898e4c8f7c08de8b8ff5bcbd81e6
Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 81f26254
......@@ -30,23 +30,23 @@ define('INSTITUTIONALADMIN', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$type = param_alpha('type', 'suspended');
// Filter for institutional admins:
$instsql = $USER->get('admin') ? '' : '
AND ui.institution IN (' . join(',', array_map('db_quote', array_keys($USER->get('institutions')))) . ')';
// NOTE: the check is not done on the 'active' column here, since suspended
// users are by definition not active. However deleted users are filtered out.
$count = get_field_sql('
SELECT COUNT(*)
FROM (
SELECT u.id
FROM {usr} u
LEFT OUTER JOIN {usr_institution} ui ON (ui.usr = u.id)
WHERE suspendedcusr IS NOT NULL
WHERE ' . ($type == 'expired' ? 'u.expiry < current_timestamp' : 'suspendedcusr IS NOT NULL') . '
AND deleted = 0 ' . $instsql . '
GROUP BY u.id
) AS a');
......@@ -54,16 +54,16 @@ $count = get_field_sql('
$data = get_records_sql_assoc('
SELECT
u.id, u.firstname, u.lastname, u.studentid, u.suspendedctime, u.suspendedreason AS reason,
ua.firstname AS cusrfirstname, ua.lastname AS cusrlastname
ua.firstname AS cusrfirstname, ua.lastname AS cusrlastname, ' . db_format_tsfield('u.expiry', 'expiry') . '
FROM {usr} u
LEFT JOIN {usr} ua on (ua.id = u.suspendedcusr)
LEFT OUTER JOIN {usr_institution} ui ON (ui.usr = u.id)
WHERE u.suspendedcusr IS NOT NULL
WHERE ' . ($type == 'expired' ? 'u.expiry < current_timestamp' : 'u.suspendedcusr IS NOT NULL') . '
AND u.deleted = 0 ' . $instsql . '
GROUP BY
u.id, u.firstname, u.lastname, u.studentid, u.suspendedctime, u.suspendedreason,
ua.firstname, ua.lastname
ORDER BY u.suspendedctime, u.id
ua.firstname, ua.lastname, u.expiry
ORDER BY ' . ($type == 'expired' ? 'u.expiry' : 'u.suspendedctime') . ', u.id
LIMIT ?
OFFSET ?', array($limit, $offset));
......@@ -87,6 +87,7 @@ else {
$record->firstname = $record->cusrfirstname;
$record->lastname = $record->cusrlastname;
$record->cusrname = full_name($record);
$record->expiry = $record->expiry ? format_date($record->expiry, 'strftimew3cdate') : '-';
unset($record->firstname, $record->lastname);
}
}
......
......@@ -35,7 +35,33 @@ define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'suspendedusers');
require_once('pieforms/pieform.php');
$type = param_alpha('type', 'suspended') == 'expired' ? 'expired' : 'suspended';
$enc_type = json_encode($type);
$typeform = pieform(
array(
'name' => 'usertype',
'elements' => array(
'type' => array(
'type' => 'select',
'title' => get_string('show') . ':',
'options' => array(
'suspended' => get_string('suspendedusers', 'admin'),
'expired' => get_string('expiredusers', 'admin'),
),
'defaultvalue' => $type,
),
'typesubmit' => array(
'type' => 'submit',
'class' => 'js-hidden',
'value' => get_string('change'),
),
),
)
);
$smarty = smarty(array('tablerenderer'));
$smarty->assign('typeform', $typeform);
$smarty->assign('INLINEJAVASCRIPT', <<<EOF
var suspendedlist = new TableRenderer(
......@@ -51,11 +77,24 @@ var suspendedlist = new TableRenderer(
},
'cusrname',
'reason',
'expiry',
function (rowdata) { return TD(null, INPUT({'type': 'checkbox', 'name': 'usr_' + rowdata.id})); }
]
);
suspendedlist.type = {$enc_type};
suspendedlist.statevars.push('type');
suspendedlist.updateOnLoad();
addLoadEvent(function() {
connect('usertype_type', 'onchange', function() {
if (suspendedlist.type != $('usertype_type').value) {
suspendedlist.offset = 0;
suspendedlist.type = $('usertype_type').value;
suspendedlist.doupdate();
}
});
});
EOF
);
......@@ -74,7 +113,12 @@ $form = new Pieform(array(
'confirm' => get_string('confirmdeleteusers', 'admin'),
'name' => 'delete',
'value' => get_string('deleteusers', 'admin')
)
),
'unexpire' => array(
'type' => 'submit',
'name' => 'unexpire',
'value' => get_string('unexpireusers', 'admin')
),
)
));
$smarty->assign('buttonformopen', $form->get_form_tag());
......@@ -94,6 +138,18 @@ function buttons_submit_unsuspend(Pieform $form, $values) {
redirect('/admin/users/suspended.php');
}
function buttons_submit_unexpire(Pieform $form, $values) {
global $SESSION;
$ids = get_user_ids_from_post();
foreach ($ids as $userid) {
unexpire_user($userid);
}
$SESSION->add_ok_msg(get_string('usersreactivated', 'admin'));
redirect('/admin/users/suspended.php?type=expired');
}
function buttons_submit_delete(Pieform $form, $values) {
global $SESSION;
......
......@@ -597,6 +597,10 @@ $string['usersunsuspendedsuccessfully'] = 'Users unsuspended successfully';
$string['suspendingadmin'] = 'Suspending Admin';
$string['usersuspended'] = 'User suspended';
$string['userunsuspended'] = 'User unsuspended';
$string['expiredusers'] = 'Expired users';
$string['expired'] = 'Expired';
$string['unexpireusers'] = 'Reactivate expired users';
$string['usersreactivated'] = 'Users reactivated';
// User account settings
$string['accountsettings'] = 'Account settings';
......
......@@ -1282,6 +1282,31 @@ function expire_user($userid) {
* @param int $userid The ID of user to unexpire
*/
function unexpire_user($userid) {
$lifetime = get_config('defaultaccountlifetime');
$now = time();
$dbnow = db_format_timestamp($now);
$values = array($dbnow, $userid, $dbnow);
if ($lifetime) {
$newexpiry = '?';
array_unshift($values, db_format_timestamp($now + $lifetime));
}
else {
$newexpiry = 'NULL';
}
// Update the lastaccess time here to stop users who are currently
// inactive from expiring again on the next cron run. We can leave
// inactivemailsent turned on until the user logs in again.
execute_sql("
UPDATE {usr} SET expiry = $newexpiry, expirymailsent = 0, lastaccess = ?
WHERE id = ? AND expiry IS NOT NULL AND expiry < ?",
$values
);
handle_event('unexpireuser', $userid);
}
......
{include file="header.tpl"}
{$typeform|safe}
{$buttonformopen|safe}
{$buttonform|safe}
<table id="suspendedlist" class="table fullwidth">
......@@ -10,6 +11,7 @@
<th>{str tag=studentid}</th>
<th>{str tag=suspendingadmin section=admin}</th>
<th>{str tag=suspensionreason section=admin}</th>
<th>{str tag=expired section=admin}</th>
<th>{str tag=select}</th>
</tr>
</thead>
......
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