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\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
            ->add_fields("{$contextalias}.contextlevel")
140
            ->set_is_sortable(true)
1441 ariadna 141
            ->add_callback(static function(?string $level): string {
1 efrain 142
                if ($level === null) {
143
                    return '';
144
                }
145
 
1441 ariadna 146
                return context_helper::get_level_name((int) $level);
1 efrain 147
            });
148
 
149
        // Path.
150
        $columns[] = (new column(
151
            'path',
152
            new lang_string('path'),
153
            $this->get_entity_name()
154
        ))
155
            ->add_joins($this->get_joins())
156
            ->set_type(column::TYPE_TEXT)
157
            ->add_field("{$contextalias}.path")
158
            ->set_is_sortable(true);
159
 
160
        // Parent (note we select the parent path in SQL, so that aggregation/grouping is on the parent data itself).
161
        $columns[] = (new column(
162
            'parent',
163
            new lang_string('contextparent'),
164
            $this->get_entity_name()
165
        ))
166
            ->add_joins($this->get_joins())
167
            ->set_type(column::TYPE_TEXT)
168
            // The "path" column looks like "/1/2/3", for context ID 3. In order to select/group by the parent context, we
169
            // concatenate a trailing slash (to prevent partial matches, e.g. "/1/2/31"), then replace "/3/" with empty string.
170
            ->add_field("
171
                REPLACE(
172
                    " . $DB->sql_concat("{$contextalias}.path", "'/'") . ",
173
                    " . $DB->sql_concat("'/'", $DB->sql_cast_to_char("{$contextalias}.id"), "'/'") . ",
174
                    ''
175
                )", 'parent'
176
            )
177
            // Sorting may not order alphabetically, but will at least group contexts together.
178
            ->set_is_sortable(true)
179
            ->add_callback(static function (?string $parent): string {
180
 
181
                // System level (no parent) or null.
182
                if ($parent === '' || $parent === null) {
183
                    return '';
184
                }
185
 
186
                $contextids = explode('/', $parent);
187
                $contextid = (int) array_pop($contextids);
188
 
189
                return context_helper::instance_by_id($contextid)->get_context_name();
190
            });
191
 
192
        return $columns;
193
    }
194
 
195
    /**
196
     * Return list of all available filters
197
     *
198
     * @return filter[]
199
     */
200
    protected function get_all_filters(): array {
201
        $contextalias = $this->get_table_alias('context');
202
 
203
        // Level.
204
        $filters[] = (new filter(
205
            select::class,
206
            'level',
207
            new lang_string('contextlevel'),
208
            $this->get_entity_name(),
209
            "{$contextalias}.contextlevel"
210
        ))
211
            ->add_joins($this->get_joins())
212
            ->set_options_callback(static function(): array {
213
                $levels = context_helper::get_all_levels();
214
 
215
                return array_map(static function(string $levelclass): string {
216
                    return $levelclass::get_level_name();
217
                }, $levels);
218
            });
219
 
220
        // Path.
221
        $filters[] = (new filter(
222
            text::class,
223
            'path',
224
            new lang_string('path'),
225
            $this->get_entity_name(),
226
            "{$contextalias}.path"
227
        ))
228
            ->add_joins($this->get_joins());
229
 
230
        return $filters;
231
    }
232
}