Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * A Moodle form base class for editing local filter settings.
  20   *
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package    core
  23   * @subpackage filter
  24   */
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->libdir . '/formslib.php');
  28  
  29  abstract class filter_local_settings_form extends moodleform {
  30      protected $filter;
  31      protected $context;
  32  
  33      public function __construct($submiturl, $filter, $context) {
  34          $this->filter = $filter;
  35          $this->context = $context;
  36          parent::__construct($submiturl);
  37      }
  38  
  39      /**
  40       * Build the form definition. Rather than overriding this method, you
  41       * should probably override definition_inner instead.
  42       *
  43       * This method adds the necessary hidden fields and submit buttons,
  44       * and calls definition_inner to insert the custom controls in the appropriate place.
  45       */
  46      public function definition() {
  47          $mform = $this->_form;
  48  
  49          $this->definition_inner($mform);
  50  
  51          $mform->addElement('hidden', 'contextid');
  52          $mform->setType('contextid', PARAM_INT);
  53          $mform->setDefault('contextid', $this->context->id);
  54  
  55          $mform->addElement('hidden', 'filter');
  56          $mform->setType('filter', PARAM_SAFEPATH);
  57          $mform->setDefault('filter', $this->filter);
  58  
  59          $this->add_action_buttons();
  60      }
  61  
  62      /**
  63       * Override this method to add your form controls.
  64       * @param $mform the form we are building. $this->_form, but passed in for convenience.
  65       */
  66      abstract protected function definition_inner($mform);
  67  
  68      /**
  69       * Override this method to save the settings to the database. The default
  70       * implementation will probably be sufficient for most simple cases.
  71       * @param object $data the form data that was submitted.
  72       */
  73      public function save_changes($data) {
  74          $data = (array) $data;
  75          unset($data['filter']);
  76          unset($data['contextid']);
  77          foreach ($data as $name => $value) {
  78              if ($value !== '') {
  79                  filter_set_local_config($this->filter, $this->context->id, $name, $value);
  80              } else {
  81                  filter_unset_local_config($this->filter, $this->context->id, $name);
  82              }
  83          }
  84      }
  85  }