MDL-38435 form: Sections can be expanded by default
[moodle.git] / login / signup_form.php
CommitLineData
6c3ef410 1<?php
296c2a79 2
c30949a9
PS
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/>.
17
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 */
26
27defined('MOODLE_INTERNAL') || die();
bfebaf64 28
296c2a79 29require_once($CFG->libdir.'/formslib.php');
831d450e 30require_once($CFG->dirroot.'/user/profile/lib.php');
296c2a79 31
dd0feda5 32class login_signup_form extends moodleform {
6bc1e5d5 33 function definition() {
34 global $USER, $CFG;
e8a34ad4 35
c30949a9 36 $mform = $this->_form;
e8a34ad4 37
6bc1e5d5 38 $mform->addElement('header', '', get_string('createuserandpass'), '');
e8a34ad4 39
40
d4787515 41 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
6bc1e5d5 42 $mform->setType('username', PARAM_NOTAGS);
18d8136e 43 $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
e8a34ad4 44
3bf11ca4
DM
45 if (!empty($CFG->passwordpolicy)){
46 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 47 }
54bb33eb 48 $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
6bc1e5d5 49 $mform->setType('password', PARAM_RAW);
18d8136e 50 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
e8a34ad4 51
6bc1e5d5 52 $mform->addElement('header', '', get_string('supplyinfo'),'');
e8a34ad4 53
d4787515 54 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
6bc1e5d5 55 $mform->setType('email', PARAM_NOTAGS);
18d8136e 56 $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 57
d4787515 58 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
6bc1e5d5 59 $mform->setType('email2', PARAM_NOTAGS);
18d8136e 60 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 61
8e803c3f 62 $nameordercheck = new stdClass();
baacc970 63 $nameordercheck->firstname = 'a';
64 $nameordercheck->lastname = 'b';
65 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
66 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
67 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
68 } else {
69 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
70 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
71 }
72
6bc1e5d5 73 $mform->setType('firstname', PARAM_TEXT);
18d8136e 74 $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
e8a34ad4 75
6bc1e5d5 76 $mform->setType('lastname', PARAM_TEXT);
18d8136e 77 $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
e8a34ad4 78
c5c0d2ff 79 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
6bc1e5d5 80 $mform->setType('city', PARAM_TEXT);
18d8136e 81 $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
fa7f750c
PS
82 if (!empty($CFG->defaultcity)) {
83 $mform->setDefault('city', $CFG->defaultcity);
84 }
e8a34ad4 85
0aa759b0 86 $country = get_string_manager()->get_list_of_countries();
6bc1e5d5 87 $default_country[''] = get_string('selectacountry');
88 $country = array_merge($default_country, $country);
89 $mform->addElement('select', 'country', get_string('country'), $country);
18d8136e 90 $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
cbc54f2c 91
92 if( !empty($CFG->country) ){
93 $mform->setDefault('country', $CFG->country);
94 }else{
95 $mform->setDefault('country', '');
96 }
f85509b5 97
c30949a9 98 if ($this->signup_captcha_enabled()) {
83947a36 99 $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
8db551df 100 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5d6308d4 101 }
e8a34ad4 102
831d450e 103 profile_signup_fields($mform);
104
18d8136e 105 if (!empty($CFG->sitepolicy)) {
106 $mform->addElement('header', '', get_string('policyagreement'), '');
107 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
108 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
109 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
110 }
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');
120 }
e8a34ad4 121
a78890d5 122 function validation($data, $files) {
32648682 123 global $CFG, $DB;
a78890d5 124 $errors = parent::validation($data, $files);
e8a34ad4 125
3fe6b721 126 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 127
32648682 128 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
6bc1e5d5 129 $errors['username'] = get_string('usernameexists');
130 } else {
07ed083e 131 //check allowed characters
6f3451e5 132 if ($data['username'] !== textlib::strtolower($data['username'])) {
07ed083e 133 $errors['username'] = get_string('usernamelowercase');
6b8ad965
PS
134 } else {
135 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
79604225 136 $errors['username'] = get_string('invalidusername');
6bc1e5d5 137 }
6b8ad965 138
6bc1e5d5 139 }
140 }
141
142 //check if user exists in external db
143 //TODO: maybe we should check all enabled plugins instead
152bc5a2 144 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 145 $errors['username'] = get_string('usernameexists');
146 }
147
148
149 if (! validate_email($data['email'])) {
150 $errors['email'] = get_string('invalidemail');
151
32648682 152 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
6bc1e5d5 153 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
154 }
e8a34ad4 155 if (empty($data['email2'])) {
296c2a79 156 $errors['email2'] = get_string('missingemail');
e8a34ad4 157
158 } else if ($data['email2'] != $data['email']) {
296c2a79 159 $errors['email2'] = get_string('invalidemail');
e8a34ad4 160 }
6bc1e5d5 161 if (!isset($errors['email'])) {
162 if ($err = email_is_not_allowed($data['email'])) {
163 $errors['email'] = $err;
164 }
e8a34ad4 165
6bc1e5d5 166 }
e8a34ad4 167
a78890d5 168 $errmsg = '';
83022298 169 if (!check_password_policy($data['password'], $errmsg)) {
170 $errors['password'] = $errmsg;
171 }
f85509b5 172
c30949a9 173 if ($this->signup_captcha_enabled()) {
f85509b5 174 $recaptcha_element = $this->_form->getElement('recaptcha_element');
175 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
176 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
177 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
178 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
179 $errors['recaptcha'] = $result;
180 }
181 } else {
182 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 183 }
184 }
c862e2f4 185 // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
9247265b 186 $dataobject = (object)$data;
c862e2f4 187 $dataobject->id = 0;
9247265b 188 $errors += profile_validation($dataobject, $files);
e8a34ad4 189
f85509b5 190 return $errors;
191
c30949a9 192 }
f85509b5 193
c30949a9
PS
194 /**
195 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
196 * @return bool
197 */
198 function signup_captcha_enabled() {
199 global $CFG;
200 return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');
6bc1e5d5 201 }
c30949a9 202
e8a34ad4 203}