-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbulkuserpage.class.php
379 lines (315 loc) · 12.7 KB
/
bulkuserpage.class.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* ELIS(TM): Enterprise Learning Intelligence Suite
* Copyright (C) 2008-2013 Remote-Learner.net Inc (http://www.remote-learner.net)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package local_elisprogram
* @author Remote-Learner.net Inc
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2008-2013 Remote-Learner.net Inc (http://www.remote-learner.net)
*
*/
/**
* Page for bulk user actions.
*/
defined('MOODLE_INTERNAL') || die();
require_once elispm::lib('selectionpage.class.php');
require_once elispm::file('form/bulkuserform.class.php');
require_once elispm::lib('data/user.class.php');
/* *** TBD ***
require_once CURMAN_DIRLOCATION . '/lib/table.class.php';
require_once CURMAN_DIRLOCATION . '/lib/contexts.php';
*** */
/**
* Bulk user action page.
*/
class bulkuserpage extends selectionpage {
var $pagename = 'bulkuser';
var $section = 'admn';
var $form_class = 'bulkuserform';
static $contexts = array();
static function get_contexts($capability) {
if (!isset(bulkuserpage::$contexts[$capability])) {
global $USER;
bulkuserpage::$contexts[$capability] = get_contexts_by_capability_for_user('user', $capability, $USER->id);
}
return bulkuserpage::$contexts[$capability];
}
function can_do_default() {
//this allows for cluster role assignments to be taken into account
$contexts = bulkuserpage::get_contexts('local/elisprogram:user_edit');
return !$contexts->is_empty();
}
/**
* Determine whether the current user can delete users.
* @return bool Whether the user has permission or not
*/
public function can_do_delete() {
$contexts = bulkuserpage::get_contexts('local/elisprogram:user_delete');
return !$contexts->is_empty();
}
function get_title_default() {
return get_string('userbulk', 'admin');
}
function build_navbar_default($who = null) {
return $this->navbar->add(get_string('userbulk', 'admin'), $this->url);
}
/**
* Generate the bulk user form object.
* @return object Bulk user form object
*/
protected function get_selection_form() {
$customdata = array('inactive' => get_string('mark_inactive', 'local_elisprogram'));
if ($this->can_do_delete()) {
$customdata['delete'] = get_string('delete', 'local_elisprogram');
}
return new bulkuserform(null, $customdata);
}
function get_selection_filter() {
// filter
$filter = new bulk_user_filtering('index.php', array('s' => 'bulkuser'));
return $filter;
}
function get_records($filter) {
global $USER;
$sort = optional_param('sort', 'name', PARAM_ALPHA);
$dir = optional_param('dir', 'ASC', PARAM_ALPHA);
$pagenum = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 30, PARAM_INT);
if ($sort == 'name') {
$sort = 'lastname';
}
$extrasql = $filter->get_sql_filter();
//filter based on cluster role assignments
$context_set = pm_context_set::for_user_with_capability('cluster', 'local/elisprogram:user_edit', $USER->id);
// Get list of users
$items = usermanagement_get_users_recordset($sort, $dir, $perpage * $pagenum,
$perpage, $extrasql, $context_set);
$numitems = usermanagement_count_users($extrasql, $context_set);
return array($items, $numitems);
}
function print_selection_filter($filter) {
$filter->display_add();
$filter->display_active();
}
function get_records_from_selection($record_ids) {
$sort = $this->optional_param('sort', 'name', PARAM_ALPHA);
$dir = $this->optional_param('dir', 'ASC', PARAM_ALPHA);
$perpage = $this->optional_param('perpage', 30, PARAM_INT);
$page = $this->optional_param('page', 0, PARAM_INT);
$users = user::find(new in_list_filter('id', $record_ids), array($sort => $dir), $page * $perpage, $perpage);
return $users;
}
function create_selection_table($records, $baseurl) {
$baseurl = new moodle_url($baseurl);
return new bulkusertable($records, $baseurl);
}
function process_selection($data) {
global $DB, $OUTPUT;
if (empty($data->_selection)) {
print_error('no_items_selected', 'local_elisprogram', $this->get_basepage()->url);
} else {
$usersstring = implode(', ', array_map('fullname', $DB->get_records_select('local_elisprogram_usr', 'id in ('.implode(',',$data->_selection).')')));
$buttoncontinue = new single_button(
new moodle_url('index.php',
array('s' => $this->pagename,
'action' => $data->do,
'selectedusers' => implode(',',$data->_selection))),
get_string('yes'), 'POST');
$buttoncancel = new single_button(
new moodle_url('index.php',
array('s' => $this->pagename)),
get_string('no'), 'GET');
echo $OUTPUT->confirm(get_string('confirm_bulk_'. $data->do, 'local_elisprogram', $usersstring), $buttoncontinue, $buttoncancel);
}
}
function print_tabs() {
}
function do_inactive() { // action_inactive()
global $DB;
$users = explode(',',$this->required_param('selectedusers',PARAM_TEXT));
$this->session_selection_deletion();
// make sure everything is an int
foreach ($users as $key => $val) {
$users[$key] = (int)$val;
if (empty($users[$key])) {
unset($users[$key]);
}
}
$result = $DB->execute('UPDATE {'. user::TABLE .'} SET inactive = 1
WHERE id in ('. implode(',', $users) .')');
$tmppage = new bulkuserpage();
if ($result) {
redirect($tmppage->url, get_string('success_bulk_inactive', 'local_elisprogram'));
} else {
print_error('error_bulk_inactive', 'local_elisprogram', $tmppage->url);
}
}
function do_delete() { // action_delete()
require_once elispm::lib('data/user.class.php');
$this->session_selection_deletion();
$users = explode(',', $this->required_param('selectedusers', PARAM_TEXT));
// make sure everything is an int
foreach ($users as $key => $val) {
$users[$key] = (int)$val;
if (empty($users[$key])) {
unset($users[$key]);
}
}
foreach ($users as $userid) {
$userobj = new user($userid);
$userobj->delete(); // TBD: try {} catch () {} ???
}
$tmppage = new bulkuserpage();
redirect($tmppage->url, get_string('success_bulk_delete', 'local_elisprogram'));
}
}
/**
* Table to display the list of potential users.
*/
class bulkusertable extends selection_table {
private $display_date;
function __construct(&$items, $url) {
$columns = array(
'_selection' => array('header' => get_string('select'), 'sortable' => false),
'idnumber' => array('header' => get_string('id', 'local_elisprogram')),
'name' => array('header' => get_string('name', 'local_elisprogram')),
'country' => array('header' => get_string('country', 'local_elisprogram')),
'timecreated' => array('header' => get_string('registered_date', 'local_elisprogram'))
);
$sort = optional_param('sort', 'name', PARAM_ALPHA);
$dir = optional_param('dir', 'ASC', PARAM_ALPHA);
if ($dir !== 'DESC') {
$dir = 'ASC';
}
if (isset($columns[$sort])) {
$columns[$sort]['sortable'] = $dir;
} else {
$sort = 'name';
$columns[$sort]['sortable'] = $dir;
}
parent::__construct($items, $columns, $url);
$this->display_date = new display_date_item(get_string('pm_date_format', 'local_elisprogram'));
}
function get_item_display_timecreated($column, $item) {
return $this->display_date->display($column, $item);
}
function get_item_display_name($column, $item) {
if (isset($item->name)) {
return $item->name;
} else {
return display_table::display_user_fullname_item($column, $item);
}
}
}
/**
* User filter that adds a filter for users who do not have an associated
* Moodle user.
*/
class bulk_user_filtering extends pm_user_filtering {
function bulk_user_filtering($baseurl, $extraparams) {
$fieldnames = array(
'realname' => 0,
'lastname' => 1,
'firstname' => 1,
'idnumber' => 1,
'email' => 0,
'city' => 1,
'country' => 1,
'username' => 0,
'language' => 1,
'clusterid' => 1,
'curriculumid' => 1,
'inactive' => 1,
'nomoodleuser' => 1,
);
parent::pm_user_filtering($fieldnames, $baseurl, $extraparams);
}
function get_field($fieldname, $advanced) {
switch ($fieldname) {
case 'nomoodleuser':
return new no_moodle_user_filter('nomoodleuser', get_string('nomoodleuser_filt', 'local_elisprogram'), $advanced, 'idnumber');
default:
return parent::get_field($fieldname, $advanced);
}
}
}
/**
* Filter for showing users who don't have an associated Moodle user
*/
class no_moodle_user_filter extends user_filter_type {
var $_field;
/**
* Constructor
* @param string $name the name of the filter instance
* @param string $label the label of the filter instance
* @param boolean $advanced advanced form element flag
* @param string $field user table filed name
* @param array $options select options
*/
function no_moodle_user_filter($name, $label, $advanced, $field) {
parent::user_filter_type($name, $label, $advanced);
$this->_field = $field;
}
/**
* Adds controls specific to this filter in the form.
* @param object $mform a MoodleForm object to setup
*/
function setupForm(&$mform) {
$mform->addElement('checkbox', $this->_name, $this->_label);
// TODO: add help
//$mform->setHelpButton($this->_name, array('simpleselect', $this->_label, 'filters'));
if ($this->_advanced) {
$mform->setAdvanced($this->_name);
}
}
/**
* Retrieves data from the form data
* @param object $formdata data submited with the form
* @return mixed array filter data or false when filter not set
*/
function check_data($formdata) {
$field = $this->_name;
if (array_key_exists($field, $formdata)) {
if($formdata->$field != 0) {
return array('value' => (string)$formdata->$field);
}
}
return false;
}
/**
* Returns the condition to be used with SQL where
* @param array $data filter settings
* @return string the filtering condition or null if the filter is disabled
*/
function get_sql_filter($data) {
$sql = "(NOT EXISTS (SELECT _u.id FROM {user} _u WHERE _u.idnumber = {local_elisprogram_usr}.{$this->_field})
OR NOT EXISTS (SELECT _um.id FROM {local_elisprogram_usr_mdl} _um WHERE _um.cuserid = {local_elisprogram_usr}.id))";
return array($sql, array());
}
/**
* Returns a human friendly description of the filter used as label.
* @param array $data filter settings
* @return string active filter label
*/
function get_label($data) {
$retval = '';
if(!empty($data['value'])) {
$retval = get_string('nomoodleuser_filt', 'local_elisprogram');
}
return $retval;
}
}