Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 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   * User sign-up form.
  19   *
  20   * @package    core
  21   * @subpackage auth
  22   * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
  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/profile/lib.php');
  30  require_once($CFG->dirroot . '/user/editlib.php');
  31  require_once ('lib.php');
  32  
  33  class login_signup_form extends moodleform implements renderable, templatable {
  34      function definition() {
  35          global $USER, $CFG;
  36  
  37          $mform = $this->_form;
  38  
  39          $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12" autocapitalize="none"');
  40          $mform->setType('username', PARAM_RAW);
  41          $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
  42  
  43          if (!empty($CFG->passwordpolicy)){
  44              $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  45          }
  46          $mform->addElement('password', 'password', get_string('password'), [
  47              'maxlength' => 32,
  48              'size' => 12,
  49              'autocomplete' => 'new-password'
  50          ]);
  51          $mform->setType('password', core_user::get_property_type('password'));
  52          $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
  53  
  54          $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
  55          $mform->setType('email', core_user::get_property_type('email'));
  56          $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
  57          $mform->setForceLtr('email');
  58  
  59          $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
  60          $mform->setType('email2', core_user::get_property_type('email'));
  61          $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
  62          $mform->setForceLtr('email2');
  63  
  64          $namefields = useredit_get_required_name_fields();
  65          foreach ($namefields as $field) {
  66              $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
  67              $mform->setType($field, core_user::get_property_type('firstname'));
  68              $stringid = 'missing' . $field;
  69              if (!get_string_manager()->string_exists($stringid, 'moodle')) {
  70                  $stringid = 'required';
  71              }
  72              $mform->addRule($field, get_string($stringid), 'required', null, 'client');
  73          }
  74  
  75          $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
  76          $mform->setType('city', core_user::get_property_type('city'));
  77          if (!empty($CFG->defaultcity)) {
  78              $mform->setDefault('city', $CFG->defaultcity);
  79          }
  80  
  81          $country = get_string_manager()->get_list_of_countries();
  82          $default_country[''] = get_string('selectacountry');
  83          $country = array_merge($default_country, $country);
  84          $mform->addElement('select', 'country', get_string('country'), $country);
  85  
  86          if( !empty($CFG->country) ){
  87              $mform->setDefault('country', $CFG->country);
  88          }else{
  89              $mform->setDefault('country', '');
  90          }
  91  
  92          profile_signup_fields($mform);
  93  
  94          if (signup_captcha_enabled()) {
  95              $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'));
  96              $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
  97              $mform->closeHeaderBefore('recaptcha_element');
  98          }
  99  
 100          // Hook for plugins to extend form definition.
 101          core_login_extend_signup_form($mform);
 102  
 103          // Add "Agree to sitepolicy" controls. By default it is a link to the policy text and a checkbox but
 104          // it can be implemented differently in custom sitepolicy handlers.
 105          $manager = new \core_privacy\local\sitepolicy\manager();
 106          $manager->signup_form($mform);
 107  
 108          // buttons
 109          $this->set_display_vertical();
 110          $this->add_action_buttons(true, get_string('createaccount'));
 111  
 112      }
 113  
 114      function definition_after_data(){
 115          $mform = $this->_form;
 116          $mform->applyFilter('username', 'trim');
 117  
 118          // Trim required name fields.
 119          foreach (useredit_get_required_name_fields() as $field) {
 120              $mform->applyFilter($field, 'trim');
 121          }
 122      }
 123  
 124      /**
 125       * Validate user supplied data on the signup form.
 126       *
 127       * @param array $data array of ("fieldname"=>value) of submitted data
 128       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 129       * @return array of "element_name"=>"error_description" if there are errors,
 130       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 131       */
 132      public function validation($data, $files) {
 133          $errors = parent::validation($data, $files);
 134  
 135          // Extend validation for any form extensions from plugins.
 136          $errors = array_merge($errors, core_login_validate_extend_signup_form($data));
 137  
 138          if (signup_captcha_enabled()) {
 139              $recaptchaelement = $this->_form->getElement('recaptcha_element');
 140              if (!empty($this->_form->_submitValues['g-recaptcha-response'])) {
 141                  $response = $this->_form->_submitValues['g-recaptcha-response'];
 142                  if (!$recaptchaelement->verify($response)) {
 143                      $errors['recaptcha_element'] = get_string('incorrectpleasetryagain', 'auth');
 144                  }
 145              } else {
 146                  $errors['recaptcha_element'] = get_string('missingrecaptchachallengefield');
 147              }
 148          }
 149  
 150          $errors += signup_validate_data($data, $files);
 151  
 152          return $errors;
 153      }
 154  
 155      /**
 156       * Export this data so it can be used as the context for a mustache template.
 157       *
 158       * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
 159       * @return array
 160       */
 161      public function export_for_template(renderer_base $output) {
 162          ob_start();
 163          $this->display();
 164          $formhtml = ob_get_contents();
 165          ob_end_clean();
 166          $context = [
 167              'formhtml' => $formhtml
 168          ];
 169          return $context;
 170      }
 171  }