Prerequisites
Feature Request - The User Story
I wanted to extract the name and abbreviation of the role that users have, and I did it like this. I hope you'll add this in the next version.
I added this after the following line in the classes/text_filter.php directory: if (stripos($text, '{userid}') !== false) { $replace['/\{userid\}/i'] = $USER->id; } and completed my task.
I can retrieve information in the format {roleshortnames} and {roles}.
// Tag: {roles} / {roleshortnames}.
// Description: User roles in current page context (names or shortnames).
// Parameters: None.
if (stripos($text, '{roles}') !== false || stripos($text, '{roleshortnames}') !== false) {
static $rolescache = []; // Cache per (userid-contextid) for this request.
$contextid = $PAGE->context->id;
$cachekey = $USER->id . '-' . $contextid;
if (!isset($rolescache[$cachekey])) {
$roles = get_user_roles($PAGE->context, $USER->id, true);
$names = [];
$shortnames = [];
foreach ($roles as $r) {
if (!empty($r->name)) {
$names[] = $r->name;
}
if (!empty($r->shortname)) {
$shortnames[] = $r->shortname;
}
}
$names = implode(', ', array_values(array_unique(array_filter($names))));
$shortnames = implode(', ', array_values(array_unique(array_filter($shortnames))));
$rolescache[$cachekey] = [$names, $shortnames];
}
[$rolenames, $roleshortnames] = $rolescache[$cachekey];
if (stripos($text, '{roles}') !== false) {
$replace['/\{roles\}/i'] = $rolenames;
}
if (stripos($text, '{roleshortnames}') !== false) {
$replace['/\{roleshortnames\}/i'] = $roleshortnames;
}
}
Screenshots / Mock-ups
No response
Alterative you have considered
No response
Additional information
No response
Planning on submitting a solution in a pull request (PR)?
Yes
Code of Conduct
Prerequisites
Feature Request - The User Story
I wanted to extract the name and abbreviation of the role that users have, and I did it like this. I hope you'll add this in the next version.
I added this after the following line in the classes/text_filter.php directory:
if (stripos($text, '{userid}') !== false) { $replace['/\{userid\}/i'] = $USER->id; }and completed my task.I can retrieve information in the format {roleshortnames} and {roles}.
// Tag: {roles} / {roleshortnames}.
// Description: User roles in current page context (names or shortnames).
// Parameters: None.
if (stripos($text, '{roles}') !== false || stripos($text, '{roleshortnames}') !== false) {
}
Screenshots / Mock-ups
No response
Alterative you have considered
No response
Additional information
No response
Planning on submitting a solution in a pull request (PR)?
Yes
Code of Conduct