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
namespace block_dedication\local\systemreports;
18
 
19
use block_dedication\local\entities\{dedication, enrolment, groups};
20
use core_reportbuilder\local\helpers\database;
21
use core_reportbuilder\local\entities\user;
22
use core_reportbuilder\local\report\action;
23
use core_reportbuilder\system_report;
24
use moodle_url;
25
use pix_icon;
26
 
27
/**
28
 * Dedication system level report.
29
 *
30
 * @package    block_dedication
31
 * @copyright  2022 Canterbury University
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class course extends system_report {
35
 
36
    /**
37
     * Initialise report, we need to set the main table, load our entities and set columns/filters
38
     */
39
    protected function initialise(): void {
40
        global $DB, $PAGE, $USER;
41
 
42
        $courseentity = new \core_reportbuilder\local\entities\course();
43
        $course = $courseentity->get_table_alias('course');
44
        $this->add_entity($courseentity);
45
 
46
        $this->set_main_table('course', $course);
47
 
48
        // We need to ensure page context is always set, as required by output and string formatting.
49
        $courserecord = get_course($this->get_context()->instanceid);
50
        $PAGE->set_context($this->get_context());
51
 
52
        $enrolmententity = new enrolment();
53
        $userenrolment = $enrolmententity->get_table_alias('user_enrolments');
54
        $enrol = $enrolmententity->get_table_alias('enrol');
55
        $enroljoin = "LEFT JOIN {enrol} {$enrol} ON {$enrol}.courseid = {$course}.id";
56
        $userenrolmentjoin = " LEFT JOIN {user_enrolments} {$userenrolment} ON {$userenrolment}.enrolid = {$enrol}.id";
57
        $enrolmententity->add_joins([$enroljoin, $userenrolmentjoin]);
58
        $this->add_entity($enrolmententity);
59
 
60
        // Join user entity.
61
        $userentity = new user();
62
        $user = $userentity->get_table_alias('user');
63
        $userentity->add_joins([$enroljoin, $userenrolmentjoin]);
64
        $userentity->add_join("LEFT JOIN {user} {$user} ON {$userenrolment}.userid = {$user}.id AND {$user}.deleted = 0");
65
        $this->add_entity($userentity);
66
 
67
        $this->add_base_fields("{$user}.id as userid");
68
 
69
        $dedicationentity = new dedication();
70
        $dedicationalias = $dedicationentity->get_table_alias('block_dedication');
71
        // Note: rather than joining normally, we have to do a subselect so we can get the SUM() aggregation.
72
        // In future once MDL-76392 lands, we should be able to do this better.
73
        $dedicationentity->add_join("JOIN (
74
                                   SELECT SUM(timespent) as timespent, userid, courseid
75
                                     FROM {block_dedication} GROUP BY userid, courseid) {$dedicationalias} ON
76
                                          {$dedicationalias}.userid = {$user}.id and {$dedicationalias}.courseid = {$course}.id");
77
        $this->add_entity($dedicationentity);
78
 
79
        $groupnamesssql = $DB->sql_group_concat('gr.name', ', ');
80
 
81
        $groupidssql = $DB->sql_group_concat('gm.groupid', ',');
82
        $groupidssql = $DB->sql_concat("','", $groupidssql, "','");
83
 
84
        $groupsentity = new groups();
85
        $groupsalias = $groupsentity->get_table_alias('groups');
86
 
87
        $groupjointype = "LEFT JOIN";
88
        if ($PAGE->course->groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $PAGE->context)) {
89
            $visiblegroups = groups_get_all_groups($PAGE->course->id, 0, $PAGE->course->defaultgroupingid, 'g.id');
90
        } else {
91
            $visiblegroups = groups_get_all_groups($PAGE->course->id, $USER->id, $PAGE->course->defaultgroupingid, 'g.id');
92
            $groupjointype = "JOIN";
93
        }
94
 
95
        if (!empty($visiblegroups)) {
96
            $vglikesql = '(';
97
            foreach ($visiblegroups as $vg) {
98
                $vglikesql .= $vg->id . ',';
99
            }
100
            $vglikesql = substr($vglikesql, 0, -1) . ')';
101
            $vglikesql = "AND gm.groupid IN $vglikesql";
102
        } else {
103
            $vglikesql = '';
104
        }
105
 
106
        $groupsentity->add_join("$groupjointype (
107
                            SELECT gm.userid, gr.courseid, $groupidssql groupids, $groupnamesssql groupnames
108
                            FROM {groups_members} gm
109
                            JOIN {groups} gr ON gr.id = gm.groupid
110
                            $vglikesql
111
                            GROUP BY gm.userid, gr.courseid
112
                        ) $groupsalias
113
                        ON $groupsalias.userid = {$user}.id AND $groupsalias.courseid = {$course}.id");
114
 
115
        $this->add_entity($groupsentity);
116
 
117
        $param1 = database::generate_param_name();
118
        $wheresql = "$course.id = :$param1";
119
 
120
        $this->add_base_condition_sql($wheresql,
121
            [$param1 => $courserecord->id]);
122
 
123
        // Now we can call our helper methods to add the content we want to include in the report.
124
        $this->add_columns();
125
        $this->add_filters();
126
 
127
        // Action to download individual task log.
128
        $this->add_action((new action(
129
            new moodle_url('/blocks/dedication/user.php', ['id' => $courserecord->id, 'userid' => ":userid"]),
130
            new pix_icon('i/search', get_string('viewsessiondurationreport', 'block_dedication')))));
131
 
132
        if (has_capability('report/log:view', \context_course::instance($courserecord->id))) {
133
            $this->add_action((new action(
134
                new moodle_url('/report/log/user.php', ['id' => ":userid", 'course' => $courserecord->id, 'mode' => 'all']),
135
                new pix_icon('i/search', get_string('alllogs')))));
136
        }
137
 
138
        // Set if report can be downloaded.
139
        $this->set_downloadable(true);
140
    }
141
 
142
    /**
143
     * Validates access to view this report
144
     *
145
     * @return bool
146
     */
147
    protected function can_view(): bool {
148
        return has_capability('block/dedication:viewreports', $this->get_context());
149
    }
150
 
151
    /**
152
     * Adds the columns we want to display in the report
153
     *
154
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
155
     * unique identifier
156
     */
157
    public function add_columns(): void {
158
        $columns = [
159
            'user:fullnamewithpicturelink',
160
            'groups:groupnames',
161
            'dedication:timespent',
162
        ];
163
 
164
        $this->add_columns_from_entities($columns);
165
    }
166
 
167
    /**
168
     * Adds the filters we want to display in the report
169
     *
170
     * They are all provided by the entities we previously added in the {@see initialise} method, referencing each by their
171
     * unique identifier
172
     */
173
    protected function add_filters(): void {
174
        $filters = [
175
            'user:fullname',
176
            'dedication:timespent',
177
            'groups:group',
178
        ];
179
 
180
        $this->add_filters_from_entities($filters);
181
    }
182
}