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 core_question\output;
  18  
  19  use core\output\datafilter;
  20  use renderer_base;
  21  use stdClass;
  22  
  23  /**
  24   * Class for rendering filters on the base view.
  25   *
  26   * @package    core_question
  27   * @copyright  2021 Catalyst IT Australia Pty Ltd
  28   * @author     Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class question_bank_filter_ui extends datafilter {
  32  
  33      /** @var int $perpage number of records per page. */
  34      protected $perpage = 0;
  35  
  36      /** @var array Parameters for the page URL. */
  37      protected $pagevars;
  38  
  39      /** @var array $searchconditions Searchconditions for the filter. */
  40      /** @var array $additionalparams Conditino objects for the current filter. */
  41      /** @var string $component Component for calling the fragment callback. */
  42      /** @var string $callback Fragment callback. */
  43      /** @var string $view View class name. */
  44      /** @var int|null $cmid if in an activity, the cmid. */
  45      /** @var array $extraparams Additional parameters used by view classes. */
  46  
  47      /**
  48       * Create a new datafilter
  49       *
  50       * @param \context $context The context of the course being displayed
  51       * @param array $searchconditions Array of condition objects for the current filters
  52       * @param array $additionalparams Additional display parameters
  53       * @param string $component the component for the fragment
  54       * @param string $callback the callback for the fragment
  55       * @param string $view the view class
  56       * @param ?string $tableregionid The ID of the region to update with the fragment
  57       * @param ?int $cmid if in an activity, the cmid.
  58       * @param array $pagevars current filter parameters
  59       * @param array $extraparams additional parameters required for a particular view class.
  60       */
  61      public function __construct(
  62          \context $context,
  63          protected array $searchconditions,
  64          protected array $additionalparams,
  65          protected string $component,
  66          protected string $callback,
  67          protected string $view,
  68          ?string $tableregionid = null,
  69          protected ?int $cmid = null,
  70          array $pagevars = [],
  71          protected array $extraparams = []
  72      ) {
  73          parent::__construct($context, $tableregionid);
  74          if (array_key_exists('sortData', $pagevars)) {
  75              foreach ($pagevars['sortData'] as $sortname => $sortorder) {
  76                  unset($pagevars['sortData'][$sortname]);
  77                  $pagevars['sortData'][str_replace('\\', '\\\\', $sortname)] = $sortorder;
  78              }
  79          }
  80          $this->pagevars = $pagevars;
  81      }
  82  
  83      /**
  84       * Get data for all filter types.
  85       *
  86       * @return array
  87       */
  88      protected function get_filtertypes(): array {
  89  
  90          $filtertypes = [];
  91  
  92          foreach ($this->searchconditions as $searchcondition) {
  93              $filtertypes[] = $this->get_filter_object(
  94                  $searchcondition->get_condition_key(),
  95                  $searchcondition->get_title(),
  96                  $searchcondition->allow_custom(),
  97                  $searchcondition->allow_multiple(),
  98                  $searchcondition->get_filter_class(),
  99                  $searchcondition->get_initial_values(),
 100                  $searchcondition->allow_empty(),
 101                  $searchcondition->get_filteroptions(),
 102                  $searchcondition->is_required(),
 103                  $searchcondition->get_join_list(),
 104              );
 105          }
 106  
 107          return $filtertypes;
 108      }
 109  
 110      /**
 111       * Export the renderer data in a mustache template friendly format.
 112       *
 113       * @param renderer_base $output Unused.
 114       * @return stdClass Data in a format compatible with a mustache template.
 115       */
 116      public function export_for_template(renderer_base $output): stdClass {
 117          $defaultcategory = question_get_default_category($this->context->id);
 118          $courseid = null;
 119          if ($this->context->contextlevel == CONTEXT_COURSE) {
 120              $courseid = $this->context->instanceid;
 121          }
 122          if (empty($courseid)) {
 123              $courseid = $this->searchconditions['category']->get_course_id();
 124          }
 125          return (object) [
 126              'tableregionid' => $this->tableregionid,
 127              'courseid' => $courseid,
 128              'filtertypes' => $this->get_filtertypes(),
 129              'selected' => 'category',
 130              'rownumber' => 1,
 131              'categoryid' => $defaultcategory->id,
 132              'perpage' => $this->additionalparams['perpage'] ?? 0,
 133              'contextid' => $this->context->id,
 134              'component' => $this->component,
 135              'callback' => $this->callback,
 136              'view' => str_replace('\\', '\\\\', $this->view),
 137              'cmid' => $this->cmid ?? 0,
 138              'pagevars' => json_encode($this->pagevars),
 139              'extraparams' => json_encode($this->extraparams),
 140          ];
 141      }
 142  }