signup_form.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // This file is not 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 auth_emailrut
  20. * @copyright 1999 Martin Dougiamas <dougiamas.com>
  21. * @maintainer 2020 Hackware Human <hackware.cl>
  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. // https://gist.github.com/rbarrigav/3881019
  31. public static function valid_rut($rut)
  32. {
  33. if (!preg_match('/[0-9]{7,8}-[0-9k]/', $rut)) {
  34. return false;
  35. }
  36. $dv = substr($rut, -1);
  37. $numero = substr($rut, 0, strlen($rut)-2);
  38. $i = 2;
  39. $suma = 0;
  40. foreach(array_reverse(str_split($numero)) as $v)
  41. {
  42. if($i==8)
  43. $i = 2;
  44. $suma += $v * $i;
  45. ++$i;
  46. }
  47. $dvr = 11 - ($suma % 11);
  48. if($dvr == 11)
  49. $dvr = 0;
  50. if($dvr == 10)
  51. $dvr = 'K';
  52. if($dvr == strtoupper($dv))
  53. return true;
  54. else
  55. return false;
  56. }
  57. function definition() {
  58. global $USER, $CFG;
  59. $mform = $this->_form;
  60. $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
  61. $mform->addElement('text', 'username', get_string('rut', 'auth_emailrut'), 'maxlength="10" size="12" autocapitalize="none"');
  62. $mform->setType('username', PARAM_RAW);
  63. $mform->addRule('username', get_string('missingrut', 'auth_emailrut'), 'required', null, 'client');
  64. if (!empty($CFG->passwordpolicy)){
  65. $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
  66. }
  67. $mform->addElement('password', 'password', get_string('password'), 'maxlength="32" size="12"');
  68. $mform->setType('password', core_user::get_property_type('password'));
  69. $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
  70. $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
  71. $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
  72. $mform->setType('email', core_user::get_property_type('email'));
  73. $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
  74. $mform->setForceLtr('email');
  75. $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
  76. $mform->setType('email2', core_user::get_property_type('email'));
  77. $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
  78. $mform->setForceLtr('email2');
  79. $namefields = useredit_get_required_name_fields();
  80. foreach ($namefields as $field) {
  81. $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
  82. $mform->setType($field, core_user::get_property_type('firstname'));
  83. $stringid = 'missing' . $field;
  84. if (!get_string_manager()->string_exists($stringid, 'moodle')) {
  85. $stringid = 'required';
  86. }
  87. $mform->addRule($field, get_string($stringid), 'required', null, 'client');
  88. }
  89. $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
  90. $mform->setType('city', core_user::get_property_type('city'));
  91. if (!empty($CFG->defaultcity)) {
  92. $mform->setDefault('city', $CFG->defaultcity);
  93. }
  94. $country = get_string_manager()->get_list_of_countries();
  95. $default_country[''] = get_string('selectacountry');
  96. $country = array_merge($default_country, $country);
  97. $mform->addElement('select', 'country', get_string('country'), $country);
  98. if( !empty($CFG->country) ){
  99. $mform->setDefault('country', $CFG->country);
  100. }else{
  101. $mform->setDefault('country', '');
  102. }
  103. profile_signup_fields($mform);
  104. if (signup_captcha_enabled()) {
  105. $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'));
  106. $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
  107. $mform->closeHeaderBefore('recaptcha_element');
  108. }
  109. // Hook for plugins to extend form definition.
  110. core_login_extend_signup_form($mform);
  111. // Add "Agree to sitepolicy" controls. By default it is a link to the policy text and a checkbox but
  112. // it can be implemented differently in custom sitepolicy handlers.
  113. $manager = new \core_privacy\local\sitepolicy\manager();
  114. $manager->signup_form($mform);
  115. // buttons
  116. $this->add_action_buttons(true, get_string('createaccount'));
  117. }
  118. function definition_after_data(){
  119. $mform = $this->_form;
  120. $mform->applyFilter('username', 'trim');
  121. // Trim required name fields.
  122. foreach (useredit_get_required_name_fields() as $field) {
  123. $mform->applyFilter($field, 'trim');
  124. }
  125. }
  126. /**
  127. * Validate user supplied data on the signup form.
  128. *
  129. * @param array $data array of ("fieldname"=>value) of submitted data
  130. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  131. * @return array of "element_name"=>"error_description" if there are errors,
  132. * or an empty array if everything is OK (true allowed for backwards compatibility too).
  133. */
  134. public function validation($data, $files) {
  135. $errors = parent::validation($data, $files);
  136. // Extend validation for any form extensions from plugins.
  137. $errors = array_merge($errors, core_login_validate_extend_signup_form($data));
  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. $errors += signup_validate_data($data, $files);
  150. if (!static::valid_rut($data['username'])) {
  151. $errors['username'] = get_string('invalidrut', 'auth_emailrut');
  152. };
  153. return $errors;
  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. }