Commit | Line | Data |
---|---|---|
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 | ||
25 | defined('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 | */ | |
34 | class report_log_table_log extends table_sql { | |
35 | ||
36 | /** @var array list of user fullnames shown in report */ | |
37 | private $userfullnames = array(); | |
38 | ||
1cfce08e DM |
39 | /** |
40 | * @deprecated since Moodle 2.9 MDL-48595 - please do not use this argument any more. | |
41 | * @todo MDL-49291 This will be deleted in 3.1 | |
42 | * @var array list of course short names shown in report. | |
43 | */ | |
ac8976c8 RT |
44 | private $courseshortnames = array(); |
45 | ||
46 | /** @var array list of context name shown in report */ | |
47 | private $contextname = array(); | |
48 | ||
49 | /** @var stdClass filters parameters */ | |
50 | private $filterparams; | |
51 | ||
52 | /** | |
53 | * Sets up the table_log parameters. | |
54 | * | |
55 | * @param string $uniqueid unique id of form. | |
56 | * @param stdClass $filterparams (optional) filter params. | |
57 | * - int courseid: id of course | |
58 | * - int userid: user id | |
59 | * - int|string modid: Module id or "site_errors" to view site errors | |
60 | * - int groupid: Group id | |
1cfce08e | 61 | * - \core\log\sql_reader logreader: reader from which data will be fetched. |
ac8976c8 RT |
62 | * - int edulevel: educational level. |
63 | * - string action: view action | |
64 | * - int date: Date from which logs to be viewed. | |
65 | */ | |
66 | public function __construct($uniqueid, $filterparams = null) { | |
67 | parent::__construct($uniqueid); | |
68 | ||
69 | $this->set_attribute('class', 'reportlog generaltable generalbox'); | |
70 | $this->filterparams = $filterparams; | |
71 | // Add course column if logs are displayed for site. | |
72 | $cols = array(); | |
73 | $headers = array(); | |
74 | if (empty($filterparams->courseid)) { | |
75 | $cols = array('course'); | |
76 | $headers = array(get_string('course')); | |
77 | } | |
78 | ||
79 | $this->define_columns(array_merge($cols, array('time', 'fullnameuser', 'relatedfullnameuser', 'context', 'component', | |
80 | 'eventname', 'description', 'origin', 'ip'))); | |
81 | $this->define_headers(array_merge($headers, array( | |
82 | get_string('time'), | |
83 | get_string('fullnameuser'), | |
84 | get_string('eventrelatedfullnameuser', 'report_log'), | |
85 | get_string('eventcontext', 'report_log'), | |
86 | get_string('eventcomponent', 'report_log'), | |
87 | get_string('eventname'), | |
88 | get_string('description'), | |
89 | get_string('eventorigin', 'report_log'), | |
90 | get_string('ip_address') | |
91 | ) | |
92 | )); | |
93 | $this->collapsible(false); | |
94 | $this->sortable(false); | |
95 | $this->pageable(true); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Generate the course column. | |
100 | * | |
1cfce08e DM |
101 | * @deprecated since Moodle 2.9 MDL-48595 - please do not use this function any more. |
102 | * @todo MDL-49291 This will be deleted in 3.1 | |
ac8976c8 RT |
103 | * @param stdClass $event event data. |
104 | * @return string HTML for the course column. | |
105 | */ | |
106 | public function col_course($event) { | |
1cfce08e DM |
107 | |
108 | debugging('col_course() is deprecated, there is no such column', DEBUG_DEVELOPER); | |
109 | ||
ac8976c8 RT |
110 | if (empty($event->courseid) || empty($this->courseshortnames[$event->courseid])) { |
111 | return '-'; | |
112 | } else { | |
113 | return $this->courseshortnames[$event->courseid]; | |
114 | } | |
115 | } | |
116 | ||
1cfce08e DM |
117 | /** |
118 | * Gets the user full name. | |
119 | * | |
120 | * This function is useful because, in the unlikely case that the user is | |
121 | * not already loaded in $this->userfullnames it will fetch it from db. | |
122 | * | |
123 | * @since Moodle 2.9 | |
124 | * @param int $userid | |
125 | * @return string|false | |
126 | */ | |
127 | protected function get_user_fullname($userid) { | |
128 | global $DB; | |
129 | ||
5803e36c DM |
130 | if (empty($userid)) { |
131 | return false; | |
132 | } | |
133 | ||
1cfce08e DM |
134 | if (!empty($this->userfullnames[$userid])) { |
135 | return $this->userfullnames[$userid]; | |
136 | } | |
137 | ||
138 | // We already looked for the user and it does not exist. | |
139 | if ($this->userfullnames[$userid] === false) { | |
140 | return false; | |
141 | } | |
142 | ||
143 | // If we reach that point new users logs have been generated since the last users db query. | |
144 | list($usql, $uparams) = $DB->get_in_or_equal($userid); | |
145 | $sql = "SELECT id," . get_all_user_name_fields(true) . " FROM {user} WHERE id " . $usql; | |
146 | if (!$user = $DB->get_records_sql($sql, $uparams)) { | |
147 | return false; | |
148 | } | |
149 | ||
150 | $this->userfullnames[$userid] = fullname($user); | |
151 | return $this->userfullnames[$userid]; | |
152 | } | |
153 | ||
ac8976c8 RT |
154 | /** |
155 | * Generate the time column. | |
156 | * | |
157 | * @param stdClass $event event data. | |
158 | * @return string HTML for the time column | |
159 | */ | |
160 | public function col_time($event) { | |
72ae4ec8 DP |
161 | |
162 | if (empty($this->download)) { | |
163 | $dateformat = get_string('strftimerecent', 'core_langconfig'); | |
164 | } else { | |
165 | $dateformat = get_string('strftimedatetimeshort', 'core_langconfig'); | |
166 | } | |
167 | return userdate($event->timecreated, $dateformat); | |
ac8976c8 RT |
168 | } |
169 | ||
170 | /** | |
171 | * Generate the username column. | |
172 | * | |
173 | * @param stdClass $event event data. | |
174 | * @return string HTML for the username column | |
175 | */ | |
176 | public function col_fullnameuser($event) { | |
177 | // Get extra event data for origin and realuserid. | |
178 | $logextra = $event->get_logextra(); | |
179 | ||
180 | // Add username who did the action. | |
181 | if (!empty($logextra['realuserid'])) { | |
182 | $a = new stdClass(); | |
1cfce08e DM |
183 | if (!$a->realusername = $this->get_user_fullname($logextra['realuserid'])) { |
184 | $a->realusername = '-'; | |
185 | } | |
186 | if (!$a->asusername = $this->get_user_fullname($event->userid)) { | |
187 | $a->asusername = '-'; | |
1ac341e2 | 188 | } |
b175341e | 189 | if (empty($this->download)) { |
1cfce08e DM |
190 | $params = array('id' => $logextra['realuserid']); |
191 | if ($event->courseid) { | |
192 | $params['course'] = $event->courseid; | |
193 | } | |
b175341e TB |
194 | $a->realusername = html_writer::link(new moodle_url('/user/view.php', $params), $a->realusername); |
195 | $params['id'] = $event->userid; | |
196 | $a->asusername = html_writer::link(new moodle_url('/user/view.php', $params), $a->asusername); | |
197 | } | |
ac8976c8 | 198 | $username = get_string('eventloggedas', 'report_log', $a); |
1cfce08e DM |
199 | |
200 | } else if (!empty($event->userid) && $username = $this->get_user_fullname($event->userid)) { | |
b175341e | 201 | if (empty($this->download)) { |
1cfce08e DM |
202 | $params = array('id' => $event->userid); |
203 | if ($event->courseid) { | |
204 | $params['course'] = $event->courseid; | |
205 | } | |
b175341e TB |
206 | $username = html_writer::link(new moodle_url('/user/view.php', $params), $username); |
207 | } | |
ac8976c8 RT |
208 | } else { |
209 | $username = '-'; | |
210 | } | |
211 | return $username; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Generate the related username column. | |
216 | * | |
217 | * @param stdClass $event event data. | |
218 | * @return string HTML for the related username column | |
219 | */ | |
220 | public function col_relatedfullnameuser($event) { | |
221 | // Add affected user. | |
1cfce08e | 222 | if (!empty($event->relateduserid) && $username = $this->get_user_fullname($event->relateduserid)) { |
b175341e | 223 | if (empty($this->download)) { |
1cfce08e DM |
224 | $params = array('id' => $event->relateduserid); |
225 | if ($event->courseid) { | |
226 | $params['course'] = $event->courseid; | |
227 | } | |
b175341e TB |
228 | $username = html_writer::link(new moodle_url('/user/view.php', $params), $username); |
229 | } | |
ac8976c8 | 230 | } else { |
b175341e | 231 | $username = '-'; |
ac8976c8 | 232 | } |
b175341e | 233 | return $username; |
ac8976c8 RT |
234 | } |
235 | ||
236 | /** | |
237 | * Generate the context column. | |
238 | * | |
239 | * @param stdClass $event event data. | |
240 | * @return string HTML for the context column | |
241 | */ | |
242 | public function col_context($event) { | |
243 | // Add context name. | |
244 | if ($event->contextid) { | |
245 | // If context name was fetched before then return, else get one. | |
246 | if (isset($this->contextname[$event->contextid])) { | |
247 | return $this->contextname[$event->contextid]; | |
248 | } else { | |
249 | $context = context::instance_by_id($event->contextid, IGNORE_MISSING); | |
250 | if ($context) { | |
251 | $contextname = $context->get_context_name(true); | |
40b10c30 | 252 | if (empty($this->download) && $url = $context->get_url()) { |
ac8976c8 RT |
253 | $contextname = html_writer::link($url, $contextname); |
254 | } | |
255 | } else { | |
256 | $contextname = get_string('other'); | |
257 | } | |
258 | } | |
259 | } else { | |
260 | $contextname = get_string('other'); | |
261 | } | |
262 | ||
263 | $this->contextname[$event->contextid] = $contextname; | |
264 | return $contextname; | |
265 | } | |
266 | ||
267 | /** | |
268 | * Generate the component column. | |
269 | * | |
270 | * @param stdClass $event event data. | |
271 | * @return string HTML for the component column | |
272 | */ | |
273 | public function col_component($event) { | |
274 | // Component. | |
275 | $componentname = $event->component; | |
276 | if (($event->component === 'core') || ($event->component === 'legacy')) { | |
277 | return get_string('coresystem'); | |
278 | } else if (get_string_manager()->string_exists('pluginname', $event->component)) { | |
279 | return get_string('pluginname', $event->component); | |
280 | } else { | |
281 | return $componentname; | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Generate the event name column. | |
287 | * | |
288 | * @param stdClass $event event data. | |
289 | * @return string HTML for the event name column | |
290 | */ | |
291 | public function col_eventname($event) { | |
292 | // Event name. | |
1ac341e2 AA |
293 | if ($this->filterparams->logreader instanceof logstore_legacy\log\store) { |
294 | // Hack for support of logstore_legacy. | |
295 | $eventname = $event->eventname; | |
296 | } else { | |
297 | $eventname = $event->get_name(); | |
298 | } | |
b175341e TB |
299 | // Only encode as an action link if we're not downloading. |
300 | if (($url = $event->get_url()) && empty($this->download)) { | |
1ac341e2 | 301 | $eventname = $this->action_link($url, $eventname, 'action'); |
ac8976c8 RT |
302 | } |
303 | return $eventname; | |
304 | } | |
305 | ||
306 | /** | |
307 | * Generate the description column. | |
308 | * | |
309 | * @param stdClass $event event data. | |
310 | * @return string HTML for the description column | |
311 | */ | |
312 | public function col_description($event) { | |
313 | // Description. | |
314 | return $event->get_description(); | |
315 | } | |
316 | ||
317 | /** | |
318 | * Generate the origin column. | |
319 | * | |
320 | * @param stdClass $event event data. | |
321 | * @return string HTML for the origin column | |
322 | */ | |
323 | public function col_origin($event) { | |
324 | // Get extra event data for origin and realuserid. | |
325 | $logextra = $event->get_logextra(); | |
326 | ||
327 | // Add event origin, normally IP/cron. | |
328 | return $logextra['origin']; | |
329 | } | |
330 | ||
331 | /** | |
332 | * Generate the ip column. | |
333 | * | |
334 | * @param stdClass $event event data. | |
335 | * @return string HTML for the ip column | |
336 | */ | |
337 | public function col_ip($event) { | |
338 | // Get extra event data for origin and realuserid. | |
339 | $logextra = $event->get_logextra(); | |
b175341e | 340 | $ip = $logextra['ip']; |
ac8976c8 | 341 | |
b175341e TB |
342 | if (empty($this->download)) { |
343 | $url = new moodle_url("/iplookup/index.php?ip={$ip}&user={$event->userid}"); | |
344 | $ip = $this->action_link($url, $ip, 'ip'); | |
345 | } | |
346 | return $ip; | |
1ac341e2 AA |
347 | } |
348 | ||
349 | /** | |
350 | * Method to create a link with popup action. | |
351 | * | |
352 | * @param moodle_url $url The url to open. | |
353 | * @param string $text Anchor text for the link. | |
354 | * @param string $name Name of the popup window. | |
355 | * | |
356 | * @return string html to use. | |
357 | */ | |
358 | protected function action_link(moodle_url $url, $text, $name = 'popup') { | |
359 | global $OUTPUT; | |
360 | $link = new action_link($url, $text, new popup_action('click', $url, $name, array('height' => 440, 'width' => 700))); | |
361 | return $OUTPUT->render($link); | |
ac8976c8 RT |
362 | } |
363 | ||
364 | /** | |
365 | * Helper function to get legacy crud action. | |
366 | * | |
367 | * @param string $crud crud action | |
368 | * @return string legacy action. | |
369 | */ | |
370 | public function get_legacy_crud_action($crud) { | |
371 | $legacyactionmap = array('c' => 'add', 'r' => 'view', 'u' => 'update', 'd' => 'delete'); | |
372 | if (array_key_exists($crud, $legacyactionmap)) { | |
373 | return $legacyactionmap[$crud]; | |
374 | } else { | |
375 | // From old legacy log. | |
376 | return '-view'; | |
377 | } | |
378 | } | |
379 | ||
380 | /** | |
381 | * Helper function which is used by build logs to get action sql and param. | |
382 | * | |
383 | * @return array sql and param for action. | |
384 | */ | |
385 | public function get_action_sql() { | |
386 | global $DB; | |
387 | ||
388 | // In new logs we have a field to pick, and in legacy try get this from action. | |
389 | if ($this->filterparams->logreader instanceof logstore_legacy\log\store) { | |
390 | $action = $this->get_legacy_crud_action($this->filterparams->action); | |
391 | $firstletter = substr($action, 0, 1); | |
392 | if ($firstletter == '-') { | |
393 | $sql = $DB->sql_like('action', ':action', false, true, true); | |
394 | $params['action'] = '%'.substr($action, 1).'%'; | |
395 | } else { | |
396 | $sql = $DB->sql_like('action', ':action', false); | |
397 | $params['action'] = '%'.$action.'%'; | |
398 | } | |
43a7ac72 | 399 | } else if (!empty($this->filterparams->action)) { |
ac8976c8 RT |
400 | $sql = "crud = :crud"; |
401 | $params['crud'] = $this->filterparams->action; | |
43a7ac72 MG |
402 | } else { |
403 | // Add condition for all possible values of crud (to use db index). | |
404 | list($sql, $params) = $DB->get_in_or_equal(array('c', 'r', 'u', 'd'), | |
405 | SQL_PARAMS_NAMED, 'crud'); | |
406 | $sql = "crud ".$sql; | |
ac8976c8 RT |
407 | } |
408 | return array($sql, $params); | |
409 | } | |
410 | ||
39110100 EM |
411 | /** |
412 | * Helper function which is used by build logs to get course module sql and param. | |
413 | * | |
414 | * @return array sql and param for action. | |
415 | */ | |
416 | public function get_cm_sql() { | |
417 | $joins = array(); | |
418 | $params = array(); | |
419 | ||
420 | if ($this->filterparams->logreader instanceof logstore_legacy\log\store) { | |
421 | // The legacy store doesn't support context level. | |
422 | $joins[] = "cmid = :cmid"; | |
423 | $params['cmid'] = $this->filterparams->modid; | |
424 | } else { | |
425 | $joins[] = "contextinstanceid = :contextinstanceid"; | |
426 | $joins[] = "contextlevel = :contextmodule"; | |
427 | $params['contextinstanceid'] = $this->filterparams->modid; | |
428 | $params['contextmodule'] = CONTEXT_MODULE; | |
429 | } | |
430 | ||
431 | $sql = implode(' AND ', $joins); | |
432 | return array($sql, $params); | |
433 | } | |
434 | ||
ac8976c8 RT |
435 | /** |
436 | * Query the reader. Store results in the object for use by build_table. | |
437 | * | |
438 | * @param int $pagesize size of page for paginated displayed table. | |
439 | * @param bool $useinitialsbar do you want to use the initials bar. | |
440 | */ | |
441 | public function query_db($pagesize, $useinitialsbar = true) { | |
43a7ac72 | 442 | global $DB; |
ac8976c8 RT |
443 | |
444 | $joins = array(); | |
445 | $params = array(); | |
446 | ||
43a7ac72 MG |
447 | // If we filter by userid and module id we also need to filter by crud and edulevel to ensure DB index is engaged. |
448 | $useextendeddbindex = !($this->filterparams->logreader instanceof logstore_legacy\log\store) | |
449 | && !empty($this->filterparams->userid) && !empty($this->filterparams->modid); | |
450 | ||
ac8976c8 | 451 | $groupid = 0; |
7cd004e2 | 452 | if (!empty($this->filterparams->courseid) && $this->filterparams->courseid != SITEID) { |
ac8976c8 RT |
453 | if (!empty($this->filterparams->groupid)) { |
454 | $groupid = $this->filterparams->groupid; | |
455 | } | |
456 | ||
457 | $joins[] = "courseid = :courseid"; | |
458 | $params['courseid'] = $this->filterparams->courseid; | |
459 | } | |
460 | ||
461 | if (!empty($this->filterparams->siteerrors)) { | |
462 | $joins[] = "( action='error' OR action='infected' OR action='failed' )"; | |
463 | } | |
464 | ||
465 | if (!empty($this->filterparams->modid)) { | |
39110100 EM |
466 | list($actionsql, $actionparams) = $this->get_cm_sql(); |
467 | $joins[] = $actionsql; | |
468 | $params = array_merge($params, $actionparams); | |
ac8976c8 RT |
469 | } |
470 | ||
43a7ac72 | 471 | if (!empty($this->filterparams->action) || $useextendeddbindex) { |
ac8976c8 RT |
472 | list($actionsql, $actionparams) = $this->get_action_sql(); |
473 | $joins[] = $actionsql; | |
474 | $params = array_merge($params, $actionparams); | |
475 | } | |
476 | ||
477 | // Getting all members of a group. | |
478 | if ($groupid and empty($this->filterparams->userid)) { | |
479 | if ($gusers = groups_get_members($groupid)) { | |
480 | $gusers = array_keys($gusers); | |
481 | $joins[] = 'userid IN (' . implode(',', $gusers) . ')'; | |
482 | } else { | |
483 | $joins[] = 'userid = 0'; // No users in groups, so we want something that will always be false. | |
484 | } | |
485 | } else if (!empty($this->filterparams->userid)) { | |
486 | $joins[] = "userid = :userid"; | |
487 | $params['userid'] = $this->filterparams->userid; | |
488 | } | |
489 | ||
490 | if (!empty($this->filterparams->date)) { | |
c906551f | 491 | $joins[] = "timecreated > :date AND timecreated < :enddate"; |
ac8976c8 | 492 | $params['date'] = $this->filterparams->date; |
c906551f | 493 | $params['enddate'] = $this->filterparams->date + DAYSECS; // Show logs only for the selected date. |
ac8976c8 RT |
494 | } |
495 | ||
496 | if (isset($this->filterparams->edulevel) && ($this->filterparams->edulevel >= 0)) { | |
497 | $joins[] = "edulevel = :edulevel"; | |
498 | $params['edulevel'] = $this->filterparams->edulevel; | |
43a7ac72 MG |
499 | } else if ($useextendeddbindex) { |
500 | list($edulevelsql, $edulevelparams) = $DB->get_in_or_equal(array(\core\event\base::LEVEL_OTHER, | |
501 | \core\event\base::LEVEL_PARTICIPATING, \core\event\base::LEVEL_TEACHING), SQL_PARAMS_NAMED, 'edulevel'); | |
502 | $joins[] = "edulevel ".$edulevelsql; | |
503 | $params = array_merge($params, $edulevelparams); | |
504 | } | |
505 | ||
506 | if (!($this->filterparams->logreader instanceof logstore_legacy\log\store)) { | |
507 | // Filter out anonymous actions, this is N/A for legacy log because it never stores them. | |
508 | $joins[] = "anonymous = 0"; | |
ac8976c8 RT |
509 | } |
510 | ||
511 | $selector = implode(' AND ', $joins); | |
512 | ||
513 | if (!$this->is_downloading()) { | |
514 | $total = $this->filterparams->logreader->get_events_select_count($selector, $params); | |
515 | $this->pagesize($pagesize, $total); | |
fb9a2cdd DM |
516 | } else { |
517 | $this->pageable(false); | |
ac8976c8 | 518 | } |
fb9a2cdd | 519 | |
1cfce08e DM |
520 | // Get the users and course data. |
521 | $this->rawdata = $this->filterparams->logreader->get_events_select_iterator($selector, $params, | |
522 | $this->filterparams->orderby, $this->get_page_start(), $this->get_page_size()); | |
523 | ||
524 | // Update list of users which will be displayed on log page. | |
525 | $this->update_users_used(); | |
526 | ||
527 | // Get the events. Same query than before; even if it is not likely, logs from new users | |
528 | // may be added since last query so we will need to work around later to prevent problems. | |
529 | // In almost most of the cases this will be better than having two opened recordsets. | |
530 | $this->rawdata = $this->filterparams->logreader->get_events_select_iterator($selector, $params, | |
531 | $this->filterparams->orderby, $this->get_page_start(), $this->get_page_size()); | |
ac8976c8 RT |
532 | |
533 | // Set initial bars. | |
534 | if ($useinitialsbar && !$this->is_downloading()) { | |
535 | $this->initialbars($total > $pagesize); | |
536 | } | |
537 | ||
ac8976c8 RT |
538 | } |
539 | ||
540 | /** | |
541 | * Helper function to create list of course shortname and user fullname shown in log report. | |
1cfce08e | 542 | * |
ac8976c8 RT |
543 | * This will update $this->userfullnames and $this->courseshortnames array with userfullname and courseshortname (with link), |
544 | * which will be used to render logs in table. | |
1cfce08e DM |
545 | * |
546 | * @deprecated since Moodle 2.9 MDL-48595 - please do not use this function any more. | |
547 | * @todo MDL-49291 This will be deleted in 3.1 | |
548 | * @see self::update_users_used() | |
ac8976c8 RT |
549 | */ |
550 | public function update_users_and_courses_used() { | |
551 | global $SITE, $DB; | |
552 | ||
1cfce08e DM |
553 | debugging('update_users_and_courses_used() is deprecated, please use update_users_used() instead.', DEBUG_DEVELOPER); |
554 | ||
555 | // We should not call self::update_users_used() as would have to iterate twice around the list of logs. | |
556 | ||
ac8976c8 RT |
557 | $this->userfullnames = array(); |
558 | $this->courseshortnames = array($SITE->id => $SITE->shortname); | |
559 | $userids = array(); | |
560 | $courseids = array(); | |
561 | // For each event cache full username and course. | |
562 | // Get list of userids and courseids which will be shown in log report. | |
563 | foreach ($this->rawdata as $event) { | |
564 | $logextra = $event->get_logextra(); | |
1cfce08e DM |
565 | if (!empty($event->userid) && empty($userids[$event->userid])) { |
566 | $userids[$event->userid] = $event->userid; | |
ac8976c8 | 567 | } |
1cfce08e DM |
568 | if (!empty($logextra['realuserid']) && empty($userids[$logextra['realuserid']])) { |
569 | $userids[$logextra['realuserid']] = $logextra['realuserid']; | |
ac8976c8 | 570 | } |
1cfce08e DM |
571 | if (!empty($event->relateduserid) && empty($userids[$event->relateduserid])) { |
572 | $userids[$event->relateduserid] = $event->relateduserid; | |
ac8976c8 RT |
573 | } |
574 | ||
575 | if (!empty($event->courseid) && ($event->courseid != $SITE->id) && !in_array($event->courseid, $courseids)) { | |
576 | $courseids[] = $event->courseid; | |
577 | } | |
578 | } | |
579 | ||
1cfce08e DM |
580 | // Closing it just in case, we can not rewind moodle recordsets anyway. |
581 | if ($this->rawdata instanceof \core\dml\recordset_walk || | |
582 | $this->rawdata instanceof moodle_recordset) { | |
583 | $this->rawdata->close(); | |
584 | } | |
585 | ||
ac8976c8 RT |
586 | // Get user fullname and put that in return list. |
587 | if (!empty($userids)) { | |
588 | list($usql, $uparams) = $DB->get_in_or_equal($userids); | |
589 | $users = $DB->get_records_sql("SELECT id," . get_all_user_name_fields(true) . " FROM {user} WHERE id " . $usql, | |
590 | $uparams); | |
591 | foreach ($users as $userid => $user) { | |
592 | $this->userfullnames[$userid] = fullname($user); | |
1cfce08e DM |
593 | unset($userids[$userid]); |
594 | } | |
595 | ||
596 | // We fill the array with false values for the users that don't exist anymore | |
597 | // in the database so we don't need to query the db again later. | |
598 | foreach ($userids as $userid) { | |
599 | $this->userfullnames[$userid] = false; | |
ac8976c8 RT |
600 | } |
601 | } | |
602 | ||
603 | // Get course shortname and put that in return list. | |
604 | if (!empty($courseids)) { // If all logs don't belog to site level then get course info. | |
1ac341e2 AA |
605 | list($coursesql, $courseparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED); |
606 | $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); | |
607 | $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)"; | |
608 | $courseparams['contextlevel'] = CONTEXT_COURSE; | |
6b139eda | 609 | $sql = "SELECT c.id,c.shortname $ccselect FROM {course} c |
1ac341e2 AA |
610 | $ccjoin |
611 | WHERE c.id " . $coursesql; | |
612 | ||
613 | $courses = $DB->get_records_sql($sql, $courseparams); | |
ac8976c8 RT |
614 | foreach ($courses as $courseid => $course) { |
615 | $url = new moodle_url("/course/view.php", array('id' => $courseid)); | |
1ac341e2 AA |
616 | context_helper::preload_from_record($course); |
617 | $context = context_course::instance($courseid, IGNORE_MISSING); | |
618 | // Method format_string() takes care of missing contexts. | |
619 | $this->courseshortnames[$courseid] = html_writer::link($url, format_string($course->shortname, true, | |
620 | array('context' => $context))); | |
ac8976c8 RT |
621 | } |
622 | } | |
623 | } | |
1cfce08e DM |
624 | |
625 | /** | |
626 | * Helper function to create list of user fullnames shown in log report. | |
627 | * | |
628 | * This will update $this->userfullnames array with userfullname, | |
629 | * which will be used to render logs in table. | |
630 | * | |
631 | * @since Moodle 2.9 | |
632 | * @return void | |
633 | */ | |
634 | protected function update_users_used() { | |
635 | global $DB; | |
636 | ||
637 | $this->userfullnames = array(); | |
638 | $userids = array(); | |
639 | ||
640 | // For each event cache full username. | |
641 | // Get list of userids which will be shown in log report. | |
642 | foreach ($this->rawdata as $event) { | |
643 | $logextra = $event->get_logextra(); | |
644 | if (!empty($event->userid) && empty($userids[$event->userid])) { | |
645 | $userids[$event->userid] = $event->userid; | |
646 | } | |
647 | if (!empty($logextra['realuserid']) && empty($userids[$logextra['realuserid']])) { | |
648 | $userids[$logextra['realuserid']] = $logextra['realuserid']; | |
649 | } | |
650 | if (!empty($event->relateduserid) && empty($userids[$event->relateduserid])) { | |
651 | $userids[$event->relateduserid] = $event->relateduserid; | |
652 | } | |
653 | } | |
654 | $this->rawdata->close(); | |
655 | ||
656 | // Get user fullname and put that in return list. | |
657 | if (!empty($userids)) { | |
658 | list($usql, $uparams) = $DB->get_in_or_equal($userids); | |
659 | $users = $DB->get_records_sql("SELECT id," . get_all_user_name_fields(true) . " FROM {user} WHERE id " . $usql, | |
660 | $uparams); | |
661 | foreach ($users as $userid => $user) { | |
662 | $this->userfullnames[$userid] = fullname($user); | |
663 | unset($userids[$userid]); | |
664 | } | |
665 | ||
666 | // We fill the array with false values for the users that don't exist anymore | |
667 | // in the database so we don't need to query the db again later. | |
668 | foreach ($userids as $userid) { | |
669 | $this->userfullnames[$userid] = false; | |
670 | } | |
671 | } | |
672 | } | |
6b139eda | 673 | } |