Merge branch 'master-MDL-31028_2' of git://github.com/chtombleson/moodle
[moodle.git] / login / signup_form.php
blobec230c1be6bcda4d2701113dddf4ac0c80c57597
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.
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.
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
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/formslib.php');
30 require_once($CFG->dirroot.'/user/profile/lib.php');
32 class login_signup_form extends moodleform {
33 function definition() {
34 global $USER, $CFG;
36 $mform = $this->_form;
38 $mform->addElement('header', '', get_string('createuserandpass'), '');
41 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
42 $mform->setType('username', PARAM_NOTAGS);
43 $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
45 if (!empty($CFG->passwordpolicy)){
46 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
48 $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
49 $mform->setType('password', PARAM_RAW);
50 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
52 $mform->addElement('header', '', get_string('supplyinfo'),'');
54 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
55 $mform->setType('email', PARAM_NOTAGS);
56 $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
58 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
59 $mform->setType('email2', PARAM_NOTAGS);
60 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
62 $nameordercheck = new stdClass();
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"');
73 $mform->setType('firstname', PARAM_TEXT);
74 $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
76 $mform->setType('lastname', PARAM_TEXT);
77 $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
79 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
80 $mform->setType('city', PARAM_TEXT);
81 $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
82 if (!empty($CFG->defaultcity)) {
83 $mform->setDefault('city', $CFG->defaultcity);
86 $country = get_string_manager()->get_list_of_countries();
87 $default_country[''] = get_string('selectacountry');
88 $country = array_merge($default_country, $country);
89 $mform->addElement('select', 'country', get_string('country'), $country);
90 $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
92 if( !empty($CFG->country) ){
93 $mform->setDefault('country', $CFG->country);
94 }else{
95 $mform->setDefault('country', '');
98 if ($this->signup_captcha_enabled()) {
99 $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
100 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
103 profile_signup_fields($mform);
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');
112 // buttons
113 $this->add_action_buttons(true, get_string('createaccount'));
117 function definition_after_data(){
118 $mform = $this->_form;
119 $mform->applyFilter('username', 'trim');
122 function validation($data, $files) {
123 global $CFG, $DB;
124 $errors = parent::validation($data, $files);
126 $authplugin = get_auth_plugin($CFG->registerauth);
128 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
129 $errors['username'] = get_string('usernameexists');
130 } else {
131 //check allowed characters
132 if ($data['username'] !== textlib::strtolower($data['username'])) {
133 $errors['username'] = get_string('usernamelowercase');
134 } else {
135 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
136 $errors['username'] = get_string('invalidusername');
142 //check if user exists in external db
143 //TODO: maybe we should check all enabled plugins instead
144 if ($authplugin->user_exists($data['username'])) {
145 $errors['username'] = get_string('usernameexists');
149 if (! validate_email($data['email'])) {
150 $errors['email'] = get_string('invalidemail');
152 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
153 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
155 if (empty($data['email2'])) {
156 $errors['email2'] = get_string('missingemail');
158 } else if ($data['email2'] != $data['email']) {
159 $errors['email2'] = get_string('invalidemail');
161 if (!isset($errors['email'])) {
162 if ($err = email_is_not_allowed($data['email'])) {
163 $errors['email'] = $err;
168 $errmsg = '';
169 if (!check_password_policy($data['password'], $errmsg)) {
170 $errors['password'] = $errmsg;
173 if ($this->signup_captcha_enabled()) {
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;
181 } else {
182 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
185 // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
186 $dataobject = (object)$data;
187 $dataobject->id = 0;
188 $errors += profile_validation($dataobject, $files);
190 return $errors;
195 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
196 * @return bool
198 function signup_captcha_enabled() {
199 global $CFG;
200 return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');