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;
|
|
|
23 |
use core_reportbuilder\local\helpers\database;
|
|
|
24 |
use core_reportbuilder\local\report\{action, column};
|
|
|
25 |
use core_reportbuilder\system_report;
|
|
|
26 |
use lang_string;
|
|
|
27 |
use moodle_url;
|
|
|
28 |
use pix_icon;
|
|
|
29 |
use stdClass;
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die;
|
|
|
32 |
|
|
|
33 |
global $CFG;
|
|
|
34 |
require_once("{$CFG->libdir}/badgeslib.php");
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Badges system report class implementation
|
|
|
38 |
*
|
|
|
39 |
* @package core_badges
|
|
|
40 |
* @copyright 2023 David Carrillo <davidmc@moodle.com>
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
*/
|
|
|
43 |
class badges extends system_report {
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Initialise report, we need to set the main table, load our entities and set columns/filters
|
|
|
47 |
*/
|
|
|
48 |
protected function initialise(): void {
|
|
|
49 |
// Our main entity, it contains all of the column definitions that we need.
|
|
|
50 |
$badgeentity = new badge();
|
|
|
51 |
$entityalias = $badgeentity->get_table_alias('badge');
|
|
|
52 |
|
|
|
53 |
$this->set_main_table('badge', $entityalias);
|
|
|
54 |
$this->add_entity($badgeentity);
|
|
|
55 |
|
|
|
56 |
$paramtype = database::generate_param_name();
|
|
|
57 |
$context = $this->get_context();
|
|
|
58 |
if ($context instanceof system) {
|
|
|
59 |
$type = BADGE_TYPE_SITE;
|
|
|
60 |
$this->add_base_condition_sql("{$entityalias}.type = :$paramtype", [$paramtype => $type]);
|
|
|
61 |
} else {
|
|
|
62 |
$type = BADGE_TYPE_COURSE;
|
|
|
63 |
$paramcourseid = database::generate_param_name();
|
|
|
64 |
$this->add_base_condition_sql("{$entityalias}.type = :$paramtype AND {$entityalias}.courseid = :$paramcourseid",
|
|
|
65 |
[$paramtype => $type, $paramcourseid => $context->instanceid]);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// Any columns required by actions should be defined here to ensure they're always available.
|
|
|
69 |
$this->add_base_fields("{$entityalias}.id, {$entityalias}.type, {$entityalias}.courseid, {$entityalias}.status");
|
|
|
70 |
|
|
|
71 |
// Now we can call our helper methods to add the content we want to include in the report.
|
|
|
72 |
$this->add_columns($badgeentity);
|
|
|
73 |
$this->add_filters();
|
|
|
74 |
$this->add_actions();
|
|
|
75 |
|
|
|
76 |
// Set initial sorting by name.
|
|
|
77 |
$this->set_initial_sort_column('badge:namewithlink', SORT_ASC);
|
|
|
78 |
|
|
|
79 |
// Set if report can be downloaded.
|
|
|
80 |
$this->set_downloadable(false);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Validates access to view this report
|
|
|
85 |
*
|
|
|
86 |
* @return bool
|
|
|
87 |
*/
|
|
|
88 |
protected function can_view(): bool {
|
|
|
89 |
return has_any_capability([
|
|
|
90 |
'moodle/badges:viewawarded',
|
|
|
91 |
'moodle/badges:createbadge',
|
|
|
92 |
'moodle/badges:awardbadge',
|
|
|
93 |
'moodle/badges:configurecriteria',
|
|
|
94 |
'moodle/badges:configuremessages',
|
|
|
95 |
'moodle/badges:configuredetails',
|
|
|
96 |
'moodle/badges:deletebadge'], $this->get_context());
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Adds the columns we want to display in the report
|
|
|
101 |
*
|
|
|
102 |
* They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
103 |
* unique identifier. If custom columns are needed just for this report, they can be defined here.
|
|
|
104 |
*
|
|
|
105 |
* @param badge $badgeentity
|
|
|
106 |
*/
|
|
|
107 |
public function add_columns(badge $badgeentity): void {
|
|
|
108 |
$columns = [
|
|
|
109 |
'badge:image',
|
|
|
110 |
'badge:namewithlink',
|
|
|
111 |
'badge:version',
|
|
|
112 |
'badge:status',
|
|
|
113 |
'badge:criteria',
|
|
|
114 |
];
|
|
|
115 |
|
|
|
116 |
$this->add_columns_from_entities($columns);
|
|
|
117 |
|
|
|
118 |
// Issued badges column.
|
|
|
119 |
// TODO: Move this column to the entity when MDL-76392 is integrated.
|
|
|
120 |
$tempbadgealias = database::generate_alias();
|
|
|
121 |
$badgeentityalias = $badgeentity->get_table_alias('badge');
|
|
|
122 |
$this->add_column((new column(
|
|
|
123 |
'issued',
|
|
|
124 |
new lang_string('awards', 'core_badges'),
|
|
|
125 |
$badgeentity->get_entity_name()
|
|
|
126 |
))
|
|
|
127 |
->add_joins($this->get_joins())
|
|
|
128 |
->set_type(column::TYPE_INTEGER)
|
|
|
129 |
->add_field("(SELECT COUNT({$tempbadgealias}.userid)
|
|
|
130 |
FROM {badge_issued} {$tempbadgealias}
|
|
|
131 |
INNER JOIN {user} u
|
|
|
132 |
ON {$tempbadgealias}.userid = u.id
|
|
|
133 |
WHERE {$tempbadgealias}.badgeid = {$badgeentityalias}.id AND u.deleted = 0)", 'issued')
|
|
|
134 |
->set_is_sortable(true));
|
|
|
135 |
|
|
|
136 |
// Remove title from image column.
|
|
|
137 |
$this->get_column('badge:image')->set_title(null);
|
|
|
138 |
|
|
|
139 |
// Change title from namewithlink column.
|
|
|
140 |
$this->get_column('badge:namewithlink')->set_title(new lang_string('name'));
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Adds the filters we want to display in the report
|
|
|
145 |
*
|
|
|
146 |
* They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
|
|
|
147 |
* unique identifier
|
|
|
148 |
*/
|
|
|
149 |
protected function add_filters(): void {
|
|
|
150 |
$filters = [
|
|
|
151 |
'badge:name',
|
|
|
152 |
'badge:version',
|
|
|
153 |
'badge:status',
|
|
|
154 |
'badge:expiry',
|
|
|
155 |
];
|
|
|
156 |
$this->add_filters_from_entities($filters);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Add the system report actions. An extra column will be appended to each row, containing all actions added here
|
|
|
161 |
*
|
|
|
162 |
* Note the use of ":id" placeholder which will be substituted according to actual values in the row
|
|
|
163 |
*/
|
|
|
164 |
protected function add_actions(): void {
|
|
|
165 |
// Activate badge.
|
|
|
166 |
$this->add_action((new action(
|
|
|
167 |
new moodle_url('/badges/action.php', [
|
|
|
168 |
'id' => ':id',
|
|
|
169 |
'activate' => true,
|
|
|
170 |
'return' => ':return',
|
|
|
171 |
]),
|
|
|
172 |
new pix_icon('t/show', '', 'core'),
|
|
|
173 |
[],
|
|
|
174 |
false,
|
|
|
175 |
new lang_string('activate', 'badges')
|
|
|
176 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
177 |
$badge = new \core_badges\badge($row->id);
|
|
|
178 |
|
|
|
179 |
// Populate the return URL.
|
|
|
180 |
$row->return = (new moodle_url('/badges/index.php',
|
|
|
181 |
['type' => $badge->type, 'id' => (int) $badge->courseid]))->out_as_local_url(false);
|
|
|
182 |
|
|
|
183 |
return has_capability('moodle/badges:configuredetails', $badge->get_context()) &&
|
|
|
184 |
$badge->has_criteria() &&
|
|
|
185 |
($row->status == BADGE_STATUS_INACTIVE || $row->status == BADGE_STATUS_INACTIVE_LOCKED);
|
|
|
186 |
|
|
|
187 |
}));
|
|
|
188 |
|
|
|
189 |
// Deactivate badge.
|
|
|
190 |
$this->add_action((new action(
|
|
|
191 |
new moodle_url('/badges/index.php', [
|
|
|
192 |
'lock' => ':id',
|
|
|
193 |
'sesskey' => sesskey(),
|
|
|
194 |
'type' => ':type',
|
|
|
195 |
'id' => ':courseid',
|
|
|
196 |
]),
|
|
|
197 |
new pix_icon('t/hide', '', 'core'),
|
|
|
198 |
[],
|
|
|
199 |
false,
|
|
|
200 |
new lang_string('deactivate', 'badges')
|
|
|
201 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
202 |
$badge = new \core_badges\badge($row->id);
|
|
|
203 |
return has_capability('moodle/badges:configuredetails', $badge->get_context()) &&
|
|
|
204 |
$badge->has_criteria() &&
|
|
|
205 |
$row->status != BADGE_STATUS_INACTIVE && $row->status != BADGE_STATUS_INACTIVE_LOCKED;
|
|
|
206 |
}));
|
|
|
207 |
|
|
|
208 |
// Award badge manually.
|
|
|
209 |
$this->add_action((new action(
|
|
|
210 |
new moodle_url('/badges/award.php', [
|
|
|
211 |
'id' => ':id',
|
|
|
212 |
]),
|
|
|
213 |
new pix_icon('t/award', '', 'core'),
|
|
|
214 |
[],
|
|
|
215 |
false,
|
|
|
216 |
new lang_string('award', 'badges')
|
|
|
217 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
218 |
$badge = new \core_badges\badge($row->id);
|
|
|
219 |
return has_capability('moodle/badges:awardbadge', $badge->get_context()) &&
|
|
|
220 |
$badge->has_manual_award_criteria() &&
|
|
|
221 |
$badge->is_active();
|
|
|
222 |
}));
|
|
|
223 |
|
|
|
224 |
// Edit action.
|
|
|
225 |
$this->add_action((new action(
|
|
|
226 |
new moodle_url('/badges/edit.php', [
|
|
|
227 |
'id' => ':id',
|
|
|
228 |
'action' => 'badge',
|
|
|
229 |
]),
|
|
|
230 |
new pix_icon('t/edit', '', 'core'),
|
|
|
231 |
[],
|
|
|
232 |
false,
|
|
|
233 |
new lang_string('edit', 'core')
|
|
|
234 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
235 |
$context = self::get_badge_context((int)$row->type, (int)$row->courseid);
|
|
|
236 |
return has_capability('moodle/badges:configuredetails', $context);
|
|
|
237 |
|
|
|
238 |
}));
|
|
|
239 |
|
|
|
240 |
// Duplicate action.
|
|
|
241 |
$this->add_action((new action(
|
|
|
242 |
new moodle_url('/badges/action.php', [
|
|
|
243 |
'id' => ':id',
|
|
|
244 |
'copy' => 1,
|
|
|
245 |
'sesskey' => sesskey(),
|
|
|
246 |
]),
|
|
|
247 |
new pix_icon('t/copy', '', 'core'),
|
|
|
248 |
[],
|
|
|
249 |
false,
|
|
|
250 |
new lang_string('copy', 'badges')
|
|
|
251 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
252 |
$context = self::get_badge_context((int)$row->type, (int)$row->courseid);
|
|
|
253 |
return has_capability('moodle/badges:createbadge', $context);
|
|
|
254 |
}));
|
|
|
255 |
|
|
|
256 |
// Delete action.
|
|
|
257 |
$this->add_action((new action(
|
|
|
258 |
new moodle_url('/badges/index.php', [
|
|
|
259 |
'delete' => ':id',
|
|
|
260 |
'type' => ':type',
|
|
|
261 |
'id' => ':courseid',
|
|
|
262 |
]),
|
|
|
263 |
new pix_icon('t/delete', '', 'core'),
|
|
|
264 |
['class' => 'text-danger'],
|
|
|
265 |
false,
|
|
|
266 |
new lang_string('delete', 'core')
|
|
|
267 |
))->add_callback(static function(stdclass $row): bool {
|
|
|
268 |
$context = self::get_badge_context((int)$row->type, (int)$row->courseid);
|
|
|
269 |
return has_capability('moodle/badges:deletebadge', $context);
|
|
|
270 |
}));
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* Return badge context based on type and courseid
|
|
|
275 |
*
|
|
|
276 |
* @param int $type
|
|
|
277 |
* @param int $courseid
|
|
|
278 |
* @return \core\context
|
|
|
279 |
* @throws \coding_exception
|
|
|
280 |
*/
|
|
|
281 |
private static function get_badge_context(int $type, int $courseid): \core\context {
|
|
|
282 |
switch ($type) {
|
|
|
283 |
case BADGE_TYPE_SITE:
|
|
|
284 |
return system::instance();
|
|
|
285 |
case BADGE_TYPE_COURSE:
|
|
|
286 |
return course::instance($courseid);
|
|
|
287 |
default:
|
|
|
288 |
throw new \coding_exception('Wrong context');
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* CSS classes to add to the row
|
|
|
294 |
*
|
|
|
295 |
* @param stdClass $row
|
|
|
296 |
* @return string
|
|
|
297 |
*/
|
|
|
298 |
public function get_row_class(stdClass $row): string {
|
|
|
299 |
return ($row->status == BADGE_STATUS_INACTIVE_LOCKED || $row->status == BADGE_STATUS_INACTIVE) ? 'text-muted' : '';
|
|
|
300 |
}
|
|
|
301 |
}
|