Automatically generated installer lang files
[moodle.git] / reportbuilder / classes / system_report.php
blobd8de149c9be60ffa176b85abec1a5603aeb9df68
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 coding_exception;
22 use stdClass;
23 use core_reportbuilder\local\models\report;
24 use core_reportbuilder\local\report\action;
25 use core_reportbuilder\local\report\base;
26 use core_reportbuilder\local\report\column;
28 /**
29 * Base class for system reports
31 * @package core_reportbuilder
32 * @copyright 2020 Paul Holden <paulh@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 abstract class system_report extends base {
37 /** @var array $parameters */
38 private $parameters;
40 /** @var string[] $basefields List of base fields */
41 private $basefields = [];
43 /** @var action[] $actions */
44 private $actions = [];
46 /** @var column $initialsortcolumn */
47 private $initialsortcolumn;
49 /** @var int $initialsortdirection */
50 private $initialsortdirection;
52 /**
53 * System report constructor.
55 * @param report $report
56 * @param array $parameters
58 final public function __construct(report $report, array $parameters) {
59 $this->parameters = $parameters;
61 parent::__construct($report);
64 /**
65 * Validates access to view this report
67 * This is necessary to implement independently of the page that would typically embed the report because
68 * subsequent pages are requested via AJAX requests, and access should be validated each time
70 * @return bool
72 abstract protected function can_view(): bool;
74 /**
75 * Validate access to the report
77 * @throws report_access_exception
79 final public function require_can_view(): void {
80 if (!$this->can_view()) {
81 throw new report_access_exception();
85 /**
86 * Report validation
88 * @throws report_access_exception If user cannot access the report
89 * @throws coding_exception If no default column are specified
91 protected function validate(): void {
92 parent::validate();
94 $this->require_can_view();
96 // Ensure the report has some default columns specified.
97 if (empty($this->get_columns())) {
98 throw new coding_exception('No columns added');
103 * Add list of fields that have to be always included in SQL query for actions and row classes
105 * Base fields are only available in system reports because they are not compatible with aggregation
107 * @param string $sql SQL clause for the list of fields that only uses main table or base joins
109 final protected function add_base_fields(string $sql): void {
110 $this->basefields[] = $sql;
114 * Return report base fields
116 * @return array
118 final public function get_base_fields(): array {
119 return $this->basefields;
123 * Adds an action to the report
125 * @param action $action
127 final public function add_action(action $action): void {
128 $this->actions[] = $action;
132 * Whether report has any actions
134 * @return bool
136 final public function has_actions(): bool {
137 return !empty($this->actions);
141 * Return report actions
143 * @return action[]
145 final public function get_actions(): array {
146 return $this->actions;
150 * Set all report parameters
152 * @param array $parameters
154 final public function set_parameters(array $parameters): void {
155 $this->parameters = $parameters;
159 * Return all report parameters
161 * @return array
163 final public function get_parameters(): array {
164 return $this->parameters;
168 * Return specific report parameter
170 * @param string $param
171 * @param mixed $default
172 * @param string $type
173 * @return mixed
175 final public function get_parameter(string $param, $default, string $type) {
176 if (!array_key_exists($param, $this->parameters)) {
177 return $default;
180 return clean_param($this->parameters[$param], $type);
184 * Output the report
186 * @uses \core_reportbuilder\output\renderer::render_system_report()
188 * @return string
190 final public function output(): string {
191 global $PAGE;
193 /** @var \core_reportbuilder\output\renderer $renderer */
194 $renderer = $PAGE->get_renderer('core_reportbuilder');
195 $report = new \core_reportbuilder\output\system_report($this->get_report_persistent(), $this, $this->parameters);
197 return $renderer->render($report);
201 * CSS classes to add to the row. Can be overridden by system reports do define class to be added to output according to
202 * content of each row
204 * @param stdClass $row
205 * @return string
207 public function get_row_class(stdClass $row): string {
208 return '';
212 * Called before rendering each row. Can be overridden to pre-fetch/create objects and store them in the class, which can
213 * later be used in column and action callbacks
215 * @param stdClass $row
217 public function row_callback(stdClass $row): void {
218 return;
222 * Validates access to download this report.
224 * @return bool
226 final public function can_be_downloaded(): bool {
227 return $this->can_view() && $this->is_downloadable();
231 * Return list of column names that will be excluded when table is downloaded. Extending classes should override this method
232 * as appropriate
234 * @return string[] Array of column unique identifiers
236 public function get_exclude_columns_for_download(): array {
237 return [];
241 * Set initial sort column and sort direction for the report
243 * @param string $uniqueidentifier
244 * @param int $sortdirection One of SORT_ASC or SORT_DESC
245 * @throws coding_exception
247 public function set_initial_sort_column(string $uniqueidentifier, int $sortdirection): void {
248 if (!$sortcolumn = $this->get_column($uniqueidentifier)) {
249 throw new coding_exception('Unknown column identifier', $uniqueidentifier);
252 $this->initialsortcolumn = $sortcolumn;
253 $this->initialsortdirection = $sortdirection;
257 * Get initial sort column
259 * @return column|null
261 public function get_initial_sort_column(): ?column {
262 return $this->initialsortcolumn;
266 * Get initial sort column direction
268 * @return int
270 public function get_initial_sort_direction(): int {
271 return $this->initialsortdirection;