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 pix_icon;
  22  use renderer_base;
  23  use core\external\exporter;
  24  use core_reportbuilder\local\models\column;
  25  use core_reportbuilder\local\report\base;
  26  
  27  /**
  28   * Custom report columns sorting exporter class
  29   *
  30   * @package     core_reportbuilder
  31   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class custom_report_columns_sorting_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              'hassortablecolumns' => [
  55                  'type' => PARAM_BOOL,
  56                  'optional' => true,
  57              ],
  58              'sortablecolumns' => [
  59                  'type' => [
  60                      'id' => ['type' => PARAM_INT],
  61                      'title' => ['type' => PARAM_TEXT],
  62                      'heading' => ['type' => PARAM_TEXT],
  63                      'sortdirection' => ['type' => PARAM_INT],
  64                      'sortenabled' => ['type' => PARAM_INT],
  65                      'sortorder' => ['type' => PARAM_INT],
  66                      'sorticon' => [
  67                          'type' => [
  68                              'key' => ['type' => PARAM_RAW],
  69                              'component' => ['type' => PARAM_COMPONENT],
  70                              'title' => ['type' => PARAM_NOTAGS],
  71                          ],
  72                      ],
  73                      'movetitle' => ['type' => PARAM_TEXT],
  74                      'sortenabledtitle' => ['type' => PARAM_TEXT],
  75                  ],
  76                  'optional' => true,
  77                  'multiple' => true,
  78              ],
  79              'helpicon' => [
  80                  'type' => PARAM_RAW,
  81                  'optional' => true,
  82              ],
  83          ];
  84      }
  85  
  86      /**
  87       * Get the additional values to inject while exporting
  88       *
  89       * @param renderer_base $output
  90       * @return array
  91       */
  92      protected function get_other_values(renderer_base $output): array {
  93          /** @var base $report */
  94          $report = $this->related['report'];
  95  
  96          $reportid = $report->get_report_persistent()->get('id');
  97          $activecolumns = column::get_records(['reportid' => $reportid], 'sortorder');
  98          $sortablecolumns = array_filter($activecolumns, function(column $persistent) use($report): bool {
  99              $column = $report->get_column($persistent->get('uniqueidentifier'));
 100              if ($column === null) {
 101                  return false;
 102              }
 103  
 104              return $column->set_aggregation($persistent->get('aggregation'))->get_is_sortable();
 105          });
 106  
 107          $sortablecolumns = array_map(function(column $persistent) use ($report) {
 108              $columntitle = $report->get_column($persistent->get('uniqueidentifier'))->get_title();
 109              $columnheading = $persistent->get_formatted_heading($report->get_context());
 110  
 111              $columnsortascending = ($persistent->get('sortdirection') == SORT_ASC);
 112              $sortenabledtitle = $persistent->get('sortenabled') ? 'columnsortdisable' : 'columnsortenable';
 113              $sortdirectiontitle = $columnsortascending ? 'columnsortdirectiondesc' : 'columnsortdirectionasc';
 114  
 115              $icon = $columnsortascending ? 't/uplong' : 't/downlong';
 116              $sorticon = new pix_icon($icon, get_string($sortdirectiontitle, 'core_reportbuilder', $columntitle));
 117  
 118              return [
 119                  'id' => $persistent->get('id'),
 120                  'title' => $columntitle,
 121                  'heading' => $columnheading !== '' ? $columnheading : $columntitle,
 122                  'sortdirection' => $persistent->get('sortdirection'),
 123                  'sortenabled' => (int)$persistent->get('sortenabled'),
 124                  'sortorder' => $persistent->get('sortorder'),
 125                  'sorticon' => $sorticon->export_for_pix(),
 126                  'movetitle' => get_string('movesorting', 'core_reportbuilder', $columntitle),
 127                  'sortenabledtitle' => get_string($sortenabledtitle, 'core_reportbuilder', $columntitle),
 128              ];
 129          }, $sortablecolumns);
 130  
 131          return [
 132              'hassortablecolumns' => !empty($sortablecolumns),
 133              'sortablecolumns' => array_values($sortablecolumns),
 134              'helpicon' => $output->help_icon('sorting', 'core_reportbuilder'),
 135          ];
 136      }
 137  }