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