Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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 core\external\persistent_exporter;
  22  use core_table\local\filter\integer_filter;
  23  use core_table\local\filter\string_filter;
  24  use core_reportbuilder\system_report;
  25  use core_reportbuilder\form\filter;
  26  use core_reportbuilder\local\models\report;
  27  use core_reportbuilder\table\system_report_table;
  28  use core_reportbuilder\table\system_report_table_filterset;
  29  use renderer_base;
  30  
  31  /**
  32   * Report exporter class
  33   *
  34   * @package     core_reportbuilder
  35   * @copyright   2020 Paul Holden <paulh@moodle.com>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class system_report_exporter extends persistent_exporter {
  39  
  40      /**
  41       * Return the name of the class we are exporting
  42       *
  43       * @return string
  44       */
  45      protected static function define_class(): string {
  46          return report::class;
  47      }
  48  
  49      /**
  50       * Return a list of objects that are related to the persistent
  51       *
  52       * @return array
  53       */
  54      protected static function define_related(): array {
  55          return [
  56              'source' => system_report::class,
  57              'parameters' => 'string',
  58          ];
  59      }
  60  
  61      /**
  62       * Return a list of additional properties used only for display
  63       *
  64       * @return array
  65       */
  66      protected static function define_other_properties(): array {
  67          return [
  68              'table' => ['type' => PARAM_RAW],
  69              'parameters' => ['type' => PARAM_RAW],
  70              'filterspresent' => ['type' => PARAM_BOOL],
  71              'filtersapplied' => ['type' => PARAM_INT],
  72              'filtersform' => ['type' => PARAM_RAW],
  73              'attributes' => [
  74                  'type' => [
  75                      'name' => ['type' => PARAM_TEXT],
  76                      'value' => ['type' => PARAM_TEXT]
  77                  ],
  78                  'multiple' => true,
  79              ],
  80              'classes' => ['type' => PARAM_TEXT],
  81          ];
  82      }
  83  
  84      /**
  85       * Get additional values to inject while exporting
  86       *
  87       * @uses \core_reportbuilder\output\renderer::render_system_report_table()
  88       *
  89       * @param renderer_base $output
  90       * @return array
  91       */
  92      protected function get_other_values(renderer_base $output): array {
  93          /** @var system_report $source */
  94          $source = $this->related['source'];
  95  
  96          /** @var string $parameters */
  97          $parameters = $this->related['parameters'];
  98  
  99          /** @var int $reportid */
 100          $reportid = $this->persistent->get('id');
 101  
 102          // We store the report ID and parameters within the table filterset so that they are available between AJAX requests.
 103          $filterset = new system_report_table_filterset();
 104          $filterset->add_filter(new integer_filter('reportid', null, [$reportid]));
 105          $filterset->add_filter(new string_filter('parameters', null, [$parameters]));
 106  
 107          $table = system_report_table::create($reportid, (array) json_decode($parameters, true));
 108          $table->set_filterset($filterset);
 109  
 110          // Generate filters form if report uses the default form, and contains any filters.
 111          $filterspresent = $source->get_filter_form_default() && !empty($source->get_active_filters());
 112          if ($filterspresent) {
 113              $filtersform = new filter(null, null, 'post', '', [], true, [
 114                  'reportid' => $reportid,
 115                  'parameters' => $parameters,
 116              ]);
 117              $filtersform->set_data_for_dynamic_submission();
 118          }
 119  
 120          // Get the report classes and attributes.
 121          $sourceattributes = $source->get_attributes();
 122          if (isset($sourceattributes['class'])) {
 123              $classes = $sourceattributes['class'];
 124              unset($sourceattributes['class']);
 125          }
 126          $attributes = array_map(static function($key, $value): array {
 127              return ['name' => $key, 'value' => $value];
 128          }, array_keys($sourceattributes), $sourceattributes);
 129  
 130          return [
 131              'table' => $output->render($table),
 132              'parameters' => $parameters,
 133              'filterspresent' => $filterspresent,
 134              'filtersapplied' => $source->get_applied_filter_count(),
 135              'filtersform' => $filterspresent ? $filtersform->render() : '',
 136              'attributes' => $attributes,
 137              'classes' => $classes ?? '',
 138          ];
 139      }
 140  }