MDL-73975 course: Remove course_search_form template
[moodle.git] / admin / user / user_bulk_delete.php
blob2a9ca180bcc2668a6f8ed573faebf0bf94e29ff4
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 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
15 $return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
17 if (empty($SESSION->bulk_users)) {
18 redirect($return);
21 $PAGE->set_primary_active_tab('siteadminnode');
22 $PAGE->set_secondary_active_tab('users');
24 echo $OUTPUT->header();
26 //TODO: add support for large number of users
28 if ($confirm and confirm_sesskey()) {
29 $notifications = '';
30 list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
31 $rs = $DB->get_recordset_select('user', "deleted = 0 and id $in", $params);
32 foreach ($rs as $user) {
33 if (!is_siteadmin($user) and $USER->id != $user->id and delete_user($user)) {
34 unset($SESSION->bulk_users[$user->id]);
35 } else {
36 $notifications .= $OUTPUT->notification(get_string('deletednot', '', fullname($user, true)));
39 $rs->close();
40 \core\session\manager::gc(); // Remove stale sessions.
41 echo $OUTPUT->box_start('generalbox', 'notice');
42 if (!empty($notifications)) {
43 echo $notifications;
44 } else {
45 echo $OUTPUT->notification(get_string('changessaved'), 'notifysuccess');
47 $continue = new single_button($return, get_string('continue'), 'post');
48 echo $OUTPUT->render($continue);
49 echo $OUTPUT->box_end();
50 } else {
51 list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
52 $userlist = $DB->get_records_select_menu('user', "id $in", $params, 'fullname', 'id,'.$DB->sql_fullname().' AS fullname');
53 $usernames = implode(', ', $userlist);
54 echo $OUTPUT->heading(get_string('confirmation', 'admin'));
55 $formcontinue = new single_button(new moodle_url('user_bulk_delete.php',
56 ['confirm' => 1, 'returnurl' => $returnurl]), get_string('yes'));
57 $formcancel = new single_button($return, get_string('no'), 'get');
58 echo $OUTPUT->confirm(get_string('deletecheckfull', '', $usernames), $formcontinue, $formcancel);
61 echo $OUTPUT->footer();