Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_tag\reportbuilder\local\entities;
20
 
21
use context_system;
22
use core_tag_tag;
23
use stdClass;
1441 ariadna 24
use core\lang_string;
25
use core\output\html_writer;
1 efrain 26
use core_reportbuilder\local\entities\base;
27
use core_reportbuilder\local\filters\{boolean_select, date, number, tags};
28
use core_reportbuilder\local\helpers\format;
29
use core_reportbuilder\local\report\{column, filter};
30
 
31
/**
32
 * Tag entity
33
 *
34
 * @package     core_tag
35
 * @copyright   2022 Paul Holden <paulh@moodle.com>
36
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class tag extends base {
39
 
40
    /**
41
     * Database tables that this entity uses
42
     *
43
     * @return string[]
44
     */
45
    protected function get_default_tables(): array {
46
        return [
47
            'tag',
1441 ariadna 48
            'tag_instance',
1 efrain 49
        ];
50
    }
51
 
52
    /**
53
     * The default title for this entity
54
     *
55
     * @return lang_string
56
     */
57
    protected function get_default_entity_title(): lang_string {
58
        return new lang_string('tag', 'core_tag');
59
    }
60
 
61
    /**
62
     * Initialise the entity
63
     *
64
     * @return base
65
     */
66
    public function initialise(): base {
67
        $columns = $this->get_all_columns();
68
        foreach ($columns as $column) {
69
            $this->add_column($column);
70
        }
71
 
72
        // All the filters defined by the entity can also be used as conditions.
73
        $filters = $this->get_all_filters();
74
        foreach ($filters as $filter) {
75
            $this
76
                ->add_filter($filter)
77
                ->add_condition($filter);
78
        }
79
 
80
        return $this;
81
    }
82
 
83
    /**
84
     * Returns list of all available columns
85
     *
86
     * @return column[]
87
     */
88
    protected function get_all_columns(): array {
89
        $tagalias = $this->get_table_alias('tag');
90
 
91
        // Name.
92
        $columns[] = (new column(
93
            'name',
94
            new lang_string('name', 'core_tag'),
95
            $this->get_entity_name()
96
        ))
97
            ->add_joins($this->get_joins())
98
            ->add_fields("{$tagalias}.rawname, {$tagalias}.name")
99
            ->set_is_sortable(true)
100
            ->add_callback(static function($rawname, stdClass $tag): string {
101
                if ($rawname === null) {
102
                    return '';
103
                }
104
                return core_tag_tag::make_display_name($tag);
105
            });
106
 
1441 ariadna 107
        // Name with badge.
108
        $columns[] = (new column(
109
            'namewithbadge',
110
            new lang_string('namewithbadge', 'core_tag'),
111
            $this->get_entity_name()
112
        ))
113
            ->add_joins($this->get_joins())
114
            ->add_fields("{$tagalias}.rawname, {$tagalias}.name, {$tagalias}.flag, {$tagalias}.isstandard")
115
            ->set_is_sortable(true)
116
            ->set_aggregation_options('groupconcat', ['separator' => ' '])
117
            ->set_aggregation_options('groupconcatdistinct', ['separator' => ' '])
118
            ->add_callback(static function($rawname, stdClass $tag): string {
119
                if ($rawname === null) {
120
                    return '';
121
                }
122
 
123
                $displayname = core_tag_tag::make_display_name($tag);
124
                if ($tag->flag > 0) {
125
                    $displayname = html_writer::span($displayname, 'flagged-tag');
126
                }
127
 
128
                $class = 'badge bg-info text-white';
129
                if ($tag->isstandard) {
130
                    $class .= ' standardtag';
131
                }
132
 
133
                return html_writer::span($displayname, $class);
134
            });
135
 
1 efrain 136
        // Name with link.
137
        $columns[] = (new column(
138
            'namewithlink',
139
            new lang_string('namewithlink', 'core_tag'),
140
            $this->get_entity_name()
141
        ))
142
            ->add_joins($this->get_joins())
143
            ->add_fields("{$tagalias}.rawname, {$tagalias}.name, {$tagalias}.tagcollid")
144
            ->set_is_sortable(true)
145
            ->add_callback(static function($rawname, stdClass $tag): string {
146
                if ($rawname === null) {
147
                    return '';
148
                }
149
                return html_writer::link(core_tag_tag::make_url($tag->tagcollid, $tag->rawname),
150
                    core_tag_tag::make_display_name($tag));
151
            });
152
 
153
        // Description.
154
        $columns[] = (new column(
155
            'description',
156
            new lang_string('tagdescription', 'core_tag'),
157
            $this->get_entity_name()
158
        ))
159
            ->add_joins($this->get_joins())
160
            ->set_type(column::TYPE_LONGTEXT)
1441 ariadna 161
            ->add_fields("{$tagalias}.description, {$tagalias}.descriptionformat, {$tagalias}.id")
162
            ->set_is_sortable(true)
1 efrain 163
            ->add_callback(static function(?string $description, stdClass $tag): string {
164
                global $CFG;
165
                require_once("{$CFG->libdir}/filelib.php");
166
 
167
                if ($description === null) {
168
                    return '';
169
                }
170
 
171
                $context = context_system::instance();
172
                $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'tag',
173
                    'description', $tag->id);
174
 
175
                return format_text($description, $tag->descriptionformat, ['context' => $context->id]);
176
            });
