Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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          $factors = array_map(function ($element) {
  44              return $element->get_display_name();
  45          }, $factors);
  46          // Add an 'all' action.
  47          $factors['all'] = get_string('all');
  48  
  49          $mform->addElement('select', 'factor', get_string('selectfactor', 'tool_mfa'), $factors);
  50  
  51          if (!$bulkaction) {
  52              $mform->addElement('text', 'resetfactor', get_string('resetuser', 'tool_mfa'),
  53              ['placeholder' => get_string('resetfactorplaceholder', 'tool_mfa')]);
  54              $mform->setType('resetfactor', PARAM_TEXT);
  55              $mform->addRule('resetfactor', get_string('userempty', 'tool_mfa'), 'required');
  56          }
  57  
  58          $this->add_action_buttons(true, get_string('resetconfirm', 'tool_mfa'));
  59      }
  60  
  61      /**
  62       * Form validation.
  63       *
  64       * Server side rules do not work for uploaded files, implement serverside rules here if needed.
  65       *
  66       * @param array $data array of ("fieldname"=>value) of submitted data
  67       * @param array $files array of uploaded files "element_name"=>tmp_file_path
  68       * @return array of "element_name"=>"error_description" if there are errors,
  69       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
  70       */
  71      public function validation($data, $files) {
  72          global $DB;
  73          $errors = parent::validation($data, $files);
  74  
  75          if (!$data['bulkaction']) {
  76              $userinfo = $data['resetfactor'];
  77              // Try input as username first, then email.
  78              $user = $DB->get_record('user', ['username' => $userinfo]);
  79              if (empty($user)) {
  80                  // If not found, try username.
  81                  $user = $DB->get_record('user', ['email' => $userinfo]);
  82              }
  83  
  84              if (empty($user)) {
  85                  $errors['resetfactor'] = get_string('usernotfound', 'tool_mfa');
  86              } else {
  87                  // Add custom field to store user.
  88                  $this->_form->addElement('hidden', 'user', $user);
  89                  $this->_form->setType('user', PARAM_RAW);
  90              }
  91          }
  92  
  93          return $errors;
  94      }
  95  }