Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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_external\external_api;
  22  use core_external\external_value;
  23  use core_external\external_single_structure;
  24  use core_external\external_function_parameters;
  25  use core_reportbuilder\manager;
  26  use core_reportbuilder\permission;
  27  use core_reportbuilder\output\custom_report;
  28  use core_reportbuilder\external\custom_report_exporter;
  29  use moodle_url;
  30  
  31  /**
  32   * External method for getting a custom report
  33   *
  34   * @package     core_reportbuilder
  35   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class get extends external_api {
  39  
  40      /**
  41       * External method parameters
  42       *
  43       * @return external_function_parameters
  44       */
  45      public static function execute_parameters(): external_function_parameters {
  46          return new external_function_parameters([
  47              'reportid' => new external_value(PARAM_INT, 'Report ID'),
  48              'editmode' => new external_value(PARAM_BOOL, 'Whether editing mode is enabled', VALUE_DEFAULT, 0),
  49              'pagesize' => new external_value(PARAM_INT, 'Page size', VALUE_DEFAULT, 0),
  50          ]);
  51      }
  52  
  53      /**
  54       * External method execution
  55       *
  56       * @param int $reportid
  57       * @param bool $editmode
  58       * @param int $pagesize
  59       * @return array
  60       */
  61      public static function execute(int $reportid, bool $editmode, int $pagesize = 0): array {
  62          global $PAGE, $OUTPUT;
  63  
  64          [
  65              'reportid' => $reportid,
  66              'editmode' => $editmode,
  67              'pagesize' => $pagesize,
  68          ] = self::validate_parameters(self::execute_parameters(), [
  69              'reportid' => $reportid,
  70              'editmode' => $editmode,
  71              'pagesize' => $pagesize,
  72          ]);
  73  
  74          $report = manager::get_report_from_id($reportid);
  75          if ($pagesize > 0) {
  76              $report->set_default_per_page($pagesize);
  77          }
  78          self::validate_context($report->get_context());
  79  
  80          if ($editmode) {
  81              permission::require_can_edit_report($report->get_report_persistent());
  82          } else {
  83              permission::require_can_view_report($report->get_report_persistent());
  84          }
  85  
  86          // Set current URL and force bootstrap_renderer to initiate moodle page.
  87          $PAGE->set_url(new moodle_url('/'));
  88          $OUTPUT->header();
  89          $PAGE->start_collecting_javascript_requirements();
  90  
  91          $renderer = $PAGE->get_renderer('core_reportbuilder');
  92          $context = (new custom_report($report->get_report_persistent(), $editmode))->export_for_template($renderer);
  93          $context->javascript = $PAGE->requires->get_end_code();
  94  
  95          return (array)$context;
  96      }
  97  
  98      /**
  99       * External method return value
 100       *
 101       * @return external_single_structure
 102       */
 103      public static function execute_returns(): external_single_structure {
 104          return custom_report_exporter::get_read_structure();
 105      }
 106  }