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 |
/**
|
|
|
18 |
* Table log for displaying logs.
|
|
|
19 |
*
|
|
|
20 |
* @package report_loglive
|
|
|
21 |
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die;
|
|
|
26 |
require_once($CFG->libdir . '/tablelib.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Table log class for displaying logs.
|
|
|
30 |
*
|
|
|
31 |
* @since Moodle 2.7
|
|
|
32 |
* @package report_loglive
|
|
|
33 |
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class report_loglive_table_log extends table_sql {
|
|
|
37 |
|
|
|
38 |
/** @var array list of user fullnames shown in report */
|
|
|
39 |
protected $userfullnames = array();
|
|
|
40 |
|
|
|
41 |
/** @var array list of course short names shown in report */
|
|
|
42 |
protected $courseshortnames = array();
|
|
|
43 |
|
|
|
44 |
/** @var array list of context name shown in report */
|
|
|
45 |
protected $contextname = array();
|
|
|
46 |
|
|
|
47 |
/** @var stdClass filters parameters */
|
|
|
48 |
protected $filterparams;
|
|
|
49 |
|
|
|
50 |
/** @var int[] A list of users to filter by */
|
|
|
51 |
private ?array $lateuseridfilter = null;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Sets up the table_log parameters.
|
|
|
55 |
*
|
|
|
56 |
* @param string $uniqueid unique id of form.
|
|
|
57 |
* @param stdClass $filterparams (optional) filter params.
|
|
|
58 |
* - int courseid: id of course
|
|
|
59 |
* - int userid: user id
|
|
|
60 |
* - int|string modid: Module id or "site_errors" to view site errors
|
|
|
61 |
* - int groupid: Group id
|
|
|
62 |
* - \core\log\sql_reader logreader: reader from which data will be fetched.
|
|
|
63 |
* - int edulevel: educational level.
|
|
|
64 |
* - string action: view action
|
|
|
65 |
* - int date: Date from which logs to be viewed.
|
|
|
66 |
*/
|
|
|
67 |
public function __construct($uniqueid, $filterparams = null) {
|
|
|
68 |
parent::__construct($uniqueid);
|
|
|
69 |
|
|
|
70 |
$this->set_attribute('class', 'reportloglive generaltable table-sm');
|
|
|
71 |
$this->set_attribute('aria-live', 'polite');
|
|
|
72 |
$this->filterparams = $filterparams;
|
|
|
73 |
// Add course column if logs are displayed for site.
|
|
|
74 |
$cols = array();
|
|
|
75 |
$headers = array();
|
|
|
76 |
if (empty($filterparams->courseid)) {
|
|
|
77 |
$cols = array('course');
|
|
|
78 |
$headers = array(get_string('course'));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$this->define_columns(array_merge($cols, array('time', 'fullnameuser', 'relatedfullnameuser', 'context', 'component',
|
|
|
82 |
'eventname', 'description', 'origin', 'ip')));
|
|
|
83 |
$this->define_headers(array_merge($headers, array(
|
|
|
84 |
get_string('time'),
|
|
|
85 |
get_string('fullnameuser'),
|
|
|
86 |
get_string('eventrelatedfullnameuser', 'report_loglive'),
|
|
|
87 |
get_string('eventcontext', 'report_loglive'),
|
|
|
88 |
get_string('eventcomponent', 'report_loglive'),
|
|
|
89 |
get_string('eventname'),
|
|
|
90 |
get_string('description'),
|
|
|
91 |
get_string('eventorigin', 'report_loglive'),
|
|
|
92 |
get_string('ip_address')
|
|
|
93 |
)
|
|
|
94 |
));
|
|
|
95 |
$this->collapsible(false);
|
|
|
96 |
$this->sortable(false);
|
|
|
97 |
$this->pageable(true);
|
|
|
98 |
$this->is_downloadable(false);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Generate the course column.
|
|
|
103 |
*
|
|
|
104 |
* @param stdClass $event event data.
|
|
|
105 |
* @return string HTML for the course column.
|
|
|
106 |
*/
|
|
|
107 |
public function col_course($event) {
|
|
|
108 |
if (empty($event->courseid) || empty($this->courseshortnames[$event->courseid])) {
|
|
|
109 |
return '-';
|
|
|
110 |
} else {
|
|
|
111 |
return $this->courseshortnames[$event->courseid];
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Generate the time column.
|
|
|
117 |
*
|
|
|
118 |
* @param stdClass $event event data.
|
|
|
119 |
* @return string HTML for the time column
|
|
|
120 |
*/
|
|
|
121 |
public function col_time($event) {
|
|
|
122 |
$recenttimestr = get_string('strftimedatetimeaccurate', 'core_langconfig');
|
|
|
123 |
return userdate($event->timecreated, $recenttimestr);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Generate the username column.
|
|
|
128 |
*
|
|
|
129 |
* @param stdClass $event event data.
|
|
|
130 |
* @return string HTML for the username column
|
|
|
131 |
*/
|
|
|
132 |
public function col_fullnameuser($event) {
|
|
|
133 |
// Get extra event data for origin and realuserid.
|
|
|
134 |
$logextra = $event->get_logextra();
|
|
|
135 |
|
|
|
136 |
// Add username who did the action.
|
|
|
137 |
if (!empty($logextra['realuserid'])) {
|
|
|
138 |
$a = new stdClass();
|
|
|
139 |
$params = array('id' => $logextra['realuserid']);
|
|
|
140 |
if ($event->courseid) {
|
|
|
141 |
$params['course'] = $event->courseid;
|
|
|
142 |
}
|
|
|
143 |
$a->realusername = html_writer::link(new moodle_url("/user/view.php", $params),
|
|
|
144 |
$this->userfullnames[$logextra['realuserid']]);
|
|
|
145 |
$params['id'] = $event->userid;
|
|
|
146 |
$a->asusername = html_writer::link(new moodle_url("/user/view.php", $params),
|
|
|
147 |
$this->userfullnames[$event->userid]);
|
|
|
148 |
$username = get_string('eventloggedas', 'report_loglive', $a);
|
|
|
149 |
} else if (!empty($event->userid) && !empty($this->userfullnames[$event->userid])) {
|
|
|
150 |
$params = array('id' => $event->userid);
|
|
|
151 |
if ($event->courseid) {
|
|
|
152 |
$params['course'] = $event->courseid;
|
|
|
153 |
}
|
|
|
154 |
$username = html_writer::link(new moodle_url("/user/view.php", $params), $this->userfullnames[$event->userid]);
|
|
|
155 |
} else {
|
|
|
156 |
$username = '-';
|
|
|
157 |
}
|
|
|
158 |
return $username;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Generate the related username column.
|
|
|
163 |
*
|
|
|
164 |
* @param stdClass $event event data.
|
|
|
165 |
* @return string HTML for the related username column
|
|
|
166 |
*/
|
|
|
167 |
public function col_relatedfullnameuser($event) {
|
|
|
168 |
// Add affected user.
|
|
|
169 |
if (!empty($event->relateduserid) && isset($this->userfullnames[$event->relateduserid])) {
|
|
|
170 |
$params = array('id' => $event->relateduserid);
|
|
|
171 |
if ($event->courseid) {
|
|
|
172 |
$params['course'] = $event->courseid;
|
|
|
173 |
}
|
|
|
174 |
return html_writer::link(new moodle_url("/user/view.php", $params), $this->userfullnames[$event->relateduserid]);
|
|
|
175 |
} else {
|
|
|
176 |
return '-';
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Generate the context column.
|
|
|
182 |
*
|
|
|
183 |
* @param stdClass $event event data.
|
|
|
184 |
* @return string HTML for the context column
|
|
|
185 |
*/
|
|
|
186 |
public function col_context($event) {
|
|
|
187 |
// Add context name.
|
|
|
188 |
if ($event->contextid) {
|
|
|
189 |
// If context name was fetched before then return, else get one.
|
|
|
190 |
if (isset($this->contextname[$event->contextid])) {
|
|
|
191 |
return $this->contextname[$event->contextid];
|
|
|
192 |
} else {
|
|
|
193 |
$context = context::instance_by_id($event->contextid, IGNORE_MISSING);
|
|
|
194 |
if ($context) {
|
|
|
195 |
$contextname = $context->get_context_name(true);
|
|
|
196 |
if ($url = $context->get_url()) {
|
|
|
197 |
$contextname = html_writer::link($url, $contextname);
|
|
|
198 |
}
|
|
|
199 |
} else {
|
|
|
200 |
$contextname = get_string('other');
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
} else {
|
|
|
204 |
$contextname = get_string('other');
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
$this->contextname[$event->contextid] = $contextname;
|
|
|
208 |
return $contextname;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* Generate the component column.
|
|
|
213 |
*
|
|
|
214 |
* @param stdClass $event event data.
|
|
|
215 |
* @return string HTML for the component column
|
|
|
216 |
*/
|
|
|
217 |
public function col_component($event) {
|
|
|
218 |
// Component.
|
|
|
219 |
$componentname = $event->component;
|
|
|
220 |
if (($event->component === 'core') || ($event->component === 'legacy')) {
|
|
|
221 |
return get_string('coresystem');
|
|
|
222 |
} else if (get_string_manager()->string_exists('pluginname', $event->component)) {
|
|
|
223 |
return get_string('pluginname', $event->component);
|
|
|
224 |
} else {
|
|
|
225 |
return $componentname;
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Generate the event name column.
|
|
|
231 |
*
|
|
|
232 |
* @param stdClass $event event data.
|
|
|
233 |
* @return string HTML for the event name column
|
|
|
234 |
*/
|
|
|
235 |
public function col_eventname($event) {
|
|
|
236 |
$eventname = $event->get_name();
|
|
|
237 |
if ($url = $event->get_url()) {
|
|
|
238 |
$eventname = $this->action_link($url, $eventname, 'action');
|
|
|
239 |
}
|
|
|
240 |
return $eventname;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* Generate the description column.
|
|
|
245 |
*
|
|
|
246 |
* @param stdClass $event event data.
|
|
|
247 |
* @return string HTML for the description column
|
|
|
248 |
*/
|
|
|
249 |
public function col_description($event) {
|
|
|
250 |
// Description.
|
|
|
251 |
return $event->get_description();
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
/**
|
|
|
255 |
* Generate the origin column.
|
|
|
256 |
*
|
|
|
257 |
* @param stdClass $event event data.
|
|
|
258 |
* @return string HTML for the origin column
|
|
|
259 |
*/
|
|
|
260 |
public function col_origin($event) {
|
|
|
261 |
// Get extra event data for origin and realuserid.
|
|
|
262 |
$logextra = $event->get_logextra();
|
|
|
263 |
|
|
|
264 |
// Add event origin, normally IP/cron.
|
|
|
265 |
return $logextra['origin'];
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* Generate the ip column.
|
|
|
270 |
*
|
|
|
271 |
* @param stdClass $event event data.
|
|
|
272 |
* @return string HTML for the ip column
|
|
|
273 |
*/
|
|
|
274 |
public function col_ip($event) {
|
|
|
275 |
// Get extra event data for origin and realuserid.
|
|
|
276 |
$logextra = $event->get_logextra();
|
|
|
277 |
|
|
|
278 |
$url = new moodle_url("/iplookup/index.php?popup=1&ip={$logextra['ip']}&user=$event->userid");
|
|
|
279 |
return $this->action_link($url, $logextra['ip'], 'ip');
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
/**
|
|
|
283 |
* Method to create a link with popup action.
|
|
|
284 |
*
|
|
|
285 |
* @param moodle_url $url The url to open.
|
|
|
286 |
* @param string $text Anchor text for the link.
|
|
|
287 |
* @param string $name Name of the popup window.
|
|
|
288 |
*
|
|
|
289 |
* @return string html to use.
|
|
|
290 |
*/
|
|
|
291 |
protected function action_link(moodle_url $url, $text, $name = 'popup') {
|
|
|
292 |
global $OUTPUT;
|
|
|
293 |
$link = new action_link($url, $text, new popup_action('click', $url, $name, array('height' => 550, 'width' => 700)));
|
|
|
294 |
return $OUTPUT->render($link);
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
/**
|
|
|
298 |
* Query the reader. Store results in the object for use by build_table.
|
|
|
299 |
*
|
|
|
300 |
* @param int $pagesize size of page for paginated displayed table.
|
|
|
301 |
* @param bool $useinitialsbar do you want to use the initials bar.
|
|
|
302 |
*/
|
|
|
303 |
public function query_db($pagesize, $useinitialsbar = true) {
|
|
|
304 |
$joins = [];
|
|
|
305 |
$params = [];
|
|
|
306 |
|
|
|
307 |
if (!empty($this->filterparams->courseid)) {
|
|
|
308 |
$joins[] = "courseid = :courseid";
|
|
|
309 |
$params['courseid'] = $this->filterparams->courseid;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
// Getting all members of a group.
|
|
|
313 |
[
|
|
|
314 |
'joins' => $groupjoins,
|
|
|
315 |
'params' => $groupparams,
|
|
|
316 |
'useridfilter' => $this->lateuseridfilter,
|
|
|
317 |
] = \core\report_helper::get_group_filter($this->filterparams);
|
|
|
318 |
$joins = array_merge($joins, $groupjoins);
|
|
|
319 |
$params = array_merge($params, $groupparams);
|
|
|
320 |
|
|
|
321 |
if (!empty($this->filterparams->date)) {
|
|
|
322 |
$joins[] = "timecreated > :date";
|
|
|
323 |
$params['date'] = $this->filterparams->date;
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
if (isset($this->filterparams->anonymous)) {
|
|
|
327 |
$joins[] = "anonymous = :anon";
|
|
|
328 |
$params['anon'] = $this->filterparams->anonymous;
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
$selector = implode(' AND ', $joins);
|
|
|
332 |
|
|
|
333 |
$total = $this->filterparams->logreader->get_events_select_count($selector, $params);
|
|
|
334 |
$this->pagesize($pagesize, $total);
|
|
|
335 |
|
|
|
336 |
$this->rawdata =
|
|
|
337 |
array_filter(
|
|
|
338 |
$this->filterparams->logreader->get_events_select(
|
|
|
339 |
$selector,
|
|
|
340 |
$params,
|
|
|
341 |
$this->filterparams->orderby,
|
|
|
342 |
$this->get_page_start(),
|
|
|
343 |
$this->get_page_size(),
|
|
|
344 |
),
|
|
|
345 |
function($event) {
|
|
|
346 |
if ($this->lateuseridfilter === null) {
|
|
|
347 |
return true;
|
|
|
348 |
}
|
|
|
349 |
return isset($this->lateuseridfilter[$event->userid]);
|
|
|
350 |
},
|
|
|
351 |
);
|
|
|
352 |
|
|
|
353 |
// Set initial bars.
|
|
|
354 |
if ($useinitialsbar) {
|
|
|
355 |
$this->initialbars($total > $pagesize);
|
|
|
356 |
}
|
|
|
357 |
|
|
|
358 |
// Update list of users and courses list which will be displayed on log page.
|
|
|
359 |
$this->update_users_and_courses_used();
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
/**
|
|
|
363 |
* Helper function to create list of course shortname and user fullname shown in log report.
|
|
|
364 |
* This will update $this->userfullnames and $this->courseshortnames array with userfullname and courseshortname (with link),
|
|
|
365 |
* which will be used to render logs in table.
|
|
|
366 |
*/
|
|
|
367 |
public function update_users_and_courses_used() {
|
|
|
368 |
global $SITE, $DB;
|
|
|
369 |
|
|
|
370 |
$this->userfullnames = array();
|
|
|
371 |
$this->courseshortnames = array($SITE->id => $SITE->shortname);
|
|
|
372 |
$userids = array();
|
|
|
373 |
$courseids = array();
|
|
|
374 |
// For each event cache full username and course.
|
|
|
375 |
// Get list of userids and courseids which will be shown in log report.
|
|
|
376 |
foreach ($this->rawdata as $event) {
|
|
|
377 |
$logextra = $event->get_logextra();
|
|
|
378 |
if (!empty($event->userid) && !in_array($event->userid, $userids)) {
|
|
|
379 |
$userids[] = $event->userid;
|
|
|
380 |
}
|
|
|
381 |
if (!empty($logextra['realuserid']) && !in_array($logextra['realuserid'], $userids)) {
|
|
|
382 |
$userids[] = $logextra['realuserid'];
|
|
|
383 |
}
|
|
|
384 |
if (!empty($event->relateduserid) && !in_array($event->relateduserid, $userids)) {
|
|
|
385 |
$userids[] = $event->relateduserid;
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
if (!empty($event->courseid) && ($event->courseid != $SITE->id) && !in_array($event->courseid, $courseids)) {
|
|
|
389 |
$courseids[] = $event->courseid;
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
// Get user fullname and put that in return list.
|
|
|
394 |
if (!empty($userids)) {
|
|
|
395 |
list($usql, $uparams) = $DB->get_in_or_equal($userids);
|
|
|
396 |
$userfieldsapi = \core_user\fields::for_name();
|
|
|
397 |
$users = $DB->get_records_sql("SELECT id," .
|
|
|
398 |
$userfieldsapi->get_sql('', false, '', '', false)->selects . " FROM {user} WHERE id " . $usql,
|
|
|
399 |
$uparams);
|
|
|
400 |
foreach ($users as $userid => $user) {
|
|
|
401 |
$this->userfullnames[$userid] = fullname($user, has_capability('moodle/site:viewfullnames', $this->get_context()));
|
|
|
402 |
}
|
|
|
403 |
}
|
|
|
404 |
|
|
|
405 |
// Get course shortname and put that in return list.
|
|
|
406 |
if (!empty($courseids)) { // If all logs don't belog to site level then get course info.
|
|
|
407 |
list($coursesql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
|
|
|
408 |
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
|
|
|
409 |
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
|
|
|
410 |
$courseparams['contextlevel'] = CONTEXT_COURSE;
|
|
|
411 |
$sql = "SELECT c.id,c.shortname $ccselect FROM {course} c
|
|
|
412 |
$ccjoin
|
|
|
413 |
WHERE c.id " . $coursesql;
|
|
|
414 |
|
|
|
415 |
$courses = $DB->get_records_sql($sql, $courseparams);
|
|
|
416 |
foreach ($courses as $courseid => $course) {
|
|
|
417 |
$url = new moodle_url("/course/view.php", array('id' => $courseid));
|
|
|
418 |
context_helper::preload_from_record($course);
|
|
|
419 |
$context = context_course::instance($courseid, IGNORE_MISSING);
|
|
|
420 |
// Method format_string() takes care of missing contexts.
|
|
|
421 |
$this->courseshortnames[$courseid] = html_writer::link($url, format_string($course->shortname, true,
|
|
|
422 |
array('context' => $context)));
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
}
|
|
|
426 |
|
|
|
427 |
/**
|
|
|
428 |
* Returns the latest timestamp of the records in the table.
|
|
|
429 |
*
|
|
|
430 |
* @return int
|
|
|
431 |
*/
|
|
|
432 |
public function get_until(): int {
|
|
|
433 |
$until = $this->filterparams->date;
|
|
|
434 |
if (!empty($this->rawdata)) {
|
|
|
435 |
foreach ($this->rawdata as $row) {
|
|
|
436 |
$until = max($row->timecreated, $until);
|
|
|
437 |
}
|
|
|
438 |
}
|
|
|
439 |
return $until;
|
|
|
440 |
}
|
|
|
441 |
}
|