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
);
268 $mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries());
269 if (empty($CFG->country
)) {
270 $mform->setDefault('country', $templateuser->country
);
272 $mform->setDefault('country', $CFG->country
);
274 $mform->setAdvanced('country');
276 $choices = get_list_of_timezones();
277 $choices['99'] = get_string('serverlocaltime');
278 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
279 $mform->setDefault('timezone', $templateuser->timezone
);
280 $mform->setAdvanced('timezone');
282 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
283 $mform->setDefault('lang', $templateuser->lang
);
284 $mform->setAdvanced('lang');
286 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
287 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
288 $mform->setType('description', PARAM_CLEANHTML
);
289 $mform->addHelpButton('description', 'userdescription');
290 $mform->setAdvanced('description');
292 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
293 $mform->setAdvanced('url');
295 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
296 $mform->setType('idnumber', PARAM_NOTAGS
);
298 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
299 $mform->setType('institution', PARAM_MULTILANG
);
300 $mform->setDefault('institution', $templateuser->institution
);
302 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
303 $mform->setType('department', PARAM_MULTILANG
);
304 $mform->setDefault('department', $templateuser->department
);
306 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
307 $mform->setType('phone1', PARAM_NOTAGS
);
308 $mform->setAdvanced('phone1');
310 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
311 $mform->setType('phone2', PARAM_NOTAGS
);
312 $mform->setAdvanced('phone2');
314 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
315 $mform->setType('address', PARAM_MULTILANG
);
316 $mform->setAdvanced('address');
318 // Next the profile defaults
319 profile_definition($mform);
322 $mform->addElement('hidden', 'iid');
323 $mform->setType('iid', PARAM_INT
);
325 $mform->addElement('hidden', 'previewrows');
326 $mform->setType('previewrows', PARAM_INT
);
328 $this->add_action_buttons(true, get_string('uploadusers', 'admin'));
330 $this->set_data($data);
334 * Form tweaks that depend on current data.
336 function definition_after_data() {
337 $mform = $this->_form
;
338 $columns = $this->_customdata
['columns'];
340 foreach ($columns as $column) {
341 if ($mform->elementExists($column)) {
342 $mform->removeElement($column);
346 if (!in_array('password', $columns)) {
347 // password resetting makes sense only if password specified in csv file
348 if ($mform->elementExists('uuforcepasswordchange')) {
349 $mform->removeElement('uuforcepasswordchange');
355 * Server side validation.
357 function validation($data, $files) {
358 $errors = parent
::validation($data, $files);
359 $columns = $this->_customdata
['columns'];
360 $optype = $data['uutype'];
362 // detect if password column needed in file
363 if (!in_array('password', $columns)) {
366 if (!empty($data['uupasswordold'])) {
367 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
371 case UU_USER_ADD_UPDATE
:
372 if (empty($data['uupasswordnew'])) {
373 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
375 if (!empty($data['uupasswordold'])) {
376 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
381 if (empty($data['uupasswordnew'])) {
382 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
386 if (empty($data['uupasswordnew'])) {
387 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
393 // look for other required data
394 if ($optype != UU_USER_UPDATE
) {
395 if (!in_array('firstname', $columns)) {
396 $errors['uutype'] = get_string('missingfield', 'error', 'firstname');
399 if (!in_array('lastname', $columns)) {
400 if (isset($errors['uutype'])) {
401 $errors['uutype'] = '';
403 $errors['uutype'] = ' ';
405 $errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
408 if (!in_array('email', $columns) and empty($data['email'])) {
409 $errors['email'] = get_string('requiredtemplate', 'admin');
412 if (!in_array('city', $columns) and empty($data['city'])) {
413 $errors['city'] = get_string('required');
421 * Used to reformat the data from the editor component
425 function get_data() {
426 $data = parent
::get_data();
428 if ($data !== null and isset($data->description
)) {
429 $data->descriptionformat
= $data->description
['format'];
430 $data->description
= $data->description
['text'];