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_course\reportbuilder\local\entities;
20
 
21
use context_coursecat;
22
use context_helper;
23
use html_writer;
24
use lang_string;
25
use moodle_url;
26
use stdClass;
27
use theme_config;
28
use core_course_category;
29
use core_reportbuilder\local\entities\base;
30
use core_reportbuilder\local\filters\{category, select, text};
31
use core_reportbuilder\local\report\column;
32
use core_reportbuilder\local\report\filter;
33
 
34
/**
35
 * Course category entity
36
 *
37
 * @package     core_course
38
 * @copyright   2021 Paul Holden <paulh@moodle.com>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class course_category extends base {
42
 
43
    /**
44
     * Database tables that this entity uses
45
     *
46
     * @return string[]
47
     */
48
    protected function get_default_tables(): array {
49
        return [
50
            'context',
51
            'course_categories',
52
        ];
53
    }
54
 
55
    /**
56
     * The default title for this entity
57
     *
58
     * @return lang_string
59
     */
60
    protected function get_default_entity_title(): lang_string {
61
        return new lang_string('coursecategory');
62
    }
63
 
64
    /**
65
     * Initialise the entity
66
     *
67
     * @return base
68
     */
69
    public function initialise(): base {
70
        $columns = $this->get_all_columns();
71
        foreach ($columns as $column) {
72
            $this->add_column($column);
73
        }
74
 
75
        // All the filters defined by the entity can also be used as conditions.
76
        $filters = $this->get_all_filters();
77
        foreach ($filters as $filter) {
78
            $this
79
                ->add_filter($filter)
80
                ->add_condition($filter);
81
        }
82
 
83
        return $this;
84
    }
85
 
86
    /**
87
     * Returns list of all available columns
88
     *
89
     * @return column[]
90
     */
91
    protected function get_all_columns(): array {
92
        $tablealias = $this->get_table_alias('course_categories');
93
        $tablealiascontext = $this->get_table_alias('context');
94
 
95
        // Name column.
96
        $columns[] = (new column(
97
            'name',
98
            new lang_string('categoryname'),
99
            $this->get_entity_name()
100
        ))
101
            ->add_joins($this->get_joins())
102
            ->add_join($this->get_context_join())
103
            ->set_type(column::TYPE_TEXT)
104
            ->add_fields("{$tablealias}.name, {$tablealias}.id")
105
            ->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
106
            ->add_callback(static function(?string $name, stdClass $category): string {
107
                if (empty($category->id)) {
108
                    return '';
109
                }
110
 
111
                context_helper::preload_from_record($category);
112
                $context = context_coursecat::instance($category->id);
113
 
114
                return format_string($category->name, true, ['context' => $context]);
115
            })
116
            ->set_is_sortable(true);
117
 
118
        // Category name with link column.
119
        $columns[] = (new column(
120
            'namewithlink',
121
            new lang_string('namewithlink', 'core_course'),
122
            $this->get_entity_name()
123
        ))
124
            ->add_joins($this->get_joins())
125
            ->add_join($this->get_context_join())
126
            ->set_type(column::TYPE_TEXT)
127
            ->add_fields("{$tablealias}.name, {$tablealias}.id")
128
            ->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
129
            ->add_callback(static function(?string $name, stdClass $category): string {
130
                if (empty($category->id)) {
131
                    return '';
132
                }
133
                context_helper::preload_from_record($category);
134
                $context = context_coursecat::instance($category->id);
135
                $url = new moodle_url('/course/management.php', ['categoryid' => $category->id]);
136
                return html_writer::link($url,
137
                    format_string($category->name, true, ['context' => $context]));
138
            })
139
            ->set_is_sortable(true);
140
 
141
        // Path column.
142
        $columns[] = (new column(
143
            'path',
144
            new lang_string('categorypath'),
145
            $this->get_entity_name()
146
        ))
147
            ->add_joins($this->get_joins())
148
            ->set_type(column::TYPE_TEXT)
149
            ->add_fields("{$tablealias}.name, {$tablealias}.id")
150
            ->add_callback(static function(?string $name, stdClass $category): string {
151
                return empty($category->id) ? '' :
152
                    core_course_category::get($category->id, MUST_EXIST, true)->get_nested_name(false);
153
            })
154
            ->set_disabled_aggregation(['groupconcat', 'groupconcatdistinct'])
155
            ->set_is_sortable(true);
156
 
157
        // ID number column.
158
        $columns[] = (new column(
159
            'idnumber',
160
            new lang_string('idnumbercoursecategory'),
161
            $this->get_entity_name()
162
        ))
163
            ->add_joins($this->get_joins())
164
            ->set_type(column::TYPE_TEXT)
165
            ->add_fields("{$tablealias}.idnumber")
166
            ->set_is_sortable(true);
167
 
168
        // Description column (note we need to join/select from the context table in order to format the column).
169
        $columns[] = (new column(
170
            'description',
171
            new lang_string('description'),
172
            $this->get_entity_name()
173
        ))
174
            ->add_joins($this->get_joins())
175
            ->add_join($this->get_context_join())
176
            ->set_type(column::TYPE_LONGTEXT)
1441 ariadna 177
            ->add_fields("{$tablealias}.description, {$tablealias}.descriptionformat, {$tablealias}.id")
1 efrain 178
            ->add_fields(context_helper::get_preload_record_columns_sql($tablealiascontext))
1441 ariadna 179
            ->set_is_sortable(true)
1 efrain 180
            ->add_callback(static function(?string $description, stdClass $category): string {
181
                global $CFG;
182
                require_once("{$CFG->libdir}/filelib.php");
183
 
184
                if ($description === null) {
185
                    return '';
186
                }
187
 
188
                context_helper::preload_from_record($category);
189
                $context = context_coursecat::instance($category->id);
190
 
191
                $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'coursecat',
192
                    'description', null);
193
 
194
                return format_text($description, $category->descriptionformat, ['context' => $context->id]);
195
            });
