3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Reset forgotten password form definition.
23 * @copyright 2006 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
29 require_once $CFG->libdir
.'/formslib.php';
31 class login_forgot_password_form
extends moodleform
{
33 function definition() {
34 $mform = $this->_form
;
36 $mform->addElement('header', '', get_string('searchbyusername'), '');
38 $mform->addElement('text', 'username', get_string('username'));
39 $mform->setType('username', PARAM_RAW
);
41 $submitlabel = get_string('search');
42 $mform->addElement('submit', 'submitbuttonusername', $submitlabel);
44 $mform->addElement('header', '', get_string('searchbyemail'), '');
46 $mform->addElement('text', 'email', get_string('email'));
47 $mform->setType('email', PARAM_RAW
);
49 $submitlabel = get_string('search');
50 $mform->addElement('submit', 'submitbuttonemail', $submitlabel);
53 function validation($data, $files) {
56 $errors = parent
::validation($data, $files);
58 if ((!empty($data['username']) and !empty($data['email'])) or (empty($data['username']) and empty($data['email']))) {
59 $errors['username'] = get_string('usernameoremail');
60 $errors['email'] = get_string('usernameoremail');
62 } else if (!empty($data['email'])) {
63 if (!validate_email($data['email'])) {
64 $errors['email'] = get_string('invalidemail');
66 } else if ($DB->count_records('user', array('email'=>$data['email'])) > 1) {
67 $errors['email'] = get_string('forgottenduplicate');
70 if ($user = get_complete_user_data('email', $data['email'])) {
71 if (empty($user->confirmed
)) {
72 $errors['email'] = get_string('confirmednot');
75 if (!$user and empty($CFG->protectusernames
)) {
76 $errors['email'] = get_string('emailnotfound');
81 if ($user = get_complete_user_data('username', $data['username'])) {
82 if (empty($user->confirmed
)) {
83 $errors['email'] = get_string('confirmednot');
86 if (!$user and empty($CFG->protectusernames
)) {
87 $errors['username'] = get_string('usernamenotfound');