fixed [MDL-13181]
[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 }
e8a34ad4 65
831d450e 66 profile_signup_fields($mform);
67
18d8136e 68 if (!empty($CFG->sitepolicy)) {
69 $mform->addElement('header', '', get_string('policyagreement'), '');
70 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
71 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
72 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
73 }
74
5d2b6c1c 75 // buttons
b6d15dde 76 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 77
6bc1e5d5 78 }
296c2a79 79
6bc1e5d5 80 function definition_after_data(){
81 $mform =& $this->_form;
296c2a79 82
6bc1e5d5 83 $mform->applyFilter('username', 'moodle_strtolower');
84 $mform->applyFilter('username', 'trim');
85 }
e8a34ad4 86
a78890d5 87 function validation($data, $files) {
6bc1e5d5 88 global $CFG;
a78890d5 89 $errors = parent::validation($data, $files);
e8a34ad4 90
3fe6b721 91 $authplugin = get_auth_plugin($CFG->registerauth);
e8a34ad4 92
6bc1e5d5 93 if (record_exists('user', 'username', $data['username'], 'mnethostid', $CFG->mnet_localhost_id)) {
94 $errors['username'] = get_string('usernameexists');
95 } else {
96 if (empty($CFG->extendedusernamechars)) {
97 $string = eregi_replace("[^(-\.[:alnum:])]", '', $data['username']);
98 if (strcmp($data['username'], $string)) {
99 $errors['username'] = get_string('alphanumerical');
100 }
101 }
102 }
103
104 //check if user exists in external db
105 //TODO: maybe we should check all enabled plugins instead
152bc5a2 106 if ($authplugin->user_exists($data['username'])) {
6bc1e5d5 107 $errors['username'] = get_string('usernameexists');
108 }
109
110
111 if (! validate_email($data['email'])) {
112 $errors['email'] = get_string('invalidemail');
113
114 } else if (record_exists('user', 'email', $data['email'])) {
115 $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
116 }
e8a34ad4 117 if (empty($data['email2'])) {
296c2a79 118 $errors['email2'] = get_string('missingemail');
e8a34ad4 119
120 } else if ($data['email2'] != $data['email']) {
296c2a79 121 $errors['email2'] = get_string('invalidemail');
e8a34ad4 122 }
6bc1e5d5 123 if (!isset($errors['email'])) {
124 if ($err = email_is_not_allowed($data['email'])) {
125 $errors['email'] = $err;
126 }
e8a34ad4 127
6bc1e5d5 128 }
e8a34ad4 129
a78890d5 130 $errmsg = '';
83022298 131 if (!check_password_policy($data['password'], $errmsg)) {
132 $errors['password'] = $errmsg;
133 }
e8a34ad4 134
a78890d5 135 return $errors;
e8a34ad4 136
137
6bc1e5d5 138 }
e8a34ad4 139}
140
141?>