3 if (!defined('MOODLE_INTERNAL')) {
4 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
7 require_once $CFG->libdir
.'/formslib.php';
9 class admin_uploaduser_form1
extends moodleform
{
10 function definition (){
13 $mform =& $this->_form
;
15 $mform->addElement('header', 'settingsheader', get_string('upload'));
17 $mform->addElement('filepicker', 'userfile', get_string('file'));
18 $mform->addRule('userfile', null, 'required');
20 $choices = csv_import_reader
::get_delimiter_list();
21 $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'admin'), $choices);
22 if (array_key_exists('cfg', $choices)) {
23 $mform->setDefault('delimiter_name', 'cfg');
24 } else if (get_string('listsep', 'langconfig') == ';') {
25 $mform->setDefault('delimiter_name', 'semicolon');
27 $mform->setDefault('delimiter_name', 'comma');
30 $textlib = textlib_get_instance();
31 $choices = $textlib->get_encodings();
32 $mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices);
33 $mform->setDefault('encoding', 'UTF-8');
35 $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
36 $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'admin'), $choices);
37 $mform->setType('previewrows', PARAM_INT
);
39 $choices = array(UU_ADDNEW
=> get_string('uuoptype_addnew', 'admin'),
40 UU_ADDINC
=> get_string('uuoptype_addinc', 'admin'),
41 UU_ADD_UPDATE
=> get_string('uuoptype_addupdate', 'admin'),
42 UU_UPDATE
=> get_string('uuoptype_update', 'admin'));
43 $mform->addElement('select', 'uutype', get_string('uuoptype', 'admin'), $choices);
45 $this->add_action_buttons(false, get_string('uploadusers', 'admin'));
49 class admin_uploaduser_form2
extends moodleform
{
50 function definition (){
53 $mform =& $this->_form
;
54 $columns =& $this->_customdata
;
56 // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
57 $templateuser = $USER;
59 // upload settings and file
60 $mform->addElement('header', 'settingsheader', get_string('settings'));
61 $mform->addElement('static', 'uutypelabel', get_string('uuoptype', 'admin') );
63 $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
64 $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices);
65 $mform->setDefault('uupasswordnew', 0);
66 $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_UPDATE
);
68 $choices = array(0 => get_string('nochanges', 'admin'),
69 1 => get_string('uuupdatefromfile', 'admin'),
70 2 => get_string('uuupdateall', 'admin'),
71 3 => get_string('uuupdatemissing', 'admin'));
72 $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices);
73 $mform->setDefault('uuupdatetype', 0);
74 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDNEW
);
75 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDINC
);
77 $choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update'));
78 $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices);
79 $mform->setDefault('uupasswordold', 0);
80 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDNEW
);
81 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDINC
);
82 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
83 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
85 $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin'));
86 $mform->setDefault('uuallowrenames', 0);
87 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDNEW
);
88 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDINC
);
90 $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin'));
91 $mform->setDefault('uuallowdeletes', 0);
92 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDNEW
);
93 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDINC
);
95 $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'admin'));
96 $mform->setDefault('uunoemailduplicates', 1);
98 $choices = array(0 => get_string('no'),
99 1 => get_string('uubulknew', 'admin'),
100 2 => get_string('uubulkupdated', 'admin'),
101 3 => get_string('uubulkall', 'admin'));
102 $mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices);
103 $mform->setDefault('uubulk', 0);
107 foreach ($columns as $column) {
108 if (preg_match('/^type\d+$/', $column)) {
114 $mform->addElement('header', 'rolesheader', get_string('roles'));
116 $choices = uu_allowed_roles(true);
118 $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'admin'), $choices);
119 if ($studentroles = get_archetype_roles('student')) {
120 foreach ($studentroles as $role) {
121 if (isset($choices[$role->id
])) {
122 $mform->setDefault('uulegacy1', $role->id
);
126 unset($studentroles);
129 $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'admin'), $choices);
130 if ($editteacherroles = get_archetype_roles('editingteacher')) {
131 foreach ($editteacherroles as $role) {
132 if (isset($choices[$role->id
])) {
133 $mform->setDefault('uulegacy2', $role->id
);
137 unset($editteacherroles);
140 $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'admin'), $choices);
141 if ($teacherroles = get_archetype_roles('teacher')) {
142 foreach ($teacherroles as $role) {
143 if (isset($choices[$role->id
])) {
144 $mform->setDefault('uulegacy3', $role->id
);
148 unset($teacherroles);
153 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin'));
155 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
156 $mform->addRule('username', get_string('requiredtemplate', 'admin'), 'required', null, 'client');
158 // only enabled and known to work plugins
159 $choices = uu_allowed_auths();
160 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
161 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
162 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
163 $mform->setAdvanced('auth');
165 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
167 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
168 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
169 $mform->setDefault('maildisplay', 2);
171 $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
172 $mform->addElement('select', 'emailstop', get_string('emailactive'), $choices);
174 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
175 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
176 $mform->setDefault('mailformat', 1);
177 $mform->setAdvanced('mailformat');
179 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
180 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
181 $mform->setDefault('maildigest', 0);
182 $mform->setAdvanced('maildigest');
184 $choices = array(0 => get_string('autosubscribeyes'), 1 => get_string('autosubscribeno'));
185 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
186 $mform->setDefault('autosubscribe', 1);
188 $editors = editors_get_enabled();
189 if (count($editors) > 1) {
191 $choices['0'] = get_string('texteditor');
192 $choices['1'] = get_string('htmleditor');
193 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
194 $mform->setDefault('htmleditor', 1);
196 $mform->addElement('hidden', 'htmleditor');
197 $mform->setDefault('htmleditor', 1);
198 $mform->setType('htmleditor', PARAM_INT
);
201 if (empty($CFG->enableajax
)) {
202 $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno'));
204 $choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes'));
205 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
206 $mform->setDefault('ajax', 1);
208 $mform->setAdvanced('ajax');
210 $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
211 $mform->setType('city', PARAM_MULTILANG
);
212 $mform->setDefault('city', $templateuser->city
);
214 $mform->addElement('select', 'country', get_string('selectacountry'), get_string_manager()->get_list_of_countries());
215 $mform->setDefault('country', $templateuser->country
);
216 $mform->setAdvanced('country');
218 $choices = get_list_of_timezones();
219 $choices['99'] = get_string('serverlocaltime');
220 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
221 $mform->setDefault('timezone', $templateuser->timezone
);
222 $mform->setAdvanced('timezone');
224 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
225 $mform->setDefault('lang', $templateuser->lang
);
226 $mform->setAdvanced('lang');
228 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
229 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
230 $mform->setType('description', PARAM_CLEANHTML
);
231 $mform->addHelpButton('description', 'userdescription');
232 $mform->setAdvanced('description');
234 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
235 $mform->setAdvanced('url');
237 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
238 $mform->setType('idnumber', PARAM_CLEAN
);
240 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
241 $mform->setType('institution', PARAM_MULTILANG
);
242 $mform->setDefault('institution', $templateuser->institution
);
244 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
245 $mform->setType('department', PARAM_MULTILANG
);
246 $mform->setDefault('department', $templateuser->department
);
248 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
249 $mform->setType('phone1', PARAM_CLEAN
);
250 $mform->setAdvanced('phone1');
252 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
253 $mform->setType('phone2', PARAM_CLEAN
);
254 $mform->setAdvanced('phone2');
256 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
257 $mform->setType('address', PARAM_MULTILANG
);
258 $mform->setAdvanced('address');
260 // Next the profile defaults
261 profile_definition($mform);
264 $mform->addElement('hidden', 'iid');
265 $mform->setType('iid', PARAM_INT
);
267 $mform->addElement('hidden', 'previewrows');
268 $mform->setType('previewrows', PARAM_INT
);
270 $mform->addElement('hidden', 'readcount');
271 $mform->setType('readcount', PARAM_INT
);
273 $mform->addElement('hidden', 'uutype');
274 $mform->setType('uutype', PARAM_INT
);
276 $this->add_action_buttons(true, get_string('uploadusers', 'admin'));
280 * Form tweaks that depend on current data.
282 function definition_after_data() {
283 $mform =& $this->_form
;
284 $columns =& $this->_customdata
;
286 foreach ($columns as $column) {
287 if ($mform->elementExists($column)) {
288 $mform->removeElement($column);
294 * Server side validation.
296 function validation($data, $files) {
297 $errors = parent
::validation($data, $files);
298 $columns =& $this->_customdata
;
299 $optype = $data['uutype'];
301 // detect if password column needed in file
302 if (!in_array('password', $columns)) {
305 if (!empty($data['uupasswordold'])) {
306 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
311 if (empty($data['uupasswordnew'])) {
312 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
314 if (!empty($data['uupasswordold'])) {
315 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
321 if (empty($data['uupasswordnew'])) {
322 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
328 // look for other required data
329 if ($optype != UU_UPDATE
) {
330 if (!in_array('firstname', $columns)) {
331 $errors['uutype'] = get_string('missingfield', 'error', 'firstname');
334 if (!in_array('lastname', $columns)) {
335 if (isset($errors['uutype'])) {
336 $errors['uutype'] = '';
338 $errors['uutype'] = ' ';
340 $errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
343 if (!in_array('email', $columns) and empty($data['email'])) {
344 $errors['email'] = get_string('requiredtemplate', 'admin');
347 if (!in_array('city', $columns) and empty($data['city'])) {
348 $errors['city'] = get_string('required');
356 * Used to reformat the data from the editor component
360 function get_data() {
361 $data = parent
::get_data();
363 if ($data !== null) {
364 $data->descriptionformat
= $data->description
['format'];
365 $data->description
= $data->description
['text'];
372 class admin_uploaduser_form3
extends moodleform
{
373 function definition (){
375 $mform =& $this->_form
;
376 $this->add_action_buttons(false, get_string('uploadnewfile'));