weekly release 5.0dev
[moodle.git] / reportbuilder / download.php
blobcb05a3d1ac15b0c4c98e2d8d9588a4c702d0ef15
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.
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/>.
17 /**
18 * Download a report
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
25 declare(strict_types=1);
27 use core_reportbuilder\permission;
28 use core_reportbuilder\system_report_factory;
30 require_once(__DIR__ . '/../config.php');
32 require_login();
34 $reportid = required_param('id', PARAM_INT);
35 $download = required_param('download', PARAM_ALPHA);
36 $parameters = optional_param('parameters', null, PARAM_RAW);
38 $reportpersistent = new \core_reportbuilder\local\models\report($reportid);
39 $context = $reportpersistent->get_context();
41 $PAGE->set_context($context);
42 $PAGE->set_url(new moodle_url('/reportbuilder/download.php'));
44 if ($reportpersistent->get('type') === \core_reportbuilder\local\report\base::TYPE_SYSTEM_REPORT) {
45 $parameters = (array) json_decode($parameters);
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);
51 if (!$systemreport->can_be_downloaded()) {
52 throw new \core_reportbuilder\exception\report_access_exception();
55 // Combine original report parameters with 'download' parameter.
56 $parameters['download'] = $download;
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);
63 $customreport = new \core_reportbuilder\output\custom_report($reportpersistent, false, $download);
64 echo $PAGE->get_renderer('core_reportbuilder')->render($customreport);