migrated signup.php over to using new formslib
[moodle.git] / login / signup_form.php
1 <?php
2 include_once $CFG->libdir.'/formslib.php';
3 class login_signup_form extends moodleform {
4         function definition() {
5                 global $USER, $CFG;
7                 $mform    =& $this->_form;
8                 $renderer =& $mform->defaultRenderer();
10                 $mform->addElement('header', '', get_string("createuserandpass"),'');
13                 $mform->addElement('text', 'username', get_string("username"),'size="12"');
14                 $mform->setType('username', PARAM_NOTAGS);
15                 $mform->addRule('username', get_string("missingusername"), 'required', null, 'client');
17                 $mform->addElement('password', 'password', get_string("password"), 'size="12"');
18                 $mform->setType('password', PARAM_RAW);
19                 $mform->addRule('password', get_string("missingpassword"), 'required', null, 'client');
21                 $mform->addElement('header', '', get_string('supplyinfo'),'');
23                 $mform->addElement('text', 'email', get_string("email"), 'size="25"');
24                 $mform->setType('email', PARAM_NOTAGS);
25                 $mform->addRule('email', get_string("missingemail"), 'required', null, 'client');
27                 $mform->addElement('text', 'email2', get_string("emailagain"), 'size="25"');
28                 $mform->setType('email2', PARAM_NOTAGS);
29                 $mform->addRule('email2', get_string("missingemail"), 'required', null, 'client');
31                 $mform->addElement('text', 'firstname', get_string("firstname"), 'size="25"');
32                 $mform->setType('firstname', PARAM_TEXT);
33                 $mform->addRule('firstname', get_string("missingfirstname"), 'required', null, 'client');
35                 $mform->addElement('text', 'lastname', get_string("lastname"), 'size="25"');
36                 $mform->setType('lastname', PARAM_TEXT);
37                 $mform->addRule('lastname', get_string("missinglastname"), 'required', null, 'client');
39                 $mform->addElement('text', 'city', get_string("city"), 'size="25"');
40                 $mform->setType('city', PARAM_TEXT);
41                 $mform->addRule('city', get_string("missingcity"), 'required', null, 'client');
43                 $country=get_list_of_countries();
44                 $default_country['']=get_string("selectacountry");
45                 $country=array_merge($default_country,$country);
46                 $mform->setDefault('country', '');
47                 $mform->addElement('select', 'country', get_string("country"), $country);
48                 $mform->addRule('country', get_string("missingcountry"), 'required', null, 'client');
50                 $mform->addElement('submit', 'submit', get_string("createaccount"));
52                 $renderer->addStopFieldsetElements('submit');
53         }
54         function definition_after_data(){
55                 $mform    =& $this->_form;
56                 $mform->applyFilter('username', 'moodle_strtolower');
57                 $mform->applyFilter('username', 'trim');
58         }
60         function validation($data) {
61                 global $CFG;
62                 $errors=array();
65                 if (record_exists("user", "username", $data['username'])){
66                         $errors['username'] = get_string("usernameexists");
67                 } else {
68                         if (empty($CFG->extendedusernamechars)) {
69                                 $string = eregi_replace("[^(-\.[:alnum:])]", "", $data['username']);
70                                 if (strcmp($data['username'], $string)) {
71                                         $errors['username'] = get_string("alphanumerical");
72                                 }
73                         }
74                 }
75                 if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){
76                         if (auth_user_exists($data['username'])) {
77                                 $errors['username'] = get_string("usernameexists");
78                         }
79                 }
82                 if (! validate_email($data['email'])) {
83                         $errors['email'] = get_string("invalidemail");
85                 }else if (record_exists("user", "email", $data['email'])) {
86                         $errors['email'] = get_string("emailexists")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
87                 }
88         if (empty($data['email2'])) {
89             $errors['email2'] = get_string("missingemail");
91         } else if ($data['email2'] != $data['email']) {
92             $errors['email2'] = get_string("invalidemail");
93         }
94                 if (!isset($errors['email'])) {
95                         if ($err = email_is_not_allowed($data['email'])) {
96                                 $errors['email'] = $err;
97                         }
99                 }
102                 if (0 == count($errors)){
103                         return true;
104                 } else {
105                         return $errors;
106                 }
109         }
112 ?>