Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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;
20
 
21
use context;
22
use core_reportbuilder\local\models\report;
23
use core_reportbuilder\local\report\base;
1441 ariadna 24
use core_reportbuilder\exception\source_invalid_exception;
1 efrain 25
 
26
/**
27
 * Factory class for creating system report instances
28
 *
29
 * @package     core_reportbuilder
30
 * @copyright   2020 Paul Holden <paulh@moodle.com>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class system_report_factory {
34
 
35
    /**
36
     * Create and return instance of given system report source
37
     *
38
     * @param string $source Class path of system report definition
39
     * @param context $context
40
     * @param string $component
41
     * @param string $area
42
     * @param int $itemid
43
     * @param array $parameters Simple key/value pairs, accessed inside reports using $this->get_parameter()
44
     * @return system_report
45
     * @throws source_invalid_exception
46
     */
47
    public static function create(string $source, context $context, string $component = '', string $area = '',
48
            int $itemid = 0, array $parameters = []): system_report {
49
 
50
        // Exit early if source isn't a system report.
51
        if (!manager::report_source_exists($source, system_report::class)) {
52
            throw new source_invalid_exception($source);
53
        }
54
 
55
        $report = static::get_report_persistent($source, $context, $component, $area, $itemid);
56
 
57
        return manager::get_report_from_persistent($report, $parameters);
58
    }
59
 
60
    /**
61
     * Given a report source, with accompanying context information, return a persistent report instance
62
     *
63
     * @param string $source
64
     * @param context $context
65
     * @param string $component
66
     * @param string $area
67
     * @param int $itemid
68
     * @return report
69
     */
70
    private static function get_report_persistent(string $source, context $context, string $component = '', string $area = '',
71
            int $itemid = 0): report {
72
 
73
        $reportdata = [
74
            'type' => base::TYPE_SYSTEM_REPORT,
75
            'source' => $source,
76
            'contextid' => $context->id,
77
            'component' => $component,
78
            'area' => $area,
79
            'itemid' => $itemid,
80
        ];
81
 
82
        if ($report = report::get_record($reportdata)) {
83
             return $report;
84
        }
85
 
86
        return manager::create_report_persistent((object) $reportdata);
87
    }
88
}