Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 18... Línea 18...
18
 
18
 
Línea 19... Línea 19...
19
namespace core_badges\reportbuilder\local\systemreports;
19
namespace core_badges\reportbuilder\local\systemreports;
20
 
20
 
-
 
21
use core\context\{course, system};
21
use core\context\{course, system};
22
use core_badges\reportbuilder\local\entities\badge;
22
use core_badges\reportbuilder\local\entities\badge;
23
use core_badges\reportbuilder\local\entities\badge_issued;
23
use core_reportbuilder\local\helpers\database;
24
use core_reportbuilder\local\helpers\database;
-
 
25
use core_reportbuilder\local\report\{action, column};
24
use core_reportbuilder\local\report\{action, column};
26
use core_reportbuilder\system_report;
25
use core_reportbuilder\system_report;
27
use html_writer;
26
use lang_string;
28
use lang_string;
27
use moodle_url;
29
use moodle_url;
Línea 40... Línea 42...
40
 * @copyright  2023 David Carrillo <davidmc@moodle.com>
42
 * @copyright  2023 David Carrillo <davidmc@moodle.com>
41
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
44
 */
43
class badges extends system_report {
45
class badges extends system_report {
Línea -... Línea 46...
-
 
46
 
-
 
47
    /** @var int $badgeid The ID of the current badge row */
-
 
48
    private int $badgeid;
44
 
49
 
45
    /**
50
    /**
46
     * Initialise report, we need to set the main table, load our entities and set columns/filters
51
     * Initialise report, we need to set the main table, load our entities and set columns/filters
47
     */
52
     */
-
 
53
    protected function initialise(): void {
-
 
54
        global $USER;
48
    protected function initialise(): void {
55
 
49
        // Our main entity, it contains all of the column definitions that we need.
56
        // Our main entity, it contains all of the column definitions that we need.
50
        $badgeentity = new badge();
57
        $badgeentity = new badge();
Línea 51... Línea 58...
51
        $entityalias = $badgeentity->get_table_alias('badge');
58
        $entityalias = $badgeentity->get_table_alias('badge');
Línea 63... Línea 70...
63
            $paramcourseid = database::generate_param_name();
70
            $paramcourseid = database::generate_param_name();
64
            $this->add_base_condition_sql("{$entityalias}.type = :$paramtype AND {$entityalias}.courseid = :$paramcourseid",
71
            $this->add_base_condition_sql("{$entityalias}.type = :$paramtype AND {$entityalias}.courseid = :$paramcourseid",
65
                [$paramtype => $type, $paramcourseid => $context->instanceid]);
72
                [$paramtype => $type, $paramcourseid => $context->instanceid]);
66
        }
73
        }
Línea -... Línea 74...
-
 
74
 
-
 
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
        }
67
 
79
 
68
        // Any columns required by actions should be defined here to ensure they're always available.
80
        // Any columns required by actions should be defined here to ensure they're always available.
Línea -... Línea 81...
-
 
81
        $this->add_base_fields("{$entityalias}.id, {$entityalias}.type, {$entityalias}.courseid, {$entityalias}.status");
-
 
82
 
-
 
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
 
69
        $this->add_base_fields("{$entityalias}.id, {$entityalias}.type, {$entityalias}.courseid, {$entityalias}.status");
90
        $this->add_base_fields("{$badgeissuedalias}.uniquehash");
70
 
91
 
71
        // Now we can call our helper methods to add the content we want to include in the report.
92
        // Now we can call our helper methods to add the content we want to include in the report.
72
        $this->add_columns($badgeentity);
93
        $this->add_columns($badgeissuedalias);
Línea 73... Línea -...
73
        $this->add_filters();
-
 
74
        $this->add_actions();
94
        $this->add_filters();
Línea 75... Línea 95...
75
 
95
        $this->add_actions();
76
        // Set initial sorting by name.
96
 
77
        $this->set_initial_sort_column('badge:namewithlink', SORT_ASC);
97
        $this->set_default_no_results_notice(new lang_string('nomatchingbadges', 'core_badges'));
Línea 85... Línea 105...
85
     *
105
     *
86
     * @return bool
106
     * @return bool
87
     */
107
     */
88
    protected function can_view(): bool {
108
    protected function can_view(): bool {
89
        return has_any_capability([
109
        return has_any_capability([
-
 
110
            'moodle/badges:viewbadges',
90
            'moodle/badges:viewawarded',
111
            'moodle/badges:viewawarded',
91
            'moodle/badges:createbadge',
112
            'moodle/badges:createbadge',
92
            'moodle/badges:awardbadge',
113
            'moodle/badges:awardbadge',
93
            'moodle/badges:configurecriteria',
114
            'moodle/badges:configurecriteria',
94
            'moodle/badges:configuremessages',
115
            'moodle/badges:configuremessages',
Línea 100... Línea 121...
100
     * Adds the columns we want to display in the report
121
     * Adds the columns we want to display in the report
101
     *
122
     *
102
     * They are provided by the entities we previously added in the {@see initialise} method, referencing each by their
123
     * 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.
124
     * unique identifier. If custom columns are needed just for this report, they can be defined here.
104
     *
125
     *
105
     * @param badge $badgeentity
126
     * @param string $badgeissuedalias
106
     */
127
     */
107
    public function add_columns(badge $badgeentity): void {
128
    public function add_columns(string $badgeissuedalias): void {
108
        $columns = [
129
        $columns = [
109
            'badge:image',
-
 
110
            'badge:namewithlink',
130
            'badge:namewithimagelink',
111
            'badge:version',
-
 
112
            'badge:status',
131
            'badge:status',
113
            'badge:criteria',
132
            'badge:criteria',
114
        ];
133
        ];
Línea -... Línea 134...
-
 
134
 
-
 
135
        $canviewdraftbadges = $this->can_view_draft_badges();
-
 
136
        if (!$canviewdraftbadges) {
-
 
137
            // Remove status and recipients column.
-
 
138
            unset($columns[1]);
115
 
139
        }
Línea -... Línea 140...
-
 
140
        $this->add_columns_from_entities($columns);
-
 
141
 
-
 
142
        // Change title of the `namewithimagelink` column to 'Name'.
116
        $this->add_columns_from_entities($columns);
143
        $this->get_column('badge:namewithimagelink')->set_title(new lang_string('name'));
-
 
144
 
117
 
145
        // Recipients column.
118
        // Issued badges column.
146
        if ($canviewdraftbadges) {
119
        // TODO: Move this column to the entity when MDL-76392 is integrated.
147
            $badgeentity = $this->get_entity('badge');
120
        $tempbadgealias = database::generate_alias();
148
            $tempbadgealias = database::generate_alias();
121
        $badgeentityalias = $badgeentity->get_table_alias('badge');
149
            $badgeentityalias = $badgeentity->get_table_alias('badge');
122
        $this->add_column((new column(
150
            $this->add_column((new column(
123
            'issued',
151
                'issued',
124
            new lang_string('awards', 'core_badges'),
152
                new lang_string('awards', 'core_badges'),
125
            $badgeentity->get_entity_name()
153
                $badgeentity->get_entity_name()
126
        ))
154
            ))
127
            ->add_joins($this->get_joins())
155
                ->add_joins($this->get_joins())
128
            ->set_type(column::TYPE_INTEGER)
156
                ->set_type(column::TYPE_INTEGER)
129
            ->add_field("(SELECT COUNT({$tempbadgealias}.userid)
157
                ->add_field("(SELECT COUNT({$tempbadgealias}.userid)
130
                            FROM {badge_issued} {$tempbadgealias}
158
                                FROM {badge_issued} {$tempbadgealias}
131
                      INNER JOIN {user} u
159
                        INNER JOIN {user} u
132
                              ON {$tempbadgealias}.userid = u.id
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
                    }
-
 
167
 
-
 
168
                    return html_writer::link(new moodle_url('/badges/recipients.php', ['id' => $this->badgeid]), $count);
Línea 133... Línea 169...
133
                           WHERE {$tempbadgealias}.badgeid = {$badgeentityalias}.id AND u.deleted = 0)", 'issued')
169
                }));
-
 
170
        }
134
            ->set_is_sortable(true));
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]);
Línea 135... Línea -...
135
 
-
 
136
        // Remove title from image column.
186
                $icon = new pix_icon('i/valid', get_string('dateearned', 'badges', $date));
137
        $this->get_column('badge:image')->set_title(null);
187
                return $OUTPUT->action_icon($badgeurl, $icon, null, null, true);
Línea 138... Línea 188...
138
 
188
            });
139
        // Change title from namewithlink column.
189
 
140
        $this->get_column('badge:namewithlink')->set_title(new lang_string('name'));
190
        $this->set_initial_sort_column('badge:namewithimagelink', SORT_ASC);
Línea 150... Línea 200...
150
        $filters = [
200
        $filters = [
151
            'badge:name',
201
            'badge:name',
152
            'badge:version',
202
            'badge:version',
153
            'badge:status',
203
            'badge:status',
154
            'badge:expiry',
204
            'badge:expiry',
-
 
205
            'badge_issued:issued',
155
        ];
206
        ];
-
 
207
        if (!$this->can_view_draft_badges()) {
-
 
208
            // Remove version and status filters.
-
 
209
            unset($filters[1]);
-
 
210
            unset($filters[2]);
-
 
211
        }
156
        $this->add_filters_from_entities($filters);
212
        $this->add_filters_from_entities($filters);
157
    }
213
    }
Línea 158... Línea 214...
158
 
214
 
159
    /**
215
    /**
Línea 162... Línea 218...
162
     * Note the use of ":id" placeholder which will be substituted according to actual values in the row
218
     * Note the use of ":id" placeholder which will be substituted according to actual values in the row
163
     */
219
     */
164
    protected function add_actions(): void {
220
    protected function add_actions(): void {
165
        // Activate badge.
221
        // Activate badge.
166
        $this->add_action((new action(
222
        $this->add_action((new action(
167
            new moodle_url('/badges/action.php', [
-
 
168
                'id' => ':id',
223
            new moodle_url('#'),
169
                'activate' => true,
-
 
170
                'return' => ':return',
-
 
171
            ]),
-
 
172
            new pix_icon('t/show', '', 'core'),
224
            new pix_icon('t/show', '', 'core'),
-
 
225
            [
-
 
226
                'data-action' => 'enablebadge',
-
 
227
                'data-badgeid' => ':id',
-
 
228
                'data-badgename' => ':badgename',
-
 
229
                'data-courseid' => ':courseid',
173
            [],
230
            ],
174
            false,
231
            false,
175
            new lang_string('activate', 'badges')
232
            new lang_string('activate', 'badges')
176
        ))->add_callback(static function(stdclass $row): bool {
233
        ))->add_callback(static function(stdclass $row): bool {
177
            $badge = new \core_badges\badge($row->id);
234
            $badge = new \core_badges\badge($row->id);
-
 
235
            $row->badgename = $badge->name;
Línea 178... Línea -...
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
 
236
 
183
            return has_capability('moodle/badges:configuredetails', $badge->get_context()) &&
237
            return has_capability('moodle/badges:configurecriteria', $badge->get_context()) &&
184
                $badge->has_criteria() &&
238
                $badge->has_criteria() &&
Línea 185... Línea 239...
185
                ($row->status == BADGE_STATUS_INACTIVE || $row->status == BADGE_STATUS_INACTIVE_LOCKED);
239
                ($row->status == BADGE_STATUS_INACTIVE || $row->status == BADGE_STATUS_INACTIVE_LOCKED);
Línea 186... Línea 240...
186
 
240
 
187
        }));
241
        }));
188
 
242
 
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',
243
        // Deactivate badge.
-
 
244
        $this->add_action((new action(
-
 
245
            new moodle_url('#'),
-
 
246
            new pix_icon('t/hide', '', 'core'),
-
 
247
            [
-
 
248
                'data-action' => 'disablebadge',
195
                'id' => ':courseid',
249
                'data-badgeid' => ':id',
196
            ]),
250
                'data-badgename' => ':badgename',
197
            new pix_icon('t/hide', '', 'core'),
251
                'data-courseid' => ':courseid',
198
            [],
252
            ],
199
            false,
253
            false,
-
 
254
            new lang_string('deactivate', 'badges')
200
            new lang_string('deactivate', 'badges')
255
        ))->add_callback(static function(stdclass $row): bool {
201
        ))->add_callback(static function(stdclass $row): bool {
256
            $badge = new \core_badges\badge($row->id);
202
            $badge = new \core_badges\badge($row->id);
257
            $row->badgename = $badge->name;
203
            return has_capability('moodle/badges:configuredetails', $badge->get_context()) &&
258
            return has_capability('moodle/badges:configurecriteria', $badge->get_context()) &&
Línea 204... Línea 259...
204
                $badge->has_criteria() &&
259
                $badge->has_criteria() &&
Línea 288... Línea 343...
288
                throw new \coding_exception('Wrong context');
343
                throw new \coding_exception('Wrong context');
289
        }
344
        }
290
    }
345
    }
Línea 291... Línea 346...
291
 
346
 
-
 
347
    /**
-
 
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
 
292
    /**
372
    /**
293
     * CSS classes to add to the row
373
     * CSS classes to add to the row
294
     *
374
     *
295
     * @param stdClass $row
375
     * @param stdClass $row
296
     * @return string
376
     * @return string