mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 05:28:30 +01:00
MDL-80052 badges: Convert Course badges report to use Report builder
This commit is contained in:
parent
982581d5eb
commit
76bed1480a
@ -0,0 +1,140 @@
|
||||
<?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;
|
||||
use core_badges\reportbuilder\local\entities\badge_issued;
|
||||
use core_reportbuilder\system_report;
|
||||
use lang_string;
|
||||
use moodle_url;
|
||||
use pix_icon;
|
||||
use stdClass;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
global $CFG;
|
||||
require_once("{$CFG->libdir}/badgeslib.php");
|
||||
|
||||
/**
|
||||
* Course badges 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 course_badges extends system_report {
|
||||
|
||||
/**
|
||||
* Initialise report, we need to set the main table, load our entities and set columns/filters
|
||||
*/
|
||||
protected function initialise(): void {
|
||||
global $USER;
|
||||
// Our main entity, it contains all of the column definitions that we need.
|
||||
$badgeentity = new badge();
|
||||
$entityalias = $badgeentity->get_table_alias('badge');
|
||||
|
||||
$this->set_main_table('badge', $entityalias);
|
||||
$this->add_entity($badgeentity);
|
||||
|
||||
$type = $this->get_parameter('type', 0, PARAM_INT);
|
||||
$courseid = $this->get_parameter('courseid', 0, PARAM_INT);
|
||||
|
||||
$this->add_base_condition_simple('type', $type);
|
||||
$this->add_base_condition_simple('courseid', $courseid);
|
||||
$this->add_base_condition_sql("({$entityalias}.status = " . BADGE_STATUS_ACTIVE .
|
||||
" OR {$entityalias}.status = " . BADGE_STATUS_ACTIVE_LOCKED . ")");
|
||||
|
||||
$badgeissuedentity = new badge_issued();
|
||||
$badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
|
||||
$this->add_entity($badgeissuedentity
|
||||
->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
|
||||
ON {$entityalias}.id = {$badgeissuedalias}.badgeid AND {$badgeissuedalias}.userid = ".$USER->id)
|
||||
);
|
||||
|
||||
$this->add_base_fields("{$badgeissuedalias}.uniquehash");
|
||||
|
||||
// Now we can call our helper methods to add the content we want to include in the report.
|
||||
$this->add_columns($badgeissuedalias);
|
||||
$this->add_filters();
|
||||
|
||||
// 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:viewbadges', $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.
|
||||
*
|
||||
* @param string $badgeissuedalias
|
||||
*/
|
||||
public function add_columns(string $badgeissuedalias): void {
|
||||
$columns = [
|
||||
'badge:image',
|
||||
'badge:name',
|
||||
'badge:description',
|
||||
'badge:criteria',
|
||||
'badge_issued:issued',
|
||||
];
|
||||
$this->add_columns_from_entities($columns);
|
||||
|
||||
$this->get_column('badge_issued:issued')
|
||||
->set_title(new lang_string('awardedtoyou', 'core_badges'))
|
||||
->add_fields("{$badgeissuedalias}.uniquehash")
|
||||
->set_callback(static function(?int $value, stdClass $row) {
|
||||
global $OUTPUT;
|
||||
|
||||
if (!$value) {
|
||||
return '';
|
||||
}
|
||||
$format = get_string('strftimedatefullshort', 'core_langconfig');
|
||||
$date = $value ? userdate($value, $format) : '';
|
||||
$badgeurl = new moodle_url('/badges/badge.php', ['hash' => $row->uniquehash]);
|
||||
$icon = new pix_icon('i/valid', get_string('dateearned', 'badges', $date));
|
||||
return $OUTPUT->action_icon($badgeurl, $icon, null, null, true);
|
||||
});
|
||||
|
||||
$this->set_initial_sort_column('badge:name', SORT_ASC);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = [
|
||||
'badge:name',
|
||||
'badge_issued:issued',
|
||||
];
|
||||
|
||||
$this->add_filters_from_entities($filters);
|
||||
}
|
||||
}
|
@ -443,8 +443,12 @@ class core_badges_renderer extends plugin_renderer_base {
|
||||
*
|
||||
* @param \core_badges\output\badge_collection $badges
|
||||
* @return string
|
||||
*
|
||||
* @deprecated since Moodle 4.4
|
||||
* @todo MDL-80455 this will be removed in Moodle 4.8
|
||||
*/
|
||||
protected function render_badge_collection(\core_badges\output\badge_collection $badges) {
|
||||
debugging('The method render_badge_collection() has been deprecated', DEBUG_DEVELOPER);
|
||||
$paging = new paging_bar($badges->totalcount, $badges->page, $badges->perpage, $this->page->url, 'page');
|
||||
$htmlpagingbar = $this->render($paging);
|
||||
$table = new html_table();
|
||||
|
@ -100,3 +100,6 @@ Feature: Manage badges is not shown when there are no existing badges.
|
||||
And "Manage badges" "button" should not exist
|
||||
And "Add a new badge" "button" should not exist
|
||||
And I should not see "There are currently no badges available for users to earn."
|
||||
And the following should exist in the "reportbuilder-table" table:
|
||||
| Name | Description | Criteria |
|
||||
| Testing course badge | Testing course badge description | Awarded by: Teacher |
|
||||
|
@ -1,6 +1,11 @@
|
||||
This files describes API changes in /badges/*,
|
||||
information provided here is intended especially for developers.
|
||||
|
||||
=== 4.4 ===
|
||||
* The following methods have been deprecated and now Report builder reports are used instead:
|
||||
- render_badge_collection()
|
||||
- render_badge_recipients()
|
||||
|
||||
=== 4.3 ===
|
||||
* The following, deprecated since 3.11, have been removed and can no longer be used:
|
||||
- `badges/ajax.js`
|
||||
|
@ -29,9 +29,6 @@ require_once($CFG->libdir . '/badgeslib.php');
|
||||
|
||||
$type = required_param('type', PARAM_INT);
|
||||
$courseid = optional_param('id', 0, PARAM_INT);
|
||||
$sortby = optional_param('sort', 'name', PARAM_ALPHA);
|
||||
$sorthow = optional_param('dir', 'DESC', PARAM_ALPHA);
|
||||
$page = optional_param('page', 0, PARAM_INT);
|
||||
|
||||
require_login();
|
||||
|
||||
@ -43,22 +40,10 @@ if (empty($CFG->badges_allowcoursebadges) && $courseid != 0) {
|
||||
throw new \moodle_exception('coursebadgesdisabled', 'badges');
|
||||
}
|
||||
|
||||
if (!in_array($sortby, array('name', 'dateissued'))) {
|
||||
$sortby = 'name';
|
||||
}
|
||||
|
||||
if ($sorthow != 'ASC' && $sorthow != 'DESC') {
|
||||
$sorthow = 'ASC';
|
||||
}
|
||||
|
||||
if ($page < 0) {
|
||||
$page = 0;
|
||||
}
|
||||
|
||||
if ($course = $DB->get_record('course', array('id' => $courseid))) {
|
||||
$PAGE->set_url('/badges/view.php', array('type' => $type, 'id' => $course->id, 'sort' => $sortby, 'dir' => $sorthow));
|
||||
$PAGE->set_url('/badges/view.php', ['type' => $type, 'id' => $course->id]);
|
||||
} else {
|
||||
$PAGE->set_url('/badges/view.php', array('type' => $type, 'sort' => $sortby, 'dir' => $sorthow));
|
||||
$PAGE->set_url('/badges/view.php', ['type' => $type]);
|
||||
}
|
||||
|
||||
$PAGE->add_body_class('limitedwidth');
|
||||
@ -109,25 +94,15 @@ echo $output->header();
|
||||
echo $output->render_tertiary_navigation($actionbar);
|
||||
echo $OUTPUT->heading($title);
|
||||
|
||||
$totalcount = count(badges_get_badges($type, $courseid, '', '', 0, 0, $USER->id));
|
||||
$records = badges_get_badges($type, $courseid, $sortby, $sorthow, $page, BADGE_PERPAGE, $USER->id);
|
||||
|
||||
if ($totalcount) {
|
||||
if ($course && $course->startdate > time()) {
|
||||
echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
|
||||
}
|
||||
|
||||
$badges = new \core_badges\output\badge_collection($records);
|
||||
$badges->sort = $sortby;
|
||||
$badges->dir = $sorthow;
|
||||
$badges->page = $page;
|
||||
$badges->perpage = BADGE_PERPAGE;
|
||||
$badges->totalcount = $totalcount;
|
||||
|
||||
echo $output->render($badges);
|
||||
} else {
|
||||
echo $output->notification(get_string('nobadges', 'badges'), 'info');
|
||||
if ($course && $course->startdate > time()) {
|
||||
echo $OUTPUT->box(get_string('error:notifycoursedate', 'badges'), 'generalbox notifyproblem');
|
||||
}
|
||||
|
||||
$report = \core_reportbuilder\system_report_factory::create(\core_badges\reportbuilder\local\systemreports\course_badges::class,
|
||||
$PAGE->context, '', '', 0, ['type' => $type, 'courseid' => $courseid]);
|
||||
$report->set_default_no_results_notice(new lang_string('nobadges', 'badges'));
|
||||
echo $report->output();
|
||||
|
||||
// Trigger event, badge listing viewed.
|
||||
$eventparams = array('context' => $PAGE->context, 'other' => $eventotherparams);
|
||||
$event = \core\event\badge_listing_viewed::create($eventparams);
|
||||
|
Loading…
x
Reference in New Issue
Block a user