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\table;
|
|
|
20 |
|
|
|
21 |
use action_menu;
|
|
|
22 |
use action_menu_filler;
|
|
|
23 |
use core_table\local\filter\filterset;
|
|
|
24 |
use html_writer;
|
|
|
25 |
use moodle_exception;
|
|
|
26 |
use stdClass;
|
|
|
27 |
use core_reportbuilder\{manager, system_report};
|
|
|
28 |
use core_reportbuilder\local\models\report;
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* System report dynamic table class
|
|
|
32 |
*
|
|
|
33 |
* @package core_reportbuilder
|
|
|
34 |
* @copyright 2020 Paul Holden <paulh@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class system_report_table extends base_report_table {
|
|
|
38 |
|
|
|
39 |
/** @var system_report $report */
|
|
|
40 |
protected $report;
|
|
|
41 |
|
|
|
42 |
/** @var string Unique ID prefix for the table */
|
|
|
43 |
private const UNIQUEID_PREFIX = 'system-report-table-';
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Table constructor. Note that the passed unique ID value must match the pattern "system-report-table-(\d+)" so that
|
|
|
47 |
* dynamic updates continue to load the same report
|
|
|
48 |
*
|
|
|
49 |
* @param string $uniqueid
|
|
|
50 |
* @param array $parameters
|
|
|
51 |
* @throws moodle_exception For invalid unique ID
|
|
|
52 |
*/
|
|
|
53 |
public function __construct(string $uniqueid, array $parameters = []) {
|
|
|
54 |
if (!preg_match('/^' . self::UNIQUEID_PREFIX . '(?<id>\d+)$/', $uniqueid, $matches)) {
|
|
|
55 |
throw new moodle_exception('invalidsystemreportid', 'core_reportbuilder', '', null, $uniqueid);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
parent::__construct($uniqueid);
|
|
|
59 |
|
|
|
60 |
// If we are loading via a dynamic table AJAX request, defer the report loading until the filterset is added to
|
|
|
61 |
// the table, as it is used to populate the report $parameters during construction.
|
|
|
62 |
$serviceinfo = optional_param('info', null, PARAM_RAW);
|
|
|
63 |
if ($serviceinfo !== 'core_table_get_dynamic_table_content') {
|
|
|
64 |
$this->load_report_instance((int) $matches['id'], $parameters);
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Load the report persistent, and accompanying system report instance.
|
|
|
70 |
*
|
|
|
71 |
* @param int $reportid
|
|
|
72 |
* @param array $parameters
|
|
|
73 |
*/
|
|
|
74 |
private function load_report_instance(int $reportid, array $parameters): void {
|
|
|
75 |
global $PAGE;
|
|
|
76 |
|
|
|
77 |
$this->persistent = new report($reportid);
|
|
|
78 |
$this->report = manager::get_report_from_persistent($this->persistent, $parameters);
|
|
|
79 |
|
|
|
80 |
// TODO: can probably be removed pending MDL-72974.
|
|
|
81 |
$PAGE->set_context($this->persistent->get_context());
|
|
|
82 |
|
|
|
83 |
$fields = $this->report->get_base_fields();
|
|
|
84 |
$maintable = $this->report->get_main_table();
|
|
|
85 |
$maintablealias = $this->report->get_main_table_alias();
|
|
|
86 |
$joins = $this->report->get_joins();
|
|
|
87 |
[$where, $params] = $this->report->get_base_condition();
|
|
|
88 |
|
|
|
89 |
$this->set_attribute('data-region', 'reportbuilder-table');
|
|
|
90 |
$this->set_attribute('class', $this->attributes['class'] . ' reportbuilder-table');
|
|
|
91 |
|
|
|
92 |
// Download options.
|
|
|
93 |
$this->showdownloadbuttonsat = [TABLE_P_BOTTOM];
|
|
|
94 |
$this->is_downloading($parameters['download'] ?? null, $this->report->get_downloadfilename());
|
|
|
95 |
|
|
|
96 |
// Retrieve all report columns. If we are downloading the report, remove as required.
|
|
|
97 |
$columns = $this->report->get_active_columns();
|
|
|
98 |
if ($this->is_downloading()) {
|
|
|
99 |
$columns = array_diff_key($columns,
|
|
|
100 |
array_flip($this->report->get_exclude_columns_for_download()));
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
$columnheaders = $columnsattributes = [];
|
|
|
104 |
|
|
|
105 |
// Check whether report has checkbox toggle defined, note that select all is excluded during download.
|
|
|
106 |
if (($checkbox = $this->report->get_checkbox_toggleall(true)) && !$this->is_downloading()) {
|
|
|
107 |
$columnheaders['selectall'] = $PAGE->get_renderer('core')->render($checkbox);
|
|
|
108 |
$this->no_sorting('selectall');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
$columnindex = 1;
|
|
|
112 |
foreach ($columns as $identifier => $column) {
|
|
|
113 |
$column->set_index($columnindex++);
|
|
|
114 |
|
|
|
115 |
$columnheaders[$column->get_column_alias()] = $column->get_title();
|
|
|
116 |
|
|
|
117 |
// Specify whether column should behave as a user fullname column unless the column has a custom title set.
|
|
|
118 |
if (preg_match('/^user:fullname.*$/', $column->get_unique_identifier()) && !$column->has_custom_title()) {
|
|
|
119 |
$this->userfullnamecolumns[] = $column->get_column_alias();
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
// Add each columns fields, joins and params to our report.
|
|
|
123 |
$fields = array_merge($fields, $column->get_fields());
|
|
|
124 |
$joins = array_merge($joins, $column->get_joins());
|
|
|
125 |
$params = array_merge($params, $column->get_params());
|
|
|
126 |
|
|
|
127 |
// Disable sorting for some columns.
|
|
|
128 |
if (!$column->get_is_sortable()) {
|
|
|
129 |
$this->no_sorting($column->get_column_alias());
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// Generate column attributes to be included in each cell.
|
|
|
133 |
$columnsattributes[$column->get_column_alias()] = $column->get_attributes();
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// If the report has any actions then append appropriate column, note that actions are excluded during download.
|
|
|
137 |
if ($this->report->has_actions() && !$this->is_downloading()) {
|
|
|
138 |
$columnheaders['actions'] = html_writer::tag('span', get_string('actions', 'core_reportbuilder'), [
|
|
|
139 |
'class' => 'sr-only',
|
|
|
140 |
]);
|
|
|
141 |
$this->no_sorting('actions');
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
$this->define_columns(array_keys($columnheaders));
|
|
|
145 |
$this->define_headers(array_values($columnheaders));
|
|
|
146 |
|
|
|
147 |
// Add column attributes to the table.
|
|
|
148 |
$this->set_columnsattributes($columnsattributes);
|
|
|
149 |
|
|
|
150 |
// Initial table sort column.
|
|
|
151 |
if ($sortcolumn = $this->report->get_initial_sort_column()) {
|
|
|
152 |
$this->sortable(true, $sortcolumn->get_column_alias(), $this->report->get_initial_sort_direction());
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// Table configuration.
|
|
|
156 |
$this->initialbars(false);
|
|
|
157 |
$this->collapsible(false);
|
|
|
158 |
$this->pageable(true);
|
|
|
159 |
$this->set_default_per_page($this->report->get_default_per_page());
|
|
|
160 |
|
|
|
161 |
// Initialise table SQL properties.
|
|
|
162 |
$fieldsql = implode(', ', $fields);
|
|
|
163 |
$this->init_sql($fieldsql, "{{$maintable}} {$maintablealias}", $joins, $where, $params);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Return a new instance of the class for given report ID. We include report parameters here so they are present during
|
|
|
168 |
* initialisation
|
|
|
169 |
*
|
|
|
170 |
* @param int $reportid
|
|
|
171 |
* @param array $parameters
|
|
|
172 |
* @return static
|
|
|
173 |
*/
|
|
|
174 |
public static function create(int $reportid, array $parameters): self {
|
|
|
175 |
return new static(self::UNIQUEID_PREFIX . $reportid, $parameters);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Set the filterset in the table class. We set the report parameters here so that they are persisted while paging
|
|
|
180 |
*
|
|
|
181 |
* @param filterset $filterset
|
|
|
182 |
*/
|
|
|
183 |
public function set_filterset(filterset $filterset): void {
|
|
|
184 |
$reportid = $filterset->get_filter('reportid')->current();
|
|
|
185 |
$parameters = $filterset->get_filter('parameters')->current();
|
|
|
186 |
|
|
|
187 |
$this->load_report_instance($reportid, json_decode($parameters, true));
|
|
|
188 |
|
|
|
189 |
parent::set_filterset($filterset);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
/**
|
|
|
193 |
* Override parent method for retrieving row class with that defined by the system report
|
|
|
194 |
*
|
|
|
195 |
* @param array|stdClass $row
|
|
|
196 |
* @return string
|
|
|
197 |
*/
|
|
|
198 |
public function get_row_class($row) {
|
|
|
199 |
return $this->report->get_row_class((object) $row);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Format each row of returned data, executing defined callbacks for the row and each column
|
|
|
204 |
*
|
|
|
205 |
* @param array|stdClass $row
|
|
|
206 |
* @return array
|
|
|
207 |
*/
|
|
|
208 |
public function format_row($row) {
|
|
|
209 |
global $PAGE;
|
|
|
210 |
|
|
|
211 |
$this->report->row_callback((object) $row);
|
|
|
212 |
|
|
|
213 |
// Walk over the row, and for any key that matches one of our column aliases, call that columns format method.
|
|
|
214 |
$columnsbyalias = $this->report->get_active_columns_by_alias();
|
|
|
215 |
$row = (array) $row;
|
|
|
216 |
array_walk($row, static function(&$value, $key) use ($columnsbyalias, $row): void {
|
|
|
217 |
if (array_key_exists($key, $columnsbyalias)) {
|
|
|
218 |
$value = $columnsbyalias[$key]->format_value($row);
|
|
|
219 |
}
|
|
|
220 |
});
|
|
|
221 |
|
|
|
222 |
// Check whether report has checkbox toggle defined.
|
|
|
223 |
if ($checkbox = $this->report->get_checkbox_toggleall(false, (object) $row)) {
|
|
|
224 |
$row['selectall'] = $PAGE->get_renderer('core')->render($checkbox);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
// Now check for any actions.
|
|
|
228 |
if ($this->report->has_actions()) {
|
|
|
229 |
$row['actions'] = $this->format_row_actions((object) $row);
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
return $row;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Return formatted actions column for the row
|
|
|
237 |
*
|
|
|
238 |
* @param stdClass $row
|
|
|
239 |
* @return string
|
|
|
240 |
*/
|
|
|
241 |
private function format_row_actions(stdClass $row): string {
|
|
|
242 |
global $OUTPUT;
|
|
|
243 |
|
|
|
244 |
$menu = new action_menu();
|
|
|
245 |
$menu->set_menu_trigger($OUTPUT->pix_icon('a/setting', get_string('actions', 'core_reportbuilder')));
|
|
|
246 |
|
|
|
247 |
$actions = array_filter($this->report->get_actions(), function($action) use ($row) {
|
|
|
248 |
// Only return dividers and action items who can be displayed for current users.
|
|
|
249 |
return $action instanceof action_menu_filler || $action->get_action_link($row);
|
|
|
250 |
});
|
|
|
251 |
|
|
|
252 |
$totalactions = count($actions);
|
|
|
253 |
$actionvalues = array_values($actions);
|
|
|
254 |
foreach ($actionvalues as $position => $action) {
|
|
|
255 |
if ($action instanceof action_menu_filler) {
|
|
|
256 |
$ispreviousdivider = array_key_exists($position - 1, $actionvalues) &&
|
|
|
257 |
($actionvalues[$position - 1] instanceof action_menu_filler);
|
|
|
258 |
$isnextdivider = array_key_exists($position + 1, $actionvalues) &&
|
|
|
259 |
($actionvalues[$position + 1] instanceof action_menu_filler);
|
|
|
260 |
$isfirstdivider = ($position === 0);
|
|
|
261 |
$islastdivider = ($position === $totalactions - 1);
|
|
|
262 |
|
|
|
263 |
// Avoid add divider at last/first position and having multiple fillers in a row.
|
|
|
264 |
if ($ispreviousdivider || $isnextdivider || $isfirstdivider || $islastdivider) {
|
|
|
265 |
continue;
|
|
|
266 |
}
|
|
|
267 |
$actionlink = $action;
|
|
|
268 |
} else {
|
|
|
269 |
// Ensure the action link can be displayed for the current row.
|
|
|
270 |
$actionlink = $action->get_action_link($row);
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
if ($actionlink) {
|
|
|
274 |
$menu->add($actionlink);
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
return $OUTPUT->render($menu);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* Get the html for the download buttons
|
|
|
282 |
*
|
|
|
283 |
* @return string
|
|
|
284 |
*/
|
|
|
285 |
public function download_buttons(): string {
|
|
|
286 |
global $OUTPUT;
|
|
|
287 |
|
|
|
288 |
if ($this->report->can_be_downloaded() && !$this->is_downloading()) {
|
|
|
289 |
return $OUTPUT->download_dataformat_selector(
|
|
|
290 |
get_string('downloadas', 'table'),
|
|
|
291 |
new \moodle_url('/reportbuilder/download.php'),
|
|
|
292 |
'download',
|
|
|
293 |
[
|
|
|
294 |
'id' => $this->persistent->get('id'),
|
|
|
295 |
'parameters' => json_encode($this->report->get_parameters()),
|
|
|
296 |
]
|
|
|
297 |
);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
return '';
|
|
|
301 |
}
|
|
|
302 |
}
|