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 core_collator;
22
use core_tag_area;
23
use lang_string;
24
use stdClass;
25
use core_reportbuilder\local\entities\base;
26
use core_reportbuilder\local\filters\{date, select};
27
use core_reportbuilder\local\helpers\format;
28
use core_reportbuilder\local\report\{column, filter};
29
 
30
/**
31
 * Tag instance entity
32
 *
33
 * @package     core_tag
34
 * @copyright   2022 Paul Holden <paulh@moodle.com>
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class instance extends base {
38
 
39
    /**
40
     * Database tables that this entity uses
41
     *
42
     * @return string[]
43
     */
44
    protected function get_default_tables(): array {
45
        return [
46
            'tag_instance',
1441 ariadna 47
        ];
48
    }
49
 
50
    /**
51
     * Database tables that this entity no longer uses
52
     *
53
     * @return string[]
54
     */
55
    protected function get_deprecated_tables(): array {
56
        return [
1 efrain 57
            'context',
58
        ];
59
    }
60
 
61
    /**
62
     * The default title for this entity
63
     *
64
     * @return lang_string
65
     */
66
    protected function get_default_entity_title(): lang_string {
67
        return new lang_string('taginstance', 'core_tag');
68
    }
69
 
70
    /**
71
     * Initialise the entity
72
     *
73
     * @return base
74
     */
75
    public function initialise(): base {
76
        $columns = $this->get_all_columns();
77
        foreach ($columns as $column) {
78
            $this->add_column($column);
79
        }
80
 
81
        // All the filters defined by the entity can also be used as conditions.
82
        $filters = $this->get_all_filters();
83
        foreach ($filters as $filter) {
84
            $this
85
                ->add_filter($filter)
86
                ->add_condition($filter);
87
        }
88
 
89
        return $this;
90
    }
91
 
92
    /**
93
     * Returns list of all available columns
94
     *
95
     * @return column[]
96
     */
97
    protected function get_all_columns(): array {
98
        $instancealias = $this->get_table_alias('tag_instance');
99
 
100
        // Area.
101
        $columns[] = (new column(
102
            'area',
103
            new lang_string('tagarea', 'core_tag'),
104
            $this->get_entity_name()
105
 
106
        ))
107
            ->add_joins($this->get_joins())
108
            ->set_type(column::TYPE_TEXT)
109
            ->add_fields("{$instancealias}.component, {$instancealias}.itemtype")
110
            ->set_is_sortable(true, ["{$instancealias}.component", "{$instancealias}.itemtype"])
111
            ->add_callback(static function($component, stdClass $area): string {
112
                if ($component === null) {
113
                    return '';
114
                }
115
                return (string) core_tag_area::display_name($area->component, $area->itemtype);
116
            });
117
 
118
        // Component.
119
        $columns[] = (new column(
120
            'component',
121
            new lang_string('component', 'core_tag'),
122
            $this->get_entity_name()
123
        ))
124
            ->add_joins($this->get_joins())
125
            ->set_type(column::TYPE_TEXT)
126
            ->add_fields("{$instancealias}.component")
127
            ->set_is_sortable(true);
128
 
129
        // Item type.
130
        $columns[] = (new column(
131
            'itemtype',
132
            new lang_string('itemtype', 'core_tag'),
133
            $this->get_entity_name()
134
        ))
135
            ->add_joins($this->get_joins())
136
            ->set_type(column::TYPE_TEXT)
137
            ->add_fields("{$instancealias}.itemtype")
138
            ->set_is_sortable(true);
139
 
140
        // Item ID.
141
        $columns[] = (new column(
142
            'itemid',
143
            new lang_string('itemid', 'core_tag'),
144
            $this->get_entity_name()
145
        ))
146
            ->add_joins($this->get_joins())
147
            ->add_fields("{$instancealias}.itemid")
1441 ariadna 148
            ->set_is_sortable(true);
1 efrain 149
 
150
        // Time created.
151
        $columns[] = (new column(
152
            'timecreated',
153
            new lang_string('timecreated', 'core_reportbuilder'),
154
            $this->get_entity_name()
155
        ))
156
            ->add_joins($this->get_joins())
157
            ->set_type(column::TYPE_TIMESTAMP)
158
            ->add_fields("{$instancealias}.timecreated")
159
            ->set_is_sortable(true)
160
            ->add_callback([format::class, 'userdate']);
161
 
162
        // Time modified.
163
        $columns[] = (new column(
164
            'timemodified',
165
            new lang_string('timemodified', 'core_reportbuilder'),
166
            $this->get_entity_name()
167
        ))
168
            ->add_joins($this->get_joins())
169
            ->set_type(column::TYPE_TIMESTAMP)
170
            ->add_fields("{$instancealias}.timemodified")
171
            ->set_is_sortable(true)
172
            ->add_callback([format::class, 'userdate']);
173
 
174
        return $columns;
175
    }
176
 
177
    /**
178
     * Return list of all available filters
179
     *
180
     * @return filter[]
181
     */
182
    protected function get_all_filters(): array {
183
        global $DB;
184
 
185
        $instancealias = $this->get_table_alias('tag_instance');
186
 
187
        // Area.
188
        $filters[] = (new filter(
189
            select::class,
190
            'area',
191
            new lang_string('tagarea', 'core_tag'),
192
            $this->get_entity_name(),
193
            $DB->sql_concat("{$instancealias}.component", "'/'", "{$instancealias}.itemtype")
194
        ))
195
            ->add_joins($this->get_joins())
196
            ->set_options_callback(static function(): array {
197
                $options = [];
198
                foreach (core_tag_area::get_areas() as $areas) {
199
                    foreach ($areas as $area) {
200
                        $options["{$area->component}/{$area->itemtype}"] = core_tag_area::display_name(
201
                            $area->component, $area->itemtype);
202
                    }
203
                }
204
 
205
                core_collator::asort($options);
206
                return $options;
207
            });
208
 
209
        // Time created.
210
        $filters[] = (new filter(
211
            date::class,
212
            'timecreated',
213
            new lang_string('timecreated', 'core_reportbuilder'),
214
            $this->get_entity_name(),
215
            "{$instancealias}.timecreated"
216
        ))
217
            ->add_joins($this->get_joins())
218
            ->set_limited_operators([
219
                date::DATE_ANY,
220
                date::DATE_CURRENT,
221
                date::DATE_LAST,
222
                date::DATE_RANGE,
223
            ]);
224
 
225
        // Time modified.
226
        $filters[] = (new filter(
227
            date::class,
228
            'timemodified',
229
            new lang_string('timemodified', 'core_reportbuilder'),
230
            $this->get_entity_name(),
231
            "{$instancealias}.timemodified"
232
        ))
233
            ->add_joins($this->get_joins())
234
            ->set_limited_operators([
235
                date::DATE_ANY,
236
                date::DATE_CURRENT,
237
                date::DATE_LAST,
238
                date::DATE_RANGE,
239
            ]);
240
 
241
        return $filters;
242
    }
243
}