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 402] [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 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          ];
  74      }
  75  
  76      /**
  77       * Get additional values to inject while exporting
  78       *
  79       * @uses \core_reportbuilder\output\renderer::render_system_report_table()
  80       *
  81       * @param renderer_base $output
  82       * @return array
  83       */
  84      protected function get_other_values(renderer_base $output): array {
  85          /** @var system_report $source */
  86          $source = $this->related['source'];
  87  
  88          /** @var string $parameters */
  89          $parameters = $this->related['parameters'];
  90  
  91          /** @var int $reportid */
  92          $reportid = $this->persistent->get('id');
  93  
  94          // We store the report ID and parameters within the table filterset so that they are available between AJAX requests.
  95          $filterset = new system_report_table_filterset();
  96          $filterset->add_filter(new integer_filter('reportid', null, [$reportid]));
  97          $filterset->add_filter(new string_filter('parameters', null, [$parameters]));
  98  
  99          $table = system_report_table::create($reportid, (array) json_decode($parameters, true));
 100          $table->set_filterset($filterset);
 101  
 102          // Generate filters form if report uses the default form, and contains any filters.
 103          $filterspresent = $source->get_filter_form_default() && !empty($source->get_active_filters());
 104          if ($filterspresent) {
 105              $filtersform = new filter(null, null, 'post', '', [], true, [
 106                  'reportid' => $reportid,
 107                  'parameters' => $parameters,
 108              ]);
 109              $filtersform->set_data_for_dynamic_submission();
 110          }
 111  
 112          return [
 113              'table' => $output->render($table),
 114              'parameters' => $parameters,
 115              'filterspresent' => $filterspresent,
 116              'filtersapplied' => $source->get_applied_filter_count(),
 117              'filtersform' => $filterspresent ? $filtersform->render() : '',
 118          ];
 119      }
 120  }