Merge branch 'MDL-38590_master-fix-behat' of git://github.com/dmonllao/moodle
[moodle.git] / login / forgot_password_form.php
blob997dcd553cdde808fd6b3f2935164fb853c76296
1 <?php
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.
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/>.
18 /**
19 * Reset forgotten password form definition.
21 * @package core
22 * @subpackage auth
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;
35 $mform->setDisableShortforms(true);
37 $mform->addElement('header', 'searchbyusername', get_string('searchbyusername'), '');
39 $mform->addElement('text', 'username', get_string('username'));
40 $mform->setType('username', PARAM_RAW);
42 $submitlabel = get_string('search');
43 $mform->addElement('submit', 'submitbuttonusername', $submitlabel);
45 $mform->addElement('header', 'searchbyemail', get_string('searchbyemail'), '');
47 $mform->addElement('text', 'email', get_string('email'));
48 $mform->setType('email', PARAM_RAW);
50 $submitlabel = get_string('search');
51 $mform->addElement('submit', 'submitbuttonemail', $submitlabel);
54 function validation($data, $files) {
55 global $CFG, $DB;
57 $errors = parent::validation($data, $files);
59 if ((!empty($data['username']) and !empty($data['email'])) or (empty($data['username']) and empty($data['email']))) {
60 $errors['username'] = get_string('usernameoremail');
61 $errors['email'] = get_string('usernameoremail');
63 } else if (!empty($data['email'])) {
64 if (!validate_email($data['email'])) {
65 $errors['email'] = get_string('invalidemail');
67 } else if ($DB->count_records('user', array('email'=>$data['email'])) > 1) {
68 $errors['email'] = get_string('forgottenduplicate');
70 } else {
71 if ($user = get_complete_user_data('email', $data['email'])) {
72 if (empty($user->confirmed)) {
73 $errors['email'] = get_string('confirmednot');
76 if (!$user and empty($CFG->protectusernames)) {
77 $errors['email'] = get_string('emailnotfound');
81 } else {
82 if ($user = get_complete_user_data('username', $data['username'])) {
83 if (empty($user->confirmed)) {
84 $errors['email'] = get_string('confirmednot');
87 if (!$user and empty($CFG->protectusernames)) {
88 $errors['username'] = get_string('usernamenotfound');
92 return $errors;