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 |
|
11 |
efrain |
17 |
namespace core;
|
1 |
efrain |
18 |
|
|
|
19 |
use context_course;
|
|
|
20 |
use stdClass;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* A helper class with static methods to help report plugins
|
|
|
24 |
*
|
|
|
25 |
* @package core
|
11 |
efrain |
26 |
* @subpackage report
|
1 |
efrain |
27 |
* @copyright 2021 Sujith Haridasan
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class report_helper {
|
11 |
efrain |
31 |
|
1 |
efrain |
32 |
/**
|
|
|
33 |
* Print the selector dropdown
|
|
|
34 |
*
|
|
|
35 |
* @param string $pluginname The report plugin where the header is modified
|
11 |
efrain |
36 |
* @param string $additional Additional content to display aligned with the selector
|
1 |
efrain |
37 |
*/
|
11 |
efrain |
38 |
public static function print_report_selector(string $pluginname, string $additional = ''): void {
|
1 |
efrain |
39 |
global $OUTPUT, $PAGE;
|
|
|
40 |
|
|
|
41 |
if ($reportnode = $PAGE->settingsnav->find('coursereports', \navigation_node::TYPE_CONTAINER)) {
|
|
|
42 |
|
|
|
43 |
$menuarray = \core\navigation\views\secondary::create_menu_element([$reportnode]);
|
|
|
44 |
if (empty($menuarray)) {
|
|
|
45 |
return;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$coursereports = get_string('reports');
|
|
|
49 |
$activeurl = '';
|
|
|
50 |
if (isset($menuarray[0])) {
|
|
|
51 |
// Remove the reports entry.
|
|
|
52 |
$result = array_search($coursereports, $menuarray[0][$coursereports]);
|
|
|
53 |
unset($menuarray[0][$coursereports][$result]);
|
|
|
54 |
|
|
|
55 |
// Find the active node.
|
|
|
56 |
foreach ($menuarray[0] as $key => $value) {
|
|
|
57 |
$check = array_search($pluginname, $value);
|
|
|
58 |
if ($check !== false) {
|
|
|
59 |
$activeurl = $check;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
} else {
|
|
|
63 |
$result = array_search($coursereports, $menuarray);
|
|
|
64 |
unset($menuarray[$result]);
|
|
|
65 |
|
|
|
66 |
$check = array_search($pluginname, $menuarray);
|
|
|
67 |
if ($check !== false) {
|
|
|
68 |
$activeurl = $check;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
}
|
|
|
72 |
$selectmenu = new \core\output\select_menu('reporttype', $menuarray, $activeurl);
|
1441 |
ariadna |
73 |
$selectmenu->set_label(get_string('reporttype'), ['class' => 'visually-hidden']);
|
1 |
efrain |
74 |
$options = \html_writer::tag(
|
|
|
75 |
'div',
|
|
|
76 |
$OUTPUT->render_from_template('core/tertiary_navigation_selector', $selectmenu->export_for_template($OUTPUT)),
|
11 |
efrain |
77 |
['class' => 'navitem']
|
1 |
efrain |
78 |
);
|
11 |
efrain |
79 |
|
|
|
80 |
if ($additional) {
|
|
|
81 |
$options .= \html_writer::div('', 'navitem-divider') .
|
|
|
82 |
\html_writer::div($additional, 'navitem');
|
|
|
83 |
}
|
|
|
84 |
|
1 |
efrain |
85 |
echo \html_writer::tag(
|
|
|
86 |
'div',
|
|
|
87 |
$options,
|
1441 |
ariadna |
88 |
['class' => 'tertiary-navigation full-width-bottom-border ms-0 d-flex', 'id' => 'tertiary-navigation']);
|
1 |
efrain |
89 |
} else {
|
|
|
90 |
echo $OUTPUT->heading($pluginname, 2, 'mb-3');
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @deprecated since Moodle 4.0
|
|
|
96 |
*/
|
1441 |
ariadna |
97 |
#[\core\attribute\deprecated(null, reason: 'It is no longer used', since: '4.0', final: true)]
|
|
|
98 |
public static function save_selected_report() {
|
|
|
99 |
\core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
|
1 |
efrain |
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Retrieve the right SQL / params for the group filter depending on the filterparams, course and group settings.
|
|
|
104 |
*
|
|
|
105 |
* Addionnaly, it will return the list of users visible by the current user so
|
|
|
106 |
* it can be used to filter out records that are not visible. This is mainly
|
|
|
107 |
* because we cannot use joins as the log tables can be in two different databases.
|
|
|
108 |
*
|
|
|
109 |
* @param stdClass $filterparams
|
|
|
110 |
* @return array
|
|
|
111 |
*/
|
|
|
112 |
public static function get_group_filter(stdClass $filterparams): array {
|
|
|
113 |
global $DB, $USER;
|
|
|
114 |
$useridfilter = null;
|
|
|
115 |
// First and just in case we are in separate group, just set the $useridfilter to the list
|
|
|
116 |
// of users visible by this user.
|
|
|
117 |
$courseid = $filterparams->courseid ?? SITEID;
|
|
|
118 |
$courseid = $courseid ?: SITEID; // Make sure that if courseid is set to 0 we use SITEID.
|
|
|
119 |
$course = get_course($courseid);
|
|
|
120 |
$groupmode = groups_get_course_groupmode($course);
|
|
|
121 |
$groupid = $filterparams->groupid ?? 0;
|
1441 |
ariadna |
122 |
$context = context_course::instance($courseid);
|
|
|
123 |
if ($groupid || ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context))) {
|
1 |
efrain |
124 |
if ($groupid) {
|
|
|
125 |
$cgroups = [(int) $groupid];
|
|
|
126 |
} else {
|
1441 |
ariadna |
127 |
$cgroups = groups_get_all_groups($courseid, $USER->id);
|
1 |
efrain |
128 |
$cgroups = array_keys($cgroups);
|
1441 |
ariadna |
129 |
// If you are not in any groups you can still view users without group. This may
|
|
|
130 |
// perform poorly because it will list all users in the entire system who do not
|
|
|
131 |
// belong to a group on this course.
|
|
|
132 |
if (empty($cgroups)) {
|
1 |
efrain |
133 |
$cgroups[] = USERSWITHOUTGROUP;
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
// If that's the case, limit the users to be in the groups only, defined by the filter.
|
|
|
137 |
[$groupmembersql, $groupmemberparams] = groups_get_members_ids_sql($cgroups, $context);
|
|
|
138 |
$groupusers = $DB->get_fieldset_sql($groupmembersql, $groupmemberparams);
|
|
|
139 |
$useridfilter = array_fill_keys($groupusers, true);
|
|
|
140 |
}
|
|
|
141 |
$joins = [];
|
|
|
142 |
$params = [];
|
|
|
143 |
if (empty($filterparams->userid)) {
|
|
|
144 |
if ($groupid) {
|
|
|
145 |
if ($thisgroupusers = groups_get_members($groupid)) {
|
|
|
146 |
[$sql, $sqlfilterparams] = $DB->get_in_or_equal(
|
|
|
147 |
array_keys($thisgroupusers),
|
|
|
148 |
SQL_PARAMS_NAMED,
|
|
|
149 |
);
|
|
|
150 |
$joins[] = "userid {$sql}";
|
|
|
151 |
$params = $sqlfilterparams;
|
|
|
152 |
} else {
|
|
|
153 |
$joins[] = 'userid = 0'; // No users in groups, so we want something that will always be false.
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
} else {
|
|
|
157 |
$joins[] = "userid = :userid";
|
|
|
158 |
$params['userid'] = $filterparams->userid;
|
1441 |
ariadna |
159 |
$useridfilter[$filterparams->userid] = true;
|
1 |
efrain |
160 |
}
|
|
|
161 |
|
|
|
162 |
return [
|
|
|
163 |
'joins' => $joins,
|
|
|
164 |
'params' => $params,
|
|
|
165 |
'useridfilter' => $useridfilter,
|
|
|
166 |
];
|
|
|
167 |
}
|
1441 |
ariadna |
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Check if the user is in a valid group for the course (i.e. if the user is in a group in SEPARATEGROUPS mode)
|
|
|
171 |
*
|
|
|
172 |
* @param context $context context for the course or module: if context is a course context, the course group mode is used,
|
|
|
173 |
* if it is a module context, the module effective group mode is used (combined with the current user).
|
|
|
174 |
* @param int|null $userid user id to check, if null the current user is used
|
|
|
175 |
* @return bool true if the user is in a valid group (i.e. belongs to a group in SEPARATEGROUPS MODE), false otherwise
|
|
|
176 |
*/
|
|
|
177 |
public static function has_valid_group(\context $context, ?int $userid = null): bool {
|
|
|
178 |
global $USER;
|
|
|
179 |
|
|
|
180 |
$userid = $userid ?? $USER->id;
|
|
|
181 |
|
|
|
182 |
if ($context instanceof context_course) {
|
|
|
183 |
$courseid = $context->instanceid;
|
|
|
184 |
$course = get_course($courseid);
|
|
|
185 |
$groupmode = $course->groupmode;
|
|
|
186 |
} else if ($context instanceof \context_module) {
|
|
|
187 |
$courseid = $context->get_course_context()->instanceid;
|
|
|
188 |
$modinfo = get_fast_modinfo($courseid);
|
|
|
189 |
$cm = $modinfo->get_cm($context->instanceid);
|
|
|
190 |
$groupmode = $cm->effectivegroupmode;
|
|
|
191 |
} else {
|
|
|
192 |
return true; // No groups in system context.
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
if ($groupmode != SEPARATEGROUPS) {
|
|
|
196 |
return true; // No groups or visible all groups.
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
if (!has_capability('moodle/site:accessallgroups', $context, $userid)) {
|
|
|
200 |
$usergroups = groups_get_all_groups($courseid, $userid);
|
|
|
201 |
if (empty($usergroups)) {
|
|
|
202 |
return false;
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return true;
|
|
|
207 |
}
|
1 |
efrain |
208 |
}
|