[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/login/ -> signup_form.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * User sign-up form.
  20   *
  21   * @package    core
  22   * @subpackage auth
  23   * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir.'/formslib.php');
  30  require_once($CFG->dirroot.'/user/profile/lib.php');
  31  require_once($CFG->dirroot . '/user/editlib.php');
  32  
  33  class login_signup_form extends moodleform {
  34      function definition() {
  35          global $USER, $CFG;
  36  
  37          $mform = $this->_form;
  38  
  39          $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
  40  
  41  
  42          $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
  43          $mform->setType('username', PARAM_NOTAGS);
  44          $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
  45  
  46          if (!empty($CFG->passwordpolicy)){
  47              $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  48          }
  49          $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
  50          $mform->setType('password', PARAM_RAW);
  51          $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
  52  
  53          $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
  54  
  55          $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
  56          $mform->setType('email', PARAM_NOTAGS);
  57          $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
  58  
  59          $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
  60          $mform->setType('email2', PARAM_NOTAGS);
  61          $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
  62  
  63          $namefields = useredit_get_required_name_fields();
  64          foreach ($namefields as $field) {
  65              $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
  66              $mform->setType($field, PARAM_TEXT);
  67              $stringid = 'missing' . $field;
  68              if (!get_string_manager()->string_exists($stringid, 'moodle')) {
  69                  $stringid = 'required';
  70              }
  71              $mform->addRule($field, get_string($stringid), 'required', null, 'server');
  72          }
  73  
  74          $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
  75          $mform->setType('city', PARAM_TEXT);
  76          if (!empty($CFG->defaultcity)) {
  77              $mform->setDefault('city', $CFG->defaultcity);
  78          }
  79  
  80          $country = get_string_manager()->get_list_of_countries();
  81          $default_country[''] = get_string('selectacountry');
  82          $country = array_merge($default_country, $country);
  83          $mform->addElement('select', 'country', get_string('country'), $country);
  84  
  85          if( !empty($CFG->country) ){
  86              $mform->setDefault('country', $CFG->country);
  87          }else{
  88              $mform->setDefault('country', '');
  89          }
  90  
  91          if ($this->signup_captcha_enabled()) {
  92              $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
  93              $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
  94          }
  95  
  96          profile_signup_fields($mform);
  97  
  98          if (!empty($CFG->sitepolicy)) {
  99              $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
 100              $mform->setExpanded('policyagreement');
 101              $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
 102              $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
 103              $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
 104          }
 105  
 106          // buttons
 107          $this->add_action_buttons(true, get_string('createaccount'));
 108  
 109      }
 110  
 111      function definition_after_data(){
 112          $mform = $this->_form;
 113          $mform->applyFilter('username', 'trim');
 114      }
 115  
 116      function validation($data, $files) {
 117          global $CFG, $DB;
 118          $errors = parent::validation($data, $files);
 119  
 120          $authplugin = get_auth_plugin($CFG->registerauth);
 121  
 122          if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
 123              $errors['username'] = get_string('usernameexists');
 124          } else {
 125              //check allowed characters
 126              if ($data['username'] !== core_text::strtolower($data['username'])) {
 127                  $errors['username'] = get_string('usernamelowercase');
 128              } else {
 129                  if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
 130                      $errors['username'] = get_string('invalidusername');
 131                  }
 132  
 133              }
 134          }
 135  
 136          //check if user exists in external db
 137          //TODO: maybe we should check all enabled plugins instead
 138          if ($authplugin->user_exists($data['username'])) {
 139              $errors['username'] = get_string('usernameexists');
 140          }
 141  
 142  
 143          if (! validate_email($data['email'])) {
 144              $errors['email'] = get_string('invalidemail');
 145  
 146          } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
 147              $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
 148          }
 149          if (empty($data['email2'])) {
 150              $errors['email2'] = get_string('missingemail');
 151  
 152          } else if ($data['email2'] != $data['email']) {
 153              $errors['email2'] = get_string('invalidemail');
 154          }
 155          if (!isset($errors['email'])) {
 156              if ($err = email_is_not_allowed($data['email'])) {
 157                  $errors['email'] = $err;
 158              }
 159  
 160          }
 161  
 162          $errmsg = '';
 163          if (!check_password_policy($data['password'], $errmsg)) {
 164              $errors['password'] = $errmsg;
 165          }
 166  
 167          if ($this->signup_captcha_enabled()) {
 168              $recaptcha_element = $this->_form->getElement('recaptcha_element');
 169              if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
 170                  $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
 171                  $response_field = $this->_form->_submitValues['recaptcha_response_field'];
 172                  if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
 173                      $errors['recaptcha'] = $result;
 174                  }
 175              } else {
 176                  $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
 177              }
 178          }
 179          // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
 180          $dataobject = (object)$data;
 181          $dataobject->id = 0;
 182          $errors += profile_validation($dataobject, $files);
 183  
 184          return $errors;
 185  
 186      }
 187  
 188      /**
 189       * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
 190       * @return bool
 191       */
 192      function signup_captcha_enabled() {
 193          global $CFG;
 194          return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');
 195      }
 196  
 197  }


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