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.
13//
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/>.
16
17/**
18 * Bulk user action forms
19 *
20 * @package    core
21 * @copyright  Moodle
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once($CFG->libdir.'/formslib.php');
28require_once($CFG->libdir.'/datalib.php');
29
30/**
31 * Bulk user action form
32 *
33 * @copyright  Moodle
34 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class user_bulk_action_form extends moodleform {
37
38    /**
39     * Returns an array of action_link's of all bulk actions available for this user.
40     *
41     * @return array of action_link objects
42     */
43    public function get_actions(): array {
44
45        global $CFG;
46
47        $syscontext = context_system::instance();
48        $actions = [];
49        if (has_capability('moodle/user:update', $syscontext)) {
50            $actions['confirm'] = new action_link(
51                new moodle_url('/admin/user/user_bulk_confirm.php'),
52                get_string('confirm'));
53        }
54        if (has_capability('moodle/site:readallmessages', $syscontext) && !empty($CFG->messaging)) {
55            $actions['message'] = new action_link(
56                new moodle_url('/admin/user/user_bulk_message.php'),
57                get_string('messageselectadd'));
58        }
59        if (has_capability('moodle/user:delete', $syscontext)) {
60            $actions['delete'] = new action_link(
61                new moodle_url('/admin/user/user_bulk_delete.php'),
62                get_string('delete'));
63        }
64        $actions['displayonpage'] = new action_link(
65                new moodle_url('/admin/user/user_bulk_display.php'),
66                get_string('displayonpage'));
67
68        if (has_capability('moodle/user:update', $syscontext)) {
69            $actions['download'] = new action_link(
70                new moodle_url('/admin/user/user_bulk_download.php'),
71                get_string('download', 'admin'));
72        }
73
74        if (has_capability('moodle/user:update', $syscontext)) {
75            $actions['forcepasswordchange'] = new action_link(
76                new moodle_url('/admin/user/user_bulk_forcepasswordchange.php'),
77                get_string('forcepasswordchange'));
78        }
79        if (has_capability('moodle/cohort:assign', $syscontext)) {
80            $actions['addtocohort'] = new action_link(
81                new moodle_url('/admin/user/user_bulk_cohortadd.php'),
82                get_string('bulkadd', 'core_cohort'));
83        }
84
85        // Any plugin can append actions to this list by implementing a callback
86        // <component>_bulk_user_actions() which returns an array of action_link.
87        // Each new action's key should have a frankenstyle prefix to avoid clashes.
88        // See MDL-38511 for more details.
89        $moreactions = get_plugins_with_function('bulk_user_actions', 'lib.php');
90        foreach ($moreactions as $plugintype => $plugins) {
91            foreach ($plugins as $pluginfunction) {
92                $actions += $pluginfunction();
93            }
94        }
95
96        return $actions;
97
98    }
99
100    /**
101     * Form definition
102     */
103    public function definition() {
104        global $CFG;
105
106        $mform =& $this->_form;
107
108        $actions = [0 => get_string('choose') . '...'];
109        $bulkactions = $this->get_actions();
110        foreach ($bulkactions as $key => $action) {
111            $actions[$key] = $action->text;
112        }
113        $objs = array();
114        $objs[] =& $mform->createElement('select', 'action', null, $actions);
115        $objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));
116        $mform->addElement('group', 'actionsgrp', get_string('withselectedusers'), $objs, ' ', false);
117    }
118}
119
120/**
121 * Bulk user form
122 *
123 * @copyright  Moodle
124 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
125 */
126class user_bulk_form extends moodleform {
127
128    /**
129     * Form definition
130     */
131    public function definition() {
132
133        $mform =& $this->_form;
134        $acount =& $this->_customdata['acount'];
135        $scount =& $this->_customdata['scount'];
136        $ausers =& $this->_customdata['ausers'];
137        $susers =& $this->_customdata['susers'];
138        $total  =& $this->_customdata['total'];
139
140        $achoices = array();
141        $schoices = array();
142
143        if (is_array($ausers)) {
144            if ($total == $acount) {
145                $achoices[0] = get_string('allusers', 'bulkusers', $total);
146            } else {
147                $a = new stdClass();
148                $a->total  = $total;
149                $a->count = $acount;
150                $achoices[0] = get_string('allfilteredusers', 'bulkusers', $a);
151            }
152            $achoices = $achoices + $ausers;
153
154            if ($acount > MAX_BULK_USERS) {
155                $achoices[-1] = '...';
156            }
157
158        } else {
159            $achoices[-1] = get_string('nofilteredusers', 'bulkusers', $total);
160        }
161
162        if (is_array($susers)) {
163            $a = new stdClass();
164            $a->total  = $total;
165            $a->count = $scount;
166            $schoices[0] = get_string('allselectedusers', 'bulkusers', $a);
167            $schoices = $schoices + $susers;
168
169            if ($scount > MAX_BULK_USERS) {
170                $schoices[-1] = '...';
171            }
172
173        } else {
174            $schoices[-1] = get_string('noselectedusers', 'bulkusers');
175        }
176
177        $mform->addElement('header', 'users', get_string('usersinlist', 'bulkusers'));
178
179        $objs = array();
180        $objs[0] =& $mform->createElement('select', 'ausers', get_string('available', 'bulkusers'), $achoices, 'size="15"');
181        $objs[0]->setMultiple(true);
182        $objs[1] =& $mform->createElement('select', 'susers', get_string('selected', 'bulkusers'), $schoices, 'size="15"');
183        $objs[1]->setMultiple(true);
184
185        $grp =& $mform->addElement('group', 'usersgrp', get_string('users', 'bulkusers'), $objs, ' ', false);
186        $mform->addHelpButton('usersgrp', 'users', 'bulkusers');
187
188        $mform->addElement('static', 'comment');
189
190        $objs = array();
191        $objs[] =& $mform->createElement('submit', 'addsel', get_string('addsel', 'bulkusers'));
192        $objs[] =& $mform->createElement('submit', 'removesel', get_string('removesel', 'bulkusers'));
193        $grp =& $mform->addElement('group', 'buttonsgrp', get_string('selectedlist', 'bulkusers'), $objs, null, false);
194        $mform->addHelpButton('buttonsgrp', 'selectedlist', 'bulkusers');
195        $objs = array();
196        $objs[] =& $mform->createElement('submit', 'addall', get_string('addall', 'bulkusers'));
197        $objs[] =& $mform->createElement('submit', 'removeall', get_string('removeall', 'bulkusers'));
198        $grp =& $mform->addElement('group', 'buttonsgrp2', '', $objs, null, false);
199
200        $renderer =& $mform->defaultRenderer();
201        $template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
202        $renderer->setGroupElementTemplate($template, 'usersgrp');
203    }
204}
205