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\reports;
20
 
21
use context_system;
22
use core_external\external_api;
23
use core_external\external_value;
24
use core_external\external_single_structure;
25
use core_external\external_multiple_structure;
26
use core_external\external_function_parameters;
27
use core_external\external_warnings;
28
use stdClass;
29
use core_reportbuilder\permission;
30
use core_reportbuilder\external\custom_report_details_exporter;
31
use core_reportbuilder\local\helpers\audience;
32
use core_reportbuilder\local\models\report;
33
 
34
/**
35
 * External method for listing users' custom reports
36
 *
37
 * @package     core_reportbuilder
38
 * @copyright   2022 Paul Holden <paulh@moodle.com>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class listing extends external_api {
42
 
43
    /**
44
     * External method parameters
45
     *
46
     * @return external_function_parameters
47
     */
48
    public static function execute_parameters(): external_function_parameters {
49
        return new external_function_parameters([
50
            'page' => new external_value(PARAM_INT, 'Page number', VALUE_DEFAULT, 0),
51
            'perpage' => new external_value(PARAM_INT, 'Reports per page', VALUE_DEFAULT, 10),
52
        ]);
53
    }
54
 
55
    /**
56
     * External method execution
57
     *
58
     * @param int $page
59
     * @param int $perpage
60
     * @return array
61
     */
62
    public static function execute(int $page = 0, int $perpage = 10): array {
63
        global $DB, $PAGE;
64
 
65
        [
66
            'page' => $page,
67
            'perpage' => $perpage,
68
        ] = self::validate_parameters(self::execute_parameters(), [
69
            'page' => $page,
70
            'perpage' => $perpage,
71
        ]);
72
 
73
        $context = context_system::instance();
74
        self::validate_context($context);
75
 
76
        permission::require_can_view_reports_list(null, $context);
77
 
78
        // Filter list of reports by those the user can access.
79
        [$where, $params] = audience::user_reports_list_access_sql('r');
80
        $reports = $DB->get_records_sql("
81
            SELECT r.*
82
              FROM {" . report::TABLE . "} r
83
             WHERE r.type = 0 AND {$where}
84
          ORDER BY r.name, r.id", $params, $page * $perpage, $perpage);
85
 
86
        $output = $PAGE->get_renderer('core');
87
 
88
        return [
89
            'reports' => array_map(static function(stdClass $report) use ($output): array {
90
                $exporter = new custom_report_details_exporter(new report(0, $report));
91
                return (array) $exporter->export($output);
92
            }, $reports),
93
            'warnings' => [],
94
        ];
95
    }
96
 
97
    /**
98
     * External method return value
99
     *
100
     * @return external_single_structure
101
     */
102
    public static function execute_returns(): external_single_structure {
103
        return new external_single_structure([
104
            'reports' => new external_multiple_structure(custom_report_details_exporter::get_read_structure()),
105
            'warnings' => new external_warnings(),
106
        ]);
107
    }
108
}