webservice MDL-20805 add missing exception string message
[moodle.git] / login / signup_form.php
CommitLineData
6c3ef410 1<?php
296c2a79 2
3require_once($CFG->libdir.'/formslib.php');
831d450e 4require_once($CFG->dirroot.'/user/profile/lib.php');
296c2a79 5
dd0feda5 6class login_signup_form extends moodleform {
6bc1e5d5 7 function definition() {
8 global $USER, $CFG;
e8a34ad4 9
6bc1e5d5 10 $mform =& $this->_form;
e8a34ad4 11
6bc1e5d5 12 $mform->addElement('header', '', get_string('createuserandpass'), '');
e8a34ad4 13
14
d4787515 15 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
6bc1e5d5 16 $mform->setType('username', PARAM_NOTAGS);
18d8136e 17 $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
e8a34ad4 18
851481dc
RW
19 if(!empty($CFG->passwordpolicy)){
20 $passwordpolicy = print_password_policy();
21 $mform->addElement('html', $passwordpolicy);
22 }
54bb33eb 23 $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
6bc1e5d5 24 $mform->setType('password', PARAM_RAW);
18d8136e 25 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
e8a34ad4 26
6bc1e5d5 27 $mform->addElement('header', '', get_string('supplyinfo'),'');
e8a34ad4 28
d4787515 29 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
6bc1e5d5 30 $mform->setType('email', PARAM_NOTAGS);
18d8136e 31 $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 32
d4787515 33 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
6bc1e5d5 34 $mform->setType('email2', PARAM_NOTAGS);
18d8136e 35 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
e8a34ad4 36
06d7c6d6 37 $nameordercheck = new object();
baacc970 38 $nameordercheck->firstname = 'a';
39 $nameordercheck->lastname = 'b';
40 if (fullname($nameordercheck) == 'b a' ) { // See MDL-4325
41 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
42 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
43 } else {
44 $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
45 $mform->addElement('text', 'lastname', get_string('lastname'), 'maxlength="100" size="30"');
46 }
47
6bc1e5d5 48 $mform->setType('firstname', PARAM_TEXT);
18d8136e 49 $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
e8a34ad4 50
6bc1e5d5 51 $mform->setType('lastname', PARAM_TEXT);
18d8136e 52 $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
e8a34ad4 53
d4787515 54 $mform->addElement('text', 'city', get_string('city'), 'maxlength="20" size="20"');
6bc1e5d5 55 $mform->setType('city', PARAM_TEXT);
18d8136e 56 $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
e8a34ad4 57
6bc1e5d5 58 $country = get_list_of_countries();
59 $default_country[''] = get_string('selectacountry');
60 $country = array_merge($default_country, $country);
61 $mform->addElement('select', 'country', get_string('country'), $country);
18d8136e 62 $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
cbc54f2c 63
64 if( !empty($CFG->country) ){
65 $mform->setDefault('country', $CFG->country);
66 }else{
67 $mform->setDefault('country', '');
68 }
f85509b5 69
5d6308d4 70 if (signup_captcha_enabled()) {
83947a36 71 $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
6dbcacee 72 $mform->setHelpButton('recaptcha_element', array('recaptcha', get_string('recaptcha', 'auth')));
5d6308d4 73 }
e8a34ad4 74
831d450e 75 profile_signup_fields($mform);
76
18d8136e 77 if (!empty($CFG->sitepolicy)) {
78 $mform->addElement('header', '', get_string('policyagreement'), '');
79 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
80 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
81 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
82 }
83
5d2b6c1c 84 // buttons
b6d15dde 85 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 86
6bc1e5d5 87 }
296c2a79 88
6bc1e5d5 89 function definition_after_data(){
90 $mform =& $this->_form;
296c2a79 91
6bc1e5d5 92 $mform->applyFilter('username', 'moodle_strtolower');
93 $mform->applyFilter('username', 'trim');
94 }
e8a34ad4 95
a78890d5 96 function validation($data, $files) {
32648682 97 global $CFG, $DB;
a78890d5 98 $errors = parent::validation($data, $files);
e8a34ad4 99
3fe6b721 100 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 101
32648682 102 if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
6bc1e5d5 103 $errors['username'] = get_string('usernameexists');
104 } else {
105 if (empty($CFG->extendedusernamechars)) {
6dbcacee 106 $string = preg_replace("~[^(-\.[:alnum:])]~i", '', $data['username']);
6bc1e5d5 107 if (strcmp($data['username'], $string)) {
108 $errors['username'] = get_string('alphanumerical');
109 }
110 }
111 }
112
113 //check if user exists in external db
114 //TODO: maybe we should check all enabled plugins instead
152bc5a2 115 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 116 $errors['username'] = get_string('usernameexists');
117 }
118
119
120 if (! validate_email($data['email'])) {
121 $errors['email'] = get_string('invalidemail');
122
32648682 123 } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
6bc1e5d5 124 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
125 }
e8a34ad4 126 if (empty($data['email2'])) {
296c2a79 127 $errors['email2'] = get_string('missingemail');
e8a34ad4 128
129 } else if ($data['email2'] != $data['email']) {
296c2a79 130 $errors['email2'] = get_string('invalidemail');
e8a34ad4 131 }
6bc1e5d5 132 if (!isset($errors['email'])) {
133 if ($err = email_is_not_allowed($data['email'])) {
134 $errors['email'] = $err;
135 }
e8a34ad4 136
6bc1e5d5 137 }
e8a34ad4 138
a78890d5 139 $errmsg = '';
83022298 140 if (!check_password_policy($data['password'], $errmsg)) {
141 $errors['password'] = $errmsg;
142 }
f85509b5 143
5d6308d4 144 if (signup_captcha_enabled()) {
f85509b5 145 $recaptcha_element = $this->_form->getElement('recaptcha_element');
146 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
147 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
148 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
149 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
150 $errors['recaptcha'] = $result;
151 }
152 } else {
153 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 154 }
155 }
e8a34ad4 156
f85509b5 157 return $errors;
158
159
6bc1e5d5 160 }
e8a34ad4 161}