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] [Versions 402 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\external\exporter;
  23  use core_reportbuilder\datasource;
  24  use core_reportbuilder\form\condition;
  25  use core_reportbuilder\local\report\filter;
  26  
  27  /**
  28   * Custom report conditions exporter class
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class custom_report_conditions_exporter extends exporter {
  35  
  36      /**
  37       * Return a list of objects that are related to the exporter
  38       *
  39       * @return array
  40       */
  41      protected static function define_related(): array {
  42          return [
  43              'report' => datasource::class,
  44          ];
  45      }
  46  
  47      /**
  48       * Return the list of additional properties for read structure and export
  49       *
  50       * @return array[]
  51       */
  52      protected static function define_other_properties(): array {
  53          return [
  54              'hasavailableconditions' => [
  55                  'type' => PARAM_BOOL,
  56              ],
  57              'availableconditions' => [
  58                  'type' => [
  59                      'optiongroup' => [
  60                          'type' => [
  61                              'text' => ['type' => PARAM_TEXT],
  62                              'values' => [
  63                                  'type' => [
  64                                      'value' => ['type' => PARAM_TEXT],
  65                                      'visiblename' => ['type' => PARAM_TEXT],
  66                                  ],
  67                                  'multiple' => true,
  68                              ],
  69                          ],
  70                      ],
  71                  ],
  72                  'multiple' => true,
  73              ],
  74              'hasactiveconditions' => [
  75                  'type' => PARAM_BOOL,
  76              ],
  77              'activeconditionsform' => [
  78                  'type' => PARAM_RAW,
  79              ],
  80              'helpicon' => [
  81                  'type' => PARAM_RAW,
  82              ],
  83              'javascript' => [
  84                  'type' => PARAM_RAW,
  85                  'optional' => true,
  86              ],
  87          ];
  88      }
  89  
  90      /**
  91       * Get the additional values to inject while exporting
  92       *
  93       * @param renderer_base $output
  94       * @return array
  95       */
  96      protected function get_other_values(renderer_base $output): array {
  97          /** @var datasource $report */
  98          $report = $this->related['report'];
  99  
 100          // Current condition instances contained in the report.
 101          $conditions = $report->get_active_conditions();
 102          $conditionidentifiers = array_map(static function(filter $condition): string {
 103              return $condition->get_unique_identifier();
 104          }, $conditions);
 105  
 106          $availableconditions = [];
 107  
 108          // Populate available conditions.
 109          foreach ($report->get_conditions() as $condition) {
 110  
 111              // Conditions can only be added once per report, skip if it already exists.
 112              if (in_array($condition->get_unique_identifier(), $conditionidentifiers) || $condition->get_is_deprecated()) {
 113                  continue;
 114              }
 115  
 116              $entityname = $condition->get_entity_name();
 117              if (!array_key_exists($entityname, $availableconditions)) {
 118                  $availableconditions[$entityname] = [
 119                      'optiongroup' => [
 120                          'text' => $report->get_entity_title($entityname)->out(),
 121                          'values' => [],
 122                      ],
 123                  ];
 124              }
 125  
 126              $availableconditions[$entityname]['optiongroup']['values'][] = [
 127                  'value' => $condition->get_unique_identifier(),
 128                  'visiblename' => $condition->get_header(),
 129              ];
 130          }
 131  
 132          // Generate conditions form if any present.
 133          $conditionspresent = !empty($conditions);
 134          if ($conditionspresent) {
 135              $conditionsform = new condition(null, null, 'post', '', [], true, [
 136                  'reportid' => $report->get_report_persistent()->get('id'),
 137              ]);
 138              $conditionsform->set_data_for_dynamic_submission();
 139          }
 140  
 141          return [
 142              'hasavailableconditions' => !empty($availableconditions),
 143              'availableconditions' => array_values($availableconditions),
 144              'hasactiveconditions' => $conditionspresent,
 145              'activeconditionsform' => $conditionspresent ? $conditionsform->render() : '',
 146              'helpicon' => $output->help_icon('conditions', 'core_reportbuilder'),
 147          ];
 148      }
 149  }