MDL-53139 admin: case diff issue with email
[moodle.git] / admin / tool / uploaduser / user_form.php
CommitLineData
20207b82 1<?php
8bdb31ed
PS
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 upload forms
19 *
ce15d56d
PS
20 * @package tool
21 * @subpackage uploaduser
8bdb31ed
PS
22 * @copyright 2007 Dan Poltawski
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
bfebaf64 27
0a5dffcc 28require_once $CFG->libdir.'/formslib.php';
6f4ece9f 29require_once($CFG->dirroot . '/user/editlib.php');
ed22a01b 30
8bdb31ed
PS
31/**
32 * Upload a file CVS file with user information.
33 *
8bdb31ed
PS
34 * @copyright 2007 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class admin_uploaduser_form1 extends moodleform {
38 function definition () {
39 $mform = $this->_form;
0a5dffcc 40
df7ecfe4 41 $mform->addElement('header', 'settingsheader', get_string('upload'));
ed22a01b 42
1eda38ee 43 $mform->addElement('filepicker', 'userfile', get_string('file'));
0a5dffcc 44 $mform->addRule('userfile', null, 'required');
45
e4e38544 46 $choices = csv_import_reader::get_delimiter_list();
ce15d56d 47 $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices);
df7ecfe4 48 if (array_key_exists('cfg', $choices)) {
e4e38544 49 $mform->setDefault('delimiter_name', 'cfg');
3c7e56c4 50 } else if (get_string('listsep', 'langconfig') == ';') {
e4e38544 51 $mform->setDefault('delimiter_name', 'semicolon');
df7ecfe4 52 } else {
e4e38544 53 $mform->setDefault('delimiter_name', 'comma');
df7ecfe4 54 }
55
2f1e464a 56 $choices = core_text::get_encodings();
ce15d56d 57 $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices);
ed22a01b 58 $mform->setDefault('encoding', 'UTF-8');
59
df7ecfe4 60 $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
ce15d56d 61 $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices);
df7ecfe4 62 $mform->setType('previewrows', PARAM_INT);
63
ce15d56d 64 $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser'));
df7ecfe4 65 }
66}
67
8bdb31ed
PS
68
69/**
70 * Specify user upload details
71 *
8bdb31ed
PS
72 * @copyright 2007 Petr Skoda {@link http://skodak.org}
73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
74 */
df7ecfe4 75class admin_uploaduser_form2 extends moodleform {
8bdb31ed 76 function definition () {
df7ecfe4 77 global $CFG, $USER;
78
8bdb31ed
PS
79 $mform = $this->_form;
80 $columns = $this->_customdata['columns'];
81 $data = $this->_customdata['data'];
df7ecfe4 82
e4e38544 83 // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
df7ecfe4 84 $templateuser = $USER;
85
df997f84 86 // upload settings and file
df7ecfe4 87 $mform->addElement('header', 'settingsheader', get_string('settings'));
8bdb31ed 88
ce15d56d
PS
89 $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'),
90 UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'),
91 UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'),
92 UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser'));
93 $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices);
ed22a01b 94
e4e38544 95 $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
ce15d56d 96 $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices);
3f12c146 97 $mform->setDefault('uupasswordnew', 1);
8bdb31ed 98 $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
e4e38544 99
ce15d56d
PS
100 $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'),
101 UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'),
102 UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'),
103 UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser'));
104 $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices);
8bdb31ed
PS
105 $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
106 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
107 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
e4e38544 108
ce15d56d
PS
109 $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update'));
110 $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices);
0c4807ab 111 $mform->setDefault('uupasswordold', 0);
8bdb31ed
PS
112 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
113 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
e4e38544 114 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
115 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
116
ce15d56d 117 $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'),
8bdb31ed
PS
118 UU_PWRESET_NONE => get_string('none'),
119 UU_PWRESET_ALL => get_string('all'));
120 if (empty($CFG->passwordpolicy)) {
121 unset($choices[UU_PWRESET_WEAK]);
122 }
123 $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
124
125
ce15d56d 126 $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
0c4807ab 127 $mform->setDefault('uuallowrenames', 0);
8bdb31ed
PS
128 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
129 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
e4e38544 130
ce15d56d 131 $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser'));
0c4807ab 132 $mform->setDefault('uuallowdeletes', 0);
8bdb31ed
PS
133 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
134 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
e4e38544 135
08157bfe
PS
136 $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
137 $mform->setDefault('uuallowsuspends', 1);
138 $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
139 $mform->disabledIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
140
95020832 141 if (!empty($CFG->allowaccountssameemail)) {
bc983724
MA
142 $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser'));
143 $mform->setDefault('uunoemailduplicates', 1);
144 } else {
145 $mform->addElement('hidden', 'uunoemailduplicates', 1);
146 }
147 $mform->setType('uunoemailduplicates', PARAM_BOOL);
0c4807ab 148
ce15d56d 149 $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
8bdb31ed
PS
150 $mform->setDefault('uustandardusernames', 1);
151
152 $choices = array(UU_BULK_NONE => get_string('no'),
ce15d56d
PS
153 UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'),
154 UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'),
155 UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser'));
156 $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices);
0c4807ab 157 $mform->setDefault('uubulk', 0);
e4e38544 158
df997f84 159 // roles selection
e4e38544 160 $showroles = false;
161 foreach ($columns as $column) {
162 if (preg_match('/^type\d+$/', $column)) {
163 $showroles = true;
164 break;
165 }
166 }
167 if ($showroles) {
168 $mform->addElement('header', 'rolesheader', get_string('roles'));
169
170 $choices = uu_allowed_roles(true);
171
ce15d56d 172 $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
df997f84
PS
173 if ($studentroles = get_archetype_roles('student')) {
174 foreach ($studentroles as $role) {
175 if (isset($choices[$role->id])) {
176 $mform->setDefault('uulegacy1', $role->id);
177 break;
178 }
179 }
180 unset($studentroles);
181 }
e4e38544 182
ce15d56d 183 $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
4f0c2d00 184 if ($editteacherroles = get_archetype_roles('editingteacher')) {
df997f84
PS
185 foreach ($editteacherroles as $role) {
186 if (isset($choices[$role->id])) {
187 $mform->setDefault('uulegacy2', $role->id);
188 break;
189 }
190 }
e4e38544 191 unset($editteacherroles);
e4e38544 192 }
ed22a01b 193
ce15d56d 194 $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
4f0c2d00 195 if ($teacherroles = get_archetype_roles('teacher')) {
df997f84
PS
196 foreach ($teacherroles as $role) {
197 if (isset($choices[$role->id])) {
198 $mform->setDefault('uulegacy3', $role->id);
199 break;
200 }
201 }
e4e38544 202 unset($teacherroles);
e4e38544 203 }
204 }
ed22a01b 205
df997f84 206 // default values
ce15d56d 207 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
e4e38544 208
ce15d56d 209 $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
ad46ea0b 210 $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
ce15d56d 211 $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
8bdb31ed
PS
212 $mform->disabledIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
213 $mform->disabledIf('username', 'uutype', 'eq', UU_USER_UPDATE);
214
215 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
ad46ea0b 216 $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
8bdb31ed
PS
217 $mform->disabledIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
218 $mform->disabledIf('email', 'uutype', 'eq', UU_USER_UPDATE);
066bfbfe 219
e4e38544 220 // only enabled and known to work plugins
8bdb31ed 221 $choices = uu_supported_auths();
e4e38544 222 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
ed22a01b 223 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
226de8b7 224 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ed22a01b 225 $mform->setAdvanced('auth');
0a5dffcc 226
ed22a01b 227 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
066bfbfe 228 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
5e9cbe0e 229 $mform->setDefault('maildisplay', $CFG->defaultpreference_maildisplay);
066bfbfe 230
ed22a01b 231 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
066bfbfe 232 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
5e9cbe0e 233 $mform->setDefault('mailformat', $CFG->defaultpreference_mailformat);
ed22a01b 234 $mform->setAdvanced('mailformat');
066bfbfe 235
f58e6d26 236 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
237 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
5e9cbe0e 238 $mform->setDefault('maildigest', $CFG->defaultpreference_maildigest);
f58e6d26 239 $mform->setAdvanced('maildigest');
240
22a77963 241 $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
066bfbfe 242 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
5e9cbe0e 243 $mform->setDefault('autosubscribe', $CFG->defaultpreference_autosubscribe);
066bfbfe 244
a8fd33b0 245 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
071e68f9 246 $mform->setType('city', PARAM_TEXT);
9449d0c5
PS
247 if (empty($CFG->defaultcity)) {
248 $mform->setDefault('city', $templateuser->city);
249 } else {
250 $mform->setDefault('city', $CFG->defaultcity);
251 }
ed22a01b 252
ff539362
PS
253 $choices = get_string_manager()->get_list_of_countries();
254 $choices = array(''=>get_string('selectacountry').'...') + $choices;
255 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
9449d0c5
PS
256 if (empty($CFG->country)) {
257 $mform->setDefault('country', $templateuser->country);
258 } else {
259 $mform->setDefault('country', $CFG->country);
260 }
ed22a01b 261 $mform->setAdvanced('country');
0a5dffcc 262
d6e7a63d 263 $choices = core_date::get_list_of_timezones($templateuser->timezone, true);
066bfbfe 264 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
265 $mform->setDefault('timezone', $templateuser->timezone);
ed22a01b 266 $mform->setAdvanced('timezone');
066bfbfe 267
1f96e907 268 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
066bfbfe 269 $mform->setDefault('lang', $templateuser->lang);
ed22a01b 270 $mform->setAdvanced('lang');
271
8bdc9cac
SH
272 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
273 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
274 $mform->setType('description', PARAM_CLEANHTML);
27d1a27c 275 $mform->addHelpButton('description', 'userdescription');
ed22a01b 276 $mform->setAdvanced('description');
066bfbfe 277
278 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
ad46ea0b 279 $mform->setType('url', PARAM_URL);
ed22a01b 280 $mform->setAdvanced('url');
281
a8fd33b0 282 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 283 $mform->setType('idnumber', PARAM_NOTAGS);
066bfbfe 284
a8fd33b0 285 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
071e68f9 286 $mform->setType('institution', PARAM_TEXT);
066bfbfe 287 $mform->setDefault('institution', $templateuser->institution);
288
a8fd33b0 289 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
071e68f9 290 $mform->setType('department', PARAM_TEXT);
066bfbfe 291 $mform->setDefault('department', $templateuser->department);
292
70fb46c8 293 $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
93de0ac2 294 $mform->setType('phone1', PARAM_NOTAGS);
ed22a01b 295 $mform->setAdvanced('phone1');
066bfbfe 296
55ac3d6f 297 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 298 $mform->setType('phone2', PARAM_NOTAGS);
ed22a01b 299 $mform->setAdvanced('phone2');
066bfbfe 300
a8fd33b0 301 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
071e68f9 302 $mform->setType('address', PARAM_TEXT);
ed22a01b 303 $mform->setAdvanced('address');
066bfbfe 304
df997f84 305 // Next the profile defaults
e4e38544 306 profile_definition($mform);
df7ecfe4 307
df997f84 308 // hidden fields
e4e38544 309 $mform->addElement('hidden', 'iid');
310 $mform->setType('iid', PARAM_INT);
df7ecfe4 311
312 $mform->addElement('hidden', 'previewrows');
e4e38544 313 $mform->setType('previewrows', PARAM_INT);
314
ce15d56d 315 $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
8bdb31ed
PS
316
317 $this->set_data($data);
0a5dffcc 318 }
319
e4e38544 320 /**
321 * Form tweaks that depend on current data.
322 */
df7ecfe4 323 function definition_after_data() {
8bdb31ed
PS
324 $mform = $this->_form;
325 $columns = $this->_customdata['columns'];
e4e38544 326
327 foreach ($columns as $column) {
328 if ($mform->elementExists($column)) {
329 $mform->removeElement($column);
330 }
331 }
8bdb31ed
PS
332
333 if (!in_array('password', $columns)) {
334 // password resetting makes sense only if password specified in csv file
335 if ($mform->elementExists('uuforcepasswordchange')) {
336 $mform->removeElement('uuforcepasswordchange');
337 }
338 }
e4e38544 339 }
340
341 /**
342 * Server side validation.
343 */
a78890d5 344 function validation($data, $files) {
345 $errors = parent::validation($data, $files);
8bdb31ed 346 $columns = $this->_customdata['columns'];
e4e38544 347 $optype = $data['uutype'];
348
349 // detect if password column needed in file
350 if (!in_array('password', $columns)) {
351 switch ($optype) {
8bdb31ed 352 case UU_USER_UPDATE:
e4e38544 353 if (!empty($data['uupasswordold'])) {
354 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
355 }
356 break;
357
8bdb31ed 358 case UU_USER_ADD_UPDATE:
e4e38544 359 if (empty($data['uupasswordnew'])) {
360 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
361 }
362 if (!empty($data['uupasswordold'])) {
363 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
364 }
365 break;
366
8bdb31ed 367 case UU_USER_ADDNEW:
3f12c146
RW
368 if (empty($data['uupasswordnew'])) {
369 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
370 }
371 break;
8bdb31ed 372 case UU_USER_ADDINC:
e4e38544 373 if (empty($data['uupasswordnew'])) {
374 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
375 }
376 break;
377 }
378 }
df7ecfe4 379
e4e38544 380 // look for other required data
8bdb31ed 381 if ($optype != UU_USER_UPDATE) {
6f4ece9f 382 $requiredusernames = useredit_get_required_name_fields();
c3f0f2fd 383 $missing = array();
6f4ece9f
AG
384 foreach ($requiredusernames as $requiredusername) {
385 if (!in_array($requiredusername, $columns)) {
c3f0f2fd 386 $missing[] = get_string('missingfield', 'error', $requiredusername);;
df7ecfe4 387 }
388 }
c3f0f2fd
PS
389 if ($missing) {
390 $errors['uutype'] = implode('<br />', $missing);
391 }
e4e38544 392 if (!in_array('email', $columns) and empty($data['email'])) {
ce15d56d 393 $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
e4e38544 394 }
e4e38544 395 }
a78890d5 396 return $errors;
df7ecfe4 397 }
8bdc9cac
SH
398
399 /**
400 * Used to reformat the data from the editor component
401 *
402 * @return stdClass
403 */
404 function get_data() {
405 $data = parent::get_data();
406
8bdb31ed 407 if ($data !== null and isset($data->description)) {
8bdc9cac
SH
408 $data->descriptionformat = $data->description['format'];
409 $data->description = $data->description['text'];
410 }
411
412 return $data;
413 }
0a5dffcc 414}