Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/login/signup_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 require_once($CFG->libdir.'/formslib.php');
00030 require_once($CFG->dirroot.'/user/profile/lib.php');
00031 
00032 class login_signup_form extends moodleform {
00033     function definition() {
00034         global $USER, $CFG;
00035 
00036         $mform = $this->_form;
00037 
00038         $mform->addElement('header', '', get_string('createuserandpass'), '');
00039 
00040 
00041         $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
00042         $mform->setType('username', PARAM_NOTAGS);
00043         $mform->addRule('username', get_string('missingusername'), 'required', null, 'server');
00044 
00045         if (!empty($CFG->passwordpolicy)){
00046             $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
00047         }
00048         $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
00049         $mform->setType('password', PARAM_RAW);
00050         $mform->addRule('password', get_string('missingpassword'), 'required', null, 'server');
00051 
00052         $mform->addElement('header', '', get_string('supplyinfo'),'');
00053 
00054         $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
00055         $mform->setType('email', PARAM_NOTAGS);
00056         $mform->addRule('email', get_string('missingemail'), 'required', null, 'server');
00057 
00058         $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
00059         $mform->setType('email2', PARAM_NOTAGS);
00060         $mform->addRule('email2', get_string('missingemail'), 'required', null, 'server');
00061 
00062         $nameordercheck = new stdClass();
00063         $nameordercheck->firstname = 'a';
00064         $nameordercheck->lastname  = 'b';
00065         if (fullname($nameordercheck) == 'b a' ) {  // See MDL-4325
00066             $mform->addElement('text', 'lastname',  get_string('lastname'),  'maxlength="100" size="30"');
00067             $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
00068         } else {
00069             $mform->addElement('text', 'firstname', get_string('firstname'), 'maxlength="100" size="30"');
00070             $mform->addElement('text', 'lastname',  get_string('lastname'),  'maxlength="100" size="30"');
00071         }
00072 
00073         $mform->setType('firstname', PARAM_TEXT);
00074         $mform->addRule('firstname', get_string('missingfirstname'), 'required', null, 'server');
00075 
00076         $mform->setType('lastname', PARAM_TEXT);
00077         $mform->addRule('lastname', get_string('missinglastname'), 'required', null, 'server');
00078 
00079         $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
00080         $mform->setType('city', PARAM_TEXT);
00081         $mform->addRule('city', get_string('missingcity'), 'required', null, 'server');
00082         if (!empty($CFG->defaultcity)) {
00083             $mform->setDefault('city', $CFG->defaultcity);
00084         }
00085 
00086         $country = get_string_manager()->get_list_of_countries();
00087         $default_country[''] = get_string('selectacountry');
00088         $country = array_merge($default_country, $country);
00089         $mform->addElement('select', 'country', get_string('country'), $country);
00090         $mform->addRule('country', get_string('missingcountry'), 'required', null, 'server');
00091 
00092         if( !empty($CFG->country) ){
00093             $mform->setDefault('country', $CFG->country);
00094         }else{
00095             $mform->setDefault('country', '');
00096         }
00097 
00098         if ($this->signup_captcha_enabled()) {
00099             $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'), array('https' => $CFG->loginhttps));
00100             $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
00101         }
00102 
00103         profile_signup_fields($mform);
00104 
00105         if (!empty($CFG->sitepolicy)) {
00106             $mform->addElement('header', '', get_string('policyagreement'), '');
00107             $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
00108             $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
00109             $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'server');
00110         }
00111 
00112         // buttons
00113         $this->add_action_buttons(true, get_string('createaccount'));
00114 
00115     }
00116 
00117     function definition_after_data(){
00118         $mform = $this->_form;
00119         $mform->applyFilter('username', 'trim');
00120     }
00121 
00122     function validation($data, $files) {
00123         global $CFG, $DB;
00124         $errors = parent::validation($data, $files);
00125 
00126         $authplugin = get_auth_plugin($CFG->registerauth);
00127 
00128         if ($DB->record_exists('user', array('username'=>$data['username'], 'mnethostid'=>$CFG->mnet_localhost_id))) {
00129             $errors['username'] = get_string('usernameexists');
00130         } else {
00131             //check allowed characters
00132             if ($data['username'] !== moodle_strtolower($data['username'])) {
00133                 $errors['username'] = get_string('usernamelowercase');
00134             } else {
00135                 if ($data['username'] !== clean_param($data['username'], PARAM_USERNAME)) {
00136                     $errors['username'] = get_string('invalidusername');
00137                 }
00138 
00139             }
00140         }
00141 
00142         //check if user exists in external db
00143         //TODO: maybe we should check all enabled plugins instead
00144         if ($authplugin->user_exists($data['username'])) {
00145             $errors['username'] = get_string('usernameexists');
00146         }
00147 
00148 
00149         if (! validate_email($data['email'])) {
00150             $errors['email'] = get_string('invalidemail');
00151 
00152         } else if ($DB->record_exists('user', array('email'=>$data['email']))) {
00153             $errors['email'] = get_string('emailexists').' <a href="forgot_password.php">'.get_string('newpassword').'?</a>';
00154         }
00155         if (empty($data['email2'])) {
00156             $errors['email2'] = get_string('missingemail');
00157 
00158         } else if ($data['email2'] != $data['email']) {
00159             $errors['email2'] = get_string('invalidemail');
00160         }
00161         if (!isset($errors['email'])) {
00162             if ($err = email_is_not_allowed($data['email'])) {
00163                 $errors['email'] = $err;
00164             }
00165 
00166         }
00167 
00168         $errmsg = '';
00169         if (!check_password_policy($data['password'], $errmsg)) {
00170             $errors['password'] = $errmsg;
00171         }
00172 
00173         if ($this->signup_captcha_enabled()) {
00174             $recaptcha_element = $this->_form->getElement('recaptcha_element');
00175             if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
00176                 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
00177                 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
00178                 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
00179                     $errors['recaptcha'] = $result;
00180                 }
00181             } else {
00182                 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
00183             }
00184         }
00185 
00186         return $errors;
00187 
00188     }
00189 
00194     function signup_captcha_enabled() {
00195         global $CFG;
00196         return !empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey) && get_config('auth/email', 'recaptcha');
00197     }
00198 
00199 }
 All Data Structures Namespaces Files Functions Variables Enumerations