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