MDL-40669 tool_uploaduser: allow emailstop to be set during upload.
[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);
223fc57d 98 $mform->hideIf('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 105 $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
223fc57d
DS
106 $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
107 $mform->hideIf('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);
223fc57d
DS
112 $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW);
113 $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC);
114 $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 0);
115 $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 3);
e4e38544 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);
223fc57d
DS
128 $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
129 $mform->hideIf('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);
223fc57d
DS
133 $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
134 $mform->hideIf('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);
223fc57d
DS
138 $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
139 $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
08157bfe 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');
223fc57d
DS
212 $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
213 $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
525ef9c8 214 $mform->setForceLtr('username');
8bdb31ed
PS
215
216 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
ad46ea0b 217 $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
223fc57d
DS
218 $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
219 $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE);
525ef9c8 220 $mform->setForceLtr('email');
066bfbfe 221
e4e38544 222 // only enabled and known to work plugins
8bdb31ed 223 $choices = uu_supported_auths();
e4e38544 224 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
ed22a01b 225 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
226de8b7 226 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
ed22a01b 227 $mform->setAdvanced('auth');
0a5dffcc 228
ed22a01b 229 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
066bfbfe 230 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
ac9768fc 231 $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
d2bba97d 232 $mform->addHelpButton('maildisplay', 'emaildisplay');
066bfbfe 233
c4e79fc6
PH
234 $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
235 $mform->addElement('select', 'emailstop', get_string('emailstop'), $choices);
236 $mform->setDefault('emailstop', core_user::get_property_default('emailstop'));
237 $mform->setAdvanced('emailstop');
238
ed22a01b 239 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
066bfbfe 240 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
ac9768fc 241 $mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
ed22a01b 242 $mform->setAdvanced('mailformat');
066bfbfe 243
f58e6d26 244 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
245 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
ac9768fc 246 $mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
f58e6d26 247 $mform->setAdvanced('maildigest');
248
22a77963 249 $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
066bfbfe 250 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
ac9768fc 251 $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
066bfbfe 252
a8fd33b0 253 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
071e68f9 254 $mform->setType('city', PARAM_TEXT);
9449d0c5
PS
255 if (empty($CFG->defaultcity)) {
256 $mform->setDefault('city', $templateuser->city);
257 } else {
ac9768fc 258 $mform->setDefault('city', core_user::get_property_default('city'));
9449d0c5 259 }
ed22a01b 260
ff539362
PS
261 $choices = get_string_manager()->get_list_of_countries();
262 $choices = array(''=>get_string('selectacountry').'...') + $choices;
263 $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
9449d0c5
PS
264 if (empty($CFG->country)) {
265 $mform->setDefault('country', $templateuser->country);
266 } else {
ac9768fc 267 $mform->setDefault('country', core_user::get_property_default('country'));
9449d0c5 268 }
ed22a01b 269 $mform->setAdvanced('country');
0a5dffcc 270
d6e7a63d 271 $choices = core_date::get_list_of_timezones($templateuser->timezone, true);
066bfbfe 272 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
273 $mform->setDefault('timezone', $templateuser->timezone);
ed22a01b 274 $mform->setAdvanced('timezone');
066bfbfe 275
1f96e907 276 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
066bfbfe 277 $mform->setDefault('lang', $templateuser->lang);
ed22a01b 278 $mform->setAdvanced('lang');
279
8bdc9cac
SH
280 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
281 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
282 $mform->setType('description', PARAM_CLEANHTML);
27d1a27c 283 $mform->addHelpButton('description', 'userdescription');
ed22a01b 284 $mform->setAdvanced('description');
066bfbfe 285
286 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
ad46ea0b 287 $mform->setType('url', PARAM_URL);
ed22a01b 288 $mform->setAdvanced('url');
289
a8fd33b0 290 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
93de0ac2 291 $mform->setType('idnumber', PARAM_NOTAGS);
525ef9c8 292 $mform->setForceLtr('idnumber');
066bfbfe 293
a8fd33b0 294 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
071e68f9 295 $mform->setType('institution', PARAM_TEXT);
066bfbfe 296 $mform->setDefault('institution', $templateuser->institution);
297
a8fd33b0 298 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
071e68f9 299 $mform->setType('department', PARAM_TEXT);
066bfbfe 300 $mform->setDefault('department', $templateuser->department);
301
70fb46c8 302 $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
93de0ac2 303 $mform->setType('phone1', PARAM_NOTAGS);
ed22a01b 304 $mform->setAdvanced('phone1');
525ef9c8 305 $mform->setForceLtr('phone1');
066bfbfe 306
55ac3d6f 307 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
93de0ac2 308 $mform->setType('phone2', PARAM_NOTAGS);
ed22a01b 309 $mform->setAdvanced('phone2');
525ef9c8 310 $mform->setForceLtr('phone2');
066bfbfe 311
a8fd33b0 312 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
071e68f9 313 $mform->setType('address', PARAM_TEXT);
ed22a01b 314 $mform->setAdvanced('address');
066bfbfe 315
df997f84 316 // Next the profile defaults
e4e38544 317 profile_definition($mform);
df7ecfe4 318
df997f84 319 // hidden fields
e4e38544 320 $mform->addElement('hidden', 'iid');
321 $mform->setType('iid', PARAM_INT);
df7ecfe4 322
323 $mform->addElement('hidden', 'previewrows');
e4e38544 324 $mform->setType('previewrows', PARAM_INT);
325
ce15d56d 326 $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
8bdb31ed
PS
327
328 $this->set_data($data);
0a5dffcc 329 }
330
e4e38544 331 /**
332 * Form tweaks that depend on current data.
333 */
df7ecfe4 334 function definition_after_data() {
8bdb31ed
PS
335 $mform = $this->_form;
336 $columns = $this->_customdata['columns'];
e4e38544 337
338 foreach ($columns as $column) {
339 if ($mform->elementExists($column)) {
340 $mform->removeElement($column);
341 }
342 }
8bdb31ed
PS
343
344 if (!in_array('password', $columns)) {
345 // password resetting makes sense only if password specified in csv file
346 if ($mform->elementExists('uuforcepasswordchange')) {
347 $mform->removeElement('uuforcepasswordchange');
348 }
349 }
e4e38544 350 }
351
352 /**
353 * Server side validation.
354 */
a78890d5 355 function validation($data, $files) {
356 $errors = parent::validation($data, $files);
8bdb31ed 357 $columns = $this->_customdata['columns'];
e4e38544 358 $optype = $data['uutype'];
f85800f3 359 $updatetype = $data['uuupdatetype'];
e4e38544 360
361 // detect if password column needed in file
362 if (!in_array('password', $columns)) {
363 switch ($optype) {
8bdb31ed 364 case UU_USER_UPDATE:
e4e38544 365 if (!empty($data['uupasswordold'])) {
366 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
367 }
368 break;
369
8bdb31ed 370 case UU_USER_ADD_UPDATE:
e4e38544 371 if (empty($data['uupasswordnew'])) {
372 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
373 }
374 if (!empty($data['uupasswordold'])) {
375 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
376 }
377 break;
378
8bdb31ed 379 case UU_USER_ADDNEW:
3f12c146
RW
380 if (empty($data['uupasswordnew'])) {
381 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
382 }
383 break;
8bdb31ed 384 case UU_USER_ADDINC:
e4e38544 385 if (empty($data['uupasswordnew'])) {
386 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
387 }
388 break;
389 }
390 }
df7ecfe4 391
f85800f3
MN
392 // If the 'Existing user details' value is set we need to ensure that the
393 // 'Upload type' is not set to something invalid.
394 if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) {
395 $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser');
396 }
397
e4e38544 398 // look for other required data
8bdb31ed 399 if ($optype != UU_USER_UPDATE) {
6f4ece9f 400 $requiredusernames = useredit_get_required_name_fields();
c3f0f2fd 401 $missing = array();
6f4ece9f
AG
402 foreach ($requiredusernames as $requiredusername) {
403 if (!in_array($requiredusername, $columns)) {
c3f0f2fd 404 $missing[] = get_string('missingfield', 'error', $requiredusername);;
df7ecfe4 405 }
406 }
c3f0f2fd
PS
407 if ($missing) {
408 $errors['uutype'] = implode('<br />', $missing);
409 }
e4e38544 410 if (!in_array('email', $columns) and empty($data['email'])) {
ce15d56d 411 $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
e4e38544 412 }
e4e38544 413 }
a78890d5 414 return $errors;
df7ecfe4 415 }
8bdc9cac
SH
416
417 /**
418 * Used to reformat the data from the editor component
419 *
420 * @return stdClass
421 */
422 function get_data() {
423 $data = parent::get_data();
424
8bdb31ed 425 if ($data !== null and isset($data->description)) {
8bdc9cac
SH
426 $data->descriptionformat = $data->description['format'];
427 $data->description = $data->description['text'];
428 }
429
430 return $data;
431 }
0a5dffcc 432}