| 1 | 
           efrain | 
           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 core_collator;
  | 
        
        
            | 
            | 
           22 | 
           use pix_icon;
  | 
        
        
            | 
            | 
           23 | 
           use renderer_base;
  | 
        
        
            | 
            | 
           24 | 
           use core\external\exporter;
  | 
        
        
            | 
            | 
           25 | 
           use core_reportbuilder\datasource;
  | 
        
        
            | 
            | 
           26 | 
           use core_reportbuilder\local\report\column;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           /**
  | 
        
        
            | 
            | 
           29 | 
            * Custom report columns sorting exporter class
  | 
        
        
            | 
            | 
           30 | 
            *
  | 
        
        
            | 
            | 
           31 | 
            * @package     core_reportbuilder
  | 
        
        
            | 
            | 
           32 | 
            * @copyright   2021 David Matamoros <davidmc@moodle.com>
  | 
        
        
            | 
            | 
           33 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           34 | 
            */
  | 
        
        
            | 
            | 
           35 | 
           class custom_report_columns_sorting_exporter extends exporter {
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /**
  | 
        
        
            | 
            | 
           38 | 
                * Return a list of objects that are related to the exporter
  | 
        
        
            | 
            | 
           39 | 
                *
  | 
        
        
            | 
            | 
           40 | 
                * @return array
  | 
        
        
            | 
            | 
           41 | 
                */
  | 
        
        
            | 
            | 
           42 | 
               protected static function define_related(): array {
  | 
        
        
            | 
            | 
           43 | 
                   return [
  | 
        
        
            | 
            | 
           44 | 
                       'report' => datasource::class,
  | 
        
        
            | 
            | 
           45 | 
                   ];
  | 
        
        
            | 
            | 
           46 | 
               }
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /**
  | 
        
        
            | 
            | 
           49 | 
                * Return the list of additional properties for read structure and export
  | 
        
        
            | 
            | 
           50 | 
                *
  | 
        
        
            | 
            | 
           51 | 
                * @return array[]
  | 
        
        
            | 
            | 
           52 | 
                */
  | 
        
        
            | 
            | 
           53 | 
               protected static function define_other_properties(): array {
  | 
        
        
            | 
            | 
           54 | 
                   return [
  | 
        
        
            | 
            | 
           55 | 
                       'hassortablecolumns' => [
  | 
        
        
            | 
            | 
           56 | 
                           'type' => PARAM_BOOL,
  | 
        
        
            | 
            | 
           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_BOOL],
  | 
        
        
            | 
            | 
           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 | 
                           'multiple' => true,
  | 
        
        
            | 
            | 
           77 | 
                       ],
  | 
        
        
            | 
            | 
           78 | 
                       'helpicon' => [
  | 
        
        
            | 
            | 
           79 | 
                           'type' => PARAM_RAW,
  | 
        
        
            | 
            | 
           80 | 
                       ],
  | 
        
        
            | 
            | 
           81 | 
                   ];
  | 
        
        
            | 
            | 
           82 | 
               }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
               /**
  | 
        
        
            | 
            | 
           85 | 
                * Get the additional values to inject while exporting
  | 
        
        
            | 
            | 
           86 | 
                *
  | 
        
        
            | 
            | 
           87 | 
                * @param renderer_base $output
  | 
        
        
            | 
            | 
           88 | 
                * @return array
  | 
        
        
            | 
            | 
           89 | 
                */
  | 
        
        
            | 
            | 
           90 | 
               protected function get_other_values(renderer_base $output): array {
  | 
        
        
            | 
            | 
           91 | 
                   /** @var datasource $report */
  | 
        
        
            | 
            | 
           92 | 
                   $report = $this->related['report'];
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   // Filter/retrieve all "sortable" active columns.
  | 
        
        
            | 
            | 
           95 | 
                   $sortablecolumns = array_filter($report->get_active_columns(), function(column $column): bool {
  | 
        
        
            | 
            | 
           96 | 
                       return $column->get_is_sortable();
  | 
        
        
            | 
            | 
           97 | 
                   });
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $sortablecolumns = array_map(function(column $column) use ($report): array {
  | 
        
        
            | 
            | 
           100 | 
                       $persistent = $column->get_persistent();
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                       $columntitle = $column->get_title();
  | 
        
        
            | 
            | 
           103 | 
                       $columnheading = $persistent->get_formatted_heading($report->get_context());
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                       $columnsortascending = ($persistent->get('sortdirection') == SORT_ASC);
  | 
        
        
            | 
            | 
           106 | 
                       $sortenabledtitle = $persistent->get('sortenabled') ? 'columnsortdisable' : 'columnsortenable';
  | 
        
        
            | 
            | 
           107 | 
                       $sortdirectiontitle = $columnsortascending ? 'columnsortdirectiondesc' : 'columnsortdirectionasc';
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                       $icon = $columnsortascending ? 't/uplong' : 't/downlong';
  | 
        
        
            | 
            | 
           110 | 
                       $sorticon = new pix_icon($icon, get_string($sortdirectiontitle, 'core_reportbuilder', $columntitle));
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                       return [
  | 
        
        
            | 
            | 
           113 | 
                           'id' => $persistent->get('id'),
  | 
        
        
            | 
            | 
           114 | 
                           'title' => $columntitle,
  | 
        
        
            | 
            | 
           115 | 
                           'heading' => $columnheading !== '' ? $columnheading : $columntitle,
  | 
        
        
            | 
            | 
           116 | 
                           'sortdirection' => $persistent->get('sortdirection'),
  | 
        
        
            | 
            | 
           117 | 
                           'sortenabled' => $persistent->get('sortenabled'),
  | 
        
        
            | 
            | 
           118 | 
                           'sortorder' => $persistent->get('sortorder'),
  | 
        
        
            | 
            | 
           119 | 
                           'sorticon' => $sorticon->export_for_pix(),
  | 
        
        
            | 
            | 
           120 | 
                           'movetitle' => get_string('movesorting', 'core_reportbuilder', $columntitle),
  | 
        
        
            | 
            | 
           121 | 
                           'sortenabledtitle' => get_string($sortenabledtitle, 'core_reportbuilder', $columntitle),
  | 
        
        
            | 
            | 
           122 | 
                       ];
  | 
        
        
            | 
            | 
           123 | 
                   }, $sortablecolumns);
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
                   core_collator::asort_array_of_arrays_by_key($sortablecolumns, 'sortorder', core_collator::SORT_NUMERIC);
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
                   return [
  | 
        
        
            | 
            | 
           128 | 
                       'hassortablecolumns' => !empty($sortablecolumns),
  | 
        
        
            | 
            | 
           129 | 
                       'sortablecolumns' => array_values($sortablecolumns),
  | 
        
        
            | 
            | 
           130 | 
                       'helpicon' => $output->help_icon('sorting', 'core_reportbuilder'),
  | 
        
        
            | 
            | 
           131 | 
                   ];
  | 
        
        
            | 
            | 
           132 | 
               }
  | 
        
        
            | 
            | 
           133 | 
           }
  |