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\external\exporter;
  23  use core_reportbuilder\form\condition;
  24  use core_reportbuilder\local\report\base;
  25  use core_reportbuilder\local\models\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' => base::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                  'optional' => true,
  57              ],
  58              'availableconditions' => [
  59                  'type' => [
  60                      'optiongroup' => [
  61                          'type' => [
  62                              'text' => ['type' => PARAM_TEXT],
  63                              'values' => [
  64                                  'type' => [
  65                                      'value' => ['type' => PARAM_TEXT],
  66                                      'visiblename' => ['type' => PARAM_TEXT],
  67                                  ],
  68                                  'multiple' => true,
  69                              ],
  70                          ],
  71                      ],
  72                  ],
  73                  'multiple' => true,
  74                  'optional' => true
  75              ],
  76              'hasactiveconditions' => [
  77                  'type' => PARAM_BOOL,
  78                  'optional' => true,
  79              ],
  80              'activeconditionsform' => [
  81                  'type' => PARAM_RAW,
  82                  'optional' => true,
  83              ],
  84              'helpicon' => [
  85                  'type' => PARAM_RAW,
  86                  'optional' => true,
  87              ],
  88              'javascript' => [
  89                  'type' => PARAM_RAW,
  90                  'optional' => true,
  91              ],
  92          ];
  93      }
  94  
  95      /**
  96       * Get the additional values to inject while exporting
  97       *
  98       * @param renderer_base $output
  99       * @return array
 100       */
 101      protected function get_other_values(renderer_base $output): array {
 102          /** @var base $report */
 103          $report = $this->related['report'];
 104          $reportid = $report->get_report_persistent()->get('id');
 105  
 106          // Current condition instances contained in the report.
 107          $conditioninstances = filter::get_condition_records($reportid, 'filterorder');
 108          $conditionidentifiers = array_map(static function(filter $condition): string {
 109              return $condition->get('uniqueidentifier');
 110          }, $conditioninstances);
 111  
 112          $availableconditions = [];
 113  
 114          // Populate available conditions.
 115          foreach ($report->get_conditions() as $condition) {
 116              if (in_array($condition->get_unique_identifier(), $conditionidentifiers)) {
 117                  continue;
 118              }
 119  
 120              $entityname = $condition->get_entity_name();
 121              if (!array_key_exists($entityname, $availableconditions)) {
 122                  $availableconditions[$entityname] = [
 123                      'optiongroup' => [
 124                          'text' => $report->get_entity_title($entityname)->out(),
 125                          'values' => [],
 126                      ],
 127                  ];
 128              }
 129  
 130              $availableconditions[$entityname]['optiongroup']['values'][] = [
 131                  'value' => $condition->get_unique_identifier(),
 132                  'visiblename' => $condition->get_header(),
 133              ];
 134          }
 135  
 136          // Generate filters form if report contains any filters.
 137          $conditionspresent = !empty($conditioninstances);
 138          if ($conditionspresent) {
 139              $conditionsform = new condition(null, null, 'post', '', [], true, [
 140                  'reportid' => $reportid,
 141              ]);
 142              $conditionsform->set_data_for_dynamic_submission();
 143          }
 144  
 145          return [
 146              'hasavailableconditions' => !empty($availableconditions),
 147              'availableconditions' => array_values($availableconditions),
 148              'hasactiveconditions' => $conditionspresent,
 149              'activeconditionsform' => $conditionspresent ? $conditionsform->render() : '',
 150              'helpicon' => $output->help_icon('conditions', 'core_reportbuilder'),
 151          ];
 152      }
 153  }