Merge branch 'MDL-78302-402' of https://github.com/paulholden/moodle into MOODLE_402_...
[moodle.git] / admin / user / user_bulk_delete.php
blob859c92f49ddb9f0bbc9ddc6d59f709a7d33b3843
1 <?php
2 /**
3 * script for bulk user delete operations
4 */
6 require_once('../../config.php');
7 require_once($CFG->libdir.'/adminlib.php');
9 $confirm = optional_param('confirm', 0, PARAM_BOOL);
11 admin_externalpage_setup('userbulk');
12 require_capability('moodle/user:delete', context_system::instance());
14 $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
16 if (empty($SESSION->bulk_users)) {
17 redirect($return);
20 $PAGE->set_primary_active_tab('siteadminnode');
21 $PAGE->set_secondary_active_tab('users');
23 echo $OUTPUT->header();
25 //TODO: add support for large number of users
27 if ($confirm and confirm_sesskey()) {
28 $notifications = '';
29 list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
30 $rs = $DB->get_recordset_select('user', "deleted = 0 and id $in", $params);
31 foreach ($rs as $user) {
32 if (!is_siteadmin($user) and $USER->id != $user->id and delete_user($user)) {
33 unset($SESSION->bulk_users[$user->id]);
34 } else {
35 $notifications .= $OUTPUT->notification(get_string('deletednot', '', fullname($user, true)));
38 $rs->close();
39 \core\session\manager::gc(); // Remove stale sessions.
40 echo $OUTPUT->box_start('generalbox', 'notice');
41 if (!empty($notifications)) {
42 echo $notifications;
43 } else {
44 echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess');
46 $continue = new single_button(new moodle_url($return), get_string('continue'), 'post');
47 echo $OUTPUT->render($continue);
48 echo $OUTPUT->box_end();
49 } else {
50 list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
51 $userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
52 $usernames = implode(', ', $userlist);
53 echo $OUTPUT->heading(get_string('confirmation', 'admin'));
54 $formcontinue = new single_button(new moodle_url('user_bulk_delete.php', array('confirm' => 1)), get_string('yes'));
55 $formcancel = new single_button(new moodle_url('user_bulk.php'), get_string('no'), 'get');
56 echo $OUTPUT->confirm(get_string('deletecheckfull', '', $usernames), $formcontinue, $formcancel);
59 echo $OUTPUT->footer();