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\local\systemreports;
|
|
|
20 |
|
|
|
21 |
use html_writer;
|
|
|
22 |
use lang_string;
|
|
|
23 |
use moodle_url;
|
|
|
24 |
use pix_icon;
|
|
|
25 |
use stdClass;
|
|
|
26 |
use core_reportbuilder\datasource;
|
|
|
27 |
use core_reportbuilder\manager;
|
|
|
28 |
use core_reportbuilder\system_report;
|
|
|
29 |
use core_reportbuilder\local\entities\user;
|
|
|
30 |
use core_reportbuilder\local\filters\date;
|
|
|
31 |
use core_reportbuilder\local\filters\tags;
|
|
|
32 |
use core_reportbuilder\local\filters\text;
|
|
|
33 |
use core_reportbuilder\local\filters\select;
|
|
|
34 |
use core_reportbuilder\local\helpers\audience;
|
|
|
35 |
use core_reportbuilder\local\helpers\format;
|
|
|
36 |
use core_reportbuilder\local\report\action;
|
|
|
37 |
use core_reportbuilder\local\report\column;
|
|
|
38 |
use core_reportbuilder\local\report\filter;
|
|
|
39 |
use core_reportbuilder\output\report_name_editable;
|
|
|
40 |
use core_reportbuilder\local\models\report;
|
|
|
41 |
use core_reportbuilder\permission;
|
|
|
42 |
use core_tag_tag;
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Reports list
|
|
|
46 |
*
|
|
|
47 |
* @package core_reportbuilder
|
|
|
48 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
|
|
49 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
50 |
*/
|
|
|
51 |
class reports_list extends system_report {
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* The name of our internal report entity
|
|
|
55 |
*
|
|
|
56 |
* @return string
|
|
|
57 |
*/
|
|
|
58 |
private function get_report_entity_name(): string {
|
|
|
59 |
return 'report';
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Initialise the report
|
|
|
64 |
*/
|
|
|
65 |
protected function initialise(): void {
|
|
|
66 |
$this->set_main_table('reportbuilder_report', 'rb');
|
|
|
67 |
$this->add_base_condition_simple('rb.type', self::TYPE_CUSTOM_REPORT);
|
|
|
68 |
|
|
|
69 |
// Select fields required for actions, permission checks, and row class callbacks.
|
|
|
70 |
$this->add_base_fields('rb.id, rb.name, rb.source, rb.type, rb.usercreated, rb.contextid');
|
|
|
71 |
|
|
|
72 |
// Limit the returned list to those reports the current user can access.
|
|
|
73 |
[$where, $params] = audience::user_reports_list_access_sql('rb');
|
|
|
74 |
$this->add_base_condition_sql($where, $params);
|
|
|
75 |
|
|
|
76 |
// Join user entity for "User modified" column.
|
|
|
77 |
$entityuser = new user();
|
|
|
78 |
$entityuseralias = $entityuser->get_table_alias('user');
|
|
|
79 |
|
|
|
80 |
$this->add_entity($entityuser
|
|
|
81 |
->add_join("JOIN {user} {$entityuseralias} ON {$entityuseralias}.id = rb.usermodified")
|
|
|
82 |
);
|
|
|
83 |
|
|
|
84 |
// Define our internal entity for report elements.
|
|
|
85 |
$this->annotate_entity($this->get_report_entity_name(),
|
|
|
86 |
new lang_string('customreports', 'core_reportbuilder'));
|
|
|
87 |
|
|
|
88 |
$this->add_columns();
|
|
|
89 |
$this->add_filters();
|
|
|
90 |
$this->add_actions();
|
|
|
91 |
|
|
|
92 |
$this->set_downloadable(false);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Ensure we can view the report
|
|
|
97 |
*
|
|
|
98 |
* @return bool
|
|
|
99 |
*/
|
|
|
100 |
protected function can_view(): bool {
|
|
|
101 |
return permission::can_view_reports_list();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Dim the table row for invalid datasource
|
|
|
106 |
*
|
|
|
107 |
* @param stdClass $row
|
|
|
108 |
* @return string
|
|
|
109 |
*/
|
|
|
110 |
public function get_row_class(stdClass $row): string {
|
|
|
111 |
return $this->report_source_valid($row->source) ? '' : 'text-muted';
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Add columns to report
|
|
|
116 |
*/
|
|
|
117 |
protected function add_columns(): void {
|
|
|
118 |
global $DB;
|
|
|
119 |
|
|
|
120 |
$tablealias = $this->get_main_table_alias();
|
|
|
121 |
|
|
|
122 |
// Report name column.
|
|
|
123 |
$this->add_column((new column(
|
|
|
124 |
'name',
|
|
|
125 |
new lang_string('name'),
|
|
|
126 |
$this->get_report_entity_name()
|
|
|
127 |
))
|
|
|
128 |
->set_type(column::TYPE_TEXT)
|
|
|
129 |
// We need enough fields to re-create the persistent and pass to the editable component.
|
|
|
130 |
->add_fields(implode(', ', [
|
|
|
131 |
"{$tablealias}.id",
|
|
|
132 |
"{$tablealias}.name",
|
|
|
133 |
"{$tablealias}.contextid",
|
|
|
134 |
"{$tablealias}.type",
|
|
|
135 |
"{$tablealias}.usercreated",
|
|
|
136 |
]))
|
|
|
137 |
->set_is_sortable(true, ["{$tablealias}.name"])
|
|
|
138 |
->add_callback(static function(string $value, stdClass $report): string {
|
|
|
139 |
global $PAGE;
|
|
|
140 |
|
|
|
141 |
$editable = new report_name_editable(0, new report(0, $report));
|
|
|
142 |
return $editable->render($PAGE->get_renderer('core'));
|
|
|
143 |
})
|
|
|
144 |
);
|
|
|
145 |
|
|
|
146 |
// Report source column.
|
|
|
147 |
$this->add_column((new column(
|
|
|
148 |
'source',
|
|
|
149 |
new lang_string('reportsource', 'core_reportbuilder'),
|
|
|
150 |
$this->get_report_entity_name()
|
|
|
151 |
))
|
|
|
152 |
->set_type(column::TYPE_TEXT)
|
|
|
153 |
->add_fields("{$tablealias}.source")
|
|
|
154 |
->set_is_sortable(true)
|
|
|
155 |
->add_callback(function(string $value, stdClass $row) {
|
|
|
156 |
if (!$this->report_source_valid($value)) {
|
|
|
157 |
// Add danger badge if report source is not valid (either it's missing, or has errors).
|
|
|
158 |
return html_writer::span(get_string('errorsourceinvalid', 'core_reportbuilder'), 'badge bg-danger text-white');
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
return call_user_func([$value, 'get_name']);
|
|
|
162 |
})
|
|
|
163 |
);
|
|
|
164 |
|
|
|
165 |
// Tags column. TODO: Reuse tag entity column when MDL-76392 is integrated.
|
|
|
166 |
$tagfieldconcatsql = $DB->sql_group_concat(
|
|
|
167 |
field: $DB->sql_concat_join("'|'", ['t.name', 't.rawname']),
|
|
|
168 |
sort: 't.name',
|
|
|
169 |
);
|
|
|
170 |
$this->add_column((new column(
|
|
|
171 |
'tags',
|
|
|
172 |
new lang_string('tags'),
|
|
|
173 |
$this->get_report_entity_name(),
|
|
|
174 |
))
|
|
|
175 |
->set_type(column::TYPE_TEXT)
|
|
|
176 |
->add_field("(
|
|
|
177 |
SELECT {$tagfieldconcatsql}
|
|
|
178 |
FROM {tag_instance} ti
|
|
|
179 |
JOIN {tag} t ON t.id = ti.tagid
|
|
|
180 |
WHERE ti.component = 'core_reportbuilder' AND ti.itemtype = 'reportbuilder_report'
|
|
|
181 |
AND ti.itemid = {$tablealias}.id
|
|
|
182 |
)", 'tags')
|
|
|
183 |
->set_is_sortable(true)
|
|
|
184 |
->set_is_available(core_tag_tag::is_enabled('core_reportbuilder', 'reportbuilder_report') === true)
|
|
|
185 |
->add_callback(static function(?string $tags): string {
|
|
|
186 |
return implode(', ', array_map(static function(string $tag): string {
|
|
|
187 |
[$name, $rawname] = explode('|', $tag);
|
|
|
188 |
return core_tag_tag::make_display_name((object) [
|
|
|
189 |
'name' => $name,
|
|
|
190 |
'rawname' => $rawname,
|
|
|
191 |
]);
|
|
|
192 |
}, preg_split('/, /', (string) $tags, -1, PREG_SPLIT_NO_EMPTY)));
|
|
|
193 |
})
|
|
|
194 |
);
|
|
|
195 |
|
|
|
196 |
// Time created column.
|
|
|
197 |
$this->add_column((new column(
|
|
|
198 |
'timecreated',
|
|
|
199 |
new lang_string('timecreated', 'core_reportbuilder'),
|
|
|
200 |
$this->get_report_entity_name()
|
|
|
201 |
))
|
|
|
202 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
203 |
->add_fields("{$tablealias}.timecreated")
|
|
|
204 |
->set_is_sortable(true)
|
|
|
205 |
->add_callback([format::class, 'userdate'])
|
|
|
206 |
);
|
|
|
207 |
|
|
|
208 |
// Time modified column.
|
|
|
209 |
$this->add_column((new column(
|
|
|
210 |
'timemodified',
|
|
|
211 |
new lang_string('timemodified', 'core_reportbuilder'),
|
|
|
212 |
$this->get_report_entity_name()
|
|
|
213 |
))
|
|
|
214 |
->set_type(column::TYPE_TIMESTAMP)
|
|
|
215 |
->add_fields("{$tablealias}.timemodified")
|
|
|
216 |
->set_is_sortable(true)
|
|
|
217 |
->add_callback([format::class, 'userdate'])
|
|
|
218 |
);
|
|
|
219 |
|
|
|
220 |
// The user who modified the report.
|
|
|
221 |
$this->add_column_from_entity('user:fullname')
|
|
|
222 |
->set_title(new lang_string('usermodified', 'reportbuilder'));
|
|
|
223 |
|
|
|
224 |
// Initial sorting.
|
|
|
225 |
$this->set_initial_sort_column('report:timecreated', SORT_DESC);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* Add filters to report
|
|
|
230 |
*/
|
|
|
231 |
protected function add_filters(): void {
|
|
|
232 |
$tablealias = $this->get_main_table_alias();
|
|
|
233 |
|
|
|
234 |
// Name filter.
|
|
|
235 |
$this->add_filter((new filter(
|
|
|
236 |
text::class,
|
|
|
237 |
'name',
|
|
|
238 |
new lang_string('name'),
|
|
|
239 |
$this->get_report_entity_name(),
|
|
|
240 |
"{$tablealias}.name"
|
|
|
241 |
)));
|
|
|
242 |
|
|
|
243 |
// Source filter.
|
|
|
244 |
$this->add_filter((new filter(
|
|
|
245 |
select::class,
|
|
|
246 |
'source',
|
|
|
247 |
new lang_string('reportsource', 'core_reportbuilder'),
|
|
|
248 |
$this->get_report_entity_name(),
|
|
|
249 |
"{$tablealias}.source"
|
|
|
250 |
))
|
|
|
251 |
->set_options_callback(static function(): array {
|
|
|
252 |
return manager::get_report_datasources();
|
|
|
253 |
})
|
|
|
254 |
);
|
|
|
255 |
|
|
|
256 |
// Tags filter.
|
|
|
257 |
$this->add_filter((new filter(
|
|
|
258 |
tags::class,
|
|
|
259 |
'tags',
|
|
|
260 |
new lang_string('tags'),
|
|
|
261 |
$this->get_report_entity_name(),
|
|
|
262 |
"{$tablealias}.id",
|
|
|
263 |
))
|
|
|
264 |
->set_options([
|
|
|
265 |
'component' => 'core_reportbuilder',
|
|
|
266 |
'itemtype' => 'reportbuilder_report',
|
|
|
267 |
])
|
|
|
268 |
->set_is_available(core_tag_tag::is_enabled('core_reportbuilder', 'reportbuilder_report') === true)
|
|
|
269 |
);
|
|
|
270 |
|
|
|
271 |
// Time created filter.
|
|
|
272 |
$this->add_filter((new filter(
|
|
|
273 |
date::class,
|
|
|
274 |
'timecreated',
|
|
|
275 |
new lang_string('timecreated', 'core_reportbuilder'),
|
|
|
276 |
$this->get_report_entity_name(),
|
|
|
277 |
"{$tablealias}.timecreated"
|
|
|
278 |
))
|
|
|
279 |
->set_limited_operators([
|
|
|
280 |
date::DATE_ANY,
|
|
|
281 |
date::DATE_RANGE,
|
|
|
282 |
])
|
|
|
283 |
);
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
/**
|
|
|
287 |
* Add actions to report
|
|
|
288 |
*/
|
|
|
289 |
protected function add_actions(): void {
|
|
|
290 |
// Edit content action.
|
|
|
291 |
$this->add_action((new action(
|
|
|
292 |
new moodle_url('/reportbuilder/edit.php', ['id' => ':id']),
|
|
|
293 |
new pix_icon('t/right', ''),
|
|
|
294 |
[],
|
|
|
295 |
false,
|
|
|
296 |
new lang_string('editreportcontent', 'core_reportbuilder')
|
|
|
297 |
))
|
|
|
298 |
->add_callback(function(stdClass $row): bool {
|
|
|
299 |
return $this->report_source_valid($row->source) && permission::can_edit_report(new report(0, $row));
|
|
|
300 |
})
|
|
|
301 |
);
|
|
|
302 |
|
|
|
303 |
// Edit details action.
|
|
|
304 |
$this->add_action((new action(
|
|
|
305 |
new moodle_url('#'),
|
|
|
306 |
new pix_icon('t/edit', ''),
|
|
|
307 |
['data-action' => 'report-edit', 'data-report-id' => ':id'],
|
|
|
308 |
false,
|
|
|
309 |
new lang_string('editreportdetails', 'core_reportbuilder')
|
|
|
310 |
))
|
|
|
311 |
->add_callback(function(stdClass $row): bool {
|
|
|
312 |
return $this->report_source_valid($row->source) && permission::can_edit_report(new report(0, $row));
|
|
|
313 |
})
|
|
|
314 |
);
|
|
|
315 |
|
|
|
316 |
// Preview action.
|
|
|
317 |
$this->add_action((new action(
|
|
|
318 |
new moodle_url('/reportbuilder/view.php', ['id' => ':id']),
|
|
|
319 |
new pix_icon('i/search', ''),
|
|
|
320 |
[],
|
|
|
321 |
false,
|
|
|
322 |
new lang_string('viewreport', 'core_reportbuilder')
|
|
|
323 |
))
|
|
|
324 |
->add_callback(function(stdClass $row): bool {
|
|
|
325 |
// We check this only to give the action to editors, because normal users can just click on the report name.
|
|
|
326 |
return $this->report_source_valid($row->source) && permission::can_edit_report(new report(0, $row));
|
|
|
327 |
})
|
|
|
328 |
);
|
|
|
329 |
|
|
|
330 |
// Delete action.
|
|
|
331 |
$this->add_action((new action(
|
|
|
332 |
new moodle_url('#'),
|
|
|
333 |
new pix_icon('t/delete', ''),
|
|
|
334 |
[
|
|
|
335 |
'data-action' => 'report-delete',
|
|
|
336 |
'data-report-id' => ':id',
|
|
|
337 |
'data-report-name' => ':name',
|
|
|
338 |
'class' => 'text-danger',
|
|
|
339 |
],
|
|
|
340 |
false,
|
|
|
341 |
new lang_string('deletereport', 'core_reportbuilder')
|
|
|
342 |
))
|
|
|
343 |
->add_callback(function(stdClass $row): bool {
|
|
|
344 |
|
|
|
345 |
// Ensure data name attribute is properly formatted.
|
|
|
346 |
$report = new report(0, $row);
|
|
|
347 |
$row->name = $report->get_formatted_name();
|
|
|
348 |
|
|
|
349 |
// We don't check whether report is valid to ensure editor can always delete them.
|
|
|
350 |
return permission::can_edit_report($report);
|
|
|
351 |
})
|
|
|
352 |
);
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
/**
|
|
|
356 |
* Helper to determine whether given report source is valid (it both exists, and is available)
|
|
|
357 |
*
|
|
|
358 |
* @param string $source
|
|
|
359 |
* @return bool
|
|
|
360 |
*/
|
|
|
361 |
private function report_source_valid(string $source): bool {
|
|
|
362 |
return manager::report_source_exists($source, datasource::class) && manager::report_source_available($source);
|
|
|
363 |
}
|
|
|
364 |
}
|