Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\external;
  20  
  21  use renderer_base;
  22  use core\external\exporter;
  23  use core_reportbuilder\datasource;
  24  use core_reportbuilder\local\report\filter;
  25  use core_reportbuilder\output\filter_heading_editable;
  26  
  27  /**
  28   * Custom report filters exporter class
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class custom_report_filters_exporter extends exporter {
  35  
  36      /**
  37       * Return a list of objects that are related to the exporter
  38       *
  39       * @return array
  40       */
  41      protected static function define_related(): array {
  42          return [
  43              'report' => datasource::class,
  44          ];
  45      }
  46  
  47      /**
  48       * Return the list of additional properties for read structure and export
  49       *
  50       * @return array[]
  51       */
  52      protected static function define_other_properties(): array {
  53          return [
  54              'hasavailablefilters' => [
  55                  'type' => PARAM_BOOL,
  56              ],
  57              'availablefilters' => [
  58                  'type' => [
  59                      'optiongroup' => [
  60                          'type' => [
  61                              'text' => ['type' => PARAM_TEXT],
  62                              'values' => [
  63                                  'type' => [
  64                                      'value' => ['type' => PARAM_TEXT],
  65                                      'visiblename' => ['type' => PARAM_TEXT],
  66                                  ],
  67                                  'multiple' => true,
  68                              ],
  69                          ],
  70                      ],
  71                  ],
  72                  'multiple' => true,
  73              ],
  74              'hasactivefilters' => [
  75                  'type' => PARAM_BOOL,
  76              ],
  77              'activefilters' => [
  78                  'type' => [
  79                      'id' => ['type' => PARAM_INT],
  80                      'heading' => ['type' => PARAM_TEXT],
  81                      'headingeditable' => ['type' => PARAM_RAW],
  82                      'sortorder' => ['type' => PARAM_INT],
  83                      'movetitle' => ['type' => PARAM_TEXT],
  84                      'entityname' => ['type' => PARAM_TEXT],
  85                  ],
  86                  'multiple' => true,
  87              ],
  88              'helpicon' => [
  89                  'type' => PARAM_RAW,
  90              ],
  91          ];
  92      }
  93  
  94      /**
  95       * Get the additional values to inject while exporting
  96       *
  97       * @param renderer_base $output
  98       * @return array
  99       */
 100      protected function get_other_values(renderer_base $output): array {
 101          /** @var datasource $report */
 102          $report = $this->related['report'];
 103  
 104          // Current filter instances contained in the report.
 105          $filters = $report->get_active_filters();
 106          $filteridentifiers = array_map(static function(filter $filter): string {
 107              return $filter->get_unique_identifier();
 108          }, $filters);
 109  
 110          $availablefilters = $activefilters = [];
 111  
 112          // Populate available filters.
 113          foreach ($report->get_filters() as $filter) {
 114              if (in_array($filter->get_unique_identifier(), $filteridentifiers)) {
 115                  continue;
 116              }
 117  
 118              $entityname = $filter->get_entity_name();
 119              if (!array_key_exists($entityname, $availablefilters)) {
 120                  $availablefilters[$entityname] = [
 121                      'optiongroup' => [
 122                          'text' => $report->get_entity_title($entityname)->out(),
 123                          'values' => [],
 124                      ],
 125                  ];
 126              }
 127  
 128              $availablefilters[$entityname]['optiongroup']['values'][] = [
 129                  'value' => $filter->get_unique_identifier(),
 130                  'visiblename' => $filter->get_header(),
 131              ];
 132          }
 133  
 134          // Populate active filters.
 135          $filterinstances = $report->get_filter_instances();
 136          foreach ($filterinstances as $filterinstance) {
 137              $persistent = $filterinstance->get_filter_persistent();
 138  
 139              $entityname = $filterinstance->get_entity_name();
 140              $displayvalue = $filterinstance->get_header();
 141              $editable = new filter_heading_editable(0, $persistent);
 142  
 143              $activefilters[] = [
 144                  'id' => $persistent->get('id'),
 145                  'entityname' => $report->get_entity_title($entityname)->out(),
 146                  'heading' => $displayvalue,
 147                  'headingeditable' => $editable->render($output),
 148                  'sortorder' => $persistent->get('filterorder'),
 149                  'movetitle' => get_string('movefilter', 'core_reportbuilder', $displayvalue),
 150              ];
 151          }
 152  
 153          return [
 154              'hasavailablefilters' => !empty($availablefilters),
 155              'availablefilters' => array_values($availablefilters),
 156              'hasactivefilters' => !empty($activefilters),
 157              'activefilters' => $activefilters,
 158              'helpicon' => $output->help_icon('filters', 'core_reportbuilder'),
 159          ];
 160      }
 161  }