123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- defined('MOODLE_INTERNAL') || die();
- require_once($CFG->libdir.'/formslib.php');
- require_once($CFG->dirroot.'/user/profile/lib.php');
- require_once($CFG->dirroot . '/user/editlib.php');
- require_once('lib.php');
- class login_signup_form extends moodleform implements renderable, templatable {
-
- public static function valid_rut($rut)
- {
- if (!preg_match('/[0-9]{7,8}-[0-9k]/', $rut)) {
- return false;
- }
- $dv = substr($rut, -1);
- $numero = substr($rut, 0, strlen($rut)-2);
- $i = 2;
- $suma = 0;
- foreach(array_reverse(str_split($numero)) as $v)
- {
- if($i==8)
- $i = 2;
- $suma += $v * $i;
- ++$i;
- }
- $dvr = 11 - ($suma % 11);
-
- if($dvr == 11)
- $dvr = 0;
- if($dvr == 10)
- $dvr = 'K';
- if($dvr == strtoupper($dv))
- return true;
- else
- return false;
- }
- function definition() {
- global $USER, $CFG;
- $mform = $this->_form;
- $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
- $mform->addElement('text', 'username', get_string('rut', 'auth_emailrut'), 'maxlength="10" size="12" autocapitalize="none"');
- $mform->setType('username', PARAM_RAW);
- $mform->addRule('username', get_string('missingrut', 'auth_emailrut'), 'required', null, 'client');
- if (!empty($CFG->passwordpolicy)){
- $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
- }
- $mform->addElement('password', 'password', get_string('password'), 'maxlength="32" size="12"');
- $mform->setType('password', core_user::get_property_type('password'));
- $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
- $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
- $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
- $mform->setType('email', core_user::get_property_type('email'));
- $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
- $mform->setForceLtr('email');
- $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
- $mform->setType('email2', core_user::get_property_type('email'));
- $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
- $mform->setForceLtr('email2');
- $namefields = useredit_get_required_name_fields();
- foreach ($namefields as $field) {
- $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
- $mform->setType($field, core_user::get_property_type('firstname'));
- $stringid = 'missing' . $field;
- if (!get_string_manager()->string_exists($stringid, 'moodle')) {
- $stringid = 'required';
- }
- $mform->addRule($field, get_string($stringid), 'required', null, 'client');
- }
- $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
- $mform->setType('city', core_user::get_property_type('city'));
- if (!empty($CFG->defaultcity)) {
- $mform->setDefault('city', $CFG->defaultcity);
- }
- $country = get_string_manager()->get_list_of_countries();
- $default_country[''] = get_string('selectacountry');
- $country = array_merge($default_country, $country);
- $mform->addElement('select', 'country', get_string('country'), $country);
- if( !empty($CFG->country) ){
- $mform->setDefault('country', $CFG->country);
- }else{
- $mform->setDefault('country', '');
- }
- profile_signup_fields($mform);
- if (signup_captcha_enabled()) {
- $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'));
- $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
- $mform->closeHeaderBefore('recaptcha_element');
- }
-
- core_login_extend_signup_form($mform);
-
-
- $manager = new \core_privacy\local\sitepolicy\manager();
- $manager->signup_form($mform);
-
- $this->add_action_buttons(true, get_string('createaccount'));
- }
- function definition_after_data(){
- $mform = $this->_form;
- $mform->applyFilter('username', 'trim');
-
- foreach (useredit_get_required_name_fields() as $field) {
- $mform->applyFilter($field, 'trim');
- }
- }
-
- public function validation($data, $files) {
- $errors = parent::validation($data, $files);
-
- $errors = array_merge($errors, core_login_validate_extend_signup_form($data));
- if (signup_captcha_enabled()) {
- $recaptchaelement = $this->_form->getElement('recaptcha_element');
- if (!empty($this->_form->_submitValues['g-recaptcha-response'])) {
- $response = $this->_form->_submitValues['g-recaptcha-response'];
- if (!$recaptchaelement->verify($response)) {
- $errors['recaptcha_element'] = get_string('incorrectpleasetryagain', 'auth');
- }
- } else {
- $errors['recaptcha_element'] = get_string('missingrecaptchachallengefield');
- }
- }
- $errors += signup_validate_data($data, $files);
- if (!static::valid_rut($data['username'])) {
- $errors['username'] = get_string('invalidrut', 'auth_emailrut');
- };
- return $errors;
- }
-
- public function export_for_template(renderer_base $output) {
- ob_start();
- $this->display();
- $formhtml = ob_get_contents();
- ob_end_clean();
- $context = [
- 'formhtml' => $formhtml
- ];
- return $context;
- }
- }
|