forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploaduser_form.php
432 lines (359 loc) · 18.3 KB
/
uploaduser_form.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?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/>.
/**
* Bulk user upload forms
*
* @package core
* @subpackage admin
* @copyright 2007 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once $CFG->libdir.'/formslib.php';
/**
* Upload a file CVS file with user information.
*
* @package core
* @subpackage admin
* @copyright 2007 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_uploaduser_form1 extends moodleform {
function definition () {
$mform = $this->_form;
$mform->addElement('header', 'settingsheader', get_string('upload'));
$mform->addElement('filepicker', 'userfile', get_string('file'));
$mform->addRule('userfile', null, 'required');
$choices = csv_import_reader::get_delimiter_list();
$mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'admin'), $choices);
if (array_key_exists('cfg', $choices)) {
$mform->setDefault('delimiter_name', 'cfg');
} else if (get_string('listsep', 'langconfig') == ';') {
$mform->setDefault('delimiter_name', 'semicolon');
} else {
$mform->setDefault('delimiter_name', 'comma');
}
$textlib = textlib_get_instance();
$choices = $textlib->get_encodings();
$mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices);
$mform->setDefault('encoding', 'UTF-8');
$choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
$mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'admin'), $choices);
$mform->setType('previewrows', PARAM_INT);
$this->add_action_buttons(false, get_string('uploadusers', 'admin'));
}
}
/**
* Specify user upload details
*
* @package core
* @subpackage admin
* @copyright 2007 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class admin_uploaduser_form2 extends moodleform {
function definition () {
global $CFG, $USER;
$mform = $this->_form;
$columns = $this->_customdata['columns'];
$data = $this->_customdata['data'];
// I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
$templateuser = $USER;
// upload settings and file
$mform->addElement('header', 'settingsheader', get_string('settings'));
$choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'admin'),
UU_USER_ADDINC => get_string('uuoptype_addinc', 'admin'),
UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'admin'),
UU_USER_UPDATE => get_string('uuoptype_update', 'admin'));
$mform->addElement('select', 'uutype', get_string('uuoptype', 'admin'), $choices);
$choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
$mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices);
$mform->setDefault('uupasswordnew', 1);
$mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
$choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'admin'),
UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'admin'),
UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'admin'),
UU_UPDATE_MISSING => get_string('uuupdatemissing', 'admin'));
$mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices);
$mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
$mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
$mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
$choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update'));
$mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices);
$mform->setDefault('uupasswordold', 0);
$mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
$mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
$mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
$mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
$choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'admin'),
UU_PWRESET_NONE => get_string('none'),
UU_PWRESET_ALL => get_string('all'));
if (empty($CFG->passwordpolicy)) {
unset($choices[UU_PWRESET_WEAK]);
}
$mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
$mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin'));
$mform->setDefault('uuallowrenames', 0);
$mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
$mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
$mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin'));
$mform->setDefault('uuallowdeletes', 0);
$mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
$mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
$mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'admin'));
$mform->setDefault('uunoemailduplicates', 1);
$mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'admin'));
$mform->setDefault('uustandardusernames', 1);
$choices = array(UU_BULK_NONE => get_string('no'),
UU_BULK_NEW => get_string('uubulknew', 'admin'),
UU_BULK_UPDATED => get_string('uubulkupdated', 'admin'),
UU_BULK_ALL => get_string('uubulkall', 'admin'));
$mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices);
$mform->setDefault('uubulk', 0);
// roles selection
$showroles = false;
foreach ($columns as $column) {
if (preg_match('/^type\d+$/', $column)) {
$showroles = true;
break;
}
}
if ($showroles) {
$mform->addElement('header', 'rolesheader', get_string('roles'));
$choices = uu_allowed_roles(true);
$mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'admin'), $choices);
if ($studentroles = get_archetype_roles('student')) {
foreach ($studentroles as $role) {
if (isset($choices[$role->id])) {
$mform->setDefault('uulegacy1', $role->id);
break;
}
}
unset($studentroles);
}
$mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'admin'), $choices);
if ($editteacherroles = get_archetype_roles('editingteacher')) {
foreach ($editteacherroles as $role) {
if (isset($choices[$role->id])) {
$mform->setDefault('uulegacy2', $role->id);
break;
}
}
unset($editteacherroles);
}
$mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'admin'), $choices);
if ($teacherroles = get_archetype_roles('teacher')) {
foreach ($teacherroles as $role) {
if (isset($choices[$role->id])) {
$mform->setDefault('uulegacy3', $role->id);
break;
}
}
unset($teacherroles);
}
}
// default values
$mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin'));
$mform->addElement('text', 'username', get_string('uuusernametemplate', 'admin'), 'size="20"');
$mform->addRule('username', get_string('requiredtemplate', 'admin'), 'required', null, 'client');
$mform->disabledIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
$mform->disabledIf('username', 'uutype', 'eq', UU_USER_UPDATE);
$mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
$mform->disabledIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
$mform->disabledIf('email', 'uutype', 'eq', UU_USER_UPDATE);
// only enabled and known to work plugins
$choices = uu_supported_auths();
$mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
$mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
$mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
$mform->setAdvanced('auth');
$choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
$mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
$mform->setDefault('maildisplay', 2);
$choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
$mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
$mform->setDefault('mailformat', 1);
$mform->setAdvanced('mailformat');
$choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
$mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
$mform->setDefault('maildigest', 0);
$mform->setAdvanced('maildigest');
$choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
$mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
$mform->setDefault('autosubscribe', 1);
$editors = editors_get_enabled();
if (count($editors) > 1) {
$choices = array();
$choices['0'] = get_string('texteditor');
$choices['1'] = get_string('htmleditor');
$mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
$mform->setDefault('htmleditor', 1);
} else {
$mform->addElement('hidden', 'htmleditor');
$mform->setDefault('htmleditor', 1);
$mform->setType('htmleditor', PARAM_INT);
}
if (empty($CFG->enableajax)) {
$mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno'));
} else {
$choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes'));
$mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
$mform->setDefault('ajax', 1);
}
$mform->setAdvanced('ajax');
$mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
$mform->setType('city', PARAM_MULTILANG);
if (empty($CFG->defaultcity)) {
$mform->setDefault('city', $templateuser->city);
} else {
$mform->setDefault('city', $CFG->defaultcity);
}
$mform->addRule('city', get_string('required'), 'required');
$mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries());
if (empty($CFG->country)) {
$mform->setDefault('country', $templateuser->country);
} else {
$mform->setDefault('country', $CFG->country);
}
$mform->setAdvanced('country');
$choices = get_list_of_timezones();
$choices['99'] = get_string('serverlocaltime');
$mform->addElement('select', 'timezone', get_string('timezone'), $choices);
$mform->setDefault('timezone', $templateuser->timezone);
$mform->setAdvanced('timezone');
$mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
$mform->setDefault('lang', $templateuser->lang);
$mform->setAdvanced('lang');
$editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
$mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
$mform->setType('description', PARAM_CLEANHTML);
$mform->addHelpButton('description', 'userdescription');
$mform->setAdvanced('description');
$mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
$mform->setAdvanced('url');
$mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
$mform->setType('idnumber', PARAM_NOTAGS);
$mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
$mform->setType('institution', PARAM_MULTILANG);
$mform->setDefault('institution', $templateuser->institution);
$mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
$mform->setType('department', PARAM_MULTILANG);
$mform->setDefault('department', $templateuser->department);
$mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
$mform->setType('phone1', PARAM_NOTAGS);
$mform->setAdvanced('phone1');
$mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
$mform->setType('phone2', PARAM_NOTAGS);
$mform->setAdvanced('phone2');
$mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
$mform->setType('address', PARAM_MULTILANG);
$mform->setAdvanced('address');
// Next the profile defaults
profile_definition($mform);
// hidden fields
$mform->addElement('hidden', 'iid');
$mform->setType('iid', PARAM_INT);
$mform->addElement('hidden', 'previewrows');
$mform->setType('previewrows', PARAM_INT);
$this->add_action_buttons(true, get_string('uploadusers', 'admin'));
$this->set_data($data);
}
/**
* Form tweaks that depend on current data.
*/
function definition_after_data() {
$mform = $this->_form;
$columns = $this->_customdata['columns'];
foreach ($columns as $column) {
if ($mform->elementExists($column)) {
$mform->removeElement($column);
}
}
if (!in_array('password', $columns)) {
// password resetting makes sense only if password specified in csv file
if ($mform->elementExists('uuforcepasswordchange')) {
$mform->removeElement('uuforcepasswordchange');
}
}
}
/**
* Server side validation.
*/
function validation($data, $files) {
$errors = parent::validation($data, $files);
$columns = $this->_customdata['columns'];
$optype = $data['uutype'];
// detect if password column needed in file
if (!in_array('password', $columns)) {
switch ($optype) {
case UU_USER_UPDATE:
if (!empty($data['uupasswordold'])) {
$errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
}
break;
case UU_USER_ADD_UPDATE:
if (empty($data['uupasswordnew'])) {
$errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
}
if (!empty($data['uupasswordold'])) {
$errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
}
break;
case UU_USER_ADDNEW:
if (empty($data['uupasswordnew'])) {
$errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
}
break;
case UU_USER_ADDINC:
if (empty($data['uupasswordnew'])) {
$errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
}
break;
}
}
// look for other required data
if ($optype != UU_USER_UPDATE) {
if (!in_array('firstname', $columns)) {
$errors['uutype'] = get_string('missingfield', 'error', 'firstname');
}
if (!in_array('lastname', $columns)) {
if (isset($errors['uutype'])) {
$errors['uutype'] = '';
} else {
$errors['uutype'] = ' ';
}
$errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
}
if (!in_array('email', $columns) and empty($data['email'])) {
$errors['email'] = get_string('requiredtemplate', 'admin');
}
}
return $errors;
}
/**
* Used to reformat the data from the editor component
*
* @return stdClass
*/
function get_data() {
$data = parent::get_data();
if ($data !== null and isset($data->description)) {
$data->descriptionformat = $data->description['format'];
$data->description = $data->description['text'];
}
return $data;
}
}