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