Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Class containing the filter options data for rendering the autocomplete element for the data requests page.
  19   *
  20   * @package    tool_dataprivacy
  21   * @copyright  2018 Jun Pataleta
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_dataprivacy\output;
  25  
  26  use moodle_url;
  27  use renderable;
  28  use renderer_base;
  29  use stdClass;
  30  use templatable;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  /**
  35   * Class containing the filter options data for rendering the autocomplete element for the data requests page.
  36   *
  37   * @copyright  2018 Jun Pataleta
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class request_filter implements renderable, templatable {
  41  
  42      /** @var array $filteroptions The filter options. */
  43      protected $filteroptions;
  44  
  45      /** @var array $selectedoptions The list of selected filter option values. */
  46      protected $selectedoptions;
  47  
  48      /** @var moodle_url|string $baseurl The url with params needed to call up this page. */
  49      protected $baseurl;
  50  
  51      /**
  52       * request_filter constructor.
  53       *
  54       * @param array $filteroptions The filter options.
  55       * @param array $selectedoptions The list of selected filter option values.
  56       * @param string|moodle_url $baseurl The url with params needed to call up this page.
  57       */
  58      public function __construct($filteroptions, $selectedoptions, $baseurl = null) {
  59          $this->filteroptions = $filteroptions;
  60          $this->selectedoptions = $selectedoptions;
  61          if (!empty($baseurl)) {
  62              $this->baseurl = new moodle_url($baseurl);
  63          }
  64      }
  65  
  66      /**
  67       * Function to export the renderer data in a format that is suitable for a mustache template.
  68       *
  69       * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
  70       * @return stdClass|array
  71       */
  72      public function export_for_template(renderer_base $output) {
  73          global $PAGE;
  74          $data = new stdClass();
  75          if (empty($this->baseurl)) {
  76              $this->baseurl = $PAGE->url;
  77          }
  78          $data->action = $this->baseurl->out(false);
  79  
  80          foreach ($this->selectedoptions as $option) {
  81              if (!isset($this->filteroptions[$option])) {
  82                  $this->filteroptions[$option] = $option;
  83              }
  84          }
  85  
  86          $data->filteroptions = [];
  87          foreach ($this->filteroptions as $value => $label) {
  88              $selected = in_array($value, $this->selectedoptions);
  89              $filteroption = (object)[
  90                  'value' => $value,
  91                  'label' => $label
  92              ];
  93              $filteroption->selected = $selected;
  94              $data->filteroptions[] = $filteroption;
  95          }
  96          return $data;
  97      }
  98  }