From 7c1d536b0db53db890d1afa5aebf4dfba9dced67 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Thu, 20 Jan 2022 10:30:42 +0100 Subject: [PATCH] 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. --- badges/tests/behat/badge_navigation.feature | 19 +++++++-- badges/tests/behat/nobadge_navigation.feature | 42 +++++++++++++++++++ course/lib.php | 29 ++++++++++++- course/tests/courselib_test.php | 9 +++- course/tests/externallib_test.php | 2 +- 5 files changed, 93 insertions(+), 8 deletions(-) diff --git a/badges/tests/behat/badge_navigation.feature b/badges/tests/behat/badge_navigation.feature index 446919fe463..c14b2689556 100644 --- a/badges/tests/behat/badge_navigation.feature +++ b/badges/tests/behat/badge_navigation.feature @@ -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 diff --git a/badges/tests/behat/nobadge_navigation.feature b/badges/tests/behat/nobadge_navigation.feature index 2b1bcfce633..9b3a76354ae 100644 --- a/badges/tests/behat/nobadge_navigation.feature +++ b/badges/tests/behat/nobadge_navigation.feature @@ -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." diff --git a/course/lib.php b/course/lib.php index 95474d8206c..6d3089a5954 100644 --- a/course/lib.php +++ b/course/lib.php @@ -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; diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 334acaf54f9..33c1fd13fa9 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -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); } /** diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index 16a89ac0584..60ba1a207b4 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -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);