Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  
  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   */
  80  class 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          // Ensure user is able to perform user deletion.
 139          if (!has_capability('moodle/user:delete', context_system::instance())) {
 140              $mform->hardFreeze('uuallowdeletes');
 141              $mform->setConstant('uuallowdeletes', 0);
 142          }
 143          $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDNEW);
 144          $mform->hideIf('uuallowdeletes', 'uutype', 'eq', UU_USER_ADDINC);
 145  
 146          $mform->addElement('selectyesno', 'uuallowsuspends', get_string('allowsuspends', 'tool_uploaduser'));
 147          $mform->setDefault('uuallowsuspends', 1);
 148          $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDNEW);
 149          $mform->hideIf('uuallowsuspends', 'uutype', 'eq', UU_USER_ADDINC);
 150  
 151          if (!empty($CFG->allowaccountssameemail)) {
 152              $mform->addElement('selectyesno', 'uunoemailduplicates', get_string('uunoemailduplicates', 'tool_uploaduser'));
 153              $mform->setDefault('uunoemailduplicates', 1);
 154          } else {
 155              $mform->addElement('hidden', 'uunoemailduplicates', 1);
 156          }
 157          $mform->setType('uunoemailduplicates', PARAM_BOOL);
 158  
 159          $mform->addElement('selectyesno', 'uustandardusernames', get_string('uustandardusernames', 'tool_uploaduser'));
 160          $mform->setDefault('uustandardusernames', 1);
 161  
 162          $choices = array(UU_BULK_NONE    => get_string('no'),
 163                           UU_BULK_NEW     => get_string('uubulknew', 'tool_uploaduser'),
 164                           UU_BULK_UPDATED => get_string('uubulkupdated', 'tool_uploaduser'),
 165                           UU_BULK_ALL     => get_string('uubulkall', 'tool_uploaduser'));
 166          $mform->addElement('select', 'uubulk', get_string('uubulk', 'tool_uploaduser'), $choices);
 167          $mform->setDefault('uubulk', 0);
 168  
 169          // roles selection
 170          $showroles = false;
 171          foreach ($columns as $column) {
 172              if (preg_match('/^type\d+$/', $column)) {
 173                  $showroles = true;
 174                  break;
 175              }
 176          }
 177          if ($showroles) {
 178              $mform->addElement('header', 'rolesheader', get_string('roles'));
 179  
 180              $choices = uu_allowed_roles(true);
 181  
 182              $mform->addElement('select', 'uulegacy1', get_string('uulegacy1role', 'tool_uploaduser'), $choices);
 183              if ($studentroles = get_archetype_roles('student')) {
 184                  foreach ($studentroles as $role) {
 185                      if (isset($choices[$role->id])) {
 186                          $mform->setDefault('uulegacy1', $role->id);
 187                          break;
 188                      }
 189                  }
 190                  unset($studentroles);
 191              }
 192  
 193              $mform->addElement('select', 'uulegacy2', get_string('uulegacy2role', 'tool_uploaduser'), $choices);
 194              if ($editteacherroles = get_archetype_roles('editingteacher')) {
 195                  foreach ($editteacherroles as $role) {
 196                      if (isset($choices[$role->id])) {
 197                          $mform->setDefault('uulegacy2', $role->id);
 198                          break;
 199                      }
 200                  }
 201                  unset($editteacherroles);
 202              }
 203  
 204              $mform->addElement('select', 'uulegacy3', get_string('uulegacy3role', 'tool_uploaduser'), $choices);
 205              if ($teacherroles = get_archetype_roles('teacher')) {
 206                  foreach ($teacherroles as $role) {
 207                      if (isset($choices[$role->id])) {
 208                          $mform->setDefault('uulegacy3', $role->id);
 209                          break;
 210                      }
 211                  }
 212                  unset($teacherroles);
 213              }
 214          }
 215  
 216          // default values
 217          $mform->addElement('header', 'defaultheader', get_string('defaultvalues', 'tool_uploaduser'));
 218  
 219          $mform->addElement('text', 'username', get_string('uuusernametemplate', 'tool_uploaduser'), 'size="20"');
 220          $mform->setType('username', PARAM_RAW); // No cleaning here. The process verifies it later.
 221          $mform->addRule('username', get_string('requiredtemplate', 'tool_uploaduser'), 'required', null, 'client');
 222          $mform->hideIf('username', 'uutype', 'eq', UU_USER_ADD_UPDATE);
 223          $mform->hideIf('username', 'uutype', 'eq', UU_USER_UPDATE);
 224          $mform->setForceLtr('username');
 225  
 226          $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="30"');
 227          $mform->setType('email', PARAM_RAW); // No cleaning here. The process verifies it later.
 228          $mform->hideIf('email', 'uutype', 'eq', UU_USER_ADD_UPDATE);
 229          $mform->hideIf('email', 'uutype', 'eq', UU_USER_UPDATE);
 230          $mform->setForceLtr('email');
 231  
 232          // only enabled and known to work plugins
 233          $choices = uu_supported_auths();
 234          $mform->addElement('select', 'auth', get_string('chooseauthmethod','auth'), $choices);
 235          $mform->setDefault('auth', 'manual'); // manual is a sensible backwards compatible default
 236          $mform->addHelpButton('auth', 'chooseauthmethod', 'auth');
 237          $mform->setAdvanced('auth');
 238  
 239          $choices = array(0 => get_string('emaildisplayno'), 1 => get_string('emaildisplayyes'), 2 => get_string('emaildisplaycourse'));
 240          $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
 241          $mform->setDefault('maildisplay', core_user::get_property_default('maildisplay'));
 242          $mform->addHelpButton('maildisplay', 'emaildisplay');
 243  
 244          $choices = array(0 => get_string('emailenable'), 1 => get_string('emaildisable'));
 245          $mform->addElement('select', 'emailstop', get_string('emailstop'), $choices);
 246          $mform->setDefault('emailstop', core_user::get_property_default('emailstop'));
 247          $mform->setAdvanced('emailstop');
 248  
 249          $choices = array(0 => get_string('textformat'), 1 => get_string('htmlformat'));
 250          $mform->addElement('select', 'mailformat', get_string('emailformat'), $choices);
 251          $mform->setDefault('mailformat', core_user::get_property_default('mailformat'));
 252          $mform->setAdvanced('mailformat');
 253  
 254          $choices = array(0 => get_string('emaildigestoff'), 1 => get_string('emaildigestcomplete'), 2 => get_string('emaildigestsubjects'));
 255          $mform->addElement('select', 'maildigest', get_string('emaildigest'), $choices);
 256          $mform->setDefault('maildigest', core_user::get_property_default('maildigest'));
 257          $mform->setAdvanced('maildigest');
 258  
 259          $choices = array(1 => get_string('autosubscribeyes'), 0 => get_string('autosubscribeno'));
 260          $mform->addElement('select', 'autosubscribe', get_string('autosubscribe'), $choices);
 261          $mform->setDefault('autosubscribe', core_user::get_property_default('autosubscribe'));
 262  
 263          $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="25"');
 264          $mform->setType('city', PARAM_TEXT);
 265          if (empty($CFG->defaultcity)) {
 266              $mform->setDefault('city', $templateuser->city);
 267          } else {
 268              $mform->setDefault('city', core_user::get_property_default('city'));
 269          }
 270  
 271          $choices = get_string_manager()->get_list_of_countries();
 272          $choices = array(''=>get_string('selectacountry').'...') + $choices;
 273          $mform->addElement('select', 'country', get_string('selectacountry'), $choices);
 274          if (empty($CFG->country)) {
 275              $mform->setDefault('country', $templateuser->country);
 276          } else {
 277              $mform->setDefault('country', core_user::get_property_default('country'));
 278          }
 279          $mform->setAdvanced('country');
 280  
 281          $choices = core_date::get_list_of_timezones($templateuser->timezone, true);
 282          $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
 283          $mform->setDefault('timezone', $templateuser->timezone);
 284          $mform->setAdvanced('timezone');
 285  
 286          $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
 287          $mform->setDefault('lang', $templateuser->lang);
 288          $mform->setAdvanced('lang');
 289  
 290          $editoroptions = array('maxfiles'=>0, 'maxbytes'=>0, 'trusttext'=>false, 'forcehttps'=>false);
 291          $mform->addElement('editor', 'description', get_string('userdescription'), null, $editoroptions);
 292          $mform->setType('description', PARAM_CLEANHTML);
 293          $mform->addHelpButton('description', 'userdescription');
 294          $mform->setAdvanced('description');
 295  
 296          $mform->addElement('text', 'url', get_string('webpage'), 'maxlength="255" size="50"');
 297          $mform->setType('url', PARAM_URL);
 298          $mform->setAdvanced('url');
 299  
 300          $mform->addElement('text', 'idnumber', get_string('idnumber'), 'maxlength="255" size="25"');
 301          $mform->setType('idnumber', core_user::get_property_type('idnumber'));
 302          $mform->setForceLtr('idnumber');
 303  
 304          $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="255" size="25"');
 305          $mform->setType('institution', PARAM_TEXT);
 306          $mform->setDefault('institution', $templateuser->institution);
 307  
 308          $mform->addElement('text', 'department', get_string('department'), 'maxlength="255" size="25"');
 309          $mform->setType('department', PARAM_TEXT);
 310          $mform->setDefault('department', $templateuser->department);
 311  
 312          $mform->addElement('text', 'phone1', get_string('phone1'), 'maxlength="20" size="25"');
 313          $mform->setType('phone1', PARAM_NOTAGS);
 314          $mform->setAdvanced('phone1');
 315          $mform->setForceLtr('phone1');
 316  
 317          $mform->addElement('text', 'phone2', get_string('phone2'), 'maxlength="20" size="25"');
 318          $mform->setType('phone2', PARAM_NOTAGS);
 319          $mform->setAdvanced('phone2');
 320          $mform->setForceLtr('phone2');
 321  
 322          $mform->addElement('text', 'address', get_string('address'), 'maxlength="255" size="25"');
 323          $mform->setType('address', PARAM_TEXT);
 324          $mform->setAdvanced('address');
 325  
 326          // Next the profile defaults
 327          profile_definition($mform);
 328  
 329          // hidden fields
 330          $mform->addElement('hidden', 'iid');
 331          $mform->setType('iid', PARAM_INT);
 332  
 333          $mform->addElement('hidden', 'previewrows');
 334          $mform->setType('previewrows', PARAM_INT);
 335  
 336          $this->add_action_buttons(true, get_string('uploadusers', 'tool_uploaduser'));
 337  
 338          $this->set_data($data);
 339      }
 340  
 341      /**
 342       * Form tweaks that depend on current data.
 343       */
 344      function definition_after_data() {
 345          $mform   = $this->_form;
 346          $columns = $this->_customdata['columns'];
 347  
 348          foreach ($columns as $column) {
 349              if ($mform->elementExists($column)) {
 350                  $mform->removeElement($column);
 351              }
 352          }
 353  
 354          if (!in_array('password', $columns)) {
 355              // password resetting makes sense only if password specified in csv file
 356              if ($mform->elementExists('uuforcepasswordchange')) {
 357                  $mform->removeElement('uuforcepasswordchange');
 358              }
 359          }
 360      }
 361  
 362      /**
 363       * Server side validation.
 364       */
 365      function validation($data, $files) {
 366          $errors = parent::validation($data, $files);
 367          $columns = $this->_customdata['columns'];
 368          $optype  = $data['uutype'];
 369          $updatetype = $data['uuupdatetype'];
 370  
 371          // detect if password column needed in file
 372          if (!in_array('password', $columns)) {
 373              switch ($optype) {
 374                  case UU_USER_UPDATE:
 375                      if (!empty($data['uupasswordold'])) {
 376                          $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
 377                      }
 378                      break;
 379  
 380                  case UU_USER_ADD_UPDATE:
 381                      if (empty($data['uupasswordnew'])) {
 382                          $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
 383                      }
 384                      if  (!empty($data['uupasswordold'])) {
 385                          $errors['uupasswordold'] = get_string('missingfield', 'error', 'password');
 386                      }
 387                      break;
 388  
 389                  case UU_USER_ADDNEW:
 390                      if (empty($data['uupasswordnew'])) {
 391                          $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
 392                      }
 393                      break;
 394                  case UU_USER_ADDINC:
 395                      if (empty($data['uupasswordnew'])) {
 396                          $errors['uupasswordnew'] = get_string('missingfield', 'error', 'password');
 397                      }
 398                      break;
 399               }
 400          }
 401  
 402          // If the 'Existing user details' value is set we need to ensure that the
 403          // 'Upload type' is not set to something invalid.
 404          if (!empty($updatetype) && ($optype == UU_USER_ADDNEW || $optype == UU_USER_ADDINC)) {
 405              $errors['uuupdatetype'] = get_string('invalidupdatetype', 'tool_uploaduser');
 406          }
 407  
 408          // look for other required data
 409          if ($optype != UU_USER_UPDATE) {
 410              $requiredusernames = useredit_get_required_name_fields();
 411              $missing = array();
 412              foreach ($requiredusernames as $requiredusername) {
 413                  if (!in_array($requiredusername, $columns)) {
 414                      $missing[] = get_string('missingfield', 'error', $requiredusername);;
 415                  }
 416              }
 417              if ($missing) {
 418                  $errors['uutype'] = implode('<br />',  $missing);
 419              }
 420              if (!in_array('email', $columns) and empty($data['email'])) {
 421                  $errors['email'] = get_string('requiredtemplate', 'tool_uploaduser');
 422              }
 423          }
 424          return $errors;
 425      }
 426  
 427      /**
 428       * Used to reformat the data from the editor component
 429       *
 430       * @return stdClass
 431       */
 432      function get_data() {
 433          $data = parent::get_data();
 434  
 435          if ($data !== null and isset($data->description)) {
 436              $data->descriptionformat = $data->description['format'];
 437              $data->description = $data->description['text'];
 438          }
 439  
 440          return $data;
 441      }
 442  }