Merge branch 'MDL-81327-403' of https://github.com/laurentdavid/moodle into MOODLE_403_STABLE
This commit is contained in:
@@ -24,7 +24,9 @@
|
||||
*/
|
||||
|
||||
namespace core;
|
||||
use context_course;
|
||||
use moodle_url;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* A helper class with static methods to help report plugins
|
||||
@@ -110,4 +112,71 @@ class report_helper {
|
||||
}
|
||||
$USER->course_last_report[$id] = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the right SQL / params for the group filter depending on the filterparams, course and group settings.
|
||||
*
|
||||
* Addionnaly, it will return the list of users visible by the current user so
|
||||
* it can be used to filter out records that are not visible. This is mainly
|
||||
* because we cannot use joins as the log tables can be in two different databases.
|
||||
*
|
||||
* @param stdClass $filterparams
|
||||
* @return array
|
||||
*/
|
||||
public static function get_group_filter(stdClass $filterparams): array {
|
||||
global $DB, $USER;
|
||||
$useridfilter = null;
|
||||
// First and just in case we are in separate group, just set the $useridfilter to the list
|
||||
// of users visible by this user.
|
||||
$courseid = $filterparams->courseid ?? SITEID;
|
||||
$courseid = $courseid ?: SITEID; // Make sure that if courseid is set to 0 we use SITEID.
|
||||
$course = get_course($courseid);
|
||||
$groupmode = groups_get_course_groupmode($course);
|
||||
$groupid = $filterparams->groupid ?? 0;
|
||||
if ($groupmode == SEPARATEGROUPS || $groupid) {
|
||||
$context = context_course::instance($courseid);
|
||||
if ($groupid) {
|
||||
$cgroups = [(int) $groupid];
|
||||
} else {
|
||||
$cgroups = groups_get_all_groups(
|
||||
$courseid,
|
||||
has_capability('moodle/site:accessallgroups', $context) ? 0 : $USER->id
|
||||
);
|
||||
$cgroups = array_keys($cgroups);
|
||||
// If that's the case, limit the users to be in the groups only, defined by the filter.
|
||||
if (has_capability('moodle/site:accessallgroups', $context) || empty($cgroups)) {
|
||||
$cgroups[] = USERSWITHOUTGROUP;
|
||||
}
|
||||
}
|
||||
// If that's the case, limit the users to be in the groups only, defined by the filter.
|
||||
[$groupmembersql, $groupmemberparams] = groups_get_members_ids_sql($cgroups, $context);
|
||||
$groupusers = $DB->get_fieldset_sql($groupmembersql, $groupmemberparams);
|
||||
$useridfilter = array_fill_keys($groupusers, true);
|
||||
}
|
||||
$joins = [];
|
||||
$params = [];
|
||||
if (empty($filterparams->userid)) {
|
||||
if ($groupid) {
|
||||
if ($thisgroupusers = groups_get_members($groupid)) {
|
||||
[$sql, $sqlfilterparams] = $DB->get_in_or_equal(
|
||||
array_keys($thisgroupusers),
|
||||
SQL_PARAMS_NAMED,
|
||||
);
|
||||
$joins[] = "userid {$sql}";
|
||||
$params = $sqlfilterparams;
|
||||
} else {
|
||||
$joins[] = 'userid = 0'; // No users in groups, so we want something that will always be false.
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$joins[] = "userid = :userid";
|
||||
$params['userid'] = $filterparams->userid;
|
||||
}
|
||||
|
||||
return [
|
||||
'joins' => $joins,
|
||||
'params' => $params,
|
||||
'useridfilter' => $useridfilter,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user