3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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() {
36 $mform = $this->_form
;
38 $mform->addElement('header', 'createuserandpass', 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', 'supplyinfo', 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"');
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
);
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', 'policyagreement', get_string('policyagreement'), '');
107 $mform->setExpanded('policyagreement');
108 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy
.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
109 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
110 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
114 $this->add_action_buttons(true, get_string('createaccount'));
118 function definition_after_data(){
119 $mform = $this->_form
;
120 $mform->applyFilter('username', 'trim');
123 function validation($data, $files) {
125 $errors = parent
::validation($data, $files);
127 $authplugin = get_auth_plugin($CFG->registerauth
);
129 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id
))) {
130 $errors['username'] = get_string('usernameexists');
132 //check allowed characters
133 if ($data['username'] !== textlib
::strtolower($data['username'])) {
134 $errors['username'] = get_string('usernamelowercase');
136 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME
)) {
137 $errors['username'] = get_string('invalidusername');
143 //check if user exists in external db
144 //TODO: maybe we should check all enabled plugins instead
145 if ($authplugin->user_exists($data['username'])) {
146 $errors['username'] = get_string('usernameexists');
150 if (! validate_email($data['email'])) {
151 $errors['email'] = get_string('invalidemail');
153 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
154 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
156 if (empty($data['email2'])) {
157 $errors['email2'] = get_string('missingemail');
159 } else if ($data['email2'] != $data['email']) {
160 $errors['email2'] = get_string('invalidemail');
162 if (!isset($errors['email'])) {
163 if ($err = email_is_not_allowed($data['email'])) {
164 $errors['email'] = $err;
170 if (!check_password_policy($data['password'], $errmsg)) {
171 $errors['password'] = $errmsg;
174 if ($this->signup_captcha_enabled()) {
175 $recaptcha_element = $this->_form
->getElement('recaptcha_element');
176 if (!empty($this->_form
->_submitValues
['recaptcha_challenge_field'])) {
177 $challenge_field = $this->_form
->_submitValues
['recaptcha_challenge_field'];
178 $response_field = $this->_form
->_submitValues
['recaptcha_response_field'];
179 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
180 $errors['recaptcha'] = $result;
183 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
186 // Validate customisable profile fields. (profile_validation expects an object as the parameter with userid set)
187 $dataobject = (object)$data;
189 $errors +
= profile_validation($dataobject, $files);
196 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
199 function signup_captcha_enabled() {
201 return !empty($CFG->recaptchapublickey
) && !empty($CFG->recaptchaprivatekey
) && get_config('auth/email', 'recaptcha');