1441 |
ariadna |
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 |
namespace report_log;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Helper class for displaying logs.
|
|
|
21 |
*
|
|
|
22 |
* @package report_log
|
|
|
23 |
* @copyright 2024 Benjamin Walker <benjaminwalker@catalyst-au.net>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class helper {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Returns a string that attempts to infer event context display when context no longer exists.
|
|
|
30 |
* This can be used to show a minimum amount of info in log tables, but ideally this would
|
|
|
31 |
* be a last resort as log display should not change.
|
|
|
32 |
*
|
|
|
33 |
* @param \core\event\base|\stdClass $event
|
|
|
34 |
* @return string inferred context display, or an empty string.
|
|
|
35 |
*/
|
|
|
36 |
public static function get_context_fallback(\core\event\base|\stdClass $event): string {
|
|
|
37 |
if (empty($event) || empty($event->contextlevel) || empty($event->component)) {
|
|
|
38 |
return '';
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
$name = \context_helper::get_level_name($event->contextlevel);
|
|
|
42 |
$instanceid = $event->contextinstanceid ?? '';
|
|
|
43 |
|
|
|
44 |
// Use the plugin name for modules and blocks.
|
|
|
45 |
if ($event->contextlevel == CONTEXT_MODULE || $event->contextlevel == CONTEXT_BLOCK) {
|
|
|
46 |
// Some events list the component as core and store the modulename in other.
|
|
|
47 |
$component = !empty($event->other['modulename']) ? 'mod_' . $event->other['modulename'] : $event->component;
|
|
|
48 |
|
|
|
49 |
// Use module name to keep names consistent. Making component human readable is a close approximation.
|
|
|
50 |
$modulename = preg_replace('/^(mod_|block_)/', '', $component);
|
|
|
51 |
$name = str_replace('_', ' ', $modulename);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// Can't access context get_url methods, so don't bother showing a url.
|
|
|
55 |
return get_string('missingcontext', 'report_log', [
|
|
|
56 |
'name' => strtolower($name),
|
|
|
57 |
'instanceid' => $instanceid,
|
|
|
58 |
]);
|
|
|
59 |
}
|
|
|
60 |
}
|