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   * Selector filter.
  19   *
  20   * @package    tool_usertours
  21   * @copyright  2020 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_usertours\local\clientside_filter;
  25  
  26  use stdClass;
  27  use tool_usertours\tour;
  28  
  29  /**
  30   * Course filter.
  31   *
  32   * @copyright  2020 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class cssselector extends clientside_filter {
  36      /**
  37       * The name of the filter.
  38       *
  39       * @return  string
  40       */
  41      public static function get_filter_name() {
  42          return 'cssselector';
  43      }
  44  
  45      /**
  46       * Overrides the base add form element with a selector text box.
  47       *
  48       * @param \MoodleQuickForm $mform
  49       */
  50      public static function add_filter_to_form(\MoodleQuickForm &$mform) {
  51          $filtername = self::get_filter_name();
  52          $key = "filter_{$filtername}";
  53  
  54          $mform->addElement('text', $key, get_string($key, 'tool_usertours'));
  55          $mform->setType($key, PARAM_RAW);
  56          $mform->addHelpButton($key, $key, 'tool_usertours');
  57      }
  58  
  59      /**
  60       * Prepare the filter values for the form.
  61       *
  62       * @param   tour            $tour       The tour to prepare values from
  63       * @param   stdClass        $data       The data value
  64       * @return  stdClass
  65       */
  66      public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
  67          $filtername = static::get_filter_name();
  68  
  69          $key = "filter_{$filtername}";
  70          $values = $tour->get_filter_values($filtername);
  71          if (empty($values)) {
  72              $values = [""];
  73          }
  74          $data->$key = $values[0];
  75  
  76          return $data;
  77      }
  78  
  79      /**
  80       * Save the filter values from the form to the tour.
  81       *
  82       * @param   tour            $tour       The tour to save values to
  83       * @param   stdClass        $data       The data submitted in the form
  84       */
  85      public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
  86          $filtername = static::get_filter_name();
  87  
  88          $key = "filter_{$filtername}";
  89  
  90          $newvalue = [$data->$key];
  91          if (empty($data->$key)) {
  92              $newvalue = [];
  93          }
  94  
  95          $tour->set_filter_values($filtername, $newvalue);
  96      }
  97  
  98      /**
  99       * Returns the filter values needed for client side filtering.
 100       *
 101       * @param   tour            $tour       The tour to find the filter values for
 102       * @return  stdClass
 103       */
 104      public static function get_client_side_values(tour $tour): stdClass {
 105          $filtername = static::get_filter_name();
 106          $filtervalues = $tour->get_filter_values($filtername);
 107  
 108          // Filter values might not exist for tours that were created before this filter existed.
 109          if (!$filtervalues) {
 110              return new stdClass;
 111          }
 112  
 113          return (object) $filtervalues;
 114      }
 115  }