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\{date, text};
25
use core_reportbuilder\local\helpers\{database, format};
26
use core_reportbuilder\local\report\{column, filter};
27
use stdClass;
28
 
29
/**
30
 * Competency 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 competency 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',
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('competency', '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
        $contextalias = $this->get_table_alias('context');
88
        $competencyalias = $this->get_table_alias('competency');
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("{$competencyalias}.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_joins($this->get_context_joins())
108
            ->set_type(column::TYPE_LONGTEXT)
109
            ->add_fields("{$competencyalias}.description, {$competencyalias}.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 $competency): string {
113
                if ($description === null) {
114
                    return '';
115
                }
116
 
117
                context_helper::preload_from_record(clone $competency);
118
                $context = context::instance_by_id($competency->ctxid);
119
 
120
                return format_text($description, $competency->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("{$competencyalias}.idnumber")
131
            ->set_is_sortable(true);
132
 
133
        // Time created.
134
        $columns[] = (new column(
135
            'timecreated',
136
            new lang_string('timecreated', 'core_reportbuilder'),
137
            $this->get_entity_name(),
138
        ))
139
            ->add_joins($this->get_joins())
140
            ->set_type(column::TYPE_TIMESTAMP)
141
            ->add_field("{$competencyalias}.timecreated")
142
            ->set_is_sortable(true)
143
            ->add_callback([format::class, 'userdate']);
144
 
145
        // Time modified.
146
        $columns[] = (new column(
147
            'timemodified',
148
            new lang_string('timemodified', 'core_reportbuilder'),
149
            $this->get_entity_name(),
150
        ))
151
            ->add_joins($this->get_joins())
152
            ->set_type(column::TYPE_TIMESTAMP)
153
            ->add_field("{$competencyalias}.timemodified")
154
            ->set_is_sortable(true)
155
            ->add_callback([format::class, 'userdate']);
156
 
157
        return $columns;
158
    }
159
 
160
    /**
161
     * Return list of all available filters
162
     *
163
     * @return filter[]
164
     */
165
    protected function get_all_filters(): array {
166
        $competencyalias = $this->get_table_alias('competency');
167
 
168
        // Name.
169
        $filters[] = (new filter(
170
            text::class,
171
            'name',
172
            new lang_string('name'),
173
            $this->get_entity_name(),
174
            "{$competencyalias}.shortname",
175
        ))
176
            ->add_joins($this->get_joins());
177
 
178
        // ID number.
179
        $filters[] = (new filter(
180
            text::class,
181
            'idnumber',
182
            new lang_string('idnumber'),
183
            $this->get_entity_name(),
184
            "{$competencyalias}.idnumber",
185
        ))
186
            ->add_joins($this->get_joins());
187
 
188
        // Time created.
189
        $filters[] = (new filter(
190
            date::class,
191
            'timecreated',
192
            new lang_string('timecreated', 'core_reportbuilder'),
193
            $this->get_entity_name(),
194
            "{$competencyalias}.timecreated",
195
        ))
196
            ->add_joins($this->get_joins());
197
 
198
        // Time modified.
199
        $filters[] = (new filter(
200
            date::class,
201
            'timemodified',
202
            new lang_string('timemodified', 'core_reportbuilder'),
203
            $this->get_entity_name(),
204
            "{$competencyalias}.timemodified",
205
        ))
206
            ->add_joins($this->get_joins());
207
 
208
        return $filters;
209
    }
210
 
211
    /**
212
     * Return context joins
213
     *
214
     * @return string[]
215
     */
216
    private function get_context_joins(): array {
217
 
218
        // If the context table is already joined, we don't need to do that again.
219
        if ($this->has_table_join_alias('context')) {
220
            return [];
221
        }
222
 
223
        $frameworkalias = database::generate_alias();
224
        $competencyalias = $this->get_table_alias('competency');
225
        $contextalias = $this->get_table_alias('context');
226
 
227
        return [
228
            "LEFT JOIN {competency_framework} {$frameworkalias} ON {$frameworkalias}.id = {$competencyalias}.competencyframeworkid",
229
            "LEFT JOIN {context} {$contextalias} ON {$contextalias}.id = {$frameworkalias}.contextid",
230
        ];
231
    }
232
}