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 block_dedication\local\filters\select;
22
use core_reportbuilder\local\entities\base;
23
use core_reportbuilder\local\report\{column, filter};
24
use lang_string;
25
 
26
/**
27
 * Course group entity implementation - copied from 4.1 as we need to support 4.0 in this plugin.
28
 *
29
 * @package     block_dedication
30
 * @copyright   2022 Michael Kotlyar <michael.kotlyar@catalyst.net.nz>
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class groups extends base {
34
 
35
    /**
36
     * Database tables that this entity uses and their default aliases
37
     *
38
     * @return array
39
     */
40
    protected function get_default_table_aliases(): array {
41
        return ['groups' => 'g'];
42
    }
43
 
44
    /**
45
     * The default title for this entity in the list of columns/conditions/filters in the report builder
46
     *
47
     * @return lang_string
48
     */
49
    protected function get_default_entity_title(): lang_string {
50
        return new lang_string('groupentity', 'block_dedication');
51
    }
52
 
53
    /**
54
     * Initialise the entity
55
     *
56
     * @return base
57
     */
58
    public function initialise(): base {
59
        foreach ($this->get_all_columns() as $column) {
60
            $this->add_column($column);
61
        }
62
 
63
        // All the filters defined by the entity can also be used as conditions.
64
        foreach ($this->get_all_filters() as $filter) {
65
            $this
66
                ->add_filter($filter)
67
                ->add_condition($filter);
68
        }
69
 
70
        return $this;
71
    }
72
 
73
    /**
74
     * Add extra columns to course report.
75
     * @return array
76
     * @throws \coding_exception
77
     */
78
    protected function get_all_columns(): array {
79
        $groups = $this->get_table_alias('groups');
80
 
81
        $columns[] = (new column(
82
            'groupnames',
83
            new lang_string('group', 'block_dedication'),
84
            $this->get_entity_name()
85
        ))
86
            ->add_joins($this->get_joins())
87
            ->set_type(column::TYPE_TEXT)
88
            ->add_fields("{$groups}.groupnames");
89
 
90
        return $columns;
91
    }
92
 
93
    /**
94
     * Return list of all available filters
95
     *
96
     * @return filter[]
97
     */
98
    protected function get_all_filters(): array {
99
        $groupsalias = $this->get_table_alias('groups');
100
 
101
        // Module name filter.
102
        $filters[] = (new filter(
103
            select::class,
104
            'group',
105
            new lang_string('group', 'block_dedication'),
106
            $this->get_entity_name(),
107
            "$groupsalias.groupids"
108
        ))
109
            ->add_joins($this->get_joins())
110
            ->set_options_callback(static function(): array {
111
                global $PAGE, $USER;
112
                if ($PAGE->course->groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $PAGE->context)) {
113
                    $groups = groups_get_all_groups($PAGE->course->id, 0, $PAGE->course->defaultgroupingid);
114
                } else {
115
                    $groups = groups_get_all_groups($PAGE->course->id, $USER->id, $PAGE->course->defaultgroupingid);
116
                }
117
                $grouplist = [];
118
                foreach ($groups as $group) {
119
                    $grouplist[$group->id] = $group->name;
120
                }
121
                return $grouplist;
122
            });
123
 
124
        return $filters;
125
    }
126
}