Automatically generated installer lang files
[moodle.git] / reportbuilder / classes / system_report_factory.php
blob36cbb4a15a0fe63d0d3a8b0c1d6049b2fc1b201a
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 declare(strict_types=1);
19 namespace core_reportbuilder;
21 use context;
22 use core_reportbuilder\local\models\report;
23 use core_reportbuilder\local\report\base;
25 /**
26 * Factory class for creating system report instances
28 * @package core_reportbuilder
29 * @copyright 2020 Paul Holden <paulh@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class system_report_factory {
34 /**
35 * Create and return instance of given system report source
37 * @param string $source Class path of system report definition
38 * @param context $context
39 * @param string $component
40 * @param string $area
41 * @param int $itemid
42 * @param array $parameters Simple key/value pairs, accessed inside reports using $this->get_parameter()
43 * @return system_report
44 * @throws source_invalid_exception
46 public static function create(string $source, context $context, string $component = '', string $area = '',
47 int $itemid = 0, array $parameters = []): system_report {
49 // Exit early if source isn't a system report.
50 if (!manager::report_source_exists($source, system_report::class)) {
51 throw new source_invalid_exception($source);
54 $report = static::get_report_persistent($source, $context, $component, $area, $itemid);
56 return manager::get_report_from_persistent($report, $parameters);
59 /**
60 * Given a report source, with accompanying context information, return a persistent report instance
62 * @param string $source
63 * @param context $context
64 * @param string $component
65 * @param string $area
66 * @param int $itemid
67 * @return report
69 private static function get_report_persistent(string $source, context $context, string $component = '', string $area = '',
70 int $itemid = 0): report {
72 $reportdata = [
73 'type' => base::TYPE_SYSTEM_REPORT,
74 'source' => $source,
75 'contextid' => $context->id,
76 'component' => $component,
77 'area' => $area,
78 'itemid' => $itemid,
81 if ($report = report::get_record($reportdata)) {
82 return $report;
85 return manager::create_report_persistent((object) $reportdata);