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:
Sara Arjona 2022-01-26 09:25:31 +01:00
parent 049c7f0822
commit 08357782f2

View file

@ -4041,8 +4041,9 @@ 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');
// 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',
@ -4058,15 +4059,18 @@ function course_get_user_navigation_options($context, $course = null) {
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;