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 renderer_base;
  22  use core\persistent;
  23  use core\external\persistent_exporter;
  24  use core_reportbuilder\manager;
  25  use core_reportbuilder\datasource;
  26  use core_reportbuilder\form\filter as form_filter;
  27  use core_reportbuilder\local\models\report;
  28  use core_reportbuilder\table\custom_report_table;
  29  use core_reportbuilder\table\custom_report_table_filterset;
  30  use core_reportbuilder\table\custom_report_table_view;
  31  use core_reportbuilder\table\custom_report_table_view_filterset;
  32  use core_table\local\filter\integer_filter;
  33  
  34  /**
  35   * Custom report exporter class
  36   *
  37   * @package     core_reportbuilder
  38   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class custom_report_exporter extends persistent_exporter {
  42  
  43      /** @var report The persistent object we will export. */
  44      protected $persistent = null;
  45  
  46      /** @var bool */
  47      protected $editmode;
  48  
  49      /** @var string */
  50      protected $download;
  51  
  52      /**
  53       * report_exporter constructor.
  54       *
  55       * @param persistent $persistent
  56       * @param array $related
  57       * @param bool $editmode
  58       * @param string $download
  59       */
  60      public function __construct(persistent $persistent, array $related = [], bool $editmode = true, string $download = '') {
  61          parent::__construct($persistent, $related);
  62          $this->editmode = $editmode;
  63          $this->download = $download;
  64      }
  65      /**
  66       * Return the name of the class we are exporting
  67       *
  68       * @return string
  69       */
  70      protected static function define_class(): string {
  71          return report::class;
  72      }
  73  
  74      /**
  75       * Return a list of objects that are related to the persistent
  76       *
  77       * @return array
  78       */
  79      protected static function define_related(): array {
  80          return [
  81              'pagesize' => 'int?',
  82          ];
  83      }
  84  
  85      /**
  86       * Return a list of additional properties used only for display
  87       *
  88       * @return array
  89       */
  90      protected static function define_other_properties(): array {
  91          return [
  92              'table' => ['type' => PARAM_RAW],
  93              'filtersapplied' => ['type' => PARAM_INT],
  94              'filterspresent' => ['type' => PARAM_BOOL],
  95              'filtersform' => ['type' => PARAM_RAW],
  96              'editmode' => ['type' => PARAM_BOOL],
  97              'sidebarmenucards' => [
  98                  'type' => custom_report_column_cards_exporter::read_properties_definition(),
  99                  'optional' => true,
 100              ],
 101              'conditions' => [
 102                  'type' => custom_report_conditions_exporter::read_properties_definition(),
 103                  'optional' => true,
 104              ],
 105              'filters' => [
 106                  'type' => custom_report_filters_exporter::read_properties_definition(),
 107                  'optional' => true,
 108              ],
 109              'sorting' => [
 110                  'type' => custom_report_columns_sorting_exporter::read_properties_definition(),
 111                  'optional' => true,
 112              ],
 113              'cardview' => [
 114                  'type' => custom_report_card_view_exporter::read_properties_definition(),
 115                  'optional' => true,
 116              ],
 117              'javascript' => ['type' => PARAM_RAW],
 118          ];
 119      }
 120  
 121      /**
 122       * Get additional values to inject while exporting
 123       *
 124       * @param renderer_base $output
 125       * @return array
 126       */
 127      protected function get_other_values(renderer_base $output): array {
 128          /** @var datasource $report */
 129          $report = manager::get_report_from_persistent($this->persistent);
 130  
 131          $filterspresent = false;
 132          $filtersform = '';
 133  
 134          if ($this->editmode) {
 135              $table = custom_report_table::create($this->persistent->get('id'));
 136              $table->set_filterset(new custom_report_table_filterset());
 137          } else {
 138              // We store the pagesize within the table filterset so that it's available between AJAX requests.
 139              $filterset = new custom_report_table_view_filterset();
 140              $filterset->add_filter(new integer_filter('pagesize', null, [$this->related['pagesize']]));
 141  
 142              $table = custom_report_table_view::create($this->persistent->get('id'), $this->download);
 143              $table->set_filterset($filterset);
 144  
 145              // Generate filters form if report contains any filters.
 146              $filterspresent = !empty($report->get_active_filters());
 147              if ($filterspresent) {
 148                  $filtersform = $this->generate_filters_form()->render();
 149              }
 150          }
 151  
 152          // If we are editing we need all this information for the template.
 153          $editordata = [];
 154          if ($this->editmode) {
 155              $menucardsexporter = new custom_report_column_cards_exporter(null, ['report' => $report]);
 156              $editordata['sidebarmenucards'] = (array) $menucardsexporter->export($output);
 157  
 158              $conditionsexporter = new custom_report_conditions_exporter(null, ['report' => $report]);
 159              $editordata['conditions'] = (array) $conditionsexporter->export($output);
 160  
 161              $filtersexporter = new custom_report_filters_exporter(null, ['report' => $report]);
 162              $editordata['filters'] = (array) $filtersexporter->export($output);
 163  
 164              $sortingexporter = new custom_report_columns_sorting_exporter(null, ['report' => $report]);
 165              $editordata['sorting'] = (array) $sortingexporter->export($output);
 166  
 167              $cardviewexporter = new custom_report_card_view_exporter(null, ['report' => $report]);
 168              $editordata['cardview'] = (array) $cardviewexporter->export($output);
 169          }
 170  
 171          return [
 172              'table' => $output->render($table),
 173              'filtersapplied' => $report->get_applied_filter_count(),
 174              'filterspresent' => $filterspresent,
 175              'filtersform' => $filtersform,
 176              'editmode' => $this->editmode,
 177              'javascript' => '',
 178          ] + $editordata;
 179      }
 180  
 181      /**
 182       * Generate filters form for the report
 183       *
 184       * @return form_filter
 185       */
 186      private function generate_filters_form(): form_filter {
 187          $filtersform = new form_filter(null, null, 'post', '', [], true, [
 188              'reportid' => $this->persistent->get('id'),
 189              'parameters' => json_encode([]),
 190          ]);
 191          $filtersform->set_data_for_dynamic_submission();
 192  
 193          return $filtersform;
 194      }
 195  }