PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/login/signup_form.php

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