2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Bulk user upload forms
22 * @copyright 2007 Dan Poltawski
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 require_once $CFG->libdir
.'/formslib.php';
32 * Upload a file CVS file with user information.
36 * @copyright 2007 Petr Skoda {@link http://skodak.org}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class admin_uploaduser_form1
extends moodleform
{
40 function definition () {
41 $mform = $this->_form
;
43 $mform->addElement('header', 'settingsheader', get_string('upload'));
45 $mform->addElement('filepicker', 'userfile', get_string('file'));
46 $mform->addRule('userfile', null, 'required');
48 $choices = csv_import_reader
::get_delimiter_list();
49 $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'admin'), $choices);
50 if (array_key_exists('cfg', $choices)) {
51 $mform->setDefault('delimiter_name', 'cfg');
52 } else if (get_string('listsep', 'langconfig') == ';') {
53 $mform->setDefault('delimiter_name', 'semicolon');
55 $mform->setDefault('delimiter_name', 'comma');
58 $textlib = textlib_get_instance();
59 $choices = $textlib->get_encodings();
60 $mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices);
61 $mform->setDefault('encoding', 'UTF-8');
63 $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
64 $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'admin'), $choices);
65 $mform->setType('previewrows', PARAM_INT
);
67 $this->add_action_buttons(false, get_string('uploadusers', 'admin'));
73 * Specify user upload details
77 * @copyright 2007 Petr Skoda {@link http://skodak.org}
78 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
80 class admin_uploaduser_form2
extends moodleform
{
81 function definition () {
84 $mform = $this->_form
;
85 $columns = $this->_customdata
['columns'];
86 $data = $this->_customdata
['data'];
88 // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
89 $templateuser = $USER;
91 // upload settings and file
92 $mform->addElement('header', 'settingsheader', get_string('settings'));
94 $choices = array(UU_USER_ADDNEW
=> get_string('uuoptype_addnew', 'admin'),
95 UU_USER_ADDINC
=> get_string('uuoptype_addinc', 'admin'),
96 UU_USER_ADD_UPDATE
=> get_string('uuoptype_addupdate', 'admin'),
97 UU_USER_UPDATE
=> get_string('uuoptype_update', 'admin'));
98 $mform->addElement('select', 'uutype', get_string('uuoptype', 'admin'), $choices);
100 $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
101 $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices);
102 $mform->setDefault('uupasswordnew', 1);
103 $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE
);
105 $choices = array(UU_UPDATE_NOCHANGES
=> get_string('nochanges', 'admin'),
106 UU_UPDATE_FILEOVERRIDE
=> get_string('uuupdatefromfile', 'admin'),
107 UU_UPDATE_ALLOVERRIDE
=> get_string('uuupdateall', 'admin'),
108 UU_UPDATE_MISSING
=> get_string('uuupdatemissing', 'admin'));
109 $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices);
110 $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES
);
111 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW
);
112 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC
);
114 $choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update'));
115 $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices);
116 $mform->setDefault('uupasswordold', 0);
117 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW
);
118 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC
);
119 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
120 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
122 $choices = array(UU_PWRESET_WEAK
=> get_string('usersweakpassword', 'admin'),
123 UU_PWRESET_NONE
=> get_string('none'),
124 UU_PWRESET_ALL
=> get_string('all'));
125 if (empty($CFG->passwordpolicy
)) {
126 unset($choices[UU_PWRESET_WEAK
]);
128 $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices);
131 $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin'));
132 $mform->setDefault('uuallowrenames', 0);
133 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW
);
134 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC
);
136 $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin'));
137 $mform->setDefault('uuallowdeletes', 0);
138 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW
);
139 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC
);
141 $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'admin'));
142 $mform->setDefault('uunoemailduplicates', 1);
144 $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'admin'));
145 $mform->setDefault('uustandardusernames', 1);
147 $choices = array(UU_BULK_NONE
=> get_string('no'),
148 UU_BULK_NEW
=> get_string('uubulknew', 'admin'),
149 UU_BULK_UPDATED
=> get_string('uubulkupdated', 'admin'),
150 UU_BULK_ALL
=> get_string('uubulkall', 'admin'));
151 $mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices);
152 $mform->setDefault('uubulk', 0);
156 foreach ($columns as $column) {
157 if (preg_match('/^type\d+$/', $column)) {
163 $mform->addElement('header', 'rolesheader', get_string('roles'));
165 $choices = uu_allowed_roles(true);
167 $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'admin'), $choices);
168 if ($studentroles = get_archetype_roles('student')) {
169 foreach ($studentroles as $role) {
170 if (isset($choices[$role->id
])) {
171 $mform->setDefault('uulegacy1', $role->id
);
175 unset($studentroles);
178 $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'admin'), $choices);
179 if ($editteacherroles = get_archetype_roles('editingteacher')) {
180 foreach ($editteacherroles as $role) {
181 if (isset($choices[$role->id
])) {
182 $mform->setDefault('uulegacy2', $role->id
);
186 unset($editteacherroles);
189 $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'admin'), $choices);
190 if ($teacherroles = get_archetype_roles('teacher')) {
191 foreach ($teacherroles as $role) {
192 if (isset($choices[$role->id
])) {
193 $mform->setDefault('uulegacy3', $role->id
);
197 unset($teacherroles);
202 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin'));
204 $mform->addElement('text', 'username', get_string('uuusernametemplate', 'admin'), 'size="20"');
205 $mform->addRule('username', get_string('requiredtemplate', 'admin'), 'required', null, 'client');
206 $mform->disabledIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE
);
207 $mform->disabledIf('username', 'uutype', 'eq', UU_USER_UPDATE
);
209 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
210 $mform->disabledIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE
);
211 $mform->disabledIf('email', 'uutype', 'eq', UU_USER_UPDATE
);
213 // only enabled and known to work plugins
214 $choices = uu_supported_auths();
215 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
216 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
217 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
218 $mform->setAdvanced('auth');
220 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
221 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
222 $mform->setDefault('maildisplay', 2);
224 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
225 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
226 $mform->setDefault('mailformat', 1);
227 $mform->setAdvanced('mailformat');
229 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
230 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
231 $mform->setDefault('maildigest', 0);
232 $mform->setAdvanced('maildigest');
234 $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
235 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
236 $mform->setDefault('autosubscribe', 1);
238 $editors = editors_get_enabled();
239 if (count($editors) > 1) {
241 $choices['0'] = get_string('texteditor');
242 $choices['1'] = get_string('htmleditor');
243 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
244 $mform->setDefault('htmleditor', 1);
246 $mform->addElement('hidden', 'htmleditor');
247 $mform->setDefault('htmleditor', 1);
248 $mform->setType('htmleditor', PARAM_INT
);
251 if (empty($CFG->enableajax
)) {
252 $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno'));
254 $choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes'));
255 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
256 $mform->setDefault('ajax', 1);
258 $mform->setAdvanced('ajax');
260 $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
261 $mform->setType('city', PARAM_MULTILANG
);
262 if (empty($CFG->defaultcity
)) {
263 $mform->setDefault('city', $templateuser->city
);
265 $mform->setDefault('city', $CFG->defaultcity
);
267 $mform->addRule('city', get_string('required'), 'required');
269 $mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries());
270 if (empty($CFG->country
)) {
271 $mform->setDefault('country', $templateuser->country
);
273 $mform->setDefault('country', $CFG->country
);
275 $mform->setAdvanced('country');
277 $choices = get_list_of_timezones();
278 $choices['99'] = get_string('serverlocaltime');
279 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
280 $mform->setDefault('timezone', $templateuser->timezone
);
281 $mform->setAdvanced('timezone');
283 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
284 $mform->setDefault('lang', $templateuser->lang
);
285 $mform->setAdvanced('lang');
287 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
288 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
289 $mform->setType('description', PARAM_CLEANHTML
);
290 $mform->addHelpButton('description', 'userdescription');
291 $mform->setAdvanced('description');
293 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
294 $mform->setAdvanced('url');
296 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
297 $mform->setType('idnumber', PARAM_NOTAGS
);
299 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
300 $mform->setType('institution', PARAM_MULTILANG
);
301 $mform->setDefault('institution', $templateuser->institution
);
303 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
304 $mform->setType('department', PARAM_MULTILANG
);
305 $mform->setDefault('department', $templateuser->department
);
307 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
308 $mform->setType('phone1', PARAM_NOTAGS
);
309 $mform->setAdvanced('phone1');
311 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
312 $mform->setType('phone2', PARAM_NOTAGS
);
313 $mform->setAdvanced('phone2');
315 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
316 $mform->setType('address', PARAM_MULTILANG
);
317 $mform->setAdvanced('address');
319 // Next the profile defaults
320 profile_definition($mform);
323 $mform->addElement('hidden', 'iid');
324 $mform->setType('iid', PARAM_INT
);
326 $mform->addElement('hidden', 'previewrows');
327 $mform->setType('previewrows', PARAM_INT
);
329 $this->add_action_buttons(true, get_string('uploadusers', 'admin'));
331 $this->set_data($data);
335 * Form tweaks that depend on current data.
337 function definition_after_data() {
338 $mform = $this->_form
;
339 $columns = $this->_customdata
['columns'];
341 foreach ($columns as $column) {
342 if ($mform->elementExists($column)) {
343 $mform->removeElement($column);
347 if (!in_array('password', $columns)) {
348 // password resetting makes sense only if password specified in csv file
349 if ($mform->elementExists('uuforcepasswordchange')) {
350 $mform->removeElement('uuforcepasswordchange');
356 * Server side validation.
358 function validation($data, $files) {
359 $errors = parent
::validation($data, $files);
360 $columns = $this->_customdata
['columns'];
361 $optype = $data['uutype'];
363 // detect if password column needed in file
364 if (!in_array('password', $columns)) {
367 if (!empty($data['uupasswordold'])) {
368 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
372 case UU_USER_ADD_UPDATE
:
373 if (empty($data['uupasswordnew'])) {
374 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
376 if (!empty($data['uupasswordold'])) {
377 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
382 if (empty($data['uupasswordnew'])) {
383 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
387 if (empty($data['uupasswordnew'])) {
388 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
394 // look for other required data
395 if ($optype != UU_USER_UPDATE
) {
396 if (!in_array('firstname', $columns)) {
397 $errors['uutype'] = get_string('missingfield', 'error', 'firstname');
400 if (!in_array('lastname', $columns)) {
401 if (isset($errors['uutype'])) {
402 $errors['uutype'] = '';
404 $errors['uutype'] = ' ';
406 $errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
409 if (!in_array('email', $columns) and empty($data['email'])) {
410 $errors['email'] = get_string('requiredtemplate', 'admin');
418 * Used to reformat the data from the editor component
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'];