Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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_competency\reportbuilder\local\entities;
20
 
21
use core\{context, context_helper};
22
use core\lang_string;
23
use core_reportbuilder\local\entities\base;
24
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
25
use core_reportbuilder\local\helpers\format;
26
use core_reportbuilder\local\report\{column, filter};
27
use stdClass;
28
 
29
/**
30
 * Competency framework entity
31
 *
32
 * @package     core_competency
33
 * @copyright   2024 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class framework extends base {
37
 
38
    /**
39
     * Database tables that this entity uses
40
     *
41
     * @return string[]
42
     */
43
    protected function get_default_tables(): array {
44
        return [
45
            'competency_framework',
46
            'context',
47
        ];
48
    }
49
 
50
    /**
51
     * The default title for this entity
52
     *
53
     * @return lang_string
54
     */
55
    protected function get_default_entity_title(): lang_string {
56
        return new lang_string('competencyframework', 'core_competency');
57
    }
58
 
59
    /**
60
     * Initialise the entity
61
     *
62
     * @return base
63
     */
64
    public function initialise(): base {
65
        $columns = $this->get_all_columns();
66
        foreach ($columns as $column) {
67
            $this->add_column($column);
68
        }
69
 
70
        // All the filters defined by the entity can also be used as conditions.
71
        $filters = $this->get_all_filters();
72
        foreach ($filters as $filter) {
73
            $this
74
                ->add_filter($filter)
75
                ->add_condition($filter);
76
        }
77
 
78
        return $this;
79
    }
80
 
81
    /**
82
     * Returns list of all available columns
83
     *
84
     * @return column[]
85
     */
86
    protected function get_all_columns(): array {
87
        $frameworkalias = $this->get_table_alias('competency_framework');
88
        $contextalias = $this->get_table_alias('context');
89
 
90
        // Name.
91
        $columns[] = (new column(
92
            'name',
93
            new lang_string('name'),
94
            $this->get_entity_name(),
95
        ))
96
            ->add_joins($this->get_joins())
97
            ->add_field("{$frameworkalias}.shortname")
98
            ->set_is_sortable(true);
99
 
100
        // Description.
101
        $columns[] = (new column(
102
            'description',
103
            new lang_string('description'),
104
            $this->get_entity_name(),
105
        ))
106
            ->add_joins($this->get_joins())
107
            ->add_join($this->get_context_join())
108
            ->set_type(column::TYPE_LONGTEXT)
109
            ->add_fields("{$frameworkalias}.description, {$frameworkalias}.descriptionformat")
110
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
111
            ->set_is_sortable(true)
112
            ->add_callback(static function(?string $description, stdClass $framework): string {
113
                if ($description === null) {
114
                    return '';
115
                }
116
 
117
                context_helper::preload_from_record(clone $framework);
118
                $context = context::instance_by_id($framework->ctxid);
119
 
120
                return format_text($description, $framework->descriptionformat, ['context' => $context->id]);
121
            });
122
 
123
        // ID number.
124
        $columns[] = (new column(
125
            'idnumber',
126
            new lang_string('idnumber'),
127
            $this->get_entity_name(),
128
        ))
129
            ->add_joins($this->get_joins())
130
            ->add_field("{$frameworkalias}.idnumber")
131
            ->set_is_sortable(true);
132
 
133
        // Scale.
134
        $columns[] = (new column(
135
            'scale',
136
            new lang_string('scale'),
137
            $this->get_entity_name(),
138
        ))
139
            ->add_joins($this->get_joins())
140
            ->add_field("{$frameworkalias}.scaleid")
141
            ->add_callback(static function(?string $scaleid): string {
142
                $scales = get_scales_menu();
143
                return (string) ($scales[(int) $scaleid] ?? $scaleid);
144
            });
145
 
146
        // Visible.
147
        $columns[] = (new column(
148
            'visible',
149
            new lang_string('visible'),
150
            $this->get_entity_name(),
151
        ))
152
            ->add_joins($this->get_joins())
153
            ->set_type(column::TYPE_BOOLEAN)
154
            ->add_field("{$frameworkalias}.visible")
155
            ->set_is_sortable(true)
156
            ->add_callback([format::class, 'boolean_as_text']);
157
 
158
        // Time created.
159
        $columns[] = (new column(
160
            'timecreated',
161
            new lang_string('timecreated', 'core_reportbuilder'),
162
            $this->get_entity_name(),
163
        ))
164
            ->add_joins($this->get_joins())
165
            ->set_type(column::TYPE_TIMESTAMP)
166
            ->add_field("{$frameworkalias}.timecreated")
167
            ->set_is_sortable(true)
168
            ->add_callback([format::class, 'userdate']);
169
 
170
        // Time modified.
171
        $columns[] = (new column(
172
            'timemodified',
173
            new lang_string('timemodified', 'core_reportbuilder'),
174
            $this->get_entity_name(),
175
        ))
176
            ->add_joins($this->get_joins())
177
            ->set_type(column::TYPE_TIMESTAMP)
178
            ->add_field("{$frameworkalias}.timemodified")
179
            ->set_is_sortable(true)
180
            ->add_callback([format::class, 'userdate']);
181
 
182
        return $columns;
183
    }
184
 
185
    /**
186
     * Return list of all available filters
187
     *
188
     * @return filter[]
189
     */
190
    protected function get_all_filters(): array {
191
        $frameworkalias = $this->get_table_alias('competency_framework');
192
 
193
        // Name.
194
        $filters[] = (new filter(
195
            text::class,
196
            'name',
197
            new lang_string('name'),
198
            $this->get_entity_name(),
199
            "{$frameworkalias}.shortname",
200
        ))
201
            ->add_joins($this->get_joins());
202
 
203
        // ID number.
204
        $filters[] = (new filter(
205
            text::class,
206
            'idnumber',
207
            new lang_string('idnumber'),
208
            $this->get_entity_name(),
209
            "{$frameworkalias}.idnumber",
210
        ))
211
            ->add_joins($this->get_joins());
212
 
213
        // Scale.
214
        $filters[] = (new filter(
215
            select::class,
216
            'scale',
217
            new lang_string('scale'),
218
            $this->get_entity_name(),
219
            "{$frameworkalias}.scaleid",
220
        ))
221
            ->add_joins($this->get_joins())
222
            ->set_options_callback('get_scales_menu');
223
 
224
        // Visible.
225
        $filters[] = (new filter(
226
            boolean_select::class,
227
            'visible',
228
            new lang_string('visible'),
229
            $this->get_entity_name(),
230
            "{$frameworkalias}.visible",
231
        ))
232
            ->add_joins($this->get_joins());
233
 
234
        // Time created.
235
        $filters[] = (new filter(
236
            date::class,
237
            'timecreated',
238
            new lang_string('timecreated', 'core_reportbuilder'),
239
            $this->get_entity_name(),
240
            "{$frameworkalias}.timecreated",
241
        ))
242
            ->add_joins($this->get_joins());
243
 
244
        // Time modified.
245
        $filters[] = (new filter(
246
            date::class,
247
            'timemodified',
248
            new lang_string('timemodified', 'core_reportbuilder'),
249
            $this->get_entity_name(),
250
            "{$frameworkalias}.timemodified",
251
        ))
252
            ->add_joins($this->get_joins());
253
 
254
        return $filters;
255
    }
256
 
257
    /**
258
     * Return context join
259
     *
260
     * @return string
261
     */
262
    private function get_context_join(): string {
263
 
264
        // If the context table is already joined, we don't need to do that again.
265
        if ($this->has_table_join_alias('context')) {
266
            return '';
267
        }
268
 
269
        $frameworkalias = $this->get_table_alias('competency_framework');
270
        $contextalias = $this->get_table_alias('context');
271
 
272
        return "LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$frameworkalias}.contextid";
273
    }
274
}