Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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
/**
18
 * Report plugins helper class
19
 *
20
 * @package core
21
 * @subpackage report
22
 * @copyright 2021 Sujith Haridasan
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core;
27
use context_course;
28
use moodle_url;
29
use stdClass;
30
 
31
/**
32
 * A helper class with static methods to help report plugins
33
 *
34
 * @package core
35
 * @copyright 2021 Sujith Haridasan
36
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class report_helper {
39
    /**
40
     * Print the selector dropdown
41
     *
42
     * @param string $pluginname The report plugin where the header is modified
43
     * @return void
44
     */
45
    public static function print_report_selector(string $pluginname): void {
46
        global $OUTPUT, $PAGE;
47
 
48
        if ($reportnode = $PAGE->settingsnav->find('coursereports', \navigation_node::TYPE_CONTAINER)) {
49
 
50
            $menuarray = \core\navigation\views\secondary::create_menu_element([$reportnode]);
51
            if (empty($menuarray)) {
52
                return;
53
            }
54
 
55
            $coursereports = get_string('reports');
56
            $activeurl = '';
57
            if (isset($menuarray[0])) {
58
                // Remove the reports entry.
59
                $result = array_search($coursereports, $menuarray[0][$coursereports]);
60
                unset($menuarray[0][$coursereports][$result]);
61
 
62
                // Find the active node.
63
                foreach ($menuarray[0] as $key => $value) {
64
                    $check = array_search($pluginname, $value);
65
                    if ($check !== false) {
66
                        $activeurl = $check;
67
                    }
68
                }
69
            } else {
70
                $result = array_search($coursereports, $menuarray);
71
                unset($menuarray[$result]);
72
 
73
                $check = array_search($pluginname, $menuarray);
74
                if ($check !== false) {
75
                    $activeurl = $check;
76
                }
77
 
78
            }
79
            $selectmenu = new \core\output\select_menu('reporttype', $menuarray, $activeurl);
80
            $selectmenu->set_label(get_string('reporttype'), ['class' => 'sr-only']);
81
            $options = \html_writer::tag(
82
                'div',
83
                $OUTPUT->render_from_template('core/tertiary_navigation_selector', $selectmenu->export_for_template($OUTPUT)),
84
                ['class' => 'row pb-3']
85
            );
86
            echo \html_writer::tag(
87
                'div',
88
                $options,
89
                ['class' => 'tertiary-navigation full-width-bottom-border ml-0', 'id' => 'tertiary-navigation']);
90
        } else {
91
            echo $OUTPUT->heading($pluginname, 2, 'mb-3');
92
        }
93
    }
94
 
95
    /**
96
     * Save the last selected report in the session
97
     *
98
     * @deprecated since Moodle 4.0
99
     * @param int $id The course id
100
     * @param moodle_url $url The moodle url
101
     * @return void
102
     */
103
    public static function save_selected_report(int $id, moodle_url $url): void {
104
        global $USER;
105
 
106
        debugging('save_selected_report() has been deprecated because it is no longer used and will be '.
107
            'removed in future versions of Moodle', DEBUG_DEVELOPER);
108
 
109
        // Last selected report.
110
        if (!isset($USER->course_last_report)) {
111
            $USER->course_last_report = [];
112
        }
113
        $USER->course_last_report[$id] = $url;
114
    }
115
 
116
    /**
117
     * Retrieve the right SQL / params for the group filter depending on the filterparams, course and group settings.
118
     *
119
     * Addionnaly, it will return the list of users visible by the current user so
120
     * it can be used to filter out records that are not visible. This is mainly
121
     * because we cannot use joins as the log tables can be in two different databases.
122
     *
123
     * @param stdClass $filterparams
124
     * @return array
125
     */
126
    public static function get_group_filter(stdClass $filterparams): array {
127
        global $DB, $USER;
128
        $useridfilter = null;
129
        // First and just in case we are in separate group, just set the $useridfilter to the list
130
        // of users visible by this user.
131
        $courseid = $filterparams->courseid ?? SITEID;
132
        $courseid = $courseid ?: SITEID; // Make sure that if courseid is set to 0 we use SITEID.
133
        $course = get_course($courseid);
134
        $groupmode = groups_get_course_groupmode($course);
135
        $groupid = $filterparams->groupid ?? 0;
136
        if ($groupmode == SEPARATEGROUPS || $groupid) {
137
            $context = context_course::instance($courseid);
138
            if ($groupid) {
139
                $cgroups = [(int) $groupid];
140
            } else {
141
                $cgroups = groups_get_all_groups(
142
                    $courseid,
143
                    has_capability('moodle/site:accessallgroups', $context) ? 0 : $USER->id
144
                );
145
                $cgroups = array_keys($cgroups);
146
                // If that's the case, limit the users to be in the groups only, defined by the filter.
147
                if (has_capability('moodle/site:accessallgroups', $context) || empty($cgroups)) {
148
                    $cgroups[] = USERSWITHOUTGROUP;
149
                }
150
            }
151
            // If that's the case, limit the users to be in the groups only, defined by the filter.
152
            [$groupmembersql, $groupmemberparams] = groups_get_members_ids_sql($cgroups, $context);
153
            $groupusers = $DB->get_fieldset_sql($groupmembersql, $groupmemberparams);
154
            $useridfilter = array_fill_keys($groupusers, true);
155
        }
156
        $joins = [];
157
        $params = [];
158
        if (empty($filterparams->userid)) {
159
            if ($groupid) {
160
                if ($thisgroupusers = groups_get_members($groupid)) {
161
                    [$sql, $sqlfilterparams] = $DB->get_in_or_equal(
162
                        array_keys($thisgroupusers),
163
                        SQL_PARAMS_NAMED,
164
                    );
165
                    $joins[] = "userid {$sql}";
166
                    $params = $sqlfilterparams;
167
                } else {
168
                    $joins[] = 'userid = 0'; // No users in groups, so we want something that will always be false.
169
                }
170
            }
171
        } else {
172
            $joins[] = "userid = :userid";
173
            $params['userid'] = $filterparams->userid;
174
        }
175
 
176
        return [
177
            'joins' => $joins,
178
            'params' => $params,
179
            'useridfilter' => $useridfilter,
180
        ];
181
    }
182
}