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  /**
  18   * Download a report
  19   *
  20   * @package   core_reportbuilder
  21   * @copyright 2021 David Matamoros <davidmc@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  use core_reportbuilder\permission;
  28  use core_reportbuilder\system_report_factory;
  29  
  30  require_once(__DIR__ . '/../config.php');
  31  
  32  require_login();
  33  
  34  $reportid = required_param('id', PARAM_INT);
  35  $download = required_param('download', PARAM_ALPHA);
  36  $parameters = optional_param('parameters', null, PARAM_RAW);
  37  
  38  $reportpersistent = new \core_reportbuilder\local\models\report($reportid);
  39  $context = $reportpersistent->get_context();
  40  
  41  $PAGE->set_context($context);
  42  $PAGE->set_url(new moodle_url('/reportbuilder/download.php'));
  43  
  44  if ($reportpersistent->get('type') === \core_reportbuilder\local\report\base::TYPE_SYSTEM_REPORT) {
  45      $parameters = (array) json_decode($parameters);
  46  
  47      // Re-create the exact report that is being downloaded.
  48      $systemreport = system_report_factory::create($reportpersistent->get('source'), $context, $reportpersistent->get('component'),
  49          $reportpersistent->get('area'), $reportpersistent->get('itemid'), $parameters);
  50  
  51      if (!$systemreport->can_be_downloaded()) {
  52          throw new \core_reportbuilder\report_access_exception();
  53      }
  54  
  55      // Combine original report parameters with 'download' parameter.
  56      $parameters['download'] = $download;
  57  
  58      $outputreport = new \core_reportbuilder\output\system_report($reportpersistent, $systemreport, $parameters);
  59      echo $PAGE->get_renderer('core_reportbuilder')->render($outputreport);
  60  } else {
  61      permission::require_can_view_report($reportpersistent);
  62  
  63      $customreport = new \core_reportbuilder\output\custom_report($reportpersistent, false, $download);
  64      echo $PAGE->get_renderer('core_reportbuilder')->render($customreport);
  65  }