weekly release 3.2dev
[moodle.git] / login / signup_form.php
CommitLineData
6c3ef410 1<?php
296c2a79 2
c30949a9
PS
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * User sign-up form.
20 *
21 * @package core
22 * @subpackage auth
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
bfebaf64 28
296c2a79 29require_once($CFG->libdir.'/formslib.php');
831d450e 30require_once($CFG->dirroot.'/user/profile/lib.php');
0859a08d 31require_once($CFG->dirroot . '/user/editlib.php');
296c2a79 32
dd0feda5 33class login_signup_form extends moodleform {
6bc1e5d5 34 function definition() {
35 global $USER, $CFG;
e8a34ad4 36
c30949a9 37 $mform = $this->_form;
e8a34ad4 38
807cf3d0 39 $mform->addElement('header', 'createuserandpass', get_string('createuserandpass'), '');
e8a34ad4 40
41
d4787515 42 $mform->addElement('text', 'username', get_string('username'), 'maxlength="100" size="12"');
90d8bf59 43 $mform->setType('username', PARAM_RAW);
bd7a0fd3 44 $mform->addRule('username', get_string('missingusername'), 'required', null, 'client');
e8a34ad4 45
3bf11ca4
DM
46 if (!empty($CFG->passwordpolicy)){
47 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
851481dc 48 }
54bb33eb 49 $mform->addElement('passwordunmask', 'password', get_string('password'), 'maxlength="32" size="12"');
ac9768fc 50 $mform->setType('password', core_user::get_property_type('password'));
bd7a0fd3 51 $mform->addRule('password', get_string('missingpassword'), 'required', null, 'client');
e8a34ad4 52
807cf3d0 53 $mform->addElement('header', 'supplyinfo', get_string('supplyinfo'),'');
e8a34ad4 54
d4787515 55 $mform->addElement('text', 'email', get_string('email'), 'maxlength="100" size="25"');
ac9768fc 56 $mform->setType('email', core_user::get_property_type('email'));
bd7a0fd3 57 $mform->addRule('email', get_string('missingemail'), 'required', null, 'client');
525ef9c8 58 $mform->setForceLtr('email');
e8a34ad4 59
d4787515 60 $mform->addElement('text', 'email2', get_string('emailagain'), 'maxlength="100" size="25"');
ac9768fc 61 $mform->setType('email2', core_user::get_property_type('email'));
bd7a0fd3 62 $mform->addRule('email2', get_string('missingemail'), 'required', null, 'client');
525ef9c8 63 $mform->setForceLtr('email2');
e8a34ad4 64
0859a08d
AA
65 $namefields = useredit_get_required_name_fields();
66 foreach ($namefields as $field) {
67 $mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30"');
ac9768fc 68 $mform->setType($field, core_user::get_property_type('firstname'));
0859a08d
AA
69 $stringid = 'missing' . $field;
70 if (!get_string_manager()->string_exists($stringid, 'moodle')) {
71 $stringid = 'required';
72 }
bd7a0fd3 73 $mform->addRule($field, get_string($stringid), 'required', null, 'client');
baacc970 74 }
75
c5c0d2ff 76 $mform->addElement('text', 'city', get_string('city'), 'maxlength="120" size="20"');
ac9768fc 77 $mform->setType('city', core_user::get_property_type('city'));
fa7f750c
PS
78 if (!empty($CFG->defaultcity)) {
79 $mform->setDefault('city', $CFG->defaultcity);
80 }
e8a34ad4 81
0aa759b0 82 $country = get_string_manager()->get_list_of_countries();
6bc1e5d5 83 $default_country[''] = get_string('selectacountry');
84 $country = array_merge($default_country, $country);
85 $mform->addElement('select', 'country', get_string('country'), $country);
cbc54f2c 86
87 if( !empty($CFG->country) ){
88 $mform->setDefault('country', $CFG->country);
89 }else{
90 $mform->setDefault('country', '');
91 }
f85509b5 92
d83318f1
ME
93 profile_signup_fields($mform);
94
574b9d86 95 if (signup_captcha_enabled()) {
3256b358 96 $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth'), array('https' => $CFG->loginhttps));
8db551df 97 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth');
5e91f96c 98 $mform->closeHeaderBefore('recaptcha_element');
5d6308d4 99 }
e8a34ad4 100
18d8136e 101 if (!empty($CFG->sitepolicy)) {
807cf3d0
FM
102 $mform->addElement('header', 'policyagreement', get_string('policyagreement'), '');
103 $mform->setExpanded('policyagreement');
18d8136e 104 $mform->addElement('static', 'policylink', '', '<a href="'.$CFG->sitepolicy.'" onclick="this.target=\'_blank\'">'.get_String('policyagreementclick').'</a>');
105 $mform->addElement('checkbox', 'policyagreed', get_string('policyaccept'));
bd7a0fd3 106 $mform->addRule('policyagreed', get_string('policyagree'), 'required', null, 'client');
18d8136e 107 }
108
5d2b6c1c 109 // buttons
b6d15dde 110 $this->add_action_buttons(true, get_string('createaccount'));
e8a34ad4 111
6bc1e5d5 112 }
296c2a79 113
6bc1e5d5 114 function definition_after_data(){
c30949a9 115 $mform = $this->_form;
6bc1e5d5 116 $mform->applyFilter('username', 'trim');
e9c27d1e
DM
117
118 // Trim required name fields.
119 foreach (useredit_get_required_name_fields() as $field) {
120 $mform->applyFilter($field, 'trim');
121 }
6bc1e5d5 122 }
e8a34ad4 123
a78890d5 124 function validation($data, $files) {
a78890d5 125 $errors = parent::validation($data, $files);
e8a34ad4 126
574b9d86 127 if (signup_captcha_enabled()) {
f85509b5 128 $recaptcha_element = $this->_form->getElement('recaptcha_element');
129 if (!empty($this->_form->_submitValues['recaptcha_challenge_field'])) {
130 $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
131 $response_field = $this->_form->_submitValues['recaptcha_response_field'];
132 if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
133 $errors['recaptcha'] = $result;
134 }
135 } else {
136 $errors['recaptcha'] = get_string('missingrecaptchachallengefield');
5d6308d4 137 }
138 }
e8a34ad4 139
574b9d86 140 $errors += signup_validate_data($data, $files);
f85509b5 141
574b9d86 142 return $errors;
f85509b5 143
6bc1e5d5 144 }
e8a34ad4 145}