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