2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Group visibility methods
21 * @copyright 2022 onwards Catalyst IT EU {@link https://catalyst-eu.net}
22 * @author Mark Johnson <mark.johnson@catalyst-eu.net>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 * Group visibility methods.
34 * Store the number groups with visibility other than ALL on the course.
36 * @param int $courseid Course ID to update the cache for.
37 * @param \cache|null $cache Existing cache instance. If null, once will be created.
39 * @throws \dml_exception
41 public static function update_hiddengroups_cache(int $courseid, ?\cache
$cache = null): void
{
44 $cache = \cache
::make('core', 'coursehiddengroups');
46 $hiddengroups = $DB->count_records_select('groups', 'courseid = ? AND visibility != ?',
47 [$courseid, GROUPS_VISIBILITY_ALL
]);
48 $cache->set($courseid, $hiddengroups);
52 * Return whether a course currently had hidden groups.
54 * This can be used as a shortcut to decide whether visibility restrictions need to be applied. If this returns false,
55 * we may be able to use cached data, or do a much simpler query.
57 * @param int $courseid
59 * @throws \coding_exception
60 * @throws \dml_exception
62 public static function course_has_hidden_groups(int $courseid): bool {
63 $cache = \cache
::make('core', 'coursehiddengroups');
64 $hiddengroups = $cache->get($courseid);
65 if ($hiddengroups === false) {
66 self
::update_hiddengroups_cache($courseid, $cache);
67 $cache->get($courseid);
69 return $hiddengroups > 0;
73 * Can the current user view all the groups on the course?
75 * Returns true if there are no groups on the course with visibility != ALL,
76 * or if the user has viewhiddengroups.
78 * This is useful for deciding whether we need to perform additional visibility checkes
79 * such as the sql_* methods of this class.
81 * @param int $courseid
84 public static function can_view_all_groups(int $courseid): bool {
85 $viewhidden = has_capability('moodle/course:viewhiddengroups', \context_course
::instance($courseid));
86 $hashidden = self
::course_has_hidden_groups($courseid);
87 return $viewhidden ||
!$hashidden;
91 * Return SQL conditions for determining whether a user can see a group and its memberships.
94 * @param string $groupsalias The SQL alias being used for the groups table.
95 * @param string $groupsmembersalias The SQL alias being used for the groups_members table.
96 * @return array [$where, $params]
98 public static function sql_group_visibility_where(int $userid,
99 string $groupsalias = 'g', string $groupsmembersalias = 'gm'): array {
101 // Apply visibility restrictions.
102 // Everyone can see who is in groups with ALL visibility.
103 $where = "({$groupsalias}.visibility = :all";
104 $params['all'] = GROUPS_VISIBILITY_ALL
;
105 if ($userid == $USER->id
) {
106 // If the user is looking at their own groups, they can see those with MEMBERS or OWN visibility.
107 $where .= " OR {$groupsalias}.visibility IN (:members, :own)";
108 $params['members'] = GROUPS_VISIBILITY_MEMBERS
;
109 $params['own'] = GROUPS_VISIBILITY_OWN
;
111 list($memberssql, $membersparams) = self
::sql_members_visibility_condition($groupsalias, $groupsmembersalias);
112 // If someone else's groups, they can see those with MEMBERS visibilty, only if they are a member too.
113 $where .= " OR ($memberssql)";
114 $params = array_merge($params, $membersparams);
117 return [$where, $params];
121 * Return SQL conditions for determining whether a user can see a group's members.
123 * @param string $groupsalias The SQL alias being used for the groups table.
124 * @param string $groupsmembersalias The SQL alias being used for the groups_members table.
125 * @param string $useralias The SQL alias being used for the user table.
126 * @param string $paramprefix Prefix for the parameter names.
127 * @return array [$where, $params]
129 public static function sql_member_visibility_where(
130 string $groupsalias = 'g',
131 string $groupsmembersalias = 'gm',
132 string $useralias = 'u',
133 string $paramprefix = '',
137 list($memberssql, $membersparams) = self
::sql_members_visibility_condition($groupsalias, $groupsmembersalias, $paramprefix);
140 {$groupsalias}.visibility = :{$paramprefix}all
142 OR ({$groupsalias}.visibility = :{$paramprefix}own AND {$useralias}.id = :{$paramprefix}currentuser2)
145 "{$paramprefix}all" => GROUPS_VISIBILITY_ALL
,
146 "{$paramprefix}own" => GROUPS_VISIBILITY_OWN
,
147 "{$paramprefix}currentuser2" => $USER->id
,
149 $params = array_merge($params, $membersparams);
150 return [$where, $params];
154 * Return a condition to check if a user can view a group because it has MEMBERS visibility and they are a member.
156 * @param string $groupsalias The SQL alias being used for the groups table.
157 * @param string $groupsmembersalias The SQL alias being used for the groups_members table.
158 * @param string $paramprefix Prefix for the parameter names.
159 * @return array [$sql, $params]
161 protected static function sql_members_visibility_condition(
162 string $groupsalias = 'g',
163 string $groupsmembersalias = 'gm',
164 string $paramprefix = '',
167 $sql = "{$groupsalias}.visibility = :{$paramprefix}members
170 FROM {groups_members} gm2
171 WHERE gm2.groupid = {$groupsmembersalias}.groupid
172 AND gm2.userid = :{$paramprefix}currentuser
175 "{$paramprefix}members" => GROUPS_VISIBILITY_MEMBERS
,
176 "{$paramprefix}currentuser" => $USER->id
179 return [$sql, $params];