Proyectos de Subversion Moodle

Rev

| 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 html_writer;
24
use lang_string;
25
use stdClass;
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',
48
        ];
49
    }
50
 
51
    /**
52
     * The default title for this entity
53
     *
54
     * @return lang_string
55
     */
56
    protected function get_default_entity_title(): lang_string {
57
        return new lang_string('tag', 'core_tag');
58
    }
59
 
60
    /**
61
     * Initialise the entity
62
     *
63
     * @return base
64
     */
65
    public function initialise(): base {
66
        $columns = $this->get_all_columns();
67
        foreach ($columns as $column) {
68
            $this->add_column($column);
69
        }
70
 
71
        // All the filters defined by the entity can also be used as conditions.
72
        $filters = $this->get_all_filters();
73
        foreach ($filters as $filter) {
74
            $this
75
                ->add_filter($filter)
76
                ->add_condition($filter);
77
        }
78
 
79
        return $this;
80
    }
81
 
82
    /**
83
     * Returns list of all available columns
84
     *
85
     * @return column[]
86
     */
87
    protected function get_all_columns(): array {
88
        global $DB;
89
 
90
        $tagalias = $this->get_table_alias('tag');
91
 
92
        // Name.
93
        $columns[] = (new column(
94
            'name',
95
            new lang_string('name', 'core_tag'),
96
            $this->get_entity_name()
97
        ))
98
            ->add_joins($this->get_joins())
99
            ->set_type(column::TYPE_TEXT)
100
            ->add_fields("{$tagalias}.rawname, {$tagalias}.name")
101
            ->set_is_sortable(true)
102
            ->add_callback(static function($rawname, stdClass $tag): string {
103
                if ($rawname === null) {
104
                    return '';
105
                }
106
                return core_tag_tag::make_display_name($tag);
107
            });
108
 
109
        // Name with link.
110
        $columns[] = (new column(
111
            'namewithlink',
112
            new lang_string('namewithlink', 'core_tag'),
113
            $this->get_entity_name()
114
        ))
115
            ->add_joins($this->get_joins())
116
            ->set_type(column::TYPE_TEXT)
117
            ->add_fields("{$tagalias}.rawname, {$tagalias}.name, {$tagalias}.tagcollid")
118
            ->set_is_sortable(true)
119
            ->add_callback(static function($rawname, stdClass $tag): string {
120
                if ($rawname === null) {
121
                    return '';
122
                }
123
                return html_writer::link(core_tag_tag::make_url($tag->tagcollid, $tag->rawname),
124
                    core_tag_tag::make_display_name($tag));
125
            });
126
 
127
        // Description.
128
        $descriptionfieldsql = "{$tagalias}.description";
129
        if ($DB->get_dbfamily() === 'oracle') {
130
            $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
131
        }
132
        $columns[] = (new column(
133
            'description',
134
            new lang_string('tagdescription', 'core_tag'),
135
            $this->get_entity_name()
136
        ))
137
            ->add_joins($this->get_joins())
138
            ->set_type(column::TYPE_LONGTEXT)
139
            ->add_field($descriptionfieldsql, 'description')
140
            ->add_fields("{$tagalias}.descriptionformat, {$tagalias}.id")
141
            ->add_callback(static function(?string $description, stdClass $tag): string {
142
                global $CFG;
143
                require_once("{$CFG->libdir}/filelib.php");
144
 
145
                if ($description === null) {
146
                    return '';
147
                }
148
 
149
                $context = context_system::instance();
150
                $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'tag',
151
                    'description', $tag->id);
152
 
153
                return format_text($description, $tag->descriptionformat, ['context' => $context->id]);
154
            });
155
 
156
        // Standard.
157
        $columns[] = (new column(
158
            'standard',
159
            new lang_string('standardtag', 'core_tag'),
160
            $this->get_entity_name()
161
        ))
162
            ->add_joins($this->get_joins())
163
            ->set_type(column::TYPE_BOOLEAN)
164
            ->add_fields("{$tagalias}.isstandard")
165
            ->set_is_sortable(true)
166
            ->add_callback([format::class, 'boolean_as_text']);
167
 
168
        // Flagged.
169
        $columns[] = (new column(
170
            'flagged',
171
            new lang_string('flagged', 'core_tag'),
172
            $this->get_entity_name()
173
        ))
174
            ->add_joins($this->get_joins())
175
            ->set_type(column::TYPE_BOOLEAN)
176
            ->add_field("CASE WHEN {$tagalias}.flag > 0 THEN 1 ELSE {$tagalias}.flag END", 'flag')
177
            ->set_is_sortable(true, ["{$tagalias}.flag"])
178
            ->add_callback([format::class, 'boolean_as_text']);
179
 
180
        // Flag count.
181
        $columns[] = (new column(
182
            'flagcount',
183
            new lang_string('flagcount', 'core_tag'),
184
            $this->get_entity_name()
185
        ))
186
            ->add_joins($this->get_joins())
187
            ->set_type(column::TYPE_INTEGER)
188
            ->add_fields("{$tagalias}.flag")
189
            ->set_is_sortable(true);
190
 
191
        // Time modified.
192
        $columns[] = (new column(
193
            'timemodified',
194
            new lang_string('timemodified', 'core_reportbuilder'),
195
            $this->get_entity_name()
196
        ))
197
            ->add_joins($this->get_joins())
198
            ->set_type(column::TYPE_TIMESTAMP)
199
            ->add_fields("{$tagalias}.timemodified")
200
            ->set_is_sortable(true)
201
            ->add_callback([format::class, 'userdate']);
202
 
203
        return $columns;
204
    }
205
 
206
    /**
207
     * Return list of all available filters
208
     *
209
     * @return filter[]
210
     */
211
    protected function get_all_filters(): array {
212
        $tagalias = $this->get_table_alias('tag');
213
 
214
        // Name.
215
        $filters[] = (new filter(
216
            tags::class,
217
            'name',
218
            new lang_string('name', 'core_tag'),
219
            $this->get_entity_name(),
220
            "{$tagalias}.id"
221
        ))
222
            ->add_joins($this->get_joins());
223
 
224
        // Standard.
225
        $filters[] = (new filter(
226
            boolean_select::class,
227
            'standard',
228
            new lang_string('standardtag', 'core_tag'),
229
            $this->get_entity_name(),
230
            "{$tagalias}.isstandard"
231
        ))
232
            ->add_joins($this->get_joins());
233
 
234
        // Flagged.
235
        $filters[] = (new filter(
236
            boolean_select::class,
237
            'flagged',
238
            new lang_string('flagged', 'core_tag'),
239
            $this->get_entity_name(),
240
            "CASE WHEN {$tagalias}.flag > 0 THEN 1 ELSE {$tagalias}.flag END"
241
        ))
242
            ->add_joins($this->get_joins());
243
 
244
        // Flag count.
245
        $filters[] = (new filter(
246
            number::class,
247
            'flagcount',
248
            new lang_string('flagcount', 'core_tag'),
249
            $this->get_entity_name(),
250
            "{$tagalias}.flag"
251
        ))
252
            ->add_joins($this->get_joins());
253
 
254
        // Time modified.
255
        $filters[] = (new filter(
256
            date::class,
257
            'timemodified',
258
            new lang_string('timemodified', 'core_reportbuilder'),
259
            $this->get_entity_name(),
260
            "{$tagalias}.timemodified"
261
        ))
262
            ->add_joins($this->get_joins())
263
            ->set_limited_operators([
264
                date::DATE_ANY,
265
                date::DATE_CURRENT,
266
                date::DATE_LAST,
267
                date::DATE_RANGE,
268
            ]);
269
 
270
        return $filters;
271
    }
272
}