Skip to content

Commit

Permalink
MDL-80052 badges: Convert Badge recipients report to use Report builder
Browse files Browse the repository at this point in the history
  • Loading branch information
dravek committed Dec 19, 2023
1 parent 513f3b0 commit 982581d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 45 deletions.
1 change: 0 additions & 1 deletion badges/classes/output/manage_badge_action_bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ protected function get_badge_administration_mapping_construct(): array {
],
'bawards' => [
'url' => '/badges/recipients.php',
'additionalparams' => ['sort' => 'dateissued', 'dir' => 'DESC'],
'capability' => 'moodle/badges:viewawarded'
],
'bendorsement' => [
Expand Down
125 changes: 125 additions & 0 deletions badges/classes/reportbuilder/local/systemreports/recipients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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 <davidmc@moodle.com>
* @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')
)));
}
}
44 changes: 5 additions & 39 deletions badges/recipients.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,13 @@
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();

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));
Expand All @@ -73,7 +58,7 @@
}

$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);
Expand All @@ -88,28 +73,9 @@
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();
4 changes: 4 additions & 0 deletions badges/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,12 @@ public function print_criteria_actions(badge $badge) {
*
* @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();
Expand Down
15 changes: 10 additions & 5 deletions badges/tests/behat/view_badge.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:"
Expand All @@ -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:"
Expand Down Expand Up @@ -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"

Expand All @@ -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"

0 comments on commit 982581d

Please sign in to comment.