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.

Differences Between: [Versions 401 and 403] [Versions 402 and 403]

   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 core\output;
  18  
  19  use context;
  20  use renderable;
  21  use stdClass;
  22  use templatable;
  23  
  24  /**
  25   * The filter renderable class.
  26   *
  27   * @package    core
  28   * @copyright  2021 Catalyst IT Australia Pty Ltd
  29   * @author     Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  abstract class datafilter implements renderable, templatable {
  33  
  34      /** @var int None of the following match */
  35      public const JOINTYPE_NONE = 0;
  36  
  37      /** @var int Any of the following match */
  38      public const JOINTYPE_ANY = 1;
  39  
  40      /** @var int All of the following match */
  41      public const JOINTYPE_ALL = 2;
  42  
  43      /** @var context $context The context where the filters are being rendered. */
  44      protected $context;
  45  
  46      /** @var string $tableregionid Container of the table to be updated by this filter, is used to retrieve the table */
  47      protected $tableregionid;
  48  
  49      /** @var stdClass $course The course shown */
  50      protected $course;
  51  
  52      /**
  53       * Filter constructor.
  54       *
  55       * @param context $context The context where the filters are being rendered
  56       * @param string|null $tableregionid Container of the table which will be updated by this filter
  57       */
  58      public function __construct(context $context, ?string $tableregionid = null) {
  59          $this->context = $context;
  60          $this->tableregionid = $tableregionid;
  61  
  62          if ($context instanceof \context_course) {
  63              $this->course = get_course($context->instanceid);
  64          }
  65      }
  66  
  67      /**
  68       * Get data for all filter types.
  69       *
  70       * @return array
  71       */
  72      abstract protected function get_filtertypes(): array;
  73  
  74      /**
  75       * Get a standardised filter object.
  76       *
  77       * @param string $name
  78       * @param string $title
  79       * @param bool $custom
  80       * @param bool $multiple
  81       * @param string|null $filterclass
  82       * @param array $values
  83       * @param bool $allowempty
  84       * @return stdClass|null
  85       */
  86      protected function get_filter_object(
  87          string $name,
  88          string $title,
  89          bool $custom,
  90          bool $multiple,
  91          ?string $filterclass,
  92          array $values,
  93          bool $allowempty = false,
  94          ?stdClass $filteroptions = null,
  95          bool $required = false,
  96          array $joinlist = [self::JOINTYPE_NONE, self::JOINTYPE_ANY, self::JOINTYPE_ALL]
  97      ): ?stdClass {
  98  
  99          if (!$allowempty && empty($values)) {
 100              // Do not show empty filters.
 101              return null;
 102          }
 103  
 104          return (object) [
 105              'name' => $name,
 106              'title' => $title,
 107              'allowcustom' => $custom,
 108              'allowmultiple' => $multiple,
 109              'filtertypeclass' => $filterclass,
 110              'values' => $values,
 111              'filteroptions' => $filteroptions,
 112              'required' => $required,
 113              'joinlist' => json_encode($joinlist)
 114          ];
 115      }
 116  }