Automatic installer lang files (20100826)
[moodle.git] / login / signup_form.php
blobd4f1bdf0aacac9ce947ead46d6a8146ecee8e44d
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 object();
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');
83 $country = get_string_manager()->get_list_of_countries();
84 $default_country[''] = get_string('selectacountry');
85 $country = array_merge($default_country, $country);
86 $mform->addElement('select', 'country', get_string('country'), $country);
87 $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
89 if( !empty($CFG->country) ){
90 $mform->setDefault('country', $CFG->country);
91 }else{
92 $mform->setDefault('country', '');
95 if ($this->signup_captcha_enabled()) {
96 $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
97 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
100 profile_signup_fields($mform);
102 if (!empty($CFG->sitepolicy)) {
103 $mform->addElement('header', '', get_string('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, 'server');
109 // buttons
110 $this->add_action_buttons(true, get_string('createaccount'));
114 function definition_after_data(){
115 $mform = $this->_form;
116 $mform->applyFilter('username', 'trim');
119 function validation($data, $files) {
120 global $CFG, $DB;
121 $errors = parent::validation($data, $files);
123 $authplugin = get_auth_plugin($CFG->registerauth);
125 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
126 $errors['username'] = get_string('usernameexists');
127 } else {
128 //check allowed characters
129 if ($data['username'] !== moodle_strtolower($data['username'])) {
130 $errors['username'] = get_string('usernamelowercase');
131 } else {
132 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
133 $errors['username'] = get_string('invalidusername');
139 //check if user exists in external db
140 //TODO: maybe we should check all enabled plugins instead
141 if ($authplugin->user_exists($data['username'])) {
142 $errors['username'] = get_string('usernameexists');
146 if (! validate_email($data['email'])) {
147 $errors['email'] = get_string('invalidemail');
149 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
150 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
152 if (empty($data['email2'])) {
153 $errors['email2'] = get_string('missingemail');
155 } else if ($data['email2'] != $data['email']) {
156 $errors['email2'] = get_string('invalidemail');
158 if (!isset($errors['email'])) {
159 if ($err = email_is_not_allowed($data['email'])) {
160 $errors['email'] = $err;
165 $errmsg = '';
166 if (!check_password_policy($data['password'], $errmsg)) {
167 $errors['password'] = $errmsg;
170 if ($this->signup_captcha_enabled()) {
171 $recaptcha_element = $this->_form->getElement('recaptcha_element');
172 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
173 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
174 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
175 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
176 $errors['recaptcha'] = $result;
178 } else {
179 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
183 return $errors;
188 * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
189 * @return bool
191 function signup_captcha_enabled() {
192 global $CFG;
193 return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');