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.

Differences Between: [Versions 401 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\reports;
  20  
  21  use core_reportbuilder\manager;
  22  use core_external\external_api;
  23  use core_external\external_value;
  24  use core_external\external_single_structure;
  25  use core_external\external_function_parameters;
  26  use core_external\external_warnings;
  27  use core_reportbuilder\permission;
  28  use core_reportbuilder\external\{custom_report_data_exporter, custom_report_details_exporter};
  29  
  30  /**
  31   * External method for retrieving custom report content
  32   *
  33   * @package     core_reportbuilder
  34   * @copyright   2022 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              'reportid' => new external_value(PARAM_INT, 'Report ID'),
  47              'page' => new external_value(PARAM_INT, 'Page number', VALUE_DEFAULT, 0),
  48              'perpage' => new external_value(PARAM_INT, 'Reports per page', VALUE_DEFAULT, 10),
  49          ]);
  50      }
  51  
  52      /**
  53       * External method execution
  54       *
  55       * @param int $reportid
  56       * @param int $page
  57       * @param int $perpage
  58       * @return array
  59       */
  60      public static function execute(int $reportid, int $page = 0, int $perpage = 10): array {
  61          global $PAGE;
  62  
  63          [
  64              'reportid' => $reportid,
  65              'page' => $page,
  66              'perpage' => $perpage,
  67          ] = self::validate_parameters(self::execute_parameters(), [
  68              'reportid' => $reportid,
  69              'page' => $page,
  70              'perpage' => $perpage,
  71          ]);
  72  
  73          $report = manager::get_report_from_id($reportid);
  74          self::validate_context($report->get_context());
  75  
  76          $persistent = $report->get_report_persistent();
  77          permission::require_can_view_report($persistent);
  78  
  79          $output = $PAGE->get_renderer('core');
  80  
  81          return [
  82              'details' => (array) (new custom_report_details_exporter($persistent))->export($output),
  83              'data' => (array) (new custom_report_data_exporter(null, [
  84                  'report' => $report, 'page' => $page, 'perpage' => $perpage,
  85              ]))->export($output),
  86              'warnings' => [],
  87          ];
  88      }
  89  
  90      /**
  91       * External method return value
  92       *
  93       * @return external_single_structure
  94       */
  95      public static function execute_returns(): external_single_structure {
  96          return new external_single_structure([
  97              'details' => custom_report_details_exporter::get_read_structure(),
  98              'data' => custom_report_data_exporter::get_read_structure(),
  99              'warnings' => new external_warnings(),
 100          ]);
 101      }
 102  }