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.
   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\systemreports;
  20  
  21  use core_external\external_api;
  22  use core_external\external_multiple_structure;
  23  use core_external\external_single_structure;
  24  use core_external\external_function_parameters;
  25  use core_external\external_value;
  26  use core_external\external_warnings;
  27  use core_reportbuilder\system_report_factory;
  28  use core_reportbuilder\external\system_report_data_exporter;
  29  
  30  /**
  31   * External method for retrieving system report content
  32   *
  33   * @package     core_reportbuilder
  34   * @copyright   2023 Paul Holden <paulh@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class retrieve extends external_api {
  38  
  39      /**
  40       * External method parameters
  41       *
  42       * @return external_function_parameters
  43       */
  44      public static function execute_parameters(): external_function_parameters {
  45          return new external_function_parameters([
  46              'source' => new external_value(PARAM_RAW, 'Report class path'),
  47              'context' => self::get_context_parameters(),
  48              'component' => new external_value(PARAM_COMPONENT, 'Report component', VALUE_DEFAULT, ''),
  49              'area' => new external_value(PARAM_AREA, 'Report area', VALUE_DEFAULT, ''),
  50              'itemid' => new external_value(PARAM_INT, 'Report item ID', VALUE_DEFAULT, 0),
  51              'parameters' => new external_multiple_structure(
  52                  new external_single_structure([
  53                      'name' => new external_value(PARAM_RAW),
  54                      'value' => new external_value(PARAM_RAW),
  55                  ]),
  56                  'Report parameters', VALUE_DEFAULT, []
  57              ),
  58              'page' => new external_value(PARAM_INT, 'Page number', VALUE_DEFAULT, 0),
  59              'perpage' => new external_value(PARAM_INT, 'Reports per page', VALUE_DEFAULT, 10),
  60          ]);
  61      }
  62  
  63      /**
  64       * External method execution
  65       *
  66       * @param string $source
  67       * @param array $context
  68       * @param string $component
  69       * @param string $area
  70       * @param int $itemid
  71       * @param array[] $parameters
  72       * @param int $page
  73       * @param int $perpage
  74       * @return array[]
  75       */
  76      public static function execute(
  77          string $source,
  78          array $context,
  79          string $component = '',
  80          string $area = '',
  81          int $itemid = 0,
  82          array $parameters = [],
  83          int $page = 0,
  84          int $perpage = 10,
  85      ): array {
  86          global $PAGE;
  87  
  88          [
  89              'source' => $source,
  90              'context' => $context,
  91              'component' => $component,
  92              'area' => $area,
  93              'itemid' => $itemid,
  94              'parameters' => $parameters,
  95              'page' => $page,
  96              'perpage' => $perpage,
  97          ] = self::validate_parameters(self::execute_parameters(), [
  98              'source' => $source,
  99              'context' => $context,
 100              'component' => $component,
 101              'area' => $area,
 102              'itemid' => $itemid,
 103              'parameters' => $parameters,
 104              'page' => $page,
 105              'perpage' => $perpage,
 106          ]);
 107  
 108          $context = self::get_context_from_params($context);
 109          self::validate_context($context);
 110  
 111          // Flatten the report parameters.
 112          $parameters = array_combine(array_column($parameters, 'name'), array_column($parameters, 'value'));
 113  
 114          $report = system_report_factory::create($source, $context, $component, $area, $itemid, $parameters);
 115          $report->require_can_view();
 116  
 117          $output = $PAGE->get_renderer('core');
 118  
 119          return [
 120              'data' => (array) (new system_report_data_exporter(null, [
 121                  'report' => $report, 'page' => $page, 'perpage' => $perpage,
 122              ]))->export($output),
 123              'warnings' => [],
 124          ];
 125      }
 126  
 127      /**
 128       * External method return value
 129       *
 130       * @return external_single_structure
 131       */
 132      public static function execute_returns(): external_single_structure {
 133          return new external_single_structure([
 134              'data' => system_report_data_exporter::get_read_structure(),
 135              'warnings' => new external_warnings(),
 136          ]);
 137      }
 138  }