From fafeeda7e14e78d338cfb567e510a60984c0b851 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Thu, 15 Aug 2024 16:12:59 +0100 Subject: [PATCH] MDL-82455 badges: link report recipients count to appropriate page. --- .../local/systemreports/badges.php | 23 +++++++++++++++++-- badges/tests/behat/manage_badges.feature | 16 +++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/badges/classes/reportbuilder/local/systemreports/badges.php b/badges/classes/reportbuilder/local/systemreports/badges.php index 305cf840da2..33a841718a6 100644 --- a/badges/classes/reportbuilder/local/systemreports/badges.php +++ b/badges/classes/reportbuilder/local/systemreports/badges.php @@ -23,6 +23,7 @@ use core_badges\reportbuilder\local\entities\badge; use core_reportbuilder\local\helpers\database; use core_reportbuilder\local\report\{action, column}; use core_reportbuilder\system_report; +use html_writer; use lang_string; use moodle_url; use pix_icon; @@ -42,6 +43,9 @@ require_once("{$CFG->libdir}/badgeslib.php"); */ class badges extends system_report { + /** @var int $badgeid The ID of the current badge row */ + private int $badgeid; + /** * Initialise report, we need to set the main table, load our entities and set columns/filters */ @@ -116,7 +120,6 @@ class badges extends system_report { $this->add_columns_from_entities($columns); // Issued badges column. - // TODO: Move this column to the entity when MDL-76392 is integrated. $tempbadgealias = database::generate_alias(); $badgeentityalias = $badgeentity->get_table_alias('badge'); $this->add_column((new column( @@ -131,7 +134,14 @@ class badges extends system_report { INNER JOIN {user} u ON {$tempbadgealias}.userid = u.id WHERE {$tempbadgealias}.badgeid = {$badgeentityalias}.id AND u.deleted = 0)", 'issued') - ->set_is_sortable(true)); + ->set_is_sortable(true) + ->set_callback(function(int $count): string { + if (!has_capability('moodle/badges:viewawarded', $this->get_context())) { + return (string) $count; + } + + return html_writer::link(new moodle_url('/badges/recipients.php', ['id' => $this->badgeid]), $count); + })); // Remove title from image column. $this->get_column('badge:image')->set_title(null); @@ -289,6 +299,15 @@ class badges extends system_report { } } + /** + * Store the ID of the badge within each row + * + * @param stdClass $row + */ + public function row_callback(stdClass $row): void { + $this->badgeid = (int) $row->id; + } + /** * CSS classes to add to the row * diff --git a/badges/tests/behat/manage_badges.feature b/badges/tests/behat/manage_badges.feature index 73433fbd1ed..ba300aea1ea 100644 --- a/badges/tests/behat/manage_badges.feature +++ b/badges/tests/behat/manage_badges.feature @@ -90,7 +90,10 @@ Feature: Manage badges | Badge #1 | Not available | Scenario: Award a badge - Given I log in as "admin" + Given the following "users" exist: + | username | firstname | lastname | email | + | user1 | User | One | user1@example.com | + When I log in as "admin" And I navigate to "Badges > Manage badges" in site administration And I press "Edit" action in the "Badge #1" report row And I select "Criteria" from the "jump" singleselect @@ -101,9 +104,14 @@ Feature: Manage badges And I press "Enable access" action in the "Badge #1" report row And I press "Continue" And I press "Award badge" action in the "Badge #1" report row - And I set the field "potentialrecipients[]" to "Admin User (moodle@example.com)" + And I set the field "potentialrecipients[]" to "Admin User (moodle@example.com),User One (user1@example.com)" And I press "Award badge" And I navigate to "Badges > Manage badges" in site administration - Then the following should exist in the "reportbuilder-table" table: + Then the following should exist in the "Badges" table: | Name | Badge status | Recipients | - | Badge #1 | Available | 1 | + | Badge #1 | Available | 2 | + And I click on "2" "link" in the "Badge #1" "table_row" + And the following should exist in the "Recipients" table: + | -1- | + | Admin User | + | User One |