Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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\systemreports;
20
 
21
use core_external\external_api;
22
use core_external\external_multiple_structure;
23
use core_external\external_single_structure;
24
use core_external\external_function_parameters;
25
use core_external\external_value;
26
use core_reportbuilder\report_access_exception;
27
use core_reportbuilder\system_report_factory;
28
 
29
/**
30
 * External method for validating access to a system report
31
 *
32
 * @package     core_reportbuilder
33
 * @copyright   2023 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class can_view extends external_api {
37
 
38
    /**
39
     * External method parameters
40
     *
41
     * @return external_function_parameters
42
     */
43
    public static function execute_parameters(): external_function_parameters {
44
        return new external_function_parameters([
45
            'source' => new external_value(PARAM_RAW, 'Report class path'),
46
            'context' => self::get_context_parameters(),
47
            'component' => new external_value(PARAM_COMPONENT, 'Report component', VALUE_DEFAULT, ''),
48
            'area' => new external_value(PARAM_AREA, 'Report area', VALUE_DEFAULT, ''),
49
            'itemid' => new external_value(PARAM_INT, 'Report item ID', VALUE_DEFAULT, 0),
50
            'parameters' => new external_multiple_structure(
51
                new external_single_structure([
52
                    'name' => new external_value(PARAM_RAW),
53
                    'value' => new external_value(PARAM_RAW),
54
                ]),
55
                'Report parameters', VALUE_DEFAULT, []
56
            ),
57
        ]);
58
    }
59
 
60
    /**
61
     * External method execution
62
     *
63
     * @param string $source
64
     * @param array $context
65
     * @param string $component
66
     * @param string $area
67
     * @param int $itemid
68
     * @param array[] $parameters
69
     * @return bool
70
     */
71
    public static function execute(
72
        string $source,
73
        array $context,
74
        string $component = '',
75
        string $area = '',
76
        int $itemid = 0,
77
        array $parameters = [],
78
    ): bool {
79
 
80
        [
81
            'source' => $source,
82
            'context' => $context,
83
            'component' => $component,
84
            'area' => $area,
85
            'itemid' => $itemid,
86
            'parameters' => $parameters,
87
        ] = self::validate_parameters(self::execute_parameters(), [
88
            'source' => $source,
89
            'context' => $context,
90
            'component' => $component,
91
            'area' => $area,
92
            'itemid' => $itemid,
93
            'parameters' => $parameters,
94
        ]);
95
 
96
        $context = self::get_context_from_params($context);
97
        self::validate_context($context);
98
 
99
        // Flatten the report parameters.
100
        $parameters = array_combine(array_column($parameters, 'name'), array_column($parameters, 'value'));
101
 
102
        try {
103
            $report = system_report_factory::create($source, $context, $component, $area, $itemid, $parameters);
104
            $report->require_can_view();
105
        } catch (report_access_exception $exception) {
106
            return false;
107
        }
108
 
109
        return true;
110
    }
111
 
112
    /**
113
     * External method return value
114
     *
115
     * @return external_value
116
     */
117
    public static function execute_returns(): external_value {
118
        return new external_value(PARAM_BOOL);
119
    }
120
}