weekly release 4.0dev
[moodle.git] / login / signup_form.php
CommitLineData
6c3ef410 1<?php
c30949a9
PS
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
26defined('MOODLE_INTERNAL') || die();
bfebaf64 27
296c2a79 28require_once($CFG->libdir.'/formslib.php');
831d450e 29require_once($CFG->dirroot.'/user/profile/lib.php');
0859a08d 30require_once($CFG->dirroot . '/user/editlib.php');
dc25b71d 31require_once('lib.php');
296c2a79 32
99061152 33class login_signup_form extends moodleform implements renderable, templatable {
6bc1e5d5 34 function definition() {
35 global $USER, $CFG;
e8a34ad4 36
c30949a9 37 $mform = $this->_form;
e8a34ad4 38
807cf3d0 39 $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
e8a34ad4 40
7be51d33 41 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12" autocapitalize="none"');
90d8bf59 42 $mform->setType('username', PARAM_RAW);
bd7a0fd3 43 $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
e8a34ad4 44
3bf11ca4
DM
45 if (!empty($CFG->passwordpolicy)){
46 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 47 }
bc76ab47
SS
48 $mform->addElement('password', 'password', get_string('password'), [
49 'maxlength' => 32,
50 'size' => 12,
51 'autocomplete' => 'new-password'
52 ]);
ac9768fc 53 $mform->setType('password', core_user::get_property_type('password'));
bd7a0fd3 54 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
e8a34ad4 55
807cf3d0 56 $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
e8a34ad4 57
d4787515 58 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
ac9768fc 59 $mform->setType('email', core_user::get_property_type('email'));
bd7a0fd3 60 $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
525ef9c8 61 $mform->setForceLtr('email');
e8a34ad4 62
d4787515 63 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
ac9768fc 64 $mform->setType('email2', core_user::get_property_type('email'));
bd7a0fd3 65 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
525ef9c8 66 $mform->setForceLtr('email2');
e8a34ad4 67
0859a08d
AA
68 $namefields = useredit_get_required_name_fields();
69 foreach ($namefields as $field) {
70 $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
ac9768fc 71 $mform->setType($field, core_user::get_property_type('firstname'));
0859a08d
AA
72 $stringid = 'missing' . $field;
73 if (!get_string_manager()->string_exists($stringid, 'moodle')) {
74 $stringid = 'required';
75 }
bd7a0fd3 76 $mform->addRule($field, get_string($stringid), 'required', null, 'client');
baacc970 77 }
78
c5c0d2ff 79 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
ac9768fc 80 $mform->setType('city', core_user::get_property_type('city'));
fa7f750c
PS
81 if (!empty($CFG->defaultcity)) {
82 $mform->setDefault('city', $CFG->defaultcity);
83 }
e8a34ad4 84
0aa759b0 85 $country = get_string_manager()->get_list_of_countries();
6bc1e5d5 86 $default_country[''] = get_string('selectacountry');
87 $country = array_merge($default_country, $country);
88 $mform->addElement('select', 'country', get_string('country'), $country);
cbc54f2c 89
90 if( !empty($CFG->country) ){
91 $mform->setDefault('country', $CFG->country);
92 }else{
93 $mform->setDefault('country', '');
94 }
f85509b5 95
d83318f1
ME
96 profile_signup_fields($mform);
97
574b9d86 98 if (signup_captcha_enabled()) {
b58764ff 99 $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'));
8db551df 100 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5e91f96c 101 $mform->closeHeaderBefore('recaptcha_element');
5d6308d4 102 }
e8a34ad4 103
dc25b71d
PB
104 // Hook for plugins to extend form definition.
105 core_login_extend_signup_form($mform);
106
1727c939
MG
107 // Add "Agree to sitepolicy" controls. By default it is a link to the policy text and a checkbox but
108 // it can be implemented differently in custom sitepolicy handlers.
109 $manager = new \core_privacy\local\sitepolicy\manager();
110 $manager->signup_form($mform);
18d8136e 111
5d2b6c1c 112 // buttons
b6d15dde 113 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 114
6bc1e5d5 115 }
296c2a79 116
6bc1e5d5 117 function definition_after_data(){
c30949a9 118 $mform = $this->_form;
6bc1e5d5 119 $mform->applyFilter('username', 'trim');
e9c27d1e
DM
120
121 // Trim required name fields.
122 foreach (useredit_get_required_name_fields() as $field) {
123 $mform->applyFilter($field, 'trim');
124 }
6bc1e5d5 125 }
e8a34ad4 126
83fa59a3
JW
127 /**
128 * Validate user supplied data on the signup form.
129 *
130 * @param array $data array of ("fieldname"=>value) of submitted data
131 * @param array $files array of uploaded files "element_name"=>tmp_file_path
132 * @return array of "element_name"=>"error_description" if there are errors,
133 * or an empty array if everything is OK (true allowed for backwards compatibility too).
134 */
135 public function validation($data, $files) {
a78890d5 136 $errors = parent::validation($data, $files);
e8a34ad4 137
dc25b71d
PB
138 // Extend validation for any form extensions from plugins.
139 $errors = array_merge($errors, core_login_validate_extend_signup_form($data));
140
574b9d86 141 if (signup_captcha_enabled()) {
83fa59a3
JW
142 $recaptchaelement = $this->_form->getElement('recaptcha_element');
143 if (!empty($this->_form->_submitValues['g-recaptcha-response'])) {
144 $response = $this->_form->_submitValues['g-recaptcha-response'];
145 if (!$recaptchaelement->verify($response)) {
477ab6f8 146 $errors['recaptcha_element'] = get_string('incorrectpleasetryagain', 'auth');
f85509b5 147 }
148 } else {
477ab6f8 149 $errors['recaptcha_element'] = get_string('missingrecaptchachallengefield');
5d6308d4 150 }
151 }
e8a34ad4 152
574b9d86 153 $errors += signup_validate_data($data, $files);
f85509b5 154
574b9d86 155 return $errors;
6bc1e5d5 156 }
99061152
DW
157
158 /**
159 * Export this data so it can be used as the context for a mustache template.
160 *
161 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
162 * @return array
163 */
164 public function export_for_template(renderer_base $output) {
165 ob_start();
166 $this->display();
167 $formhtml = ob_get_contents();
168 ob_end_clean();
169 $context = [
170 'formhtml' => $formhtml
171 ];
172 return $context;
173 }
e8a34ad4 174}