MDL-72133 report_log: Add fallback context display
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace report_log;
|
||||
|
||||
/**
|
||||
* Helper class for displaying logs.
|
||||
*
|
||||
* @package report_log
|
||||
* @copyright 2024 Benjamin Walker <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class helper {
|
||||
|
||||
/**
|
||||
* Returns a string that attempts to infer event context display when context no longer exists.
|
||||
* This can be used to show a minimum amount of info in log tables, but ideally this would
|
||||
* be a last resort as log display should not change.
|
||||
*
|
||||
* @param \core\event\base|\stdClass $event
|
||||
* @return string inferred context display, or an empty string.
|
||||
*/
|
||||
public static function get_context_fallback(\core\event\base|\stdClass $event): string {
|
||||
if (empty($event) || empty($event->contextlevel) || empty($event->component)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$name = \context_helper::get_level_name($event->contextlevel);
|
||||
$instanceid = $event->contextinstanceid ?? '';
|
||||
|
||||
// Use the plugin name for modules and blocks.
|
||||
if ($event->contextlevel == CONTEXT_MODULE || $event->contextlevel == CONTEXT_BLOCK) {
|
||||
// Some events list the component as core and store the modulename in other.
|
||||
$component = !empty($event->other['modulename']) ? 'mod_' . $event->other['modulename'] : $event->component;
|
||||
|
||||
// Use module name to keep names consistent. Making component human readable is a close approximation.
|
||||
$modulename = preg_replace('/^(mod_|block_)/', '', $component);
|
||||
$name = str_replace('_', ' ', $modulename);
|
||||
}
|
||||
|
||||
// Can't access context get_url methods, so don't bother showing a url.
|
||||
return get_string('missingcontext', 'report_log', [
|
||||
'name' => strtolower($name),
|
||||
'instanceid' => $instanceid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -248,7 +248,7 @@ class report_log_table_log extends table_sql {
|
||||
if (empty($this->download) && $url = $context->get_url()) {
|
||||
$contextname = html_writer::link($url, $contextname);
|
||||
}
|
||||
} else {
|
||||
} else if (!$contextname = \report_log\helper::get_context_fallback($event)) {
|
||||
$contextname = get_string('other');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ $string['log:view'] = 'View course logs';
|
||||
$string['log:viewtoday'] = 'View today\'s logs';
|
||||
$string['page'] = 'Page {$a}';
|
||||
$string['logsformat'] = 'Logs format';
|
||||
$string['missingcontext'] = 'Deleted {$a->name} (id \'{$a->instanceid}\')';
|
||||
$string['missingcourse'] = 'Missing course (id \'{$a->instanceid}\')';
|
||||
$string['nocapability'] = 'Can not access user log report';
|
||||
$string['nologreaderenabled'] = 'No log reader enabled';
|
||||
|
||||
@@ -196,7 +196,7 @@ class report_loglive_table_log extends table_sql {
|
||||
if ($url = $context->get_url()) {
|
||||
$contextname = html_writer::link($url, $contextname);
|
||||
}
|
||||
} else {
|
||||
} else if (!$contextname = \report_log\helper::get_context_fallback($event)) {
|
||||
$contextname = get_string('other');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user