[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/uploaduser/ -> user_form.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1