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  // 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  /**
  18   * Filter base.
  19   *
  20   * @package    tool_usertours
  21   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_usertours\local\filter;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use tool_usertours\tour;
  30  use context;
  31  
  32  /**
  33   * Filter base.
  34   *
  35   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  abstract class base {
  39      /**
  40       * Any Value.
  41       */
  42      const ANYVALUE = '__ANYVALUE__';
  43  
  44      /**
  45       * The name of the filter.
  46       *
  47       * @return  string
  48       */
  49      public static function get_filter_name() {
  50          throw new \coding_exception('get_filter_name() must be defined');
  51      }
  52  
  53      /**
  54       * Retrieve the list of available filter options.
  55       *
  56       * @return  array                   An array whose keys are the valid options
  57       */
  58      public static function get_filter_options() {
  59          return [];
  60      }
  61  
  62      /**
  63       * Check whether the filter matches the specified tour and/or context.
  64       *
  65       * @param   tour        $tour       The tour to check
  66       * @param   context     $context    The context to check
  67       * @return  boolean
  68       */
  69      public static function filter_matches(tour $tour, context $context) {
  70          return true;
  71      }
  72  
  73      /**
  74       * Add the form elements for the filter to the supplied form.
  75       *
  76       * @param   MoodleQuickForm $mform      The form to add filter settings to.
  77       */
  78      public static function add_filter_to_form(\MoodleQuickForm &$mform) {
  79          $options = [
  80              static::ANYVALUE   => get_string('all'),
  81          ];
  82          $options += static::get_filter_options();
  83  
  84          $filtername = static::get_filter_name();
  85          $key = "filter_{$filtername}";
  86  
  87          $mform->addElement('select', $key, get_string($key, 'tool_usertours'), $options, [
  88                  'multiple' => true,
  89              ]);
  90          $mform->setDefault($key, static::ANYVALUE);
  91          $mform->addHelpButton($key, $key, 'tool_usertours');
  92      }
  93  
  94      /**
  95       * Prepare the filter values for the form.
  96       *
  97       * @param   tour            $tour       The tour to prepare values from
  98       * @param   stdClass        $data       The data value
  99       * @return  stdClass
 100       */
 101      public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
 102          $filtername = static::get_filter_name();
 103  
 104          $key = "filter_{$filtername}";
 105          $values = $tour->get_filter_values($filtername);
 106          if (empty($values)) {
 107              $values = static::ANYVALUE;
 108          }
 109          $data->$key = $values;
 110  
 111          return $data;
 112      }
 113  
 114      /**
 115       * Save the filter values from the form to the tour.
 116       *
 117       * @param   tour            $tour       The tour to save values to
 118       * @param   stdClass        $data       The data submitted in the form
 119       */
 120      public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
 121          $filtername = static::get_filter_name();
 122  
 123          $key = "filter_{$filtername}";
 124  
 125          $newvalue = $data->$key;
 126          foreach ($data->$key as $value) {
 127              if ($value === static::ANYVALUE) {
 128                  $newvalue = [];
 129                  break;
 130              }
 131          }
 132  
 133          $tour->set_filter_values($filtername, $newvalue);
 134      }
 135  }