]> git.mjollnir.org Git - moodle.git/commitdiff
migrated signup.php over to using new formslib
authorjamiesensei <jamiesensei>
Tue, 24 Oct 2006 11:10:48 +0000 (11:10 +0000)
committerjamiesensei <jamiesensei>
Tue, 24 Oct 2006 11:10:48 +0000 (11:10 +0000)
login/signup.php
login/signup_form.html [deleted file]
login/signup_form.php [new file with mode: 0644]

index fdb73cca5f61a653b07121f40b3dac06f83a3ff5..057c5f9563b25185c2c4ccdeec87aaafcd530659 100644 (file)
@@ -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");
         $langs    = get_list_of_languages();
         $langmenu = popup_form ("$CFG->wwwroot/login/signup.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
     }
+    print_header($newaccount, $newaccount, "<a href=\"index.php\">$login</a> -> $newaccount", $mform_signup->focus(), "", true, "<div align=\"right\">$langmenu</div>");
 
-    print_header($newaccount, $newaccount, "<a href=\"index.php\">$login</a> -> $newaccount", $focus, "", true, "<div align=\"right\">$langmenu</div>");
-    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")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
-    }
-    
-
-    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 (file)
index cea92e1..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-<?php
-   if (empty($user->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 = "";
-   }
-?>
-<table cellpadding="20" align="center"> <tr> <td class="generalbox">
-<form action="signup.php" method="post" name="form" id="form">
-<table>
-<tr valign="top">
-    <td colspan="2"><b><?php print_string("createuserandpass") ?>:</b></td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("username") ?>:</td>
-    <td><input type="text" name="username" size="12" value="<?php p($user->username) ?>" alt="<?php print_string("username") ?>" />
-    <?php if (!empty($err->username)) { formerr($err->username); } ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("password") ?>:</td>
-    <td><input type="password" name="password" size="12" value="<?php p($user->password) ?>" alt="<?php print_string("password") ?>" />
-    <?php if (!empty($err->password)) { formerr($err->password); } ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td colspan="2"><p><br />
-                  <b><?php print_string("supplyinfo") ?>:</b><br />
-                  (<?php print_string("emailmustbereal") ?>)</p>
-              </td>
-            </tr>
-<tr valign="top">
-    <td align="right"><?php print_string("email") ?>:</td>
-    <td><input type="text" name="email" size="25" value="<?php p($user->email) ?>" alt="<?php print_string("email") ?>" />
-    <?php if (!empty($err->email)) { formerr($err->email); } ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("emailagain") ?>:</td>
-    <td><input type="text" name="email2" size="25" value="<?php p($user->email2) ?>" alt="<?php print_string("emailagain") ?>" />
-    <?php if (!empty($err->email2)) { formerr($err->email2); } ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("firstname") ?>:</td>
-    <td><input type="text" name="firstname" size="25" value="<?php p($user->firstname) ?>" alt="<?php print_string("firstname") ?>" />
-    <?php if (!empty($err->firstname)) { formerr($err->firstname);} ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("lastname") ?>:</td>
-    <td><input type="text" name="lastname" size="25" value="<?php p($user->lastname) ?>" alt="<?php print_string("lastname") ?>" />
-    <?php if (!empty($err->lastname)) { formerr($err->lastname);} ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("city") ?>:</td>
-    <td><input type="text" name="city" size="25" value="<?php p($user->city) ?>" alt="<?php print_string("city") ?>" />
-    <?php if (!empty($err->city)) { formerr($err->city);} ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right"><?php print_string("country") ?>:</td>
-    <td><?php choose_from_menu (get_list_of_countries(), "country", $user->country, get_string("selectacountry"), "", "") ?>
-    <?php if (!empty($err->country)) { formerr($err->country);} ?>
-    </td>
-</tr>
-<tr>
-    <td></td>
-    <td><input type="submit" value="<?php print_string("createaccount") ?>" /></td>
-</tr>
-</table>
-</form>
-</td></tr></table>
diff --git a/login/signup_form.php b/login/signup_form.php
new file mode 100644 (file)
index 0000000..a9aa0de
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+include_once $CFG->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")." <a href=\"forgot_password.php\">".get_string("newpassword")."?</a>";
+               }
+        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;
+               }
+
+
+       }
+}
+
+?>