MDL-72852 navigation: Do not display badges when empty

If there are no course badges, students shouldn't have a link to a
page saying there are no badges available.

This patch is for displaying the Badges in the secondary navigation
only if the user can manage badges or there is, at least, one
badge available to the current user.
This commit is contained in:
Sara Arjona
2022-01-24 15:31:23 +01:00
parent e63604fb6d
commit 7c1d536b0d
5 changed files with 93 additions and 8 deletions
+15 -4
View File
@@ -84,10 +84,21 @@ Feature: Test tertiary navigation as various users
| nonediting | should not exist | Badges |
Scenario: Check navigation as a student
Given I log in as "student1"
And I am on "Course 1" course homepage
When I navigate to "Badges" in current page administration
Then "Back" "button" should not exist
Given I am on the "C1" "Course" page logged in as "student1"
And "Badges" "link" should not exist in current page administration
And I log out
# Enable one badge.
When I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Badges" in current page administration
And I click on "Manage badges" "button"
And I click on "Enable access" "link" in the "Testing course badge" "table_row"
And I press "Continue"
And I log out
# Now student should see the Badges link.
And I am on the "C1" "Course" page logged in as "student1"
Then "Badges" "link" should exist in current page administration
And I navigate to "Badges" in current page administration
And "Back" "button" should not exist
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should not exist
And I should see "Badges" is active in secondary navigation
@@ -58,3 +58,45 @@ Feature: Manage badges is not shown when there are no existing badges.
And "Manage badges" "button" should exist
# Badge is already enabled so is listed.
And I should see "Testing course badge"
Scenario: Check navigation at course level with no badges as a student
# Create a badge, but leave it not enabled for now.
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format | enablecompletion |
| Course 1 | C1 | topics | 1 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
And I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Badges > Add a new badge" in current page administration
And I set the following fields to these values:
| Name | Testing course badge |
| Version | 1.0 |
| Language | Catalan |
| Description | Testing course badge description |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Teacher" to "1"
And I press "Save"
And I log out
When I am on the "C1" "Course" page logged in as "student1"
Then "Badges" "link" should not exist in current page administration
And I log out
# Enable the badge.
And I am on the "C1" "Course" page logged in as "admin"
And I navigate to "Badges" in current page administration
And I click on "Manage badges" "button"
And I click on "Enable access" "link" in the "Testing course badge" "table_row"
And I press "Continue"
And I log out
# Now student should see the Badges link.
And I am on the "C1" "Course" page logged in as "student1"
And I follow "Badges"
And "Manage badges" "button" should not exist
And "Add a new badge" "button" should not exist
And I should not see "There are no badges available."
+27 -2
View File
@@ -3999,7 +3999,7 @@ function course_get_tagged_course_modules($tag, $exclusivemode = false, $fromcon
* @since Moodle 3.2
*/
function course_get_user_navigation_options($context, $course = null) {
global $CFG;
global $CFG, $USER;
$isloggedin = isloggedin();
$isguestuser = isguestuser();
@@ -4040,8 +4040,33 @@ function course_get_user_navigation_options($context, $course = null) {
} else {
// 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));
}
}
$options->badges = !empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges) &&
has_capability('moodle/badges:viewbadges', $context);
($canmanage || ($canview && $totalbadges > 0));
// Add view grade report is permitted.
$grades = false;
+8 -1
View File
@@ -3253,7 +3253,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertTrue($navoptions->blogs);
$this->assertFalse($navoptions->notes);
$this->assertTrue($navoptions->participants);
$this->assertTrue($navoptions->badges);
$this->assertFalse($navoptions->badges);
// Disable some options.
$CFG->badges_allowcoursebadges = 0;
@@ -3266,6 +3266,13 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertFalse($navoptions->notes);
$this->assertFalse($navoptions->participants);
$this->assertFalse($navoptions->badges);
// Re-enable some options to check badges are displayed as expected.
$CFG->badges_allowcoursebadges = 1;
assign_capability('moodle/badges:createbadge', CAP_ALLOW, $roleid, $context);
$navoptions = course_get_user_navigation_options($context);
$this->assertTrue($navoptions->badges);
}
/**
+1 -1
View File
@@ -2552,7 +2552,7 @@ class externallib_test extends externallib_advanced_testcase {
$this->assertTrue($navoptions->blogs);
$this->assertFalse($navoptions->notes);
$this->assertTrue($navoptions->participants);
$this->assertTrue($navoptions->badges);
$this->assertFalse($navoptions->badges);
$this->assertFalse($navoptions->tags);
$this->assertTrue($navoptions->grades);
$this->assertFalse($navoptions->search);