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  
  21  require_once($CFG->libdir . "/formslib.php");
  22  
  23  /**
  24   * Setup factor form
  25   *
  26   * @package     tool_mfa
  27   * @author      Mikhail Golenkov <golenkovm@gmail.com>
  28   * @copyright   Catalyst IT
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class setup_factor_form extends \moodleform {
  32  
  33      /**
  34       * {@inheritDoc}
  35       * @see moodleform::definition()
  36       */
  37      public function definition(): void {
  38          $mform = $this->_form;
  39  
  40          $factorname = $this->_customdata['factorname'];
  41          $factor = \tool_mfa\plugininfo\factor::get_factor($factorname);
  42          $mform = $factor->setup_factor_form_definition($mform);
  43          $this->xss_whitelist_static_form_elements($mform);
  44  
  45      }
  46  
  47      /**
  48       * Validates setup_factor form with given factor validation method.
  49       *
  50       * @param array $data
  51       * @param array $files
  52       * @return array
  53       */
  54      public function validation($data, $files) {
  55          $errors = parent::validation($data, $files);
  56  
  57          $factorname = $this->_customdata['factorname'];
  58          $factor = \tool_mfa\plugininfo\factor::get_factor($factorname);
  59          $errors += $factor->setup_factor_form_validation($data);
  60  
  61          return $errors;
  62      }
  63  
  64      /**
  65       * Invokes factor setup_factor_form_definition_after_data() method after form data has been set.
  66       */
  67      public function definition_after_data(): void {
  68          $mform = $this->_form;
  69  
  70          $factorname = $this->_customdata['factorname'];
  71          $factor = \tool_mfa\plugininfo\factor::get_factor($factorname);
  72          $mform = $factor->setup_factor_form_definition_after_data($mform);
  73          $this->xss_whitelist_static_form_elements($mform);
  74          $this->add_action_buttons();
  75      }
  76  
  77      /**
  78       * Form elements clean up
  79       *
  80       * @param \HTML_QuickForm $mform
  81       * @return void
  82       */
  83      private function xss_whitelist_static_form_elements($mform): void {
  84          if (!method_exists('MoodleQuickForm_static', 'set_allow_xss')) {
  85              return;
  86          }
  87  
  88          $elements = $mform->_elements;
  89          foreach ($elements as $element) {
  90              if (is_a($element, 'MoodleQuickForm_static')) {
  91                  $element->set_allow_xss(true);
  92              }
  93          }
  94      }
  95  }