From de4e2a545ad3695137a933b9155f083cfacd2f08 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 | 51 ++++++++++++++----- badges/tests/behat/manage_badges.feature | 16 ++++-- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/badges/classes/reportbuilder/local/systemreports/badges.php b/badges/classes/reportbuilder/local/systemreports/badges.php index cbe8384a1ec..832f374350f 100644 --- a/badges/classes/reportbuilder/local/systemreports/badges.php +++ b/badges/classes/reportbuilder/local/systemreports/badges.php @@ -20,10 +20,10 @@ namespace core_badges\reportbuilder\local\systemreports; use core\context\{course, system}; use core_badges\reportbuilder\local\entities\badge; -use core_reportbuilder\local\entities\user; 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; @@ -43,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 */ @@ -54,15 +57,6 @@ class badges extends system_report { $this->set_main_table('badge', $entityalias); $this->add_entity($badgeentity); - // Join user entity. - $userentity = new user(); - $useralias = $userentity->get_table_alias('user'); - $badgeissuedalias = database::generate_alias(); - $this->add_entity($userentity->add_joins([ - "LEFT JOIN {badge_issued} {$badgeissuedalias} ON {$badgeissuedalias}.badgeid = {$entityalias}.id", - "LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$badgeissuedalias}.userid AND {$useralias}.deleted = 0", - ])); - $paramtype = database::generate_param_name(); $context = $this->get_context(); if ($context instanceof system) { @@ -113,19 +107,39 @@ class badges extends system_report { * unique identifier. If custom columns are needed just for this report, they can be defined here. */ protected function add_columns(): void { + $badgeentity = $this->get_entity('badge'); + $this->add_columns_from_entities([ 'badge:image', 'badge:namewithlink', 'badge:version', 'badge:status', 'badge:criteria', - 'user:username', ]); // Issued badges column. - $this->get_column('user:username') - ->set_title(new lang_string('awards', 'core_badges')) - ->set_aggregation('count'); + $tempbadgealias = database::generate_alias(); + $badgeentityalias = $badgeentity->get_table_alias('badge'); + $this->add_column((new column( + 'issued', + new lang_string('awards', 'core_badges'), + $badgeentity->get_entity_name() + )) + ->add_joins($this->get_joins()) + ->set_type(column::TYPE_INTEGER) + ->add_field("(SELECT COUNT({$tempbadgealias}.userid) + FROM {badge_issued} {$tempbadgealias} + INNER JOIN {user} u + ON {$tempbadgealias}.userid = u.id + WHERE {$tempbadgealias}.badgeid = {$badgeentityalias}.id AND u.deleted = 0)", 'issued') + ->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); @@ -282,6 +296,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 a4e5f1c6c64..21bf0d38b0a 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,12 +104,17 @@ Feature: Manage badges And I press "Enable access" action in the "Badge #1" report row And I click on "Enable" "button" in the "Confirm" "dialogue" 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 | Scenario: View list of badges with recipients Given the following "users" exist: