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_role\reportbuilder\local\entities;
20
 
21
use context;
22
use context_helper;
23
use lang_string;
24
use stdClass;
25
use core_reportbuilder\local\entities\base;
26
use core_reportbuilder\local\filters\select;
27
use core_reportbuilder\local\report\{column, filter};
28
 
29
/**
30
 * Role entity
31
 *
32
 * @package     core_role
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 role 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
            'role',
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('role');
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
        global $DB;
88
 
89
        $contextalias = $this->get_table_alias('context');
90
        $rolealias = $this->get_table_alias('role');
91
 
92
        // Name column.
93
        $columns[] = (new column(
94
            'name',
95
            new lang_string('rolefullname', 'core_role'),
96
            $this->get_entity_name()
97
        ))
98
            ->add_joins($this->get_joins())
99
            ->set_type(column::TYPE_TEXT)
100
            ->add_fields("{$rolealias}.name, {$rolealias}.shortname, {$rolealias}.id, {$contextalias}.id AS contextid")
101
            ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
102
            // The sorting is on name, unless empty (determined by single space - thanks Oracle) then we use shortname.
103
            ->set_is_sortable(true, [
104
                "CASE WHEN " . $DB->sql_concat("{$rolealias}.name", "' '") . " = ' '
105
                      THEN {$rolealias}.shortname
106
                      ELSE {$rolealias}.name
107
                 END",
108
            ])
109
            ->set_callback(static function($name, stdClass $role): string {
110
                if ($name === null) {
111
                    return '';
112
                }
113
 
114
                context_helper::preload_from_record($role);
115
                $context = context::instance_by_id($role->contextid);
116
 
117
                return role_get_name($role, $context, ROLENAME_BOTH);
118
            });
119
 
120
        // Original name column.
121
        $columns[] = (new column(
122
            'originalname',
123
            new lang_string('roleoriginalname', 'core_role'),
124
            $this->get_entity_name()
125
        ))
126
            ->add_joins($this->get_joins())
127
            ->set_type(column::TYPE_TEXT)
128
            ->add_fields("{$rolealias}.name, {$rolealias}.shortname")
129
            // The sorting is on name, unless empty (determined by single space - thanks Oracle) then we use shortname.
130
            ->set_is_sortable(true, [
131
                "CASE WHEN " . $DB->sql_concat("{$rolealias}.name", "' '") . " = ' '
132
                      THEN {$rolealias}.shortname
133
                      ELSE {$rolealias}.name
134
                 END",
135
            ])
136
            ->set_callback(static function($name, stdClass $role): string {
137
                if ($name === null) {
138
                    return '';
139
                }
140
 
141
                return role_get_name($role, null, ROLENAME_ORIGINAL);
142
            });
143
 
144
        // Short name column.
145
        $columns[] = (new column(
146
            'shortname',
147
            new lang_string('roleshortname', 'core_role'),
148
            $this->get_entity_name()
149
        ))
150
            ->add_joins($this->get_joins())
151
            ->set_type(column::TYPE_TEXT)
152
            ->add_fields("{$rolealias}.shortname")
153
            ->set_is_sortable(true);
154
 
155
        // Description column.
156
        $descriptionfieldsql = "{$rolealias}.description";
157
        if ($DB->get_dbfamily() === 'oracle') {
158
            $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
159
        }
160
        $columns[] = (new column(
161
            'description',
162
            new lang_string('description'),
163
            $this->get_entity_name()
164
        ))
165
            ->add_joins($this->get_joins())
166
            ->set_type(column::TYPE_LONGTEXT)
167
            ->add_field($descriptionfieldsql, 'description')
168
            ->add_field("{$rolealias}.shortname")
169
            ->set_callback(static function($description, stdClass $role): string {
170
                if ($description === null) {
171
                    return '';
172
                }
173
 
174
                return role_get_description($role);
175
            });
176
 
177
        return $columns;
178
    }
179
 
180
    /**
181
     * Return list of all available filters
182
     *
183
     * @return filter[]
184
     */
185
    protected function get_all_filters(): array {
186
        $rolealias = $this->get_table_alias('role');
187
 
188
        // Name filter.
189
        $filters[] = (new filter(
190
            select::class,
191
            'name',
192
            new lang_string('rolefullname', 'core_role'),
193
            $this->get_entity_name(),
194
            "{$rolealias}.id"
195
        ))
196
            ->add_joins($this->get_joins())
197
            ->set_options_callback(static function(): array {
198
                return role_get_names(null, ROLENAME_ORIGINAL, true);
199
            });
200
 
201
        return $filters;
202
    }
203
}