177
 
178
        // Standard.
179
        $columns[] = (new column(
180
            'standard',
181
            new lang_string('standardtag', 'core_tag'),
182
            $this->get_entity_name()
183
        ))
184
            ->add_joins($this->get_joins())
185
            ->set_type(column::TYPE_BOOLEAN)
186
            ->add_fields("{$tagalias}.isstandard")
187
            ->set_is_sortable(true)
188
            ->add_callback([format::class, 'boolean_as_text']);
189
 
190
        // Flagged.
191
        $columns[] = (new column(
192
            'flagged',
193
            new lang_string('flagged', 'core_tag'),
194
            $this->get_entity_name()
195
        ))
196
            ->add_joins($this->get_joins())
197
            ->set_type(column::TYPE_BOOLEAN)
198
            ->add_field("CASE WHEN {$tagalias}.flag > 0 THEN 1 ELSE {$tagalias}.flag END", 'flag')
1441 ariadna 199
            ->set_is_sortable(true)
1 efrain 200
            ->add_callback([format::class, 'boolean_as_text']);
201
 
202
        // Flag count.
203
        $columns[] = (new column(
204
            'flagcount',
205
            new lang_string('flagcount', 'core_tag'),
206
            $this->get_entity_name()
207
        ))
208
            ->add_joins($this->get_joins())
209
            ->set_type(column::TYPE_INTEGER)
210
            ->add_fields("{$tagalias}.flag")
211
            ->set_is_sortable(true);
212
 
213
        // Time modified.
214
        $columns[] = (new column(
215
            'timemodified',
216
            new lang_string('timemodified', 'core_reportbuilder'),
217
            $this->get_entity_name()
218
        ))
219
            ->add_joins($this->get_joins())
220
            ->set_type(column::TYPE_TIMESTAMP)
221
            ->add_fields("{$tagalias}.timemodified")
222
            ->set_is_sortable(true)
223
            ->add_callback([format::class, 'userdate']);
224
 
225
        return $columns;
226
    }
227
 
228
    /**
229
     * Return list of all available filters
230
     *
231
     * @return filter[]
232
     */
233
    protected function get_all_filters(): array {
234
        $tagalias = $this->get_table_alias('tag');
235
 
236
        // Name.
237
        $filters[] = (new filter(
238
            tags::class,
239
            'name',
240
            new lang_string('name', 'core_tag'),
241
            $this->get_entity_name(),
242
            "{$tagalias}.id"
243
        ))
244
            ->add_joins($this->get_joins());
245
 
246
        // Standard.
247
        $filters[] = (new filter(
248
            boolean_select::class,
249
            'standard',
250
            new lang_string('standardtag', 'core_tag'),
251
            $this->get_entity_name(),
252
            "{$tagalias}.isstandard"
253
        ))
254
            ->add_joins($this->get_joins());
255
 
256
        // Flagged.
257
        $filters[] = (new filter(
258
            boolean_select::class,
259
            'flagged',
260
            new lang_string('flagged', 'core_tag'),
261
            $this->get_entity_name(),
262
            "CASE WHEN {$tagalias}.flag > 0 THEN 1 ELSE {$tagalias}.flag END"
263
        ))
264
            ->add_joins($this->get_joins());
265
 
266
        // Flag count.
267
        $filters[] = (new filter(
268
            number::class,
269
            'flagcount',
270
            new lang_string('flagcount', 'core_tag'),
271
            $this->get_entity_name(),
272
            "{$tagalias}.flag"
273
        ))
274
            ->add_joins($this->get_joins());
275
 
276
        // Time modified.
277
        $filters[] = (new filter(
278
            date::class,
279
            'timemodified',
280
            new lang_string('timemodified', 'core_reportbuilder'),
281
            $this->get_entity_name(),
282
            "{$tagalias}.timemodified"
283
        ))
284
            ->add_joins($this->get_joins())
285
            ->set_limited_operators([
286
                date::DATE_ANY,
287
                date::DATE_CURRENT,
288
                date::DATE_LAST,
289
                date::DATE_RANGE,
290
            ]);
291
 
292
        return $filters;
293
    }
1441 ariadna 294
 
295
    /**
296
     * Return joins necessary for retrieving tags
297
     *
298
     * @param string $component
299
     * @param string $itemtype
300
     * @param string $itemidfield
301
     * @return string[]
302
     */
303
    public function get_tag_joins(string $component, string $itemtype, string $itemidfield): array {
304
        return $this->get_tag_joins_for_entity($component, $itemtype, $itemidfield);
305
    }
1 efrain 306
}