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 block_dedication\local\entities;
20
 
21
use context_course;
22
use core_reportbuilder\local\entities\base;
23
use core_reportbuilder\local\helpers\database;
24
use core_reportbuilder\local\helpers\format;
25
use core_reportbuilder\local\report\column;
26
use core_reportbuilder\local\report\filter;
27
use core_user\output\status_field;
28
use lang_string;
29
use stdClass;
30
 
31
/**
32
 * Course enrolment entity implementation - copied from 4.1 as we need to support 4.0 in this plugin.
33
 *
34
 * @package     block_dedication
35
 * @copyright   2022 David Matamoros <davidmc@moodle.com>
36
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class enrolment extends base {
39
 
40
    /**
41
     * Database tables that this entity uses and their default aliases
42
     *
43
     * @return array
44
     */
45
    protected function get_default_table_aliases(): array {
46
        return ['user_enrolments' => 'ue', 'enrol' => 'e'];
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('enrolmententity', 'block_dedication');
56
    }
57
 
58
    /**
59
     * Initialise the entity
60
     *
61
     * @return base
62
     */
63
    public function initialise(): base {
64
        foreach ($this->get_all_columns() as $column) {
65
            $this->add_column($column);
66
        }
67
 
68
        // All the filters defined by the entity can also be used as conditions.
69
        foreach ($this->get_all_filters() as $filter) {
70
            $this
71
                ->add_filter($filter)
72
                ->add_condition($filter);
73
        }
74
 
75
        return $this;
76
    }
77
 
78
    /**
79
     * Returns list of all available columns
80
     *
81
     * @return column[]
82
     */
83
    protected function get_all_columns(): array {
84
        $userenrolments = $this->get_table_alias('user_enrolments');
85
        $enrol = $this->get_table_alias('enrol');
86
 
87
        // Enrolment method column.
88
        $columns[] = (new column(
89
            'method',
90
            new lang_string('enrolmentmethod', 'block_dedication'),
91
            $this->get_entity_name()
92
        ))
93
            ->add_joins($this->get_joins())
94
            ->set_type(column::TYPE_TEXT)
95
            ->add_fields("{$enrol}.enrol, {$enrol}.id")
96
            ->set_is_sortable(true);
97
 
98
        // Enrolment time created.
99
        $columns[] = (new column(
100
            'timecreated',
101
            new lang_string('timecreated', 'moodle'),
102
            $this->get_entity_name()
103
        ))
104
            ->add_joins($this->get_joins())
105
            ->set_type(column::TYPE_TIMESTAMP)
106
            ->add_field("{$userenrolments}.timecreated")
107
            ->set_is_sortable(true)
108
            ->add_callback([format::class, 'userdate']);
109
 
110
        // Enrolment status.
111
        $columns[] = (new column(
112
            'status',
113
            new lang_string('status', 'moodle'),
114
            $this->get_entity_name()
115
        ))
116
            ->add_joins($this->get_joins())
117
            ->set_type(column::TYPE_TEXT)
118
            ->add_field($this->get_status_field_sql(), 'status')
119
            ->add_field("{$userenrolments}.userid")
120
            ->set_is_sortable(true);
121
 
122
        // Role method column.
123
        $ctx = database::generate_alias();
124
        $ra = database::generate_alias();
125
        $r = database::generate_alias();
126
        $columns[] = (new column(
127
            'role',
128
            new lang_string('role', 'moodle'),
129
            $this->get_entity_name()
130
        ))
131
            ->add_joins($this->get_joins())
132
            ->add_join("LEFT JOIN {context} {$ctx}
133
                ON {$ctx}.instanceid = {$enrol}.courseid AND {$ctx}.contextlevel = " . CONTEXT_COURSE)
134
            ->add_join("LEFT JOIN {role_assignments} {$ra}
135
                ON {$ra}.contextid = {$ctx}.id AND {$ra}.userid = {$userenrolments}.userid")
136
            ->add_join("LEFT JOIN {role} {$r} ON {$r}.id = {$ra}.roleid")
137
            ->set_type(column::TYPE_TEXT)
138
            ->add_fields("{$r}.id, {$r}.name, {$r}.shortname, {$ctx}.instanceid")
139
            ->set_is_sortable(true, ["{$r}.shortname"])
140
            ->add_callback(static function(?string $value, stdClass $row): string {
141
                if (!$row->id) {
142
                    return '';
143
                }
144
                $context = context_course::instance($row->instanceid);
145
                return role_get_name($row, $context, ROLENAME_ALIAS);
146
            });
147
 
148
        return $columns;
149
    }
150
 
151
    /**
152
     * Generate SQL snippet suitable for returning enrolment status field
153
     *
154
     * @return string
155
     */
156
    private function get_status_field_sql(): string {
157
        $time = time();
158
        $userenrolments = $this->get_table_alias('user_enrolments');
159
        $enrol = $this->get_table_alias('enrol');
160
 
161
        return "
162
            CASE WHEN {$userenrolments}.status = " . ENROL_USER_ACTIVE . "
163
                 THEN CASE WHEN ({$userenrolments}.timestart > {$time})
164
                             OR ({$userenrolments}.timeend > 0 AND {$userenrolments}.timeend < {$time})
165
                             OR ({$enrol}.status = " . ENROL_INSTANCE_DISABLED . ")
166
                           THEN " . status_field::STATUS_NOT_CURRENT . "
167
                           ELSE " . status_field::STATUS_ACTIVE . "
168
                      END
169
                 ELSE " . status_field::STATUS_SUSPENDED . "
170
            END";
171
    }
172
 
173
    /**
174
     * Return list of all available filters
175
     *
176
     * @return filter[]
177
     */
178
    protected function get_all_filters(): array {
179
        return [];
180
    }
181
}