Merge branch 'MDL-81525-main' of https://github.com/andrewnicols/moodle
[moodle.git] / admin / user / user_bulk.php
blob5084ffe4d5b8431d49d4d2ecff570de1481e3c57
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Bulk user actions
20 * @package core
21 * @copyright Moodle
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../../config.php');
26 require_once($CFG->libdir.'/adminlib.php');
27 require_once($CFG->dirroot.'/'.$CFG->admin.'/user/lib.php');
28 require_once($CFG->dirroot.'/'.$CFG->admin.'/user/user_bulk_forms.php');
30 admin_externalpage_setup('userbulk');
32 if (!isset($SESSION->bulk_users)) {
33 $SESSION->bulk_users = array();
35 // Create the user filter form.
36 $ufiltering = new user_filtering();
38 // Create the bulk operations form.
39 $actionform = new user_bulk_action_form();
40 $actionform->set_data(['returnurl' => $PAGE->url->out_as_local_url(false)]);
41 if ($data = $actionform->get_data()) {
42 if ($data->passuserids) {
43 // This means we called the form from /admin/user.php or similar and the userids should be taken from the form
44 // data and not from $SESSION->bulk_users. For backwards compatibility we still set $SESSION->bulk_users.
45 $users = preg_split('/,/', $data->userids, -1, PREG_SPLIT_NO_EMPTY);
46 $SESSION->bulk_users = array_combine($users, $users);
48 // Check if an action should be performed and do so.
49 $bulkactions = $actionform->get_actions();
50 if (array_key_exists($data->action, $bulkactions)) {
51 redirect(new moodle_url($bulkactions[$data->action]->url, ['returnurl' => $data->returnurl ?: null]));
56 $userbulkform = new user_bulk_form(null, get_selection_data($ufiltering));
58 if ($data = $userbulkform->get_data()) {
59 if (!empty($data->addall)) {
60 add_selection_all($ufiltering);
62 } else if (!empty($data->addsel)) {
63 if (!empty($data->ausers)) {
64 if (in_array(0, $data->ausers)) {
65 add_selection_all($ufiltering);
66 } else {
67 foreach ($data->ausers as $userid) {
68 if ($userid == -1) {
69 continue;
71 if (!isset($SESSION->bulk_users[$userid])) {
72 $SESSION->bulk_users[$userid] = $userid;
78 } else if (!empty($data->removeall)) {
79 $SESSION->bulk_users = array();
81 } else if (!empty($data->removesel)) {
82 if (!empty($data->susers)) {
83 if (in_array(0, $data->susers)) {
84 $SESSION->bulk_users = array();
85 } else {
86 foreach ($data->susers as $userid) {
87 if ($userid == -1) {
88 continue;
90 unset($SESSION->bulk_users[$userid]);
96 // Reset the form selections.
97 unset($_POST);
98 $userbulkform = new user_bulk_form(null, get_selection_data($ufiltering));
100 echo $OUTPUT->header();
102 $ufiltering->display_add();
103 $ufiltering->display_active();
105 $userbulkform->display();
107 $actionform->display();
109 echo $OUTPUT->footer();