MDL-55074 theme_boost: Navigation and blocks
[moodle.git] / login / signup_form.php
1 <?php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/formslib.php');
30 require_once($CFG->dirroot.'/user/profile/lib.php');
31 require_once($CFG->dirroot . '/user/editlib.php');
33 class login_signup_form extends moodleform implements renderable, templatable {
34     function definition() {
35         global $USER, $CFG;
37         $mform = $this->_form;
39         $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
42         $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
43         $mform->setType('username', PARAM_RAW);
44         $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
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', core_user::get_property_type('password'));
51         $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
53         $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
55         $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
56         $mform->setType('email', core_user::get_property_type('email'));
57         $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
58         $mform->setForceLtr('email');
60         $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
61         $mform->setType('email2', core_user::get_property_type('email'));
62         $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
63         $mform->setForceLtr('email2');
65         $namefields = useredit_get_required_name_fields();
66         foreach ($namefields as $field) {
67             $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
68             $mform->setType($field, core_user::get_property_type('firstname'));
69             $stringid = 'missing' . $field;
70             if (!get_string_manager()->string_exists($stringid, 'moodle')) {
71                 $stringid = 'required';
72             }
73             $mform->addRule($field, get_string($stringid), 'required', null, 'client');
74         }
76         $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
77         $mform->setType('city', core_user::get_property_type('city'));
78         if (!empty($CFG->defaultcity)) {
79             $mform->setDefault('city', $CFG->defaultcity);
80         }
82         $country = get_string_manager()->get_list_of_countries();
83         $default_country[''] = get_string('selectacountry');
84         $country = array_merge($default_country, $country);
85         $mform->addElement('select', 'country', get_string('country'), $country);
87         if( !empty($CFG->country) ){
88             $mform->setDefault('country', $CFG->country);
89         }else{
90             $mform->setDefault('country', '');
91         }
93         profile_signup_fields($mform);
95         if (signup_captcha_enabled()) {
96             $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'), array('https' => $CFG->loginhttps));
97             $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
98             $mform->closeHeaderBefore('recaptcha_element');
99         }
101         if (!empty($CFG->sitepolicy)) {
102             $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
103             $mform->setExpanded('policyagreement');
104             $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
105             $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
106             $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'client');
107         }
109         // buttons
110         $this->add_action_buttons(true, get_string('createaccount'));
112     }
114     function definition_after_data(){
115         $mform = $this->_form;
116         $mform->applyFilter('username', 'trim');
118         // Trim required name fields.
119         foreach (useredit_get_required_name_fields() as $field) {
120             $mform->applyFilter($field, 'trim');
121         }
122     }
124     function validation($data, $files) {
125         $errors = parent::validation($data, $files);
127         if (signup_captcha_enabled()) {
128             $recaptcha_element = $this->_form->getElement('recaptcha_element');
129             if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
130                 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
131                 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
132                 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
133                     $errors['recaptcha'] = $result;
134                 }
135             } else {
136                 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
137             }
138         }
140         $errors += signup_validate_data($data, $files);
142         return $errors;
144     }
146     /**
147      * Export this data so it can be used as the context for a mustache template.
148      *
149      * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
150      * @return array
151      */
152     public function export_for_template(renderer_base $output) {
153         ob_start();
154         $this->display();
155         $formhtml = ob_get_contents();
156         ob_end_clean();
157         $context = [
158             'formhtml' => $formhtml
159         ];
160         return $context;
161     }