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_badges\reportbuilder\datasource;
20
 
21
use lang_string;
22
use core_reportbuilder\datasource;
23
use core_reportbuilder\local\entities\{course, user};
24
use core_reportbuilder\local\helpers\database;
25
use core_badges\reportbuilder\local\entities\{badge, badge_issued};
26
use core_cohort\reportbuilder\local\entities\cohort;
27
use core_tag\reportbuilder\local\entities\tag;
28
 
29
/**
30
 * User badges datasource
31
 *
32
 * @package     core_badges
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 users extends datasource {
37
 
38
    /**
39
     * Return user friendly name of the report source
40
     *
41
     * @return string
42
     */
43
    public static function get_name(): string {
44
        return get_string('userbadges', 'core_badges');
45
    }
46
 
47
    /**
48
     * Initialise report
49
     */
50
    protected function initialise(): void {
51
        global $CFG;
52
 
53
        $userentity = new user();
54
        $useralias = $userentity->get_table_alias('user');
55
 
56
        $this->set_main_table('user', $useralias);
57
        $this->add_entity($userentity);
58
 
59
        $paramguest = database::generate_param_name();
60
        $this->add_base_condition_sql("{$useralias}.id != :{$paramguest} AND {$useralias}.deleted = 0", [
61
            $paramguest => $CFG->siteguest,
62
        ]);
63
 
64
        // Join the badge issued entity to the user entity.
65
        $badgeissuedentity = new badge_issued();
66
        $badgeissuedalias = $badgeissuedentity->get_table_alias('badge_issued');
67
        $this->add_entity($badgeissuedentity
68
            ->add_join("LEFT JOIN {badge_issued} {$badgeissuedalias} ON {$badgeissuedalias}.userid = {$useralias}.id"));
69
 
70
        $badgeentity = new badge();
71
        $badgealias = $badgeentity->get_table_alias('badge');
72
        $this->add_entity($badgeentity
73
            ->add_joins($badgeissuedentity->get_joins())
74
            ->add_join("LEFT JOIN {badge} {$badgealias} ON {$badgealias}.id = {$badgeissuedalias}.badgeid"));
75
 
76
        // Join the tag entity.
77
        $tagentity = (new tag())
78
            ->set_table_alias('tag', $badgeentity->get_table_alias('tag'))
79
            ->set_entity_title(new lang_string('badgetags', 'core_badges'));
80
        $this->add_entity($tagentity
81
            ->add_joins($badgeentity->get_joins())
82
            ->add_joins($badgeentity->get_tag_joins()));
83
 
84
        // Join the course entity to the badge entity, coalescing courseid with the siteid for site badges.
85
        $courseentity = new course();
86
        $coursealias = $courseentity->get_table_alias('course');
87
        $this->add_entity($courseentity
88
            ->add_joins($badgeentity->get_joins())
89
            ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id =
90
                CASE WHEN {$badgealias}.id IS NULL THEN 0 ELSE COALESCE({$badgealias}.courseid, 1) END"));
91
 
92
        // Join the cohort entity.
93
        $cohortentity = new cohort();
94
        $cohortalias = $cohortentity->get_table_alias('cohort');
95
        $cohortmemberalias = database::generate_alias();
96
        $this->add_entity($cohortentity->add_joins([
97
            "LEFT JOIN {cohort_members} {$cohortmemberalias} ON {$cohortmemberalias}.userid = {$useralias}.id",
98
            "LEFT JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$cohortmemberalias}.cohortid",
99
        ]));
100
 
101
        // Add report elements from each of the entities we added to the report.
102
        $this->add_all_from_entity($userentity->get_entity_name());
103
        $this->add_all_from_entity($badgeissuedentity->get_entity_name());
104
        $this->add_all_from_entity($badgeentity->get_entity_name());
105
        $this->add_all_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink'], ['name'], ['name']);
106
        $this->add_all_from_entity($courseentity->get_entity_name());
107
        $this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
108
            ['cohortselect', 'name', 'idnumber', 'customfield*'], ['cohortselect', 'name', 'idnumber', 'customfield*']);
109
    }
110
 
111
    /**
112
     * Return the columns that will be added to the report upon creation
113
     *
114
     * @return string[]
115
     */
116
    public function get_default_columns(): array {
117
        return [
118
            'user:fullname',
119
            'badge:name',
120
            'badge:description',
121
            'badge_issued:issued',
122
        ];
123
    }
124
 
125
    /**
126
     * Return the column sorting that will be added to the report upon creation
127
     *
128
     * @return int[]
129
     */
130
    public function get_default_column_sorting(): array {
131
        return [
132
            'user:fullname' => SORT_ASC,
133
            'badge:name' => SORT_ASC,
134
            'badge_issued:issued' => SORT_ASC,
135
        ];
136
    }
137
 
138
    /**
139
     * Return the filters that will be added to the report upon creation
140
     *
141
     * @return string[]
142
     */
143
    public function get_default_filters(): array {
144
        return [
145
            'user:fullname',
146
            'badge:name',
147
            'badge_issued:issued',
148
        ];
149
    }
150
 
151
    /**
152
     * Return the conditions that will be added to the report upon creation
153
     *
154
     * @return string[]
155
     */
156
    public function get_default_conditions(): array {
157
        return [
158
            'badge:type',
159
            'badge:name',
160
        ];
161
    }
162
}