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 $this->set_upload_manager(new upload_manager('userfile', false, false, null, false, 0, true, true, false));
17 $mform->addElement('header', 'settingsheader', get_string('upload'));
19 $mform->addElement('file', 'userfile', get_string('file'), 'size="40"');
20 $mform->addRule('userfile', null, 'required');
22 $choices = csv_import_reader
::get_delimiter_list();
23 $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'admin'), $choices);
24 if (array_key_exists('cfg', $choices)) {
25 $mform->setDefault('delimiter_name', 'cfg');
26 } else if (get_string('listsep') == ';') {
27 $mform->setDefault('delimiter_name', 'semicolon');
29 $mform->setDefault('delimiter_name', 'comma');
32 $textlib = textlib_get_instance();
33 $choices = $textlib->get_encodings();
34 $mform->addElement('select', 'encoding', get_string('encoding', 'admin'), $choices);
35 $mform->setDefault('encoding', 'UTF-8');
37 $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
38 $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'admin'), $choices);
39 $mform->setType('previewrows', PARAM_INT
);
41 $this->add_action_buttons(false, get_string('uploadusers'));
45 class admin_uploaduser_form2
extends moodleform
{
46 function definition (){
49 //no editors here - we need proper empty fields
50 $CFG->htmleditor
= null;
52 $mform =& $this->_form
;
53 $columns =& $this->_customdata
;
55 // I am the template user, why should it be the administrator? we have roles now, other ppl may use this script ;-)
56 $templateuser = $USER;
58 // upload settings and file
59 $mform->addElement('header', 'settingsheader', get_string('settings'));
61 $choices = array(UU_ADDNEW
=> get_string('uuoptype_addnew', 'admin'),
62 UU_ADDINC
=> get_string('uuoptype_addinc', 'admin'),
63 UU_ADD_UPDATE
=> get_string('uuoptype_addupdate', 'admin'),
64 UU_UPDATE
=> get_string('uuoptype_update', 'admin'));
65 $mform->addElement('select', 'uutype', get_string('uuoptype', 'admin'), $choices);
67 $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth'));
68 $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'admin'), $choices);
69 $mform->setDefault('uupasswordnew', 0);
70 $mform->disabledIf('uupasswordnew', 'uutype', 'eq', UU_UPDATE
);
72 $choices = array(0 => get_string('nochanges', 'admin'),
73 1 => get_string('uuupdatefromfile', 'admin'),
74 2 => get_string('uuupdateall', 'admin'),
75 3 => get_string('uuupdatemissing', 'admin'));
76 $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'admin'), $choices);
77 $mform->setDefault('uuupdatetype', 0);
78 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDNEW
);
79 $mform->disabledIf('uuupdatetype', 'uutype', 'eq', UU_ADDINC
);
81 $choices = array(0 => get_string('nochanges', 'admin'), 1 => get_string('update'));
82 $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'admin'), $choices);
83 $mform->setDefault('uupasswordold', 0);
84 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDNEW
);
85 $mform->disabledIf('uupasswordold', 'uutype', 'eq', UU_ADDINC
);
86 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 0);
87 $mform->disabledIf('uupasswordold', 'uuupdatetype', 'eq', 3);
89 $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'admin'));
90 $mform->setDefault('uuallowrenames', 0);
91 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDNEW
);
92 $mform->disabledIf('uuallowrenames', 'uutype', 'eq', UU_ADDINC
);
94 $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'admin'));
95 $mform->setDefault('uuallowdeletes', 0);
96 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDNEW
);
97 $mform->disabledIf('uuallowdeletes', 'uutype', 'eq', UU_ADDINC
);
99 $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'admin'));
100 $mform->setDefault('uunoemailduplicates', 0);
102 $choices = array(0 => get_string('no'),
103 1 => get_string('uubulknew', 'admin'),
104 2 => get_string('uubulkupdated', 'admin'),
105 3 => get_string('uubulkall', 'admin'));
106 $mform->addElement('select', 'uubulk', get_string('uubulk', 'admin'), $choices);
107 $mform->setDefault('uubulk', 0);
111 foreach ($columns as $column) {
112 if (preg_match('/^type\d+$/', $column)) {
118 $mform->addElement('header', 'rolesheader', get_string('roles'));
120 $choices = uu_allowed_roles(true);
122 $choices[0] = get_string('uucoursedefaultrole', 'admin');
123 $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'admin'), $choices);
124 $mform->setDefault('uulegacy1', 0);
127 $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'admin'), $choices);
128 if ($editteacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW
)) {
129 $editteacherrole = array_shift($editteacherroles); /// Take the first one
130 $mform->setDefault('uulegacy2', $editteacherrole->id
);
131 unset($editteacherroles);
133 $mform->setDefault('uulegacy2', $CFG->defaultcourseroleid
);
136 $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'admin'), $choices);
137 if ($teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
)) {
138 $teacherrole = array_shift($teacherroles); /// Take the first one
139 $mform->setDefault('uulegacy3', $teacherrole->id
);
140 unset($teacherroles);
142 $mform->setDefault('uulegacy3', $CFG->defaultcourseroleid
);
147 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'admin'));
149 $mform->addElement('text', 'username', get_string('username'), 'size="20"');
150 $mform->addRule('username', get_string('requiredtemplate', 'admin'), 'required', null, 'client');
152 // only enabled and known to work plugins
153 $choices = uu_allowed_auths();
154 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
155 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
156 $mform->setHelpButton('auth', array('authchange', get_string('chooseauthmethod','auth')));
157 $mform->setAdvanced('auth');
159 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
161 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
162 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
163 $mform->setDefault('maildisplay', 2);
165 $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
166 $mform->addElement('select', 'emailstop', get_string('emailactive'), $choices);
168 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
169 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
170 $mform->setDefault('mailformat', 1);
171 $mform->setAdvanced('mailformat');
173 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
174 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
175 $mform->setDefault('maildigest', 0);
176 $mform->setAdvanced('maildigest');
178 $choices = array(0 => get_string('autosubscribeyes'), 1 => get_string('autosubscribeno'));
179 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
180 $mform->setDefault('autosubscribe', 1);
182 if ($CFG->htmleditor
) {
183 $choices = array(0 => get_string('texteditor'), 1 => get_string('htmleditor'));
184 $mform->addElement('select', 'htmleditor', get_string('textediting'), $choices);
185 $mform->setDefault('htmleditor', 1);
187 $mform->addElement('static', 'htmleditor', get_string('textediting'), get_string('texteditor'));
189 $mform->setAdvanced('htmleditor');
191 if (empty($CFG->enableajax
)) {
192 $mform->addElement('static', 'ajax', get_string('ajaxuse'), get_string('ajaxno'));
194 $choices = array( 0 => get_string('ajaxno'), 1 => get_string('ajaxyes'));
195 $mform->addElement('select', 'ajax', get_string('ajaxuse'), $choices);
196 $mform->setDefault('ajax', 1);
198 $mform->setAdvanced('ajax');
200 $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
201 $mform->setType('city', PARAM_MULTILANG
);
202 $mform->setDefault('city', $templateuser->city
);
204 $mform->addElement('select', 'country', get_string('selectacountry'), get_list_of_countries());
205 $mform->setDefault('country', $templateuser->country
);
206 $mform->setAdvanced('country');
208 $choices = get_list_of_timezones();
209 $choices['99'] = get_string('serverlocaltime');
210 $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
211 $mform->setDefault('timezone', $templateuser->timezone
);
212 $mform->setAdvanced('timezone');
214 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_list_of_languages());
215 $mform->setDefault('lang', $templateuser->lang
);
216 $mform->setAdvanced('lang');
218 $mform->addElement('htmleditor', 'description', get_string('userdescription'));
219 $mform->setType('description', PARAM_CLEAN
);
220 $mform->setHelpButton('description', array('text', get_string('helptext')));
221 $mform->setAdvanced('description');
223 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
224 $mform->setAdvanced('url');
226 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="64" size="25"');
227 $mform->setType('idnumber', PARAM_CLEAN
);
229 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
230 $mform->setType('institution', PARAM_MULTILANG
);
231 $mform->setDefault('institution', $templateuser->institution
);
233 $mform->addElement('text', 'department', get_string('department'), 'maxlength="30" size="25"');
234 $mform->setType('department', PARAM_MULTILANG
);
235 $mform->setDefault('department', $templateuser->department
);
237 $mform->addElement('text', 'phone1', get_string('phone'), 'maxlength="20" size="25"');
238 $mform->setType('phone1', PARAM_CLEAN
);
239 $mform->setAdvanced('phone1');
241 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
242 $mform->setType('phone2', PARAM_CLEAN
);
243 $mform->setAdvanced('phone2');
245 $mform->addElement('text', 'address', get_string('address'), 'maxlength="70" size="25"');
246 $mform->setType('address', PARAM_MULTILANG
);
247 $mform->setAdvanced('address');
249 /// Next the profile defaults
250 profile_definition($mform);
253 $mform->addElement('hidden', 'iid');
254 $mform->setType('iid', PARAM_INT
);
256 $mform->addElement('hidden', 'previewrows');
257 $mform->setType('previewrows', PARAM_INT
);
259 $mform->addElement('hidden', 'readcount');
260 $mform->setType('readcount', PARAM_INT
);
262 $this->add_action_buttons(true, get_string('uploadusers'));
266 * Form tweaks that depend on current data.
268 function definition_after_data() {
269 $mform =& $this->_form
;
270 $columns =& $this->_customdata
;
272 foreach ($columns as $column) {
273 if ($mform->elementExists($column)) {
274 $mform->removeElement($column);
280 * Server side validation.
282 function validation($data, $files) {
283 $errors = parent
::validation($data, $files);
284 $columns =& $this->_customdata
;
285 $optype = $data['uutype'];
287 // detect if password column needed in file
288 if (!in_array('password', $columns)) {
291 if (!empty($data['uupasswordold'])) {
292 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
297 if (empty($data['uupasswordnew'])) {
298 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
300 if (!empty($data['uupasswordold'])) {
301 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
307 if (empty($data['uupasswordnew'])) {
308 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
314 // look for other required data
315 if ($optype != UU_UPDATE
) {
316 if (!in_array('firstname', $columns)) {
317 $errors['uutype'] = get_string('missingfield', 'error', 'firstname');
320 if (!in_array('lastname', $columns)) {
321 if (isset($errors['uutype'])) {
322 $errors['uutype'] = '';
324 $errors['uutype'] = ' ';
326 $errors['uutype'] .= get_string('missingfield', 'error', 'lastname');
329 if (!in_array('email', $columns) and empty($data['email'])) {
330 $errors['email'] = get_string('requiredtemplate', 'admin');
333 if (!in_array('city', $columns) and empty($data['city'])) {
334 $errors['city'] = get_string('required');