MDL-40669 tool_uploaduser: allow emailstop to be set during upload.
[moodle.git] / admin / tool / uploaduser / user_form.php
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/>.
17 /**
18  * Bulk user upload forms
19  *
20  * @package    tool
21  * @subpackage uploaduser
22  * @copyright  2007 Dan Poltawski
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 require_once $CFG->libdir.'/formslib.php';
29 require_once($CFG->dirroot . '/user/editlib.php');
31 /**
32  * Upload a file CVS file with user information.
33  *
34  * @copyright  2007 Petr Skoda  {@link http://skodak.org}
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class admin_uploaduser_form1 extends moodleform {
38     function definition () {
39         $mform = $this->_form;
41         $mform->addElement('header', 'settingsheader', get_string('upload'));
43         $mform->addElement('filepicker', 'userfile', get_string('file'));
44         $mform->addRule('userfile', null, 'required');
46         $choices = csv_import_reader::get_delimiter_list();
47         $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices);
48         if (array_key_exists('cfg', $choices)) {
49             $mform->setDefault('delimiter_name', 'cfg');
50         } else if (get_string('listsep', 'langconfig') == ';') {
51             $mform->setDefault('delimiter_name', 'semicolon');
52         } else {
53             $mform->setDefault('delimiter_name', 'comma');
54         }
56         $choices = core_text::get_encodings();
57         $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices);
58         $mform->setDefault('encoding', 'UTF-8');
60         $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
61         $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices);
62         $mform->setType('previewrows', PARAM_INT);
64         $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser'));
65     }
66 }
69 /**
70  * Specify user upload details
71  *
72  * @copyright  2007 Petr Skoda  {@link http://skodak.org}
73  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
74  */
75 class admin_uploaduser_form2 extends moodleform {
76     function definition () {
77         global $CFG, $USER;
79         $mform   = $this->_form;
80         $columns = $this->_customdata['columns'];
81         $data    = $this->_customdata['data'];
83         // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
84         $templateuser = $USER;
86         // upload settings and file
87         $mform->addElement('header', 'settingsheader', get_string('settings'));
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);
95         $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
96         $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices);
97         $mform->setDefault('uupasswordnew', 1);
98         $mform->hideIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE);
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);
105         $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES);
106         $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW);
107         $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC);
109         $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update'));
110         $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices);
111         $mform->setDefault('uupasswordold', 0);
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);
117         $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'),
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);
126         $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser'));
127         $mform->setDefault('uuallowrenames', 0);
128         $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW);
129         $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC);
131         $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser'));
132         $mform->setDefault('uuallowdeletes', 0);
133         $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
134         $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
136         $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
137         $mform->setDefault('uuallowsuspends', 1);
138         $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
139         $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
141         if (!empty($CFG->allowaccountssameemail)) {
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);
149         $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
150         $mform->setDefault('uustandardusernames', 1);
152         $choices = array(UU_BULK_NONE    => get_string('no'),
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);
157         $mform->setDefault('uubulk', 0);
159         // roles selection
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'));
170             $choices = uu_allowed_roles(true);
172             $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
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             }
183             $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
184             if ($editteacherroles = get_archetype_roles('editingteacher')) {
185                 foreach ($editteacherroles as $role) {
186                     if (isset($choices[$role->id])) {
187                         $mform->setDefault('uulegacy2', $role->id);
188                         break;
189                     }
190                 }
191                 unset($editteacherroles);
192             }
194             $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
195             if ($teacherroles = get_archetype_roles('teacher')) {
196                 foreach ($teacherroles as $role) {
197                     if (isset($choices[$role->id])) {
198                         $mform->setDefault('uulegacy3', $role->id);
199                         break;
200                     }
201                 }
202                 unset($teacherroles);
203             }
204         }
206         // default values
207         $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
209         $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
210         $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
211         $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
212         $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
213         $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
214         $mform->setForceLtr('username');
216         $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
217         $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
218         $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
219         $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE);
220         $mform->setForceLtr('email');
222         // only enabled and known to work plugins
223         $choices = uu_supported_auths();
224         $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
225         $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
226         $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
227         $mform->setAdvanced('auth');
229         $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
230         $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
231         $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
232         $mform->addHelpButton('maildisplay', 'emaildisplay');
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');
239         $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
240         $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
241         $mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
242         $mform->setAdvanced('mailformat');
244         $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
245         $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
246         $mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
247         $mform->setAdvanced('maildigest');
249         $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
250         $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
251         $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
253         $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
254         $mform->setType('city', PARAM_TEXT);
255         if (empty($CFG->defaultcity)) {
256             $mform->setDefault('city', $templateuser->city);
257         } else {
258             $mform->setDefault('city', core_user::get_property_default('city'));
259         }
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);
264         if (empty($CFG->country)) {
265             $mform->setDefault('country', $templateuser->country);
266         } else {
267             $mform->setDefault('country', core_user::get_property_default('country'));
268         }
269         $mform->setAdvanced('country');
271         $choices = core_date::get_list_of_timezones($templateuser->timezone, true);
272         $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
273         $mform->setDefault('timezone', $templateuser->timezone);
274         $mform->setAdvanced('timezone');
276         $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
277         $mform->setDefault('lang', $templateuser->lang);
278         $mform->setAdvanced('lang');
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);
283         $mform->addHelpButton('description', 'userdescription');
284         $mform->setAdvanced('description');
286         $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
287         $mform->setType('url', PARAM_URL);
288         $mform->setAdvanced('url');
290         $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
291         $mform->setType('idnumber', PARAM_NOTAGS);
292         $mform->setForceLtr('idnumber');
294         $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
295         $mform->setType('institution', PARAM_TEXT);
296         $mform->setDefault('institution', $templateuser->institution);
298         $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
299         $mform->setType('department', PARAM_TEXT);
300         $mform->setDefault('department', $templateuser->department);
302         $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
303         $mform->setType('phone1', PARAM_NOTAGS);
304         $mform->setAdvanced('phone1');
305         $mform->setForceLtr('phone1');
307         $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
308         $mform->setType('phone2', PARAM_NOTAGS);
309         $mform->setAdvanced('phone2');
310         $mform->setForceLtr('phone2');
312         $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
313         $mform->setType('address', PARAM_TEXT);
314         $mform->setAdvanced('address');
316         // Next the profile defaults
317         profile_definition($mform);
319         // hidden fields
320         $mform->addElement('hidden', 'iid');
321         $mform->setType('iid', PARAM_INT);
323         $mform->addElement('hidden', 'previewrows');
324         $mform->setType('previewrows', PARAM_INT);
326         $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
328         $this->set_data($data);
329     }
331     /**
332      * Form tweaks that depend on current data.
333      */
334     function definition_after_data() {
335         $mform   = $this->_form;
336         $columns = $this->_customdata['columns'];
338         foreach ($columns as $column) {
339             if ($mform->elementExists($column)) {
340                 $mform->removeElement($column);
341             }
342         }
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         }
350     }
352     /**
353      * Server side validation.
354      */
355     function validation($data, $files) {
356         $errors = parent::validation($data, $files);
357         $columns = $this->_customdata['columns'];
358         $optype  = $data['uutype'];
359         $updatetype = $data['uuupdatetype'];
361         // detect if password column needed in file
362         if (!in_array('password', $columns)) {
363             switch ($optype) {
364                 case UU_USER_UPDATE:
365                     if (!empty($data['uupasswordold'])) {
366                         $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
367                     }
368                     break;
370                 case UU_USER_ADD_UPDATE:
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;
379                 case UU_USER_ADDNEW:
380                     if (empty($data['uupasswordnew'])) {
381                         $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
382                     }
383                     break;
384                 case UU_USER_ADDINC:
385                     if (empty($data['uupasswordnew'])) {
386                         $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
387                     }
388                     break;
389              }
390         }
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         }
398         // look for other required data
399         if ($optype != UU_USER_UPDATE) {
400             $requiredusernames = useredit_get_required_name_fields();
401             $missing = array();
402             foreach ($requiredusernames as $requiredusername) {
403                 if (!in_array($requiredusername, $columns)) {
404                     $missing[] = get_string('missingfield', 'error', $requiredusername);;
405                 }
406             }
407             if ($missing) {
408                 $errors['uutype'] = implode('<br />',  $missing);
409             }
410             if (!in_array('email', $columns) and empty($data['email'])) {
411                 $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
412             }
413         }
414         return $errors;
415     }
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();
425         if ($data !== null and isset($data->description)) {
426             $data->descriptionformat = $data->description['format'];
427             $data->description = $data->description['text'];
428         }
430         return $data;
431     }