196
 
197
        // Theme column.
198
        $columns[] = (new column(
199
            'theme',
200
            new lang_string('theme'),
201
            $this->get_entity_name()
202
        ))
203
            ->add_joins($this->get_joins())
204
            ->set_type(column::TYPE_TEXT)
205
            ->add_fields("{$tablealias}.theme")
206
            ->set_is_sortable(true)
207
            ->add_callback(static function (?string $theme): string {
208
                if ((string) $theme === '') {
209
                    return '';
210
                }
211
 
212
                return get_string('pluginname', "theme_{$theme}");
213
            });
214
 
215
        // Course count column.
216
        $columns[] = (new column(
217
            'coursecount',
218
            new lang_string('coursecount', 'core_course'),
219
            $this->get_entity_name()
220
        ))
221
            ->add_joins($this->get_joins())
222
            ->set_type(column::TYPE_INTEGER)
223
            ->add_fields("{$tablealias}.coursecount")
224
            ->set_is_sortable(true);
225
 
226
        return $columns;
227
    }
228
 
229
    /**
230
     * Return list of all available filters
231
     *
232
     * @return filter[]
233
     */
234
    protected function get_all_filters(): array {
235
        $tablealias = $this->get_table_alias('course_categories');
236
 
237
        // Select category filter.
238
        $filters[] = (new filter(
239
            category::class,
240
            'name',
241
            new lang_string('categoryselect', 'core_reportbuilder'),
242
            $this->get_entity_name(),
243
            "{$tablealias}.id"
244
        ))
245
            ->add_joins($this->get_joins())
246
            ->set_options([
247
                'requiredcapabilities' => 'moodle/category:viewcourselist',
248
            ]);
249
 
250
        // Name filter.
251
        $filters[] = (new filter(
252
            text::class,
253
            'text',
254
            new lang_string('categoryname'),
255
            $this->get_entity_name(),
256
            "{$tablealias}.name"
257
        ))
258
            ->add_joins($this->get_joins());
259
 
260
        // ID number filter.
261
        $filters[] = (new filter(
262
            text::class,
263
            'idnumber',
264
            new lang_string('idnumbercoursecategory'),
265
            $this->get_entity_name(),
266
            "{$tablealias}.idnumber"
267
        ))
268
            ->add_joins($this->get_joins());
269
 
270
        // Theme filter.
271
        $filters[] = (new filter(
272
            select::class,
273
            'theme',
274
            new lang_string('theme'),
275
            $this->get_entity_name(),
276
            "{$tablealias}.theme",
277
        ))
278
            ->set_options_callback(static function(): array {
279
                return array_map(
280
                    fn(theme_config $theme) => $theme->get_theme_name(),
281
                    get_list_of_themes(),
282
                );
283
            })
284
            ->add_joins($this->get_joins());
285
 
286
        return $filters;
287
    }
288
 
289
    /**
290
     * Return context join used by columns
291
     *
292
     * @return string
293
     */
294
    public function get_context_join(): string {
295
        $coursecategories = $this->get_table_alias('course_categories');
296
        $context = $this->get_table_alias('context');
297
        return "LEFT JOIN {context} {$context} ON {$context}.instanceid = {$coursecategories}.id
298
            AND {$context}.contextlevel = " . CONTEXT_COURSECAT;
299
    }
300
}