[ Index ]

PHP Cross Reference of moodle-2.8

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1