-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
122 lines (105 loc) · 3.81 KB
/
index.php
File metadata and controls
122 lines (105 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use core\task\manager;
use tool_userbulkdelete\deleteuser_task;
use tool_userbulkdelete\queuecompletion_task;
require_once('../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
$confirm = optional_param('confirm', 0, PARAM_BOOL);
require_login();
admin_externalpage_setup('userbulkdelete');
require_capability('moodle/user:delete', context_system::instance());
/** @var \tool_userbulkdelete\output\renderer $renderer */
$renderer = $PAGE->get_renderer("tool_userbulkdelete");
$execute = optional_param("action", "preview", PARAM_ALPHA) === "delete" and confirm_sesskey();
$PAGE->set_button($renderer->get_backlink());
echo $OUTPUT->header();
if ($execute) {
// Do the deletion.
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$rs = $DB->get_recordset_select('user', "deleted = 0 and id $in", $params);
$pid = time();
$deletioncount = 0;
foreach ($rs as $user) {
if (!is_siteadmin($user) and $USER->id != $user->id) {
$task = deleteuser_task::create($user->id, $USER->id, $pid);
manager::queue_adhoc_task($task);
$deletioncount++;
} else {
// Remove the siteadmin and the currentuser from the session.
unset($SESSION->bulk_users[$user->id]);
}
}
echo $renderer->get_success_message($deletioncount);
if ($deletioncount) {
$notificationtask = queuecompletion_task::create($pid, $USER->id, $deletioncount);
manager::queue_adhoc_task($notificationtask);
}
unset($SESSION->bulk_users);
} else {
// Preview.
// Only create the query if we have a selection.
if (!empty($SESSION->bulk_users)) {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$rs = $DB->get_recordset_select('user', "deleted = 0 and id $in", $params);
} else {
$rs = [];
$empty = true;
}
// Table output.
$table = new html_table();
$table->head = [
'id',
get_string('fullname'),
get_string('email'),
get_string('canbedeleted', 'tool_userbulkdelete')
];
$table->size = ['3%', '47%', '50%'];
$table->align = ['left'];
$table->data = [];
$errors = [];
$possible = 0;
$empty = true;
foreach ($rs as $user) {
$empty = false;
$canbedeleted = !is_siteadmin($user) && $USER->id != $user->id;
$table->data[] = [
$user->id,
$user->firstname.' '.$user->lastname,
$user->email,
$canbedeleted ? $renderer->get_ok_picto() : $renderer->get_failed_picto()
];
if (!$canbedeleted) {
$errors[] = $user;
}
$possible++;
}
if ($rs !== []) {
$rs->close();
}
if (!$empty) {
$impossible = count($errors);
$possible -= $impossible;
echo $renderer->get_title();
echo html_writer::table($table);
echo $renderer->get_schedulepossible($possible);
echo $renderer->get_scheduleimpossible($impossible);
echo $renderer->get_dolink();
} else {
echo $renderer->get_no_selection();
}
}
echo $OUTPUT->footer();