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:32:08 +01:00
parent 1bd5d40c8f
commit d9bde2ab22
4 changed files with 129 additions and 4 deletions
@@ -0,0 +1,93 @@
@core @core_badges @_file_upload @javascript
Feature: Manage badges is not shown when there are no existing badges.
Scenario: Check navigation at site level with no badges
Given I log in as "admin"
When I navigate to "Badges > Manage badges" in site administration
And I should see "There are no badges available"
Then "Manage badges" "button" should not exist
Scenario: Check navigation at course level with no badges
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher | Teacher | 1 | teacher@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 |
| teacher | C1 | editingteacher |
And I log in as "teacher"
And I am on "Course 1" course homepage
When I follow "Badges"
Then "Manage badges" "button" should exist
And I click on "Add a new badge" "button"
And I set the following fields to these values:
| Name | Testing course badge |
| Version | 1.1 |
| Language | Basque |
| Description | Testing course badge description |
| Image author | http://author.example.com |
| Image caption | Test caption image |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I click on "Create badge" "button"
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 click on "Save" "button"
And I click on "Manage badges" "link"
And I should see "Testing course badge"
And I follow "Badges"
# Badge is not enabled yet so is not listed.
And I should not see "Testing course badge"
And I should see "There are no badges available."
And I click on "Manage badges" "button"
And I click on "Enable access" "link" in the "Testing course badge" "table_row"
And I click on "Continue" "button"
And I should see "Testing course badge"
And I follow "Badges"
# Badge is already enabled so is listed.
And I should see "Testing course badge"
And I should not see "There are no badges available."
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 follow "Badges"
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
@@ -3958,7 +3958,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();
@@ -4001,8 +4001,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
@@ -3251,7 +3251,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;
@@ -3264,6 +3264,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
@@ -2525,7 +2525,7 @@ class core_course_externallib_testcase 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);