From: jamiesensei Date: Tue, 24 Oct 2006 11:10:48 +0000 (+0000) Subject: migrated signup.php over to using new formslib X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e8a34ad424646468ec6d7c19bfaa9fa8f0c653d7;p=moodle.git migrated signup.php over to using new formslib --- diff --git a/login/signup.php b/login/signup.php index fdb73cca5f..057c5f9563 100644 --- a/login/signup.php +++ b/login/signup.php @@ -5,62 +5,48 @@ //HTTPS is potentially required in this page httpsrequired(); + include("signup_form.php"); + $mform_signup = new login_signup_form('signup.php',''); if ($CFG->auth != 'email' and (empty($CFG->auth_user_create) or !(function_exists('auth_user_create'))) ) { error("Sorry, you may not use this page."); } - if ($user = data_submitted()) { - - $user->firstname = strip_tags($user->firstname); - $user->lastname = strip_tags($user->lastname); - $user->email = strip_tags($user->email); - - validate_form($user, $err); - $user->username= trim(moodle_strtolower($user->username)); - - if (count((array)$err) == 0) { - $plainpass = $user->password; - $user->password = hash_internal_user_password($plainpass); - $user->confirmed = 0; - $user->lang = current_language(); - $user->firstaccess = time(); - $user->secret = random_string(15); - $user->auth = $CFG->auth; - if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){ - if (! auth_user_exists($user->username)) { - if (! auth_user_create($user,$plainpass)) { - error("Could not add user to authentication module!"); - } - } else { - error("User already exists on authentication database."); + if ($fromform = $mform_signup->data_submitted()) { + + $plainpass = $fromform->password; + $fromform->password = hash_internal_user_password($plainpass); + $fromform->confirmed = 0; + $fromform->lang = current_language(); + $fromform->firstaccess = time(); + $fromform->secret = random_string(15); + $fromform->auth = $CFG->auth; + if (!empty($CFG->auth_user_create) and function_exists('auth_user_create') ){ + if (! auth_user_exists($fromform->username)) { + if (! auth_user_create($fromform,$plainpass)) { + error("Could not add user to authentication module!"); } + } else { + error("User already exists on authentication database."); } + } - if (! ($user->id = insert_record("user", $user)) ) { - error("Could not add your record to the database!"); - } + if (! ($fromform->id = insert_record("user", $fromform)) ) { + error("Could not add your record to the database!"); + } - if (! send_confirmation_email($user)) { - error("Tried to send you an email but failed!"); - } - - $emailconfirm = get_string("emailconfirm"); - print_header($emailconfirm, $emailconfirm, $emailconfirm); - notice(get_string("emailconfirmsent", "", $user->email), "$CFG->wwwroot/index.php"); - exit; + if (! send_confirmation_email($fromform)) { + error("Tried to send you an email but failed!"); } - } - if (!empty($err)) { - $focus = "form.".array_shift($temparr = array_flip(get_object_vars($err))); - } else { - $focus = ""; + $emailconfirm = get_string("emailconfirm"); + print_header($emailconfirm, $emailconfirm, $emailconfirm); + notice(get_string("emailconfirmsent", "", $fromform->email), "$CFG->wwwroot/index.php"); + exit; } - if (empty($user->country) and !empty($CFG->country)) { - $user->country = $CFG->country; - } + + $newaccount = get_string("newaccount"); $login = get_string("login"); @@ -72,91 +58,12 @@ $langs = get_list_of_languages(); $langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true); } + print_header($newaccount, $newaccount, "$login -> $newaccount", $mform_signup->focus(), "", true, "
$langmenu
"); - print_header($newaccount, $newaccount, "$login -> $newaccount", $focus, "", true, "
$langmenu
"); - include("signup_form.html"); + $mform_signup->display(); print_footer(); -/****************************************************************************** - * FUNCTIONS - *****************************************************************************/ - -function validate_form($user, &$err) { - global $CFG; - - if (empty($user->username)){ - $err->username = get_string("missingusername"); - } else{ - $user->username = trim(moodle_strtolower($user->username)); - if (record_exists("user", "username", $user->username)){ - $err->username = get_string("usernameexists"); - } else { - if (empty($CFG->extendedusernamechars)) { - $string = eregi_replace("[^(-\.[:alnum:])]", "", $user->username); - if (strcmp($user->username, $string)) { - $err->username = get_string("alphanumerical"); - } - } - } - } - - if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){ - if (auth_user_exists($user->username)) { - $err->username = get_string("usernameexists"); - } - } - - - if (empty($user->password)) { - $err->password = get_string("missingpassword"); - } - - if (empty($user->firstname)) { - $err->firstname = get_string("missingfirstname"); - } - - if (empty($user->lastname)) { - $err->lastname = get_string("missinglastname"); - } - - - if (empty($user->email)) { - $err->email = get_string("missingemail"); - - } else if (! validate_email($user->email)) { - $err->email = get_string("invalidemail"); - - } else if (record_exists("user", "email", $user->email)) { - $err->email = get_string("emailexists")." ".get_string("newpassword")."?"; - } - - - if (empty($user->email2)) { - $err->email2 = get_string("missingemail"); - - } else if ($user->email2 != $user->email) { - $err->email2 = get_string("invalidemail"); - } - - - if (empty($user->city)) { - $err->city = get_string("missingcity"); - } - - if (empty($user->country)) { - $err->country = get_string("missingcountry"); - } - - if (empty($err->email)) { - if ($error = email_is_not_allowed($user->email)) { - $err->email = $error; - } - } - - return; -} - -?> +?> \ No newline at end of file diff --git a/login/signup_form.html b/login/signup_form.html deleted file mode 100644 index cea92e1301..0000000000 --- a/login/signup_form.html +++ /dev/null @@ -1,93 +0,0 @@ -username)) { - $user->username = ""; - } - if (empty($user->password)) { - $user->password = ""; - } - if (empty($user->email)) { - $user->email = ""; - } - if (empty($user->email2)) { - $user->email2 = ""; - } - if (empty($user->firstname)) { - $user->firstname = ""; - } - if (empty($user->lastname)) { - $user->lastname = ""; - } - if (empty($user->city)) { - $user->city = ""; - } - if (empty($user->country)) { - $user->country = ""; - } -?> -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:
:" /> - username)) { formerr($err->username); } ?> -
:" /> - password)) { formerr($err->password); } ?> -


- :
- ()

-
:" /> - email)) { formerr($err->email); } ?> -
:" /> - email2)) { formerr($err->email2); } ?> -
:" /> - firstname)) { formerr($err->firstname);} ?> -
:" /> - lastname)) { formerr($err->lastname);} ?> -
:" /> - city)) { formerr($err->city);} ?> -
:country, get_string("selectacountry"), "", "") ?> - country)) { formerr($err->country);} ?> -
" />
-
-
diff --git a/login/signup_form.php b/login/signup_form.php new file mode 100644 index 0000000000..a9aa0de7cc --- /dev/null +++ b/login/signup_form.php @@ -0,0 +1,112 @@ +libdir.'/formslib.php'; +class login_signup_form extends moodleform { + function definition() { + global $USER, $CFG; + + $mform =& $this->_form; + $renderer =& $mform->defaultRenderer(); + + $mform->addElement('header', '', get_string("createuserandpass"),''); + + + $mform->addElement('text', 'username', get_string("username"),'size="12"'); + $mform->setType('username', PARAM_NOTAGS); + $mform->addRule('username', get_string("missingusername"), 'required', null, 'client'); + + $mform->addElement('password', 'password', get_string("password"), 'size="12"'); + $mform->setType('password', PARAM_RAW); + $mform->addRule('password', get_string("missingpassword"), 'required', null, 'client'); + + $mform->addElement('header', '', get_string('supplyinfo'),''); + + $mform->addElement('text', 'email', get_string("email"), 'size="25"'); + $mform->setType('email', PARAM_NOTAGS); + $mform->addRule('email', get_string("missingemail"), 'required', null, 'client'); + + $mform->addElement('text', 'email2', get_string("emailagain"), 'size="25"'); + $mform->setType('email2', PARAM_NOTAGS); + $mform->addRule('email2', get_string("missingemail"), 'required', null, 'client'); + + $mform->addElement('text', 'firstname', get_string("firstname"), 'size="25"'); + $mform->setType('firstname', PARAM_TEXT); + $mform->addRule('firstname', get_string("missingfirstname"), 'required', null, 'client'); + + $mform->addElement('text', 'lastname', get_string("lastname"), 'size="25"'); + $mform->setType('lastname', PARAM_TEXT); + $mform->addRule('lastname', get_string("missinglastname"), 'required', null, 'client'); + + $mform->addElement('text', 'city', get_string("city"), 'size="25"'); + $mform->setType('city', PARAM_TEXT); + $mform->addRule('city', get_string("missingcity"), 'required', null, 'client'); + + $country=get_list_of_countries(); + $default_country['']=get_string("selectacountry"); + $country=array_merge($default_country,$country); + $mform->setDefault('country', ''); + $mform->addElement('select', 'country', get_string("country"), $country); + $mform->addRule('country', get_string("missingcountry"), 'required', null, 'client'); + + $mform->addElement('submit', 'submit', get_string("createaccount")); + + $renderer->addStopFieldsetElements('submit'); + } + function definition_after_data(){ + $mform =& $this->_form; + $mform->applyFilter('username', 'moodle_strtolower'); + $mform->applyFilter('username', 'trim'); + } + + function validation($data) { + global $CFG; + $errors=array(); + + + if (record_exists("user", "username", $data['username'])){ + $errors['username'] = get_string("usernameexists"); + } else { + if (empty($CFG->extendedusernamechars)) { + $string = eregi_replace("[^(-\.[:alnum:])]", "", $data['username']); + if (strcmp($data['username'], $string)) { + $errors['username'] = get_string("alphanumerical"); + } + } + } + if (isset($CFG->auth_user_create) and $CFG->auth_user_create==1 and function_exists('auth_user_exists') ){ + if (auth_user_exists($data['username'])) { + $errors['username'] = get_string("usernameexists"); + } + } + + + if (! validate_email($data['email'])) { + $errors['email'] = get_string("invalidemail"); + + }else if (record_exists("user", "email", $data['email'])) { + $errors['email'] = get_string("emailexists")." ".get_string("newpassword")."?"; + } + if (empty($data['email2'])) { + $errors['email2'] = get_string("missingemail"); + + } else if ($data['email2'] != $data['email']) { + $errors['email2'] = get_string("invalidemail"); + } + if (!isset($errors['email'])) { + if ($err = email_is_not_allowed($data['email'])) { + $errors['email'] = $err; + } + + } + + + if (0 == count($errors)){ + return true; + } else { + return $errors; + } + + + } +} + +?>