Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace tool_mfa\local\form;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
require_once("$CFG->libdir/formslib.php");
21
 
22
/**
23
 * Form to reset gracemode timer for users.
24
 *
25
 * @package     tool_mfa
26
 * @author      Peter Burnett <peterburnett@catalyst-au.net>
27
 * @copyright   Catalyst IT
28
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class reset_factor extends \moodleform {
31
 
32
    /**
33
     * Form definition.
34
     */
35
    public function definition(): void {
36
        $mform = $this->_form;
37
        $factors = $this->_customdata['factors'];
38
        $bulkaction = $this->_customdata['bulk'];
39
 
40
        $mform->addElement('hidden', 'bulkaction', $bulkaction);
41
        $mform->setType('bulkaction', PARAM_BOOL);
42
 
43
        $mform->addElement('hidden', 'returnurl');
44
        $mform->setType('returnurl', PARAM_LOCALURL);
45
 
46
        $factors = array_map(function ($element) {
47
            return $element->get_display_name();
48
        }, $factors);
49
        // Add an 'all' action.
50
        $factors['all'] = get_string('all');
51
 
52
        $mform->addElement('select', 'factor', get_string('selectfactor', 'tool_mfa'), $factors);
53
 
54
        if (!$bulkaction) {
55
            $mform->addElement('text', 'resetfactor', get_string('resetuser', 'tool_mfa'),
56
            ['placeholder' => get_string('resetfactorplaceholder', 'tool_mfa')]);
57
            $mform->setType('resetfactor', PARAM_TEXT);
58
            $mform->addRule('resetfactor', get_string('userempty', 'tool_mfa'), 'required');
59
        }
60
 
61
        $this->add_action_buttons(true, get_string('resetconfirm', 'tool_mfa'));
62
    }
63
 
64
    /**
65
     * Form validation.
66
     *
67
     * Server side rules do not work for uploaded files, implement serverside rules here if needed.
68
     *
69
     * @param array $data array of ("fieldname"=>value) of submitted data
70
     * @param array $files array of uploaded files "element_name"=>tmp_file_path
71
     * @return array of "element_name"=>"error_description" if there are errors,
72
     *         or an empty array if everything is OK (true allowed for backwards compatibility too).
73
     */
74
    public function validation($data, $files) {
75
        global $DB;
76
        $errors = parent::validation($data, $files);
77
 
78
        if (!$data['bulkaction']) {
79
            $userinfo = $data['resetfactor'];
80
            // Try input as username first, then email.
81
            $user = $DB->get_record('user', ['username' => $userinfo]);
82
            if (empty($user)) {
83
                // If not found, try username.
84
                $user = $DB->get_record('user', ['email' => $userinfo]);
85
            }
86
 
87
            if (empty($user)) {
88
                $errors['resetfactor'] = get_string('usernotfound', 'tool_mfa');
89
            } else {
90
                // Add custom field to store user.
91
                $this->_form->addElement('hidden', 'user', $user);
92
                $this->_form->setType('user', PARAM_RAW);
93
            }
94
        }
95
 
96
        return $errors;
97
    }
98
}