| 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 renderer_base;
  | 
        
        
            | 
            | 
           22 | 
           use core\external\exporter;
  | 
        
        
            | 
            | 
           23 | 
           use core_reportbuilder\datasource;
  | 
        
        
            | 
            | 
           24 | 
           use core_reportbuilder\table\custom_report_table_view;
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           /**
  | 
        
        
            | 
            | 
           27 | 
            * Custom report data exporter class
  | 
        
        
            | 
            | 
           28 | 
            *
  | 
        
        
            | 
            | 
           29 | 
            * @package     core_reportbuilder
  | 
        
        
            | 
            | 
           30 | 
            * @copyright   2022 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           31 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
            | 
            | 
           33 | 
           class custom_report_data_exporter extends exporter {
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
               /**
  | 
        
        
            | 
            | 
           36 | 
                * Return a list of objects that are related to the exporter
  | 
        
        
            | 
            | 
           37 | 
                *
  | 
        
        
            | 
            | 
           38 | 
                * @return array
  | 
        
        
            | 
            | 
           39 | 
                */
  | 
        
        
            | 
            | 
           40 | 
               protected static function define_related(): array {
  | 
        
        
            | 
            | 
           41 | 
                   return [
  | 
        
        
            | 
            | 
           42 | 
                       'report' => datasource::class,
  | 
        
        
            | 
            | 
           43 | 
                       'page' => 'int',
  | 
        
        
            | 
            | 
           44 | 
                       'perpage' => 'int',
  | 
        
        
            | 
            | 
           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 | 
                       'headers' => [
  | 
        
        
            | 
            | 
           56 | 
                           'type' => PARAM_RAW,
  | 
        
        
            | 
            | 
           57 | 
                           'multiple' => true,
  | 
        
        
            | 
            | 
           58 | 
                       ],
  | 
        
        
            | 
            | 
           59 | 
                       'rows' => [
  | 
        
        
            | 
            | 
           60 | 
                           'type' => [
  | 
        
        
            | 
            | 
           61 | 
                               'columns' => [
  | 
        
        
            | 
            | 
           62 | 
                                   'type' => PARAM_RAW,
  | 
        
        
            | 
            | 
           63 | 
                                   'null' => NULL_ALLOWED,
  | 
        
        
            | 
            | 
           64 | 
                                   'multiple' => true,
  | 
        
        
            | 
            | 
           65 | 
                               ],
  | 
        
        
            | 
            | 
           66 | 
                           ],
  | 
        
        
            | 
            | 
           67 | 
                           'multiple' => true,
  | 
        
        
            | 
            | 
           68 | 
                       ],
  | 
        
        
            | 
            | 
           69 | 
                       'totalrowcount' => ['type' => PARAM_INT],
  | 
        
        
            | 
            | 
           70 | 
                   ];
  | 
        
        
            | 
            | 
           71 | 
               }
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
               /**
  | 
        
        
            | 
            | 
           74 | 
                * Get the additional values to inject while exporting
  | 
        
        
            | 
            | 
           75 | 
                *
  | 
        
        
            | 
            | 
           76 | 
                * @param renderer_base $output
  | 
        
        
            | 
            | 
           77 | 
                * @return array
  | 
        
        
            | 
            | 
           78 | 
                */
  | 
        
        
            | 
            | 
           79 | 
               protected function get_other_values(renderer_base $output): array {
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
                   /** @var datasource $report */
  | 
        
        
            | 
            | 
           82 | 
                   $report = $this->related['report'];
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   $table = custom_report_table_view::create($report->get_report_persistent()->get('id'));
  | 
        
        
            | 
            | 
           85 | 
                   $table->setup();
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                   // Internally the current page is zero-based, but this method expects value plus one.
  | 
        
        
            | 
            | 
           88 | 
                   $table->set_page_number($this->related['page'] + 1);
  | 
        
        
            | 
            | 
           89 | 
                   $table->query_db($this->related['perpage'], false);
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   $tablerows = [];
  | 
        
        
            | 
            | 
           92 | 
                   foreach ($table->rawdata as $record) {
  | 
        
        
            | 
            | 
           93 | 
                       $tablerows[] = [
  | 
        
        
            | 
            | 
           94 | 
                           'columns' => array_values($table->format_row($record)),
  | 
        
        
            | 
            | 
           95 | 
                       ];
  | 
        
        
            | 
            | 
           96 | 
                   }
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                   $table->close_recordset();
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
                   return [
  | 
        
        
            | 
            | 
           101 | 
                       'headers' => $table->headers,
  | 
        
        
            | 
            | 
           102 | 
                       'rows' => $tablerows,
  | 
        
        
           | 1441 | 
           ariadna | 
           103 | 
                       'totalrowcount' => $table->totalrows,
  | 
        
        
           | 1 | 
           efrain | 
           104 | 
                   ];
  | 
        
        
            | 
            | 
           105 | 
               }
  | 
        
        
            | 
            | 
           106 | 
           }
  |