1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
declare(strict_types=1);
|
|
|
18 |
|
|
|
19 |
namespace core_badges\reportbuilder\local\systemreports;
|
|
|
20 |
|
|
|
21 |
use core\context\{course, system};
|
|
|
22 |
use core_badges\reportbuilder\local\entities\badge;
|
1441 |
ariadna |
23 |
use core_badges\reportbuilder\local\entities\badge_issued;
|
1 |
efrain |
24 |
use core_reportbuilder\local\helpers\database;
|
|
|
25 |
use core_reportbuilder\local\report\{action, column};
|
|
|
26 |
use core_reportbuilder\system_report;
|
1441 |
ariadna |
27 |
use html_writer;
|
1 |
efrain |
28 |
use lang_string;
|
|
|
29 |
use moodle_url;
|
|
|
30 |
use pix_icon;
|
|
|
31 |
use stdClass;
|
|
|
32 |
|
|
|
33 |
defined('MOODLE_INTERNAL') || die;
|
|
|
34 |
|
|
|
35 |
global $CFG;
|
|
|
36 |
require_once("{$CFG->libdir}/badgeslib.php");
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Badges system report class implementation
|
|
|
40 |
*
|
|
|
41 |
* @package core_badges
|
|
|
42 |
* @copyright 2023 David Carrillo <davidmc@moodle.com>
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
class badges extends system_report {
|
|
|
46 |
|
1441 |
ariadna |
47 |
/** @var int $badgeid The ID of the current badge row */
|
|
|
48 |
private int $badgeid;
|
|
|
49 |
|
1 |
efrain |
50 |
/**
|
|
|
51 |
* Initialise report, we need to set the main table, load our entities and set columns/filters
|
|
|
52 |
*/
|
|
|
53 |
protected function initialise(): void {
|
1441 |
ariadna |
54 |
global $USER;
|
|
|
55 |
|
1 |
efrain |
56 |
// Our main entity, it contains all of the column definitions that we need.
|
|
|
57 |
$badgeentity = new badge();
|
|
|
58 |
$entityalias = $badgeentity->get_table_alias('badge');
|
|
|
59 |
|
|
|
60 |
$this->set_main_table('badge', $entityalias);
|
|
|
61 |
$this->add_entity($badgeentity);
|
|
|
62 |
|
|
|
63 |
$paramtype = database::generate_param_name();
|
|
|
64 |
$context = $this->get_context();
|
|
|
65 |
if ($context instanceof system) {
|
|
|
66 |
$type = BADGE_TYPE_SITE;
|
|
|
67 |
$this->add_base_condition_sql("{$entityalias}.type = :$paramtype", [$paramtype => $type]);
|
|
|
68 |
} else {
|
|
|
69 |
$type = BADGE_TYPE_COURSE;
|
|
|
70 |
$paramcourseid = database::generate_param_name();
|
|
|
71 |
$this->add_base_condition_sql("{$entityalias}.type = :$paramtype AND {$entityalias}.courseid = :$paramcourseid",
|
|
|
72 |
[$paramtype => $type, $paramcourseid => $context->instanceid]);
|
|
|
73 |
}
|
|
|
74 |
|
1441 |
ariadna |
75 |
if (!$this->can_view_draft_badges()) {
|
|
|
76 |
$this->add_base_condition_sql("({$entityalias}.status = " . BADGE_STATUS_ACTIVE .
|
|
|
77 |
" OR {$entityalias}.status = " . BADGE_STATUS_ACTIVE_LOCKED . ")");
|
|
|
78 |
}
|
|
|
79 |
|
1 |
efrain |
80 |
// Any columns required by actions should be defined here to ensure they're always available.
|
|
|
81 |
$this->add_base_fields("{$entityalias}.id, {$entityalias}.type, {$entityalias}.courseid, {$entityalias}.status");
|
|
|
82 |
|
1441 |
ariadna |
83 |
$badgeissuedentity = new badge_issued();
|
|
|
84 |
$badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
|
|
|
85 |
$this->add_entity($badgeissuedentity
|
|
|
86 |
->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias}
|
|
|
87 |
ON {$entityalias}.id = {$badgeissuedalias}.badgeid AND {$badgeissuedalias}.userid = ".$USER->id)
|
|
|
88 |
);
|
|
|
89 |
|
|
|
90 |
$this->add_base_fields("{$badgeissuedalias}.uniquehash");
|
|
|
91 |
|
1 |
efrain |
92 |
// Now we can call our helper methods to add the content we want to include in the report.
|
1441 |
ariadna |
93 |
$this->add_columns($badgeissuedalias);
|
1 |
efrain |
94 |
$this->add_filters();
|
|
|
95 |
$this->add_actions();
|
|
|
96 |
|
1441 |
ariadna |
97 |
$this->set_default_no_results_notice(new lang_string('nomatchingbadges', 'core_badges'));
|
1 |
efrain |
98 |
|
|
|
99 |
// Set if report can be downloaded.
|
|
|
100 |
$this->set_downloadable(false);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Validates access to view this report
|
|
|
105 |
*
|
|
|
106 |
* @return bool
|
|
|
107 |
*/
|
|
|
108 |
protected function can_view(): bool {
|
|
|
109 |
return has_any_capability([
|
1441 |
ariadna |
110 |
'moodle/badges:viewbadges',
|
1 |
efrain |
111 |
'moodle/badges:viewawarded',
|
|
|
112 |
'moodle/badges:createbadge',
|
|
|
113 |
'moodle/badges:awardbadge',
|
|
|
114 |
'moodle/badges:configurecriteria',
|
|
|
115 |
'moodle/badges:configuremessages',
|
|
|
116 |
'moodle/badges:configuredetails',
|
|
|
117 |
'moodle/badges:deletebadge'], $this->get_context());
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Adds the columns we want to display in the report
|
|
|
122 |
*
|
|
|
123 |
* They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
124 |
* unique identifier. If custom columns are needed just for this report, they can be defined here.
|
|
|
125 |
*
|
1441 |
ariadna |
126 |
* @param string $badgeissuedalias
|
1 |
efrain |
127 |
*/
|
1441 |
ariadna |
128 |
public function add_columns(string $badgeissuedalias): void {
|
1 |
efrain |
129 |
$columns = [
|
1441 |
ariadna |
130 |
'badge:namewithimagelink',
|
1 |
efrain |
131 |
'badge:status',
|
|
|
132 |
'badge:criteria',
|
|
|
133 |
];
|
|
|
134 |
|
1441 |
ariadna |
135 |
$canviewdraftbadges = $this->can_view_draft_badges();
|
|
|
136 |
if (!$canviewdraftbadges) {
|
|
|
137 |
// Remove status and recipients column.
|
|
|
138 |
unset($columns[1]);
|
|
|
139 |
}
|
1 |
efrain |
140 |
$this->add_columns_from_entities($columns);
|
|
|
141 |
|
1441 |
ariadna |
142 |
// Change title of the `namewithimagelink` column to 'Name'.
|
|
|
143 |
$this->get_column('badge:namewithimagelink')->set_title(new lang_string('name'));
|
1 |
efrain |
144 |
|
1441 |
ariadna |
145 |
// Recipients column.
|
|
|
146 |
if ($canviewdraftbadges) {
|
|
|
147 |
$badgeentity = $this->get_entity('badge');
|
|
|
148 |
$tempbadgealias = database::generate_alias();
|
|
|
149 |
$badgeentityalias = $badgeentity->get_table_alias('badge');
|
|
|
150 |
$this->add_column((new column(
|
|
|
151 |
'issued',
|
|
|
152 |
new lang_string('awards', 'core_badges'),
|
|
|
153 |
$badgeentity->get_entity_name()
|
|
|
154 |
))
|
|
|
155 |
->add_joins($this->get_joins())
|
|
|
156 |
->set_type(column::TYPE_INTEGER)
|
|
|
157 |
->add_field("(SELECT COUNT({$tempbadgealias}.userid)
|
|
|
158 |
FROM {badge_issued} {$tempbadgealias}
|
|
|
159 |
INNER JOIN {user} u
|
|
|
160 |
ON {$tempbadgealias}.userid = u.id
|
|
|
161 |
WHERE {$tempbadgealias}.badgeid = {$badgeentityalias}.id AND u.deleted = 0)", 'issued')
|
|
|
162 |
->set_is_sortable(true)
|
|
|
163 |
->set_callback(function(int $count): string {
|
|
|
164 |
if (!has_capability('moodle/badges:viewawarded', $this->get_context())) {
|
|
|
165 |
return (string) $count;
|
|
|
166 |
}
|
1 |
efrain |
167 |
|
1441 |
ariadna |
168 |
return html_writer::link(new moodle_url('/badges/recipients.php', ['id' => $this->badgeid]), $count);
|
|
|
169 |
}));
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
// Add the date the badge was issued at the end of the report.
|
|
|
173 |
$this->add_column_from_entity('badge_issued:issued');
|
|
|
174 |
$this->get_column('badge_issued:issued')
|
|
|
175 |
->set_title(new lang_string('awardedtoyou', 'core_badges'))
|
|
|
176 |
->add_fields("{$badgeissuedalias}.uniquehash")
|
|
|
177 |
->set_callback(static function(?int $value, stdClass $row) {
|
|
|
178 |
global $OUTPUT;
|
|
|
179 |
|
|
|
180 |
if (!$value) {
|
|
|
181 |
return '';
|
|
|
182 |
}
|
|
|
183 |
$format = get_string('strftimedatefullshort', 'core_langconfig');
|
|
|
184 |
$date = $value ? userdate($value, $format) : '';
|
|
|
185 |
$badgeurl = new moodle_url('/badges/badge.php', ['hash' => $row->uniquehash]);
|
|
|
186 |
$icon = new pix_icon('i/valid', get_string('dateearned', 'badges', $date));
|
|
|
187 |
return $OUTPUT->action_icon($badgeurl, $icon, null, null, true);
|
|
|
188 |
});
|
|
|
189 |
|
|
|
190 |
$this->set_initial_sort_column('badge:namewithimagelink', SORT_ASC);
|
1 |
efrain |
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Adds the filters we want to display in the report
|
|
|
195 |
*
|
|
|
196 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
197 |
* unique identifier
|
|
|
198 |
*/
|
|
|
199 |
protected function add_filters(): void {
|
|
|
200 |
$filters = [
|
|
|
201 |
'badge:name',
|
|
|
202 |
'badge:version',
|
|
|
203 |
'badge:status',
|
|
|
204 |
'badge:expiry',
|
1441 |
ariadna |
205 |
'badge_issued:issued',
|
1 |
efrain |
206 |
];
|
1441 |
ariadna |
207 |
if (!$this->can_view_draft_badges()) {
|
|
|
208 |
// Remove version and status filters.
|
|
|
209 |
unset($filters[1]);
|
|
|
210 |
unset($filters[2]);
|
|
|
211 |
}
|
1 |
efrain |
212 |
$this->add_filters_from_entities($filters);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
|
|
|
217 |
*
|
|
|
218 |
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
|
|
|
219 |
*/
|
|
|
220 |
protected function add_actions(): void {
|
|
|
221 |
// Activate badge.
|
|
|
222 |
$this->add_action((new action(
|
1441 |
ariadna |
223 |
new moodle_url('#'),
|
1 |
efrain |
224 |
new pix_icon('t/show', '', 'core'),
|
1441 |
ariadna |
225 |
[
|
|
|
226 |
'data-action' => 'enablebadge',
|
|
|
227 |
'data-badgeid' => ':id',
|
|
|
228 |
'data-badgename' => ':badgename',
|
|
|
229 |
'data-courseid' => ':courseid',
|
|
|
230 |
],
|
1 |
efrain |
231 |
false,
|
|
|
232 |
new lang_string('activate', 'badges')
|
|
|
233 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
234 |
$badge = new \core_badges\badge($row->id);
|
1441 |
ariadna |
235 |
$row->badgename = $badge->name;
|
1 |
efrain |
236 |
|
1441 |
ariadna |
237 |
return has_capability('moodle/badges:configurecriteria', $badge->get_context()) &&
|
1 |
efrain |
238 |
$badge->has_criteria() &&
|
|
|
239 |
($row->status == BADGE_STATUS_INACTIVE || $row->status == BADGE_STATUS_INACTIVE_LOCKED);
|
|
|
240 |
|
|
|
241 |
}));
|
|
|
242 |
|
|
|
243 |
// Deactivate badge.
|
|
|
244 |
$this->add_action((new action(
|
1441 |
ariadna |
245 |
new moodle_url('#'),
|
1 |
efrain |
246 |
new pix_icon('t/hide', '', 'core'),
|
1441 |
ariadna |
247 |
[
|
|
|
248 |
'data-action' => 'disablebadge',
|
|
|
249 |
'data-badgeid' => ':id',
|
|
|
250 |
'data-badgename' => ':badgename',
|
|
|
251 |
'data-courseid' => ':courseid',
|
|
|
252 |
],
|
1 |
efrain |
253 |
false,
|
|
|
254 |
new lang_string('deactivate', 'badges')
|
|
|
255 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
256 |
$badge = new \core_badges\badge($row->id);
|
1441 |
ariadna |
257 |
$row->badgename = $badge->name;
|
|
|
258 |
return has_capability('moodle/badges:configurecriteria', $badge->get_context()) &&
|
1 |
efrain |
259 |
$badge->has_criteria() &&
|
|
|
260 |
$row->status != BADGE_STATUS_INACTIVE && $row->status != BADGE_STATUS_INACTIVE_LOCKED;
|
|
|
261 |
}));
|
|
|
262 |
|
|
|
263 |
// Award badge manually.
|
|
|
264 |
$this->add_action((new action(
|
|
|
265 |
new moodle_url('/badges/award.php', [
|
|
|
266 |
'id' => ':id',
|
|
|
267 |
]),
|
|
|
268 |
new pix_icon('t/award', '', 'core'),
|
|
|
269 |
[],
|
|
|
270 |
false,
|
|
|
271 |
new lang_string('award', 'badges')
|
|
|
272 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
273 |
$badge = new \core_badges\badge($row->id);
|
|
|
274 |
return has_capability('moodle/badges:awardbadge', $badge->get_context()) &&
|
|
|
275 |
$badge->has_manual_award_criteria() &&
|
|
|
276 |
$badge->is_active();
|
|
|
277 |
}));
|
|
|
278 |
|
|
|
279 |
// Edit action.
|
|
|
280 |
$this->add_action((new action(
|
|
|
281 |
new moodle_url('/badges/edit.php', [
|
|
|
282 |
'id' => ':id',
|
|
|
283 |
'action' => 'badge',
|
|
|
284 |
]),
|
|
|
285 |
new pix_icon('t/edit', '', 'core'),
|
|
|
286 |
[],
|
|
|
287 |
false,
|
|
|
288 |
new lang_string('edit', 'core')
|
|
|
289 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
290 |
$context = self::get_badge_context((int)$row->type, (int)$row->courseid);
|
|
|
291 |
return has_capability('moodle/badges:configuredetails', $context);
|
|
|
292 |
|
|
|
293 |
}));
|
|
|
294 |
|
|
|
295 |
// Duplicate action.
|
|
|
296 |
$this->add_action((new action(
|
|
|
297 |
new moodle_url('/badges/action.php', [
|
|
|
298 |
'id' => ':id',
|
|
|
299 |
'copy' => 1,
|
|
|
300 |
'sesskey' => sesskey(),
|
|
|
301 |
]),
|
|
|
302 |
new pix_icon('t/copy', '', 'core'),
|
|
|
303 |
[],
|
|
|
304 |
false,
|
|
|
305 |
new lang_string('copy', 'badges')
|
|
|
306 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
307 |
$context = self::get_badge_context((int)$row->type, (int)$row->courseid);
|
|
|
308 |
return has_capability('moodle/badges:createbadge', $context);
|
|
|
309 |
}));
|
|
|
310 |
|
|
|
311 |
// Delete action.
|
|
|
312 |
$this->add_action((new action(
|
|
|
313 |
new moodle_url('/badges/index.php', [
|
|
|
314 |
'delete' => ':id',
|
|
|
315 |
'type' => ':type',
|
|
|
316 |
'id' => ':courseid',
|
|
|
317 |
]),
|
|
|
318 |
new pix_icon('t/delete', '', 'core'),
|
|
|
319 |
['class' => 'text-danger'],
|
|
|
320 |
false,
|
|
|
321 |
new lang_string('delete', 'core')
|
|
|
322 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
323 |
$context = self::get_badge_context((int)$row->type, (int)$row->courseid);
|
|
|
324 |
return has_capability('moodle/badges:deletebadge', $context);
|
|
|
325 |
}));
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
/**
|
|
|
329 |
* Return badge context based on type and courseid
|
|
|
330 |
*
|
|
|
331 |
* @param int $type
|
|
|
332 |
* @param int $courseid
|
|
|
333 |
* @return \core\context
|
|
|
334 |
* @throws \coding_exception
|
|
|
335 |
*/
|
|
|
336 |
private static function get_badge_context(int $type, int $courseid): \core\context {
|
|
|
337 |
switch ($type) {
|
|
|
338 |
case BADGE_TYPE_SITE:
|
|
|
339 |
return system::instance();
|
|
|
340 |
case BADGE_TYPE_COURSE:
|
|
|
341 |
return course::instance($courseid);
|
|
|
342 |
default:
|
|
|
343 |
throw new \coding_exception('Wrong context');
|
|
|
344 |
}
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
/**
|
1441 |
ariadna |
348 |
* Check whether the user can view unpublished badges.
|
|
|
349 |
*
|
|
|
350 |
* @return bool True if the user can edit badges, false otherwise.
|
|
|
351 |
*/
|
|
|
352 |
private function can_view_draft_badges(): bool {
|
|
|
353 |
return has_any_capability([
|
|
|
354 |
'moodle/badges:viewawarded',
|
|
|
355 |
'moodle/badges:createbadge',
|
|
|
356 |
'moodle/badges:awardbadge',
|
|
|
357 |
'moodle/badges:configurecriteria',
|
|
|
358 |
'moodle/badges:configuremessages',
|
|
|
359 |
'moodle/badges:configuredetails',
|
|
|
360 |
'moodle/badges:deletebadge'], $this->get_context());
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
/**
|
|
|
364 |
* Store the ID of the badge within each row
|
|
|
365 |
*
|
|
|
366 |
* @param stdClass $row
|
|
|
367 |
*/
|
|
|
368 |
public function row_callback(stdClass $row): void {
|
|
|
369 |
$this->badgeid = (int) $row->id;
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
/**
|
1 |
efrain |
373 |
* CSS classes to add to the row
|
|
|
374 |
*
|
|
|
375 |
* @param stdClass $row
|
|
|
376 |
* @return string
|
|
|
377 |
*/
|
|
|
378 |
public function get_row_class(stdClass $row): string {
|
|
|
379 |
return ($row->status == BADGE_STATUS_INACTIVE_LOCKED || $row->status == BADGE_STATUS_INACTIVE) ? 'text-muted' : '';
|
|
|
380 |
}
|
|
|
381 |
}
|