Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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_reportbuilder\local\helpers\report as report_helper;
  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          ];
  82      }
  83  
  84      /**
  85       * Return a list of additional properties used only for display
  86       *
  87       * @return array
  88       */
  89      protected static function define_other_properties(): array {
  90          return [
  91              'table' => ['type' => PARAM_RAW],
  92              'sidebarmenucards' => ['type' => custom_report_menu_cards_exporter::read_properties_definition()],
  93              'conditions' => ['type' => custom_report_conditions_exporter::read_properties_definition()],
  94              'filters' => ['type' => custom_report_filters_exporter::read_properties_definition()],
  95              'sorting' => ['type' => custom_report_columns_sorting_exporter::read_properties_definition()],
  96              'cardview' => ['type' => custom_report_card_view_exporter::read_properties_definition()],
  97              'filtersapplied' => ['type' => PARAM_INT],
  98              'filterspresent' => ['type' => PARAM_BOOL],
  99              'filtersform' => [
 100                  'type' => PARAM_RAW,
 101                  'optional' => true,
 102              ],
 103              'editmode' => ['type' => PARAM_INT],
 104              'javascript' => ['type' => PARAM_RAW],
 105          ];
 106      }
 107  
 108      /**
 109       * Get additional values to inject while exporting
 110       *
 111       * @param renderer_base $output
 112       * @return array
 113       */
 114      protected function get_other_values(renderer_base $output): array {
 115          $filterspresent = false;
 116          $filtersform = '';
 117  
 118          if ($this->editmode) {
 119              $table = custom_report_table::create($this->persistent->get('id'));
 120              $table->set_filterset(new custom_report_table_filterset());
 121          } else {
 122              $table = custom_report_table_view::create($this->persistent->get('id'), $this->download);
 123              $table->set_filterset(new custom_report_table_view_filterset());
 124  
 125              // Generate filters form if report contains any filters.
 126              $source = $this->persistent->get('source');
 127              /** @var datasource $datasource */
 128              $datasource = new $source($this->persistent);
 129  
 130              $filterspresent = !empty($datasource->get_active_filters());
 131              if ($filterspresent) {
 132                  $filtersform = $this->generate_filters_form()->render();
 133              }
 134          }
 135  
 136          $report = manager::get_report_from_persistent($this->persistent);
 137  
 138          // If we are editing we need all this information for the template.
 139          if ($this->editmode) {
 140              $menucardexporter = new custom_report_menu_cards_exporter(null, [
 141                  'menucards' => report_helper::get_available_columns($report->get_report_persistent())
 142              ]);
 143  
 144              $menucards = (array) $menucardexporter->export($output);
 145              $conditionsexporter = new custom_report_conditions_exporter(null, ['report' => $report]);
 146              $conditions = (array) $conditionsexporter->export($output);
 147              $filtersexporter = new custom_report_filters_exporter(null, ['report' => $report]);
 148              $filters = (array) $filtersexporter->export($output);
 149              $sortingexporter = new custom_report_columns_sorting_exporter(null, ['report' => $report]);
 150              $sorting = (array) $sortingexporter->export($output);
 151              $cardviewexporter = new custom_report_card_view_exporter(null, ['report' => $report]);
 152              $cardview = (array) $cardviewexporter->export($output);
 153          }
 154  
 155          return [
 156              'table' => $output->render($table),
 157              'sidebarmenucards' => $menucards ?? [],
 158              'conditions' => $conditions ?? [],
 159              'filters' => $filters ?? [],
 160              'sorting' => $sorting ?? [],
 161              'cardview' => $cardview ?? [],
 162              'filtersapplied' => $report->get_applied_filter_count(),
 163              'filterspresent' => $filterspresent,
 164              'filtersform' => $filtersform,
 165              'editmode' => (int)$this->editmode,
 166              'javascript' => '',
 167          ];
 168      }
 169  
 170      /**
 171       * Generate filters form for the report
 172       *
 173       * @return form_filter
 174       */
 175      private function generate_filters_form(): form_filter {
 176          $filtersform = new form_filter(null, null, 'post', '', [], true, [
 177              'reportid' => $this->persistent->get('id'),
 178              'parameters' => json_encode([]),
 179          ]);
 180          $filtersform->set_data_for_dynamic_submission();
 181  
 182          return $filtersform;
 183      }
 184  }