Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [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              'attributes' => [
  97                  'type' => [
  98                      'name' => ['type' => PARAM_TEXT],
  99                      'value' => ['type' => PARAM_TEXT]
 100                  ],
 101                  'multiple' => true,
 102              ],
 103              'classes' => ['type' => PARAM_TEXT],
 104              'editmode' => ['type' => PARAM_BOOL],
 105              'sidebarmenucards' => [
 106                  'type' => custom_report_column_cards_exporter::read_properties_definition(),
 107                  'optional' => true,
 108              ],
 109              'conditions' => [
 110                  'type' => custom_report_conditions_exporter::read_properties_definition(),
 111                  'optional' => true,
 112              ],
 113              'filters' => [
 114                  'type' => custom_report_filters_exporter::read_properties_definition(),
 115                  'optional' => true,
 116              ],
 117              'sorting' => [
 118                  'type' => custom_report_columns_sorting_exporter::read_properties_definition(),
 119                  'optional' => true,
 120              ],
 121              'cardview' => [
 122                  'type' => custom_report_card_view_exporter::read_properties_definition(),
 123                  'optional' => true,
 124              ],
 125              'javascript' => ['type' => PARAM_RAW],
 126          ];
 127      }
 128  
 129      /**
 130       * Get additional values to inject while exporting
 131       *
 132       * @param renderer_base $output
 133       * @return array
 134       */
 135      protected function get_other_values(renderer_base $output): array {
 136          /** @var datasource $report */
 137          $report = manager::get_report_from_persistent($this->persistent);
 138  
 139          $filterspresent = false;
 140          $filtersform = '';
 141          $attributes = [];
 142  
 143          if ($this->editmode) {
 144              $table = custom_report_table::create($this->persistent->get('id'));
 145              $table->set_filterset(new custom_report_table_filterset());
 146          } else {
 147              // We store the pagesize within the table filterset so that it's available between AJAX requests.
 148              $filterset = new custom_report_table_view_filterset();
 149              $filterset->add_filter(new integer_filter('pagesize', null, [$this->related['pagesize']]));
 150  
 151              $table = custom_report_table_view::create($this->persistent->get('id'), $this->download);
 152              $table->set_filterset($filterset);
 153  
 154              // Generate filters form if report contains any filters.
 155              $filterspresent = !empty($report->get_active_filters());
 156              if ($filterspresent) {
 157                  $filtersform = $this->generate_filters_form()->render();
 158              }
 159  
 160              // Get the report classes and attributes.
 161              $reportattributes = $report->get_attributes();
 162              if (isset($reportattributes['class'])) {
 163                  $classes = $reportattributes['class'];
 164                  unset($reportattributes['class']);
 165              }
 166              $attributes = array_map(static function($key, $value): array {
 167                  return ['name' => $key, 'value' => $value];
 168              }, array_keys($reportattributes), $reportattributes);
 169          }
 170  
 171          // If we are editing we need all this information for the template.
 172          $editordata = [];
 173          if ($this->editmode) {
 174              $menucardsexporter = new custom_report_column_cards_exporter(null, ['report' => $report]);
 175              $editordata['sidebarmenucards'] = (array) $menucardsexporter->export($output);
 176  
 177              $conditionsexporter = new custom_report_conditions_exporter(null, ['report' => $report]);
 178              $editordata['conditions'] = (array) $conditionsexporter->export($output);
 179  
 180              $filtersexporter = new custom_report_filters_exporter(null, ['report' => $report]);
 181              $editordata['filters'] = (array) $filtersexporter->export($output);
 182  
 183              $sortingexporter = new custom_report_columns_sorting_exporter(null, ['report' => $report]);
 184              $editordata['sorting'] = (array) $sortingexporter->export($output);
 185  
 186              $cardviewexporter = new custom_report_card_view_exporter(null, ['report' => $report]);
 187              $editordata['cardview'] = (array) $cardviewexporter->export($output);
 188          }
 189  
 190          return [
 191              'table' => $output->render($table),
 192              'filtersapplied' => $report->get_applied_filter_count(),
 193              'filterspresent' => $filterspresent,
 194              'filtersform' => $filtersform,
 195              'attributes' => $attributes,
 196              'classes' => $classes ?? '',
 197              'editmode' => $this->editmode,
 198              'javascript' => '',
 199          ] + $editordata;
 200      }
 201  
 202      /**
 203       * Generate filters form for the report
 204       *
 205       * @return form_filter
 206       */
 207      private function generate_filters_form(): form_filter {
 208          $filtersform = new form_filter(null, null, 'post', '', [], true, [
 209              'reportid' => $this->persistent->get('id'),
 210              'parameters' => json_encode([]),
 211          ]);
 212          $filtersform->set_data_for_dynamic_submission();
 213  
 214          return $filtersform;
 215      }
 216  }