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