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 |
namespace core_admin\reportbuilder\local\systemreports;
|
|
|
18 |
|
|
|
19 |
use context_system;
|
|
|
20 |
use core_admin\reportbuilder\local\entities\task_log;
|
|
|
21 |
use core_reportbuilder\local\entities\user;
|
|
|
22 |
use core_reportbuilder\local\report\action;
|
1441 |
ariadna |
23 |
use core_reportbuilder\system_report;
|
|
|
24 |
use html_writer;
|
1 |
efrain |
25 |
use lang_string;
|
|
|
26 |
use moodle_url;
|
|
|
27 |
use pix_icon;
|
1441 |
ariadna |
28 |
use stdClass;
|
1 |
efrain |
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Task logs system report class implementation
|
|
|
32 |
*
|
|
|
33 |
* @package core_admin
|
|
|
34 |
* @copyright 2021 David Matamoros <davidmc@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class task_logs extends system_report {
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Initialise report, we need to set the main table, load our entities and set columns/filters
|
|
|
41 |
*/
|
|
|
42 |
protected function initialise(): void {
|
|
|
43 |
// Our main entity, it contains all of the column definitions that we need.
|
|
|
44 |
$entitymain = new task_log();
|
|
|
45 |
$entitymainalias = $entitymain->get_table_alias('task_log');
|
|
|
46 |
|
|
|
47 |
$this->set_main_table('task_log', $entitymainalias);
|
|
|
48 |
$this->add_entity($entitymain);
|
|
|
49 |
|
|
|
50 |
// Any columns required by actions should be defined here to ensure they're always available.
|
|
|
51 |
$this->add_base_fields("{$entitymainalias}.id");
|
|
|
52 |
|
|
|
53 |
// We can join the "user" entity to our "main" entity and use the fullname column from the user entity.
|
|
|
54 |
$entityuser = new user();
|
|
|
55 |
$entituseralias = $entityuser->get_table_alias('user');
|
|
|
56 |
$this->add_entity($entityuser->add_join(
|
|
|
57 |
"LEFT JOIN {user} {$entituseralias} ON {$entituseralias}.id = {$entitymainalias}.userid"
|
|
|
58 |
));
|
|
|
59 |
|
|
|
60 |
// Now we can call our helper methods to add the content we want to include in the report.
|
|
|
61 |
$this->add_columns();
|
|
|
62 |
$this->add_filters();
|
|
|
63 |
$this->add_actions();
|
|
|
64 |
|
|
|
65 |
// Set if report can be downloaded.
|
|
|
66 |
$this->set_downloadable(true, get_string('tasklogs', 'admin'));
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Validates access to view this report
|
|
|
71 |
*
|
|
|
72 |
* @return bool
|
|
|
73 |
*/
|
|
|
74 |
protected function can_view(): bool {
|
|
|
75 |
return has_capability('moodle/site:config', context_system::instance());
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Get the visible name of the report
|
|
|
80 |
*
|
|
|
81 |
* @return string
|
|
|
82 |
*/
|
|
|
83 |
public static function get_name(): string {
|
|
|
84 |
return get_string('entitytasklog', 'admin');
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Adds the columns we want to display in the report
|
|
|
89 |
*
|
|
|
90 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
91 |
* unique identifier
|
|
|
92 |
*/
|
|
|
93 |
public function add_columns(): void {
|
1441 |
ariadna |
94 |
$entitymainalias = $this->get_entity('task_log')->get_table_alias('task_log');
|
|
|
95 |
|
|
|
96 |
$this->add_columns_from_entities([
|
1 |
efrain |
97 |
'task_log:name',
|
|
|
98 |
'task_log:type',
|
|
|
99 |
'user:fullname',
|
|
|
100 |
'task_log:starttime',
|
|
|
101 |
'task_log:duration',
|
|
|
102 |
'task_log:hostname',
|
|
|
103 |
'task_log:pid',
|
|
|
104 |
'task_log:database',
|
|
|
105 |
'task_log:result',
|
1441 |
ariadna |
106 |
]);
|
1 |
efrain |
107 |
|
1441 |
ariadna |
108 |
// Wrap the task name in a link.
|
|
|
109 |
$this->get_column('task_log:name')
|
|
|
110 |
->add_field("{$entitymainalias}.id")
|
|
|
111 |
->add_callback(static function(string $output, stdClass $row): string {
|
|
|
112 |
return html_writer::link(new moodle_url('/admin/tasklogs.php', ['logid' => $row->id]), $output);
|
|
|
113 |
});
|
1 |
efrain |
114 |
|
1441 |
ariadna |
115 |
// Rename the user fullname column.
|
|
|
116 |
$this->get_column('user:fullname')
|
|
|
117 |
->set_title(new lang_string('user', 'admin'));
|
1 |
efrain |
118 |
|
|
|
119 |
// It's possible to set a default initial sort direction for one column.
|
|
|
120 |
$this->set_initial_sort_column('task_log:starttime', SORT_DESC);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Adds the filters we want to display in the report
|
|
|
125 |
*
|
|
|
126 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
127 |
* unique identifier
|
|
|
128 |
*/
|
|
|
129 |
protected function add_filters(): void {
|
1441 |
ariadna |
130 |
$this->add_filters_from_entities([
|
1 |
efrain |
131 |
'task_log:name',
|
|
|
132 |
'task_log:type',
|
|
|
133 |
'task_log:output',
|
|
|
134 |
'task_log:result',
|
|
|
135 |
'task_log:timestart',
|
|
|
136 |
'task_log:duration',
|
1441 |
ariadna |
137 |
]);
|
1 |
efrain |
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
|
|
|
142 |
*
|
|
|
143 |
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
|
|
|
144 |
*/
|
|
|
145 |
protected function add_actions(): void {
|
|
|
146 |
|
|
|
147 |
// Action to view individual task log on a popup window.
|
|
|
148 |
$this->add_action((new action(
|
|
|
149 |
new moodle_url('/admin/tasklogs.php', ['logid' => ':id']),
|
|
|
150 |
new pix_icon('e/search', ''),
|
|
|
151 |
[],
|
1441 |
ariadna |
152 |
false,
|
1 |
efrain |
153 |
new lang_string('view'),
|
|
|
154 |
)));
|
|
|
155 |
|
|
|
156 |
// Action to download individual task log.
|
|
|
157 |
$this->add_action((new action(
|
|
|
158 |
new moodle_url('/admin/tasklogs.php', ['logid' => ':id', 'download' => true]),
|
|
|
159 |
new pix_icon('t/download', ''),
|
|
|
160 |
[],
|
|
|
161 |
false,
|
|
|
162 |
new lang_string('download'),
|
|
|
163 |
)));
|
|
|
164 |
}
|
|
|
165 |
}
|