MDL-67814 h5p: recover missed xAPI settings
[moodle.git] / report / loglive / classes / table_log.php
blob798a801467aecfbead82d225830befa439f4638b
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.
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/>.
17 /**
18 * Table log for displaying logs.
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
25 defined('MOODLE_INTERNAL') || die;
26 require_once($CFG->libdir . '/tablelib.php');
28 /**
29 * Table log class for displaying logs.
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
36 class report_loglive_table_log extends table_sql {
38 /** @var array list of user fullnames shown in report */
39 protected $userfullnames = array();
41 /** @var array list of course short names shown in report */
42 protected $courseshortnames = array();
44 /** @var array list of context name shown in report */
45 protected $contextname = array();
47 /** @var stdClass filters parameters */
48 protected $filterparams;
50 /**
51 * Sets up the table_log parameters.
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_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.
64 public function __construct($uniqueid, $filterparams = null) {
65 parent::__construct($uniqueid);
67 $this->set_attribute('class', 'reportloglive generaltable table-sm');
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'));
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')
91 ));
92 $this->collapsible(false);
93 $this->sortable(false);
94 $this->pageable(true);
95 $this->is_downloadable(false);
98 /**
99 * Generate the course column.
101 * @param stdClass $event event data.
102 * @return string HTML for the course column.
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];
113 * Generate the time column.
115 * @param stdClass $event event data.
116 * @return string HTML for the time column
118 public function col_time($event) {
119 $recenttimestr = get_string('strftimedatetime', 'core_langconfig');
120 return userdate($event->timecreated, $recenttimestr);
124 * Generate the username column.
126 * @param stdClass $event event data.
127 * @return string HTML for the username column
129 public function col_fullnameuser($event) {
130 // Get extra event data for origin and realuserid.
131 $logextra = $event->get_logextra();
133 // Add username who did the action.
134 if (!empty($logextra['realuserid'])) {
135 $a = new stdClass();
136 $params = array('id' => $logextra['realuserid']);
137 if ($event->courseid) {
138 $params['course'] = $event->courseid;
140 $a->realusername = html_writer::link(new moodle_url("/user/view.php", $params),
141 $this->userfullnames[$logextra['realuserid']]);
142 $params['id'] = $event->userid;
143 $a->asusername = html_writer::link(new moodle_url("/user/view.php", $params),
144 $this->userfullnames[$event->userid]);
145 $username = get_string('eventloggedas', 'report_loglive', $a);
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;
151 $username = html_writer::link(new moodle_url("/user/view.php", $params), $this->userfullnames[$event->userid]);
152 } else {
153 $username = '-';
155 return $username;
159 * Generate the related username column.
161 * @param stdClass $event event data.
162 * @return string HTML for the related username column
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;
171 return html_writer::link(new moodle_url("/user/view.php", $params), $this->userfullnames[$event->relateduserid]);
172 } else {
173 return '-';
178 * Generate the context column.
180 * @param stdClass $event event data.
181 * @return string HTML for the context column
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);
196 } else {
197 $contextname = get_string('other');
200 } else {
201 $contextname = get_string('other');
204 $this->contextname[$event->contextid] = $contextname;
205 return $contextname;
209 * Generate the component column.
211 * @param stdClass $event event data.
212 * @return string HTML for the component column
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;
227 * Generate the event name column.
229 * @param stdClass $event event data.
230 * @return string HTML for the event name column
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();
240 if ($url = $event->get_url()) {
241 $eventname = $this->action_link($url, $eventname, 'action');
243 return $eventname;
247 * Generate the description column.
249 * @param stdClass $event event data.
250 * @return string HTML for the description column
252 public function col_description($event) {
253 // Description.
254 return $event->get_description();
258 * Generate the origin column.
260 * @param stdClass $event event data.
261 * @return string HTML for the origin column
263 public function col_origin($event) {
264 // Get extra event data for origin and realuserid.
265 $logextra = $event->get_logextra();
267 // Add event origin, normally IP/cron.
268 return $logextra['origin'];
272 * Generate the ip column.
274 * @param stdClass $event event data.
275 * @return string HTML for the ip column
277 public function col_ip($event) {
278 // Get extra event data for origin and realuserid.
279 $logextra = $event->get_logextra();
281 $url = new moodle_url("/iplookup/index.php?ip={$logextra['ip']}&user=$event->userid");
282 return $this->action_link($url, $logextra['ip'], 'ip');
286 * Method to create a link with popup action.
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.
292 * @return string html to use.
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);
301 * Query the reader. Store results in the object for use by build_table.
303 * @param int $pagesize size of page for paginated displayed table.
304 * @param bool $useinitialsbar do you want to use the initials bar.
306 public function query_db($pagesize, $useinitialsbar = true) {
308 $joins = array();
309 $params = array();
311 // Set up filtering.
312 if (!empty($this->filterparams->courseid)) {
313 $joins[] = "courseid = :courseid";
314 $params['courseid'] = $this->filterparams->courseid;
317 if (!empty($this->filterparams->date)) {
318 $joins[] = "timecreated > :date";
319 $params['date'] = $this->filterparams->date;
322 if (isset($this->filterparams->anonymous)) {
323 $joins[] = "anonymous = :anon";
324 $params['anon'] = $this->filterparams->anonymous;
327 $selector = implode(' AND ', $joins);
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());
334 // Set initial bars.
335 if ($useinitialsbar) {
336 $this->initialbars($total > $pagesize);
339 // Update list of users and courses list which will be displayed on log page.
340 $this->update_users_and_courses_used();
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.
348 public function update_users_and_courses_used() {
349 global $SITE, $DB;
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;
362 if (!empty($logextra['realuserid']) && !in_array($logextra['realuserid'], $userids)) {
363 $userids[] = $logextra['realuserid'];
365 if (!empty($event->relateduserid) && !in_array($event->relateduserid, $userids)) {
366 $userids[] = $event->relateduserid;
369 if (!empty($event->courseid) && ($event->courseid != $SITE->id) && !in_array($event->courseid, $courseids)) {
370 $courseids[] = $event->courseid;
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);
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.
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} c
391 $ccjoin
392 WHERE c.id " . $coursesql;
394 $courses = $DB->get_records_sql($sql, $courseparams);
395 foreach ($courses as $courseid => $course) {
396 $url = new moodle_url("/course/view.php", array('id' => $courseid));
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)));