Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [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 external_api;
  23  use external_function_parameters;
  24  use external_single_structure;
  25  use external_value;
  26  use external_warnings;
  27  use core_reportbuilder\permission;
  28  use core_reportbuilder\external\{custom_report_data_exporter, custom_report_details_exporter};
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once("{$CFG->libdir}/externallib.php");
  34  
  35  /**
  36   * External method for retrieving custom report content
  37   *
  38   * @package     core_reportbuilder
  39   * @copyright   2022 Paul Holden <paulh@moodle.com>
  40   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class retrieve extends external_api {
  43  
  44      /**
  45       * External method parameters
  46       *
  47       * @return external_function_parameters
  48       */
  49      public static function execute_parameters(): external_function_parameters {
  50          return new external_function_parameters([
  51              'reportid' => new external_value(PARAM_INT, 'Report ID'),
  52              'page' => new external_value(PARAM_INT, 'Page number', VALUE_DEFAULT, 0),
  53              'perpage' => new external_value(PARAM_INT, 'Reports per page', VALUE_DEFAULT, 10),
  54          ]);
  55      }
  56  
  57      /**
  58       * External method execution
  59       *
  60       * @param int $reportid
  61       * @param int $page
  62       * @param int $perpage
  63       * @return array
  64       */
  65      public static function execute(int $reportid, int $page = 0, int $perpage = 10): array {
  66          global $PAGE;
  67  
  68          [
  69              'reportid' => $reportid,
  70              'page' => $page,
  71              'perpage' => $perpage,
  72          ] = self::validate_parameters(self::execute_parameters(), [
  73              'reportid' => $reportid,
  74              'page' => $page,
  75              'perpage' => $perpage,
  76          ]);
  77  
  78          $report = manager::get_report_from_id($reportid);
  79          self::validate_context($report->get_context());
  80  
  81          $persistent = $report->get_report_persistent();
  82          permission::require_can_view_report($persistent);
  83  
  84          $output = $PAGE->get_renderer('core');
  85  
  86          return [
  87              'details' => (array) (new custom_report_details_exporter($persistent))->export($output),
  88              'data' => (array) (new custom_report_data_exporter(null, [
  89                  'report' => $report, 'page' => $page, 'perpage' => $perpage,
  90              ]))->export($output),
  91              'warnings' => [],
  92          ];
  93      }
  94  
  95      /**
  96       * External method return value
  97       *
  98       * @return external_single_structure
  99       */
 100      public static function execute_returns(): external_single_structure {
 101          return new external_single_structure([
 102              'details' => custom_report_details_exporter::get_read_structure(),
 103              'data' => custom_report_data_exporter::get_read_structure(),
 104              'warnings' => new external_warnings(),
 105          ]);
 106      }
 107  }