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/>. 16 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 */ 25 26defined('MOODLE_INTERNAL') || die(); 27 28require_once $CFG->libdir.'/formslib.php'; 29require_once($CFG->dirroot . '/user/editlib.php'); 30 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 */ 37class admin_uploaduser_form1 extends moodleform { 38 function definition () { 39 $mform = $this->_form; 40 41 $mform->addElement('header', 'settingsheader', get_string('upload')); 42 43 $url = new moodle_url('example.csv'); 44 $link = html_writer::link($url, 'example.csv'); 45 $mform->addElement('static', 'examplecsv', get_string('examplecsv', 'tool_uploaduser'), $link); 46 $mform->addHelpButton('examplecsv', 'examplecsv', 'tool_uploaduser'); 47 48 $mform->addElement('filepicker', 'userfile', get_string('file')); 49 $mform->addRule('userfile', null, 'required'); 50 51 $choices = csv_import_reader::get_delimiter_list(); 52 $mform->addElement('select', 'delimiter_name', get_string('csvdelimiter', 'tool_uploaduser'), $choices); 53 if (array_key_exists('cfg', $choices)) { 54 $mform->setDefault('delimiter_name', 'cfg'); 55 } else if (get_string('listsep', 'langconfig') == ';') { 56 $mform->setDefault('delimiter_name', 'semicolon'); 57 } else { 58 $mform->setDefault('delimiter_name', 'comma'); 59 } 60 61 $choices = core_text::get_encodings(); 62 $mform->addElement('select', 'encoding', get_string('encoding', 'tool_uploaduser'), $choices); 63 $mform->setDefault('encoding', 'UTF-8'); 64 65 $choices = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000); 66 $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'tool_uploaduser'), $choices); 67 $mform->setType('previewrows', PARAM_INT); 68 69 $this->add_action_buttons(false, get_string('uploadusers', 'tool_uploaduser')); 70 } 71} 72 73 74/** 75 * Specify user upload details 76 * 77 * @copyright 2007 Petr Skoda {@link http://skodak.org} 78 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 79 */ 80class admin_uploaduser_form2 extends moodleform { 81 function definition () { 82 global $CFG, $USER; 83 84 $mform = $this->_form; 85 $columns = $this->_customdata['columns']; 86 $data = $this->_customdata['data']; 87 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; 90 91 // upload settings and file 92 $mform->addElement('header', 'settingsheader', get_string('settings')); 93 94 $choices = array(UU_USER_ADDNEW => get_string('uuoptype_addnew', 'tool_uploaduser'), 95 UU_USER_ADDINC => get_string('uuoptype_addinc', 'tool_uploaduser'), 96 UU_USER_ADD_UPDATE => get_string('uuoptype_addupdate', 'tool_uploaduser'), 97 UU_USER_UPDATE => get_string('uuoptype_update', 'tool_uploaduser')); 98 $mform->addElement('select', 'uutype', get_string('uuoptype', 'tool_uploaduser'), $choices); 99 100 $choices = array(0 => get_string('infilefield', 'auth'), 1 => get_string('createpasswordifneeded', 'auth')); 101 $mform->addElement('select', 'uupasswordnew', get_string('uupasswordnew', 'tool_uploaduser'), $choices); 102 $mform->setDefault('uupasswordnew', 1); 103 $mform->hideIf('uupasswordnew', 'uutype', 'eq', UU_USER_UPDATE); 104 105 $choices = array(UU_UPDATE_NOCHANGES => get_string('nochanges', 'tool_uploaduser'), 106 UU_UPDATE_FILEOVERRIDE => get_string('uuupdatefromfile', 'tool_uploaduser'), 107 UU_UPDATE_ALLOVERRIDE => get_string('uuupdateall', 'tool_uploaduser'), 108 UU_UPDATE_MISSING => get_string('uuupdatemissing', 'tool_uploaduser')); 109 $mform->addElement('select', 'uuupdatetype', get_string('uuupdatetype', 'tool_uploaduser'), $choices); 110 $mform->setDefault('uuupdatetype', UU_UPDATE_NOCHANGES); 111 $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDNEW); 112 $mform->hideIf('uuupdatetype', 'uutype', 'eq', UU_USER_ADDINC); 113 114 $choices = array(0 => get_string('nochanges', 'tool_uploaduser'), 1 => get_string('update')); 115 $mform->addElement('select', 'uupasswordold', get_string('uupasswordold', 'tool_uploaduser'), $choices); 116 $mform->setDefault('uupasswordold', 0); 117 $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDNEW); 118 $mform->hideIf('uupasswordold', 'uutype', 'eq', UU_USER_ADDINC); 119 $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 0); 120 $mform->hideIf('uupasswordold', 'uuupdatetype', 'eq', 3); 121 122 $choices = array(UU_PWRESET_WEAK => get_string('usersweakpassword', 'tool_uploaduser'), 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]); 127 } 128 $mform->addElement('select', 'uuforcepasswordchange', get_string('forcepasswordchange', 'core'), $choices); 129 130 131 $mform->addElement('selectyesno', 'uuallowrenames', get_string('allowrenames', 'tool_uploaduser')); 132 $mform->setDefault('uuallowrenames', 0); 133 $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDNEW); 134 $mform->hideIf('uuallowrenames', 'uutype', 'eq', UU_USER_ADDINC); 135 136 $mform->addElement('selectyesno', 'uuallowdeletes', get_string('allowdeletes', 'tool_uploaduser')); 137 $mform->setDefault('uuallowdeletes', 0); 138 $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW); 139 $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC); 140 141 $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser')); 142 $mform->setDefault('uuallowsuspends', 1); 143 $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW); 144 $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC); 145 146 if (!empty($CFG->allowaccountssameemail)) { 147 $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser')); 148 $mform->setDefault('uunoemailduplicates', 1); 149 } else { 150 $mform->addElement('hidden', 'uunoemailduplicates', 1); 151 } 152 $mform->setType('uunoemailduplicates', PARAM_BOOL); 153 154 $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser')); 155 $mform->setDefault('uustandardusernames', 1); 156 157 $choices = array(UU_BULK_NONE => get_string('no'), 158 UU_BULK_NEW => get_string('uubulknew', 'tool_uploaduser'), 159 UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'), 160 UU_BULK_ALL => get_string('uubulkall', 'tool_uploaduser')); 161 $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices); 162 $mform->setDefault('uubulk', 0); 163 164 // roles selection 165 $showroles = false; 166 foreach ($columns as $column) { 167 if (preg_match('/^type\d+$/', $column)) { 168 $showroles = true; 169 break; 170 } 171 } 172 if ($showroles) { 173 $mform->addElement('header', 'rolesheader', get_string('roles')); 174 175 $choices = uu_allowed_roles(true); 176 177 $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices); 178 if ($studentroles = get_archetype_roles('student')) { 179 foreach ($studentroles as $role) { 180 if (isset($choices[$role->id])) { 181 $mform->setDefault('uulegacy1', $role->id); 182 break; 183 } 184 } 185 unset($studentroles); 186 } 187 188 $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices); 189 if ($editteacherroles = get_archetype_roles('editingteacher')) { 190 foreach ($editteacherroles as $role) { 191 if (isset($choices[$role->id])) { 192 $mform->setDefault('uulegacy2', $role->id); 193 break; 194 } 195 } 196 unset($editteacherroles); 197 } 198 199 $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices); 200 if ($teacherroles = get_archetype_roles('teacher')) { 201 foreach ($teacherroles as $role) { 202 if (isset($choices[$role->id])) { 203 $mform->setDefault('uulegacy3', $role->id); 204 break; 205 } 206 } 207 unset($teacherroles); 208 } 209 } 210 211 // default values 212 $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser')); 213 214 $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"'); 215 $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later. 216 $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client'); 217 $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE); 218 $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE); 219 $mform->setForceLtr('username'); 220 221 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"'); 222 $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later. 223 $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE); 224 $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE); 225 $mform->setForceLtr('email'); 226 227 // only enabled and known to work plugins 228 $choices = uu_supported_auths(); 229 $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices); 230 $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default 231 $mform->addHelpButton('auth', 'chooseauthmethod', 'auth'); 232 $mform->setAdvanced('auth'); 233 234 $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse')); 235 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices); 236 $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay')); 237 $mform->addHelpButton('maildisplay', 'emaildisplay'); 238 239 $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable')); 240 $mform->addElement('select', 'emailstop', get_string('emailstop'), $choices); 241 $mform->setDefault('emailstop', core_user::get_property_default('emailstop')); 242 $mform->setAdvanced('emailstop'); 243 244 $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat')); 245 $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices); 246 $mform->setDefault('mailformat', core_user::get_property_default('mailformat')); 247 $mform->setAdvanced('mailformat'); 248 249 $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects')); 250 $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices); 251 $mform->setDefault('maildigest', core_user::get_property_default('maildigest')); 252 $mform->setAdvanced('maildigest'); 253 254 $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno')); 255 $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices); 256 $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe')); 257 258 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"'); 259 $mform->setType('city', PARAM_TEXT); 260 if (empty($CFG->defaultcity)) { 261 $mform->setDefault('city', $templateuser->city); 262 } else { 263 $mform->setDefault('city', core_user::get_property_default('city')); 264 } 265 266 $choices = get_string_manager()->get_list_of_countries(); 267 $choices = array(''=>get_string('selectacountry').'...') + $choices; 268 $mform->addElement('select', 'country', get_string('selectacountry'), $choices); 269 if (empty($CFG->country)) { 270 $mform->setDefault('country', $templateuser->country); 271 } else { 272 $mform->setDefault('country', core_user::get_property_default('country')); 273 } 274 $mform->setAdvanced('country'); 275 276 $choices = core_date::get_list_of_timezones($templateuser->timezone, true); 277 $mform->addElement('select', 'timezone', get_string('timezone'), $choices); 278 $mform->setDefault('timezone', $templateuser->timezone); 279 $mform->setAdvanced('timezone'); 280 281 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations()); 282 $mform->setDefault('lang', $templateuser->lang); 283 $mform->setAdvanced('lang'); 284 285 $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false); 286 $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions); 287 $mform->setType('description', PARAM_CLEANHTML); 288 $mform->addHelpButton('description', 'userdescription'); 289 $mform->setAdvanced('description'); 290 291 $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"'); 292 $mform->setType('url', PARAM_URL); 293 $mform->setAdvanced('url'); 294 295 $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"'); 296 $mform->setType('idnumber', core_user::get_property_type('idnumber')); 297 $mform->setForceLtr('idnumber'); 298 299 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"'); 300 $mform->setType('institution', PARAM_TEXT); 301 $mform->setDefault('institution', $templateuser->institution); 302 303 $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"'); 304 $mform->setType('department', PARAM_TEXT); 305 $mform->setDefault('department', $templateuser->department); 306 307 $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"'); 308 $mform->setType('phone1', PARAM_NOTAGS); 309 $mform->setAdvanced('phone1'); 310 $mform->setForceLtr('phone1'); 311 312 $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"'); 313 $mform->setType('phone2', PARAM_NOTAGS); 314 $mform->setAdvanced('phone2'); 315 $mform->setForceLtr('phone2'); 316 317 $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"'); 318 $mform->setType('address', PARAM_TEXT); 319 $mform->setAdvanced('address'); 320 321 // Next the profile defaults 322 profile_definition($mform); 323 324 // hidden fields 325 $mform->addElement('hidden', 'iid'); 326 $mform->setType('iid', PARAM_INT); 327 328 $mform->addElement('hidden', 'previewrows'); 329 $mform->setType('previewrows', PARAM_INT); 330 331 $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser')); 332 333 $this->set_data($data); 334 } 335 336 /** 337 * Form tweaks that depend on current data. 338 */ 339 function definition_after_data() { 340 $mform = $this->_form; 341 $columns = $this->_customdata['columns']; 342 343 foreach ($columns as $column) { 344 if ($mform->elementExists($column)) { 345 $mform->removeElement($column); 346 } 347 } 348 349 if (!in_array('password', $columns)) { 350 // password resetting makes sense only if password specified in csv file 351 if ($mform->elementExists('uuforcepasswordchange')) { 352 $mform->removeElement('uuforcepasswordchange'); 353 } 354 } 355 } 356 357 /** 358 * Server side validation. 359 */ 360 function validation($data, $files) { 361 $errors = parent::validation($data, $files); 362 $columns = $this->_customdata['columns']; 363 $optype = $data['uutype']; 364 $updatetype = $data['uuupdatetype']; 365 366 // detect if password column needed in file 367 if (!in_array('password', $columns)) { 368 switch ($optype) { 369 case UU_USER_UPDATE: 370 if (!empty($data['uupasswordold'])) { 371 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password'); 372 } 373 break; 374 375 case UU_USER_ADD_UPDATE: 376 if (empty($data['uupasswordnew'])) { 377 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password'); 378 } 379 if (!empty($data['uupasswordold'])) { 380 $errors['uupasswordold'] = get_string('missingfield', 'error', 'password'); 381 } 382 break; 383 384 case UU_USER_ADDNEW: 385 if (empty($data['uupasswordnew'])) { 386 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password'); 387 } 388 break; 389 case UU_USER_ADDINC: 390 if (empty($data['uupasswordnew'])) { 391 $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password'); 392 } 393 break; 394 } 395 } 396 397 // If the 'Existing user details' value is set we need to ensure that the 398 // 'Upload type' is not set to something invalid. 399 if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) { 400 $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser'); 401 } 402 403 // look for other required data 404 if ($optype != UU_USER_UPDATE) { 405 $requiredusernames = useredit_get_required_name_fields(); 406 $missing = array(); 407 foreach ($requiredusernames as $requiredusername) { 408 if (!in_array($requiredusername, $columns)) { 409 $missing[] = get_string('missingfield', 'error', $requiredusername);; 410 } 411 } 412 if ($missing) { 413 $errors['uutype'] = implode('<br />', $missing); 414 } 415 if (!in_array('email', $columns) and empty($data['email'])) { 416 $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser'); 417 } 418 } 419 return $errors; 420 } 421 422 /** 423 * Used to reformat the data from the editor component 424 * 425 * @return stdClass 426 */ 427 function get_data() { 428 $data = parent::get_data(); 429 430 if ($data !== null and isset($data->description)) { 431 $data->descriptionformat = $data->description['format']; 432 $data->description = $data->description['text']; 433 } 434 435 return $data; 436 } 437} 438