MDL-72852 badges: Improve performance to display Badges nav
The course_get_user_navigation_options() is called on each request from the add_course_essentials() method, to initialise the basic navigation options that are available or not for the current user. This information is not always displayed in boost (for instance, in a module page), but it might be required by some blocks, like Navigation. However, this block is hidden by default in boost since MDL-73347, so at any point we should check if it's possible to change the way this navigations options is loaded, to only get this information when it's strictly required.
This commit is contained in:
+28
-24
@@ -4041,32 +4041,36 @@ function course_get_user_navigation_options($context, $course = null) {
|
||||
// We are in a course, so make sure we use the proper capability (course:viewparticipants).
|
||||
$options->participants = course_can_view_participants($context);
|
||||
|
||||
// Only display badges if the current user can manage them or if they can view them and have, at least, one available badge.
|
||||
require_once($CFG->dirroot.'/lib/badgeslib.php');
|
||||
$canmanage = has_any_capability([
|
||||
'moodle/badges:createbadge',
|
||||
'moodle/badges:awardbadge',
|
||||
'moodle/badges:configurecriteria',
|
||||
'moodle/badges:configuremessages',
|
||||
'moodle/badges:configuredetails',
|
||||
'moodle/badges:deletebadge',
|
||||
],
|
||||
$context
|
||||
);
|
||||
$totalbadges = [];
|
||||
$canview = false;
|
||||
if (!$canmanage) {
|
||||
// This only needs to be calculated if the user can't manage badges (to improve performance).
|
||||
$canview = has_capability('moodle/badges:viewbadges', $context);
|
||||
if (is_null($course)) {
|
||||
$totalbadges = count(badges_get_badges(BADGE_TYPE_SITE, 0, '', '', 0, 0, $USER->id));
|
||||
} else {
|
||||
$totalbadges = count(badges_get_badges(BADGE_TYPE_COURSE, $course->id, '', '', 0, 0, $USER->id));
|
||||
// Only display badges if they are enabled and the current user can manage them or if they can view them and have,
|
||||
// at least, one available badge.
|
||||
if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges)) {
|
||||
$canmanage = has_any_capability([
|
||||
'moodle/badges:createbadge',
|
||||
'moodle/badges:awardbadge',
|
||||
'moodle/badges:configurecriteria',
|
||||
'moodle/badges:configuremessages',
|
||||
'moodle/badges:configuredetails',
|
||||
'moodle/badges:deletebadge',
|
||||
],
|
||||
$context
|
||||
);
|
||||
$totalbadges = [];
|
||||
$canview = false;
|
||||
if (!$canmanage) {
|
||||
// This only needs to be calculated if the user can't manage badges (to improve performance).
|
||||
$canview = has_capability('moodle/badges:viewbadges', $context);
|
||||
if ($canview) {
|
||||
require_once($CFG->dirroot.'/lib/badgeslib.php');
|
||||
if (is_null($course)) {
|
||||
$totalbadges = count(badges_get_badges(BADGE_TYPE_SITE, 0, '', '', 0, 0, $USER->id));
|
||||
} else {
|
||||
$totalbadges = count(badges_get_badges(BADGE_TYPE_COURSE, $course->id, '', '', 0, 0, $USER->id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$options->badges = !empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) &&
|
||||
($canmanage || ($canview && $totalbadges > 0));
|
||||
$options->badges = ($canmanage || ($canview && $totalbadges > 0));
|
||||
}
|
||||
// Add view grade report is permitted.
|
||||
$grades = false;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user