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\reportbuilder\local\entities;
20
 
21
use core\context_helper;
22
use core_reportbuilder\local\entities\base;
23
use core_reportbuilder\local\filters\{select, text};
24
use core_reportbuilder\local\report\{column, filter};
25
use html_writer;
26
use lang_string;
27
use stdClass;
28
 
29
/**
30
 * Context entity
31
 *
32
 * @package     core
33
 * @copyright   2023 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class context 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
            'context',
46
        ];
47
    }
48
 
49
    /**
50
     * The default title for this entity in the list of columns/conditions/filters in the report builder
51
     *
52
     * @return lang_string
53
     */
54
    protected function get_default_entity_title(): lang_string {
55
        return new lang_string('context');
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
        global $DB;
87
 
88
        $contextalias = $this->get_table_alias('context');
89
 
90
        // Name.
91
        $columns[] = (new column(
92
            'name',
93
            new lang_string('contextname'),
94
            $this->get_entity_name()
95
        ))
96
            ->add_joins($this->get_joins())
97
            ->set_type(column::TYPE_TEXT)
98
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
99
            // Sorting may not order alphabetically, but will at least group contexts together.
100
            ->set_is_sortable(true)
101
            ->add_callback(static function($contextid, stdClass $context): string {
102
                if ($contextid === null) {
103
                    return '';
104
                }
105
 
106
                context_helper::preload_from_record($context);
107
                return context_helper::instance_by_id($contextid)->get_context_name();
108
            });
109
 
110
        // Link.
111
        $columns[] = (new column(
112
            'link',
113
            new lang_string('contexturl'),
114
            $this->get_entity_name()
115
        ))
116
            ->add_joins($this->get_joins())
117
            ->set_type(column::TYPE_TEXT)
118
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
119
            // Sorting may not order alphabetically, but will at least group contexts together.
120
            ->set_is_sortable(true)
121
            ->add_callback(static function($contextid, stdClass $context): string {
122
                if ($contextid === null) {
123
                    return '';
124
                }
125
 
126
                context_helper::preload_from_record($context);
127
                $context = context_helper::instance_by_id($contextid);
128
 
129
                return html_writer::link($context->get_url(), $context->get_context_name());
130
            });
131
 
132
        // Level.
133
        $columns[] = (new column(
134
            'level',
135
            new lang_string('contextlevel'),
136
            $this->get_entity_name()
137
        ))
138
            ->add_joins($this->get_joins())
139
            ->set_type(column::TYPE_INTEGER)
140
            ->add_fields("{$contextalias}.contextlevel")
141
            ->set_is_sortable(true)
142
            // It doesn't make sense to offer integer aggregation methods for this column.
143
            ->set_disabled_aggregation(['avg', 'max', 'min', 'sum'])
144
            ->add_callback(static function(?int $level): string {
145
                if ($level === null) {
146
                    return '';
147
                }
148
 
149
                return context_helper::get_level_name($level);
150
            });
151
 
152
        // Path.
153
        $columns[] = (new column(
154
            'path',
155
            new lang_string('path'),
156
            $this->get_entity_name()
157
        ))
158
            ->add_joins($this->get_joins())
159
            ->set_type(column::TYPE_TEXT)
160
            ->add_field("{$contextalias}.path")
161
            ->set_is_sortable(true);
162
 
163
        // Parent (note we select the parent path in SQL, so that aggregation/grouping is on the parent data itself).
164
        $columns[] = (new column(
165
            'parent',
166
            new lang_string('contextparent'),
167
            $this->get_entity_name()
168
        ))
169
            ->add_joins($this->get_joins())
170
            ->set_type(column::TYPE_TEXT)
171
            // The "path" column looks like "/1/2/3", for context ID 3. In order to select/group by the parent context, we
172
            // concatenate a trailing slash (to prevent partial matches, e.g. "/1/2/31"), then replace "/3/" with empty string.
173
            ->add_field("
174
                REPLACE(
175
                    " . $DB->sql_concat("{$contextalias}.path", "'/'") . ",
176
                    " . $DB->sql_concat("'/'", $DB->sql_cast_to_char("{$contextalias}.id"), "'/'") . ",
177
                    ''
178
                )", 'parent'
179
            )
180
            // Sorting may not order alphabetically, but will at least group contexts together.
181
            ->set_is_sortable(true)
182
            ->add_callback(static function (?string $parent): string {
183
 
184
                // System level (no parent) or null.
185
                if ($parent === '' || $parent === null) {
186
                    return '';
187
                }
188
 
189
                $contextids = explode('/', $parent);
190
                $contextid = (int) array_pop($contextids);
191
 
192
                return context_helper::instance_by_id($contextid)->get_context_name();
193
            });
194
 
195
        return $columns;
196
    }
197
 
198
    /**
199
     * Return list of all available filters
200
     *
201
     * @return filter[]
202
     */
203
    protected function get_all_filters(): array {
204
        $contextalias = $this->get_table_alias('context');
205
 
206
        // Level.
207
        $filters[] = (new filter(
208
            select::class,
209
            'level',
210
            new lang_string('contextlevel'),
211
            $this->get_entity_name(),
212
            "{$contextalias}.contextlevel"
213
        ))
214
            ->add_joins($this->get_joins())
215
            ->set_options_callback(static function(): array {
216
                $levels = context_helper::get_all_levels();
217
 
218
                return array_map(static function(string $levelclass): string {
219
                    return $levelclass::get_level_name();
220
                }, $levels);
221
            });
222
 
223
        // Path.
224
        $filters[] = (new filter(
225
            text::class,
226
            'path',
227
            new lang_string('path'),
228
            $this->get_entity_name(),
229
            "{$contextalias}.path"
230
        ))
231
            ->add_joins($this->get_joins());
232
 
233
        return $filters;
234
    }
235
}