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 core_tag_collection;
22
use lang_string;
23
use stdClass;
24
use core_reportbuilder\local\entities\base;
25
use core_reportbuilder\local\filters\{boolean_select, select};
26
use core_reportbuilder\local\helpers\format;
27
use core_reportbuilder\local\report\{column, filter};
28
 
29
/**
30
 * Tag collection entity
31
 *
32
 * @package     core_tag
33
 * @copyright   2022 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class collection 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
            'tag_coll',
46
        ];
47
    }
48
 
49
    /**
50
     * The default title for this entity
51
     *
52
     * @return lang_string
53
     */
54
    protected function get_default_entity_title(): lang_string {
55
        return new lang_string('tagcollection', 'core_tag');
56
    }
57
 
58
    /**
59
     * Initialise the entity
60
     *
61
     * @return base
62
     */
63
    public function initialise(): base {
64
        $columns = $this->get_all_columns();
65
        foreach ($columns as $column) {
66
            $this->add_column($column);
67
        }
68
 
69
        // All the filters defined by the entity can also be used as conditions.
70
        $filters = $this->get_all_filters();
71
        foreach ($filters as $filter) {
72
            $this
73
                ->add_filter($filter)
74
                ->add_condition($filter);
75
        }
76
 
77
        return $this;
78
    }
79
 
80
    /**
81
     * Returns list of all available columns
82
     *
83
     * @return column[]
84
     */
85
    protected function get_all_columns(): array {
86
        $collectionalias = $this->get_table_alias('tag_coll');
87
 
88
        // Name.
89
        $columns[] = (new column(
90
            'name',
91
            new lang_string('name'),
92
            $this->get_entity_name()
93
        ))
94
            ->add_joins($this->get_joins())
95
            ->set_type(column::TYPE_TEXT)
96
            ->add_fields("{$collectionalias}.name, {$collectionalias}.component, {$collectionalias}.isdefault,
97
                {$collectionalias}.id")
98
            ->set_is_sortable(true)
99
            ->add_callback(static function(?string $name, stdClass $collection): string {
100
                return core_tag_collection::display_name($collection);
101
            });
102
 
103
        // Default.
104
        $columns[] = (new column(
105
            'default',
106
            new lang_string('defautltagcoll', 'core_tag'),
107
            $this->get_entity_name()
108
        ))
109
            ->add_joins($this->get_joins())
110
            ->set_type(column::TYPE_BOOLEAN)
111
            ->add_fields("{$collectionalias}.isdefault")
112
            ->set_is_sortable(true)
113
            ->add_callback([format::class, 'boolean_as_text']);
114
 
115
        // Component.
116
        $columns[] = (new column(
117
            'component',
118
            new lang_string('component', 'core_tag'),
119
            $this->get_entity_name()
120
        ))
121
            ->add_joins($this->get_joins())
122
            ->set_type(column::TYPE_TEXT)
123
            ->add_fields("{$collectionalias}.component")
124
            ->set_is_sortable(true);
125
 
126
        // Searchable.
127
        $columns[] = (new column(
128
            'searchable',
129
            new lang_string('searchable', 'core_tag'),
130
            $this->get_entity_name()
131
        ))
132
            ->add_joins($this->get_joins())
133
            ->set_type(column::TYPE_BOOLEAN)
134
            ->add_fields("{$collectionalias}.searchable")
135
            ->set_is_sortable(true)
136
            ->add_callback([format::class, 'boolean_as_text']);
137
 
138
        // Custom URL.
139
        $columns[] = (new column(
140
            'customurl',
141
            new lang_string('url'),
142
            $this->get_entity_name()
143
        ))
144
            ->add_joins($this->get_joins())
145
            ->set_type(column::TYPE_TEXT)
146
            ->add_fields("{$collectionalias}.customurl")
147
            ->set_is_sortable(true);
148
 
149
        return $columns;
150
    }
151
 
152
    /**
153
     * Return list of all available filters
154
     *
155
     * @return filter[]
156
     */
157
    protected function get_all_filters(): array {
158
        $collectionalias = $this->get_table_alias('tag_coll');
159
 
160
        // Name.
161
        $filters[] = (new filter(
162
            select::class,
163
            'name',
164
            new lang_string('name'),
165
            $this->get_entity_name(),
166
            "{$collectionalias}.id"
167
        ))
168
            ->add_joins($this->get_joins())
169
            ->set_options_callback(static function(): array {
170
                global $DB;
171
 
172
                $collections = $DB->get_records('tag_coll', [], 'sortorder', 'id, name, component, isdefault');
173
                return array_map(static function(stdClass $collection): string {
174
                    return core_tag_collection::display_name($collection);
175
                }, $collections);
176
            });
177
 
178
        // Default.
179
        $filters[] = (new filter(
180
            boolean_select::class,
181
            'default',
182
            new lang_string('defautltagcoll', 'core_tag'),
183
            $this->get_entity_name(),
184
            "{$collectionalias}.isdefault"
185
        ))
186
            ->add_joins($this->get_joins());
187
 
188
        // Searchable.
189
        $filters[] = (new filter(
190
            boolean_select::class,
191
            'searchable',
192
            new lang_string('searchable', 'core_tag'),
193
            $this->get_entity_name(),
194
            "{$collectionalias}.searchable"
195
        ))
196
            ->add_joins($this->get_joins());
197
 
198
        return $filters;
199
    }
200
}