Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once($CFG->libdir.'/formslib.php');
28
require_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
 */
36
class user_bulk_action_form extends moodleform {
37
 
38
    /** @var bool */
39
    protected $hasbulkactions = false;
40
 
41
    /** @var array|null */
42
    protected $actions = null;
43
 
44
    /**
45
     * Returns an array of action_link's of all bulk actions available for this user.
46
     *
47
     * @param bool $flatlist whether to return a flat list (for easier searching) or a list with
48
     *     option groups that can be used to build a select element
49
     * @return array of action_link objects
50
     */
51
    public function get_actions(bool $flatlist = true): array {
52
        if ($this->actions === null) {
53
            $this->actions = $this->build_actions();
54
            $this->hasbulkactions = !empty($this->actions);
55
        }
56
        if ($flatlist) {
57
            return array_reduce($this->actions, fn($carry, $item) => $carry + $item, []);
58
        }
59
        return $this->actions;
60
    }
61
 
62
    /**
63
     * Builds the list of bulk user actions available for this user.
64
     *
65
     * @return array
66
     */
67
    protected function build_actions(): array {
68
 
69
        global $CFG;
70
 
71
        $canaccessbulkactions = has_any_capability(['moodle/user:update', 'moodle/user:delete'], context_system::instance());
72
 
73
        $syscontext = context_system::instance();
74
        $actions = [];
75
        if (has_capability('moodle/user:update', $syscontext)) {
76
            $actions['confirm'] = new action_link(
77
                new moodle_url('/admin/user/user_bulk_confirm.php'),
78
                get_string('confirm'));
79
        }
80
        if ($canaccessbulkactions && has_capability('moodle/site:readallmessages', $syscontext) && !empty($CFG->messaging)) {
81
            $actions['message'] = new action_link(
82
                new moodle_url('/admin/user/user_bulk_message.php'),
83
                get_string('messageselectadd'));
84
        }
85
        if (has_capability('moodle/user:delete', $syscontext)) {
86
            $actions['delete'] = new action_link(
87
                new moodle_url('/admin/user/user_bulk_delete.php'),
88
                get_string('delete'));
89
        }
90
        if ($canaccessbulkactions) {
91
            $actions['displayonpage'] = new action_link(
92
                new moodle_url('/admin/user/user_bulk_display.php'),
93
                get_string('displayonpage'));
94
        }
95
 
96
        if (has_capability('moodle/user:update', $syscontext)) {
97
            $actions['download'] = new action_link(
98
                new moodle_url('/admin/user/user_bulk_download.php'),
99
                get_string('download', 'admin'));
100
        }
101
 
102
        if (has_capability('moodle/user:update', $syscontext)) {
103
            $actions['forcepasswordchange'] = new action_link(
104
                new moodle_url('/admin/user/user_bulk_forcepasswordchange.php'),
105
                get_string('forcepasswordchange'));
106
        }
107
        if ($canaccessbulkactions && has_capability('moodle/cohort:assign', $syscontext)) {
108
            $actions['addtocohort'] = new action_link(
109
                new moodle_url('/admin/user/user_bulk_cohortadd.php'),
110
                get_string('bulkadd', 'core_cohort'));
111
        }
112
 
113
        // Collect all bulk user actions.
114
        $hook = new \core_user\hook\extend_bulk_user_actions();
115
 
116
        // Add actions from core.
117
        foreach ($actions as $identifier => $action) {
118
            $hook->add_action($identifier, $action);
119
        }
120
 
121
        // Add actions from the legacy callback 'bulk_user_actions'.
122
        $moreactions = get_plugins_with_function('bulk_user_actions', 'lib.php', true, true);
123
        foreach ($moreactions as $plugintype => $plugins) {
124
            foreach ($plugins as $pluginfunction) {
125
                $pluginactions = $pluginfunction();
126
                foreach ($pluginactions as $identifier => $action) {
127
                    $hook->add_action($identifier, $action);
128
                }
129
            }
130
        }
131
 
132
        // Any plugin can append user bulk actions to this list by implementing a hook callback.
133
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
134
 
135
        // This method may be called from 'Bulk actions' and 'Browse user list' pages. Some actions
136
        // may be irrelevant in one of the contexts and they can be excluded by specifying the
137
        // 'excludeactions' customdata.
138
        $excludeactions = $this->_customdata['excludeactions'] ?? [];
139
        foreach ($excludeactions as $excludeaction) {
140
            $hook->add_action($excludeaction, null);
141
        }
142
        return $hook->get_actions();
143
    }
144
 
145
    /**
146
     * Form definition
147
     */
148
    public function definition() {
149
        $mform =& $this->_form;
150
 
151
        // Most bulk actions perform a redirect on selection, so we shouldn't trigger formchange warnings (specifically because
152
        // the user must have _already_ changed the current form by selecting users to perform the action on).
153
        $mform->disable_form_change_checker();
154
 
155
        $mform->addElement('hidden', 'returnurl');
156
        $mform->setType('returnurl', PARAM_LOCALURL);
157
 
158
        // When 'passuserids' is specified in the customdata, the user ids are expected in the form
159
        // data rather than in the $SESSION->bulk_users .
160
        $passuserids = !empty($this->_customdata['passuserids']);
161
        $mform->addElement('hidden', 'passuserids', $passuserids);
162
        $mform->setType('passuserids', PARAM_BOOL);
163
 
164
        $mform->addElement('hidden', 'userids');
165
        $mform->setType('userids', PARAM_SEQUENCE);
166
 
167
        $actions = ['' => [0 => get_string('choose') . '...']];
168
        $bulkactions = $this->get_actions(false);
169
        foreach ($bulkactions as $category => $categoryactions) {
170
            $actions[$category] = array_map(fn($action) => $action->text, $categoryactions);
171
        }
172
        $objs = array();
173
        $objs[] = $selectel = $mform->createElement('selectgroups', 'action', get_string('userbulk', 'admin'), $actions);
174
        $selectel->setHiddenLabel(true);
175
        if (empty($this->_customdata['hidesubmit'])) {
176
            $objs[] =& $mform->createElement('submit', 'doaction', get_string('go'));
177
        }
178
        $mform->addElement('group', 'actionsgrp', get_string('withselectedusers'), $objs, ' ', false);
179
    }
180
 
181
    /**
182
     * Is there at least one available bulk action in this form
183
     *
184
     * @return bool
185
     */
186
    public function has_bulk_actions(): bool {
187
        return $this->hasbulkactions;
188
    }
189
}
190
 
191
/**
192
 * Bulk user form
193
 *
194
 * @copyright  Moodle
195
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
196
 */
197
class user_bulk_form extends moodleform {
198
 
199
    /**
200
     * Form definition
201
     */
202
    public function definition() {
203
 
204
        $mform =& $this->_form;
205
        $acount =& $this->_customdata['acount'];
206
        $scount =& $this->_customdata['scount'];
207
        $ausers =& $this->_customdata['ausers'];
208
        $susers =& $this->_customdata['susers'];
209
        $total  =& $this->_customdata['total'];
210
 
211
        $achoices = array();
212
        $schoices = array();
213
 
214
        if (is_array($ausers)) {
215
            if ($total == $acount) {
216
                $achoices[0] = get_string('allusers', 'bulkusers', $total);
217
            } else {
218
                $a = new stdClass();
219
                $a->total  = $total;
220
                $a->count = $acount;
221
                $achoices[0] = get_string('allfilteredusers', 'bulkusers', $a);
222
            }
223
            $achoices = $achoices + $ausers;
224
 
225
            if ($acount > MAX_BULK_USERS) {
226
                $achoices[-1] = '...';
227
            }
228
 
229
        } else {
230
            $achoices[-1] = get_string('nofilteredusers', 'bulkusers', $total);
231
        }
232
 
233
        if (is_array($susers)) {
234
            $a = new stdClass();
235
            $a->total  = $total;
236
            $a->count = $scount;
237
            $schoices[0] = get_string('allselectedusers', 'bulkusers', $a);
238
            $schoices = $schoices + $susers;
239
 
240
            if ($scount > MAX_BULK_USERS) {
241
                $schoices[-1] = '...';
242
            }
243
 
244
        } else {
245
            $schoices[-1] = get_string('noselectedusers', 'bulkusers');
246
        }
247
 
248
        $mform->addElement('header', 'users', get_string('usersinlist', 'bulkusers'));
249
 
250
        $objs = array();
251
        $objs[0] =& $mform->createElement('select', 'ausers', get_string('available', 'bulkusers'), $achoices, 'size="15"');
252
        $objs[0]->setMultiple(true);
253
        $objs[1] =& $mform->createElement('select', 'susers', get_string('selected', 'bulkusers'), $schoices, 'size="15"');
254
        $objs[1]->setMultiple(true);
255
 
256
        $grp =& $mform->addElement('group', 'usersgrp', get_string('users', 'bulkusers'), $objs, ' ', false);
257
        $mform->addHelpButton('usersgrp', 'users', 'bulkusers');
258
 
259
        $mform->addElement('static', 'comment');
260
 
261
        $objs = array();
262
        $objs[] =& $mform->createElement('submit', 'addsel', get_string('addsel', 'bulkusers'));
263
        $objs[] =& $mform->createElement('submit', 'removesel', get_string('removesel', 'bulkusers'));
264
        $grp =& $mform->addElement('group', 'buttonsgrp', get_string('selectedlist', 'bulkusers'), $objs, null, false);
265
        $mform->addHelpButton('buttonsgrp', 'selectedlist', 'bulkusers');
266
        $objs = array();
267
        $objs[] =& $mform->createElement('submit', 'addall', get_string('addall', 'bulkusers'));
268
        $objs[] =& $mform->createElement('submit', 'removeall', get_string('removeall', 'bulkusers'));
269
        $grp =& $mform->addElement('group', 'buttonsgrp2', '', $objs, null, false);
270
 
271
        $renderer =& $mform->defaultRenderer();
272
        $template = '<label class="qflabel" style="vertical-align:top">{label}</label> {element}';
273
        $renderer->setGroupElementTemplate($template, 'usersgrp');
274
    }
275
}