diff --git a/badges/classes/output/manage_badge_action_bar.php b/badges/classes/output/manage_badge_action_bar.php index 7344b72d47d..f2864796ad5 100644 --- a/badges/classes/output/manage_badge_action_bar.php +++ b/badges/classes/output/manage_badge_action_bar.php @@ -107,7 +107,6 @@ class manage_badge_action_bar extends base_action_bar { ], 'bawards' => [ 'url' => '/badges/recipients.php', - 'additionalparams' => ['sort' => 'dateissued', 'dir' => 'DESC'], 'capability' => 'moodle/badges:viewawarded' ], 'bendorsement' => [ diff --git a/badges/classes/reportbuilder/local/systemreports/recipients.php b/badges/classes/reportbuilder/local/systemreports/recipients.php new file mode 100644 index 00000000000..b2b2fbb2d04 --- /dev/null +++ b/badges/classes/reportbuilder/local/systemreports/recipients.php @@ -0,0 +1,125 @@ +. + +declare(strict_types=1); + +namespace core_badges\reportbuilder\local\systemreports; + +use core_badges\reportbuilder\local\entities\badge_issued; +use core_reportbuilder\local\report\action; +use core_reportbuilder\system_report; +use lang_string; +use moodle_url; +use pix_icon; + +/** + * Badge recipients system report class implementation + * + * @package core_badges + * @copyright 2023 David Carrillo + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class recipients extends system_report { + + /** + * Initialise report, we need to set the main table, load our entities and set columns/filters + */ + protected function initialise(): void { + // Our main entity, it contains all of the column definitions that we need. + $badgeissuedentity = new badge_issued(); + $entityalias = $badgeissuedentity->get_table_alias('badge_issued'); + + $this->set_main_table('badge_issued', $entityalias); + $this->add_entity($badgeissuedentity); + + $userentity = new \core_reportbuilder\local\entities\user(); + $entityuseralias = $userentity->get_table_alias('user'); + $this->add_entity($userentity + ->add_joins($userentity->get_joins()) + ->add_join("JOIN {user} {$entityuseralias} + ON {$entityuseralias}.id = {$entityalias}.userid") + ); + + $this->add_base_condition_simple('badgeid', $this->get_parameter('badgeid', 0, PARAM_INT)); + + $this->add_base_fields("{$entityalias}.uniquehash"); + + // Now we can call our helper methods to add the content we want to include in the report. + $this->add_columns(); + $this->add_filters(); + $this->add_actions(); + + // Set if report can be downloaded. + $this->set_downloadable(false); + } + + /** + * Validates access to view this report + * + * @return bool + */ + protected function can_view(): bool { + return has_capability('moodle/badges:viewawarded', $this->get_context()); + } + + /** + * Adds the columns we want to display in the report + * + * They are provided by the entities we previously added in the {@see initialise} method, referencing each by their + * unique identifier. If custom columns are needed just for this report, they can be defined here. + */ + public function add_columns(): void { + $columns = [ + 'user:fullnamewithlink', + 'badge_issued:issued', + ]; + + $this->add_columns_from_entities($columns); + $this->set_initial_sort_column('badge_issued:issued', SORT_DESC); + } + + /** + * Adds the filters we want to display in the report + * + * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their + * unique identifier + */ + protected function add_filters(): void { + $filters = [ + 'user:fullname', + 'badge_issued:issued', + ]; + + $this->add_filters_from_entities($filters); + } + + /** + * Add the system report actions. An extra column will be appended to each row, containing all actions added here + * + * Note the use of ":uniquehash" placeholder which will be substituted according to actual values in the row + */ + protected function add_actions(): void { + $this->add_action((new action( + new moodle_url('/badges/badge.php', [ + 'hash' => ':uniquehash', + ]), + new pix_icon('i/search', '', 'core'), + [], + false, + new lang_string('viewbadge', 'badges') + ))); + } +} diff --git a/badges/recipients.php b/badges/recipients.php index 7dea7f2ae02..6a1120a19db 100644 --- a/badges/recipients.php +++ b/badges/recipients.php @@ -28,9 +28,6 @@ require_once(__DIR__ . '/../config.php'); require_once($CFG->libdir . '/badgeslib.php'); $badgeid = required_param('id', PARAM_INT); -$sortby = optional_param('sort', 'dateissued', PARAM_ALPHA); -$sorthow = optional_param('dir', 'DESC', PARAM_ALPHA); -$page = optional_param('page', 0, PARAM_INT); require_login(); @@ -38,18 +35,6 @@ if (empty($CFG->enablebadges)) { throw new \moodle_exception('badgesdisabled', 'badges'); } -if (!in_array($sortby, array('firstname', 'lastname', 'dateissued'))) { - $sortby = 'dateissued'; -} - -if ($sorthow != 'ASC' and $sorthow != 'DESC') { - $sorthow = 'DESC'; -} - -if ($page < 0) { - $page = 0; -} - $badge = new badge($badgeid); $context = $badge->get_context(); $navurl = new moodle_url('/badges/index.php', array('type' => $badge->type)); @@ -73,7 +58,7 @@ if ($badge->type == BADGE_TYPE_COURSE) { } $PAGE->set_context($context); -$PAGE->set_url('/badges/recipients.php', array('id' => $badgeid, 'sort' => $sortby, 'dir' => $sorthow)); +$PAGE->set_url('/badges/recipients.php', ['id' => $badgeid]); $PAGE->set_heading($heading); $PAGE->set_title($badge->name); $PAGE->navbar->add($badge->name); @@ -88,28 +73,9 @@ echo $output->render_tertiary_navigation($actionbar); echo $OUTPUT->heading(print_badge_image($badge, $context, 'small') . ' ' . $badge->name); echo $output->print_badge_status_box($badge); -$userfieldsapi = \core_user\fields::for_name(); -$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; -$sql = "SELECT b.userid, b.dateissued, b.uniquehash, $namefields - FROM {badge_issued} b INNER JOIN {user} u - ON b.userid = u.id - WHERE b.badgeid = :badgeid AND u.deleted = 0 - ORDER BY $sortby $sorthow"; - -$totalcount = $DB->count_records('badge_issued', array('badgeid' => $badge->id)); - -if ($badge->has_awards()) { - $users = $DB->get_records_sql($sql, array('badgeid' => $badge->id), $page * BADGE_PERPAGE, BADGE_PERPAGE); - $recipients = new core_badges\output\badge_recipients($users); - $recipients->sort = $sortby; - $recipients->dir = $sorthow; - $recipients->page = $page; - $recipients->perpage = BADGE_PERPAGE; - $recipients->totalcount = $totalcount; - - echo $output->render($recipients); -} else { - echo $output->notification(get_string('noawards', 'badges')); -} +$report = \core_reportbuilder\system_report_factory::create(\core_badges\reportbuilder\local\systemreports\recipients::class, + $PAGE->context, '', '', 0, ['badgeid' => $badge->id]); +$report->set_default_no_results_notice(new lang_string('noawards', 'badges')); +echo $report->output(); echo $output->footer(); diff --git a/badges/renderer.php b/badges/renderer.php index 3880a8f24e9..6d68b27d204 100644 --- a/badges/renderer.php +++ b/badges/renderer.php @@ -784,8 +784,12 @@ class core_badges_renderer extends plugin_renderer_base { * * @param \core_badges\output\badge_recipients $recipients * @return string + * + * @deprecated since Moodle 4.4 + * @todo MDL-80455 this will be removed in Moodle 4.8 */ protected function render_badge_recipients(\core_badges\output\badge_recipients $recipients) { + debugging('The method render_badge_recipients() has been deprecated', DEBUG_DEVELOPER); $paging = new paging_bar($recipients->totalcount, $recipients->page, $recipients->perpage, $this->page->url, 'page'); $htmlpagingbar = $this->render($paging); $table = new html_table(); diff --git a/badges/tests/behat/view_badge.feature b/badges/tests/behat/view_badge.feature index 2b029382141..1f7795b3b9b 100644 --- a/badges/tests/behat/view_badge.feature +++ b/badges/tests/behat/view_badge.feature @@ -38,7 +38,8 @@ Feature: Display badges And I navigate to "Badges > Manage badges" in site administration And I follow "Testing system badge" And I select "Recipients (1)" from the "jump" singleselect - When I click on "View issued badge" "link" in the "Student 1" "table_row" + And I open the action menu in "Student 1" "table_row" + And I choose "View issued badge" in the open action menu Then I should see "Awarded to Student 1" And I should see "This badge has to be awarded by a user with the following role:" And I should not see "Expired" @@ -63,7 +64,8 @@ Feature: Display badges And I navigate to "Badges > Manage badges" in site administration And I follow "Testing system badge" And I select "Recipients (1)" from the "jump" singleselect - When I click on "View issued badge" "link" in the "Student 1" "table_row" + And I open the action menu in "Student 1" "table_row" + And I choose "View issued badge" in the open action menu Then I should see "Awarded to Student 1" And I should see "Complete ALL of the listed requirements." And I should see "This badge has to be awarded by a user with the following role:" @@ -84,7 +86,8 @@ Feature: Display badges And I press "Continue" # Check badge details are displayed. And I select "Recipients (2)" from the "jump" singleselect - When I click on "View issued badge" "link" in the "Student 1" "table_row" + And I open the action menu in "Student 1" "table_row" + And I choose "View issued badge" in the open action menu Then I should see "Awarded to Student 1" And I should see "Complete ANY of the listed requirements." And I should see "This badge has to be awarded by a user with the following role:" @@ -112,7 +115,8 @@ Feature: Display badges And I navigate to "Badges > Manage badges" in site administration And I follow "Testing system badge" And I select "Recipients (1)" from the "jump" singleselect - And I click on "View issued badge" "link" in the "Student 1" "table_row" + And I open the action menu in "Student 1" "table_row" + And I choose "View issued badge" in the open action menu Then I should see "Expires" And I should not see "Expired" @@ -136,6 +140,7 @@ Feature: Display badges And I navigate to "Badges > Manage badges" in site administration And I follow "Testing system badge" And I select "Recipients (1)" from the "jump" singleselect - And I click on "View issued badge" "link" in the "Student 1" "table_row" + And I open the action menu in "Student 1" "table_row" + And I choose "View issued badge" in the open action menu Then I should see "Expired" And I should not see "Expires"