Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
20
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 * @package    core_group
22
 */
23
 
24
defined('MOODLE_INTERNAL') || die();
25
 
26
/**
27
 * Groups not used in course or activity
28
 */
29
define('NOGROUPS', 0);
30
 
31
/**
32
 * Groups used, users do not see other groups
33
 */
34
define('SEPARATEGROUPS', 1);
35
 
36
/**
37
 * Groups used, students see other groups
38
 */
39
define('VISIBLEGROUPS', 2);
40
 
41
/**
42
 * This is for filtering users without any group.
43
 */
44
define('USERSWITHOUTGROUP', -1);
45
 
46
/**
47
 * 'None' join type, used when filtering by groups (logical NOT)
48
 */
49
define('GROUPS_JOIN_NONE', 0);
50
 
51
/**
52
 * 'Any' join type, used when filtering by groups (logical OR)
53
 */
54
define('GROUPS_JOIN_ANY', 1);
55
 
56
/**
57
 * 'All' join type, used when filtering by groups (logical AND)
58
 */
59
define('GROUPS_JOIN_ALL', 2);
60
 
61
/**
62
 * All users can see this group and its members.
63
 */
64
define('GROUPS_VISIBILITY_ALL', 0);
65
 
66
/**
67
 * Members of this group can see this group and other members.
68
 */
69
define('GROUPS_VISIBILITY_MEMBERS', 1);
70
 
71
/**
72
 * Members of this group can see the group and their own membership, but not each other's membership
73
 */
74
define('GROUPS_VISIBILITY_OWN', 2);
75
 
76
/**
77
 * No-one can see this group or its members. Members of the group will not know they are in the group.
78
 */
79
define('GROUPS_VISIBILITY_NONE', 3);
80
 
81
/**
82
 * Determines if a group with a given groupid exists.
83
 *
84
 * @category group
85
 * @param int $groupid The groupid to check for
86
 * @return bool True if the group exists, false otherwise or if an error
87
 * occurred.
88
 */
89
function groups_group_exists($groupid) {
90
    global $DB;
91
    return $DB->record_exists('groups', array('id'=>$groupid));
92
}
93
 
94
/**
95
 * Gets the name of a group with a specified id
96
 *
97
 * Before output, you should call {@see format_string} on the result
98
 *
99
 * @category group
100
 * @param int $groupid The id of the group
101
 * @return string The name of the group
102
 */
103
function groups_get_group_name($groupid) {
104
    global $DB;
105
    return $DB->get_field('groups', 'name', array('id'=>$groupid));
106
}
107
 
108
/**
109
 * Gets the name of a grouping with a specified id
110
 *
111
 * Before output, you should call {@see format_string} on the result
112
 *
113
 * @category group
114
 * @param int $groupingid The id of the grouping
115
 * @return string The name of the grouping
116
 */
117
function groups_get_grouping_name($groupingid) {
118
    global $DB;
119
    return $DB->get_field('groupings', 'name', array('id'=>$groupingid));
120
}
121
 
122
/**
123
 * Returns the groupid of a group with the name specified for the course.
124
 * Group names should be unique in course
125
 *
126
 * @category group
127
 * @param int $courseid The id of the course
128
 * @param string $name name of group (without magic quotes)
129
 * @return int $groupid
130
 */
131
function groups_get_group_by_name($courseid, $name) {
132
    $data = groups_get_course_data($courseid);
133
    foreach ($data->groups as $group) {
134
        if ($group->name == $name) {
135
            return $group->id;
136
        }
137
    }
138
    return false;
139
}
140
 
141
/**
142
 * Returns the groupid of a group with the idnumber specified for the course.
143
 * Group idnumbers should be unique within course
144
 *
145
 * @category group
146
 * @param int $courseid The id of the course
147
 * @param string $idnumber idnumber of group
148
 * @return stdClass|false group object
149
 */
150
function groups_get_group_by_idnumber($courseid, $idnumber) {
151
    if (empty($idnumber)) {
152
        return false;
153
    }
154
    $data = groups_get_course_data($courseid);
155
    foreach ($data->groups as $group) {
156
        if ($group->idnumber == $idnumber) {
157
            return $group;
158
        }
159
    }
160
    return false;
161
}
162
 
163
/**
164
 * Returns the groupingid of a grouping with the name specified for the course.
165
 * Grouping names should be unique in course
166
 *
167
 * @category group
168
 * @param int $courseid The id of the course
169
 * @param string $name name of group (without magic quotes)
170
 * @return int $groupid
171
 */
172
function groups_get_grouping_by_name($courseid, $name) {
173
    $data = groups_get_course_data($courseid);
174
    foreach ($data->groupings as $grouping) {
175
        if ($grouping->name == $name) {
176
            return $grouping->id;
177
        }
178
    }
179
    return false;
180
}
181
 
182
/**
183
 * Returns the groupingid of a grouping with the idnumber specified for the course.
184
 * Grouping names should be unique within course
185
 *
186
 * @category group
187
 * @param int $courseid The id of the course
188
 * @param string $idnumber idnumber of the group
189
 * @return stdClass|false grouping object
190
 */
191
function groups_get_grouping_by_idnumber($courseid, $idnumber) {
192
    if (empty($idnumber)) {
193
        return false;
194
    }
195
    $data = groups_get_course_data($courseid);
196
    foreach ($data->groupings as $grouping) {
197
        if ($grouping->idnumber == $idnumber) {
198
            return $grouping;
199
        }
200
    }
201
    return false;
202
}
203
 
204
/**
205
 * Get the group object
206
 *
207
 * @category group
208
 * @param int $groupid ID of the group.
209
 * @param string $fields (default is all fields)
210
 * @param int $strictness (IGNORE_MISSING - default)
211
 * @return bool|stdClass group object or false if not found
212
 * @throws dml_exception
213
 */
214
function groups_get_group($groupid, $fields = '*', $strictness = IGNORE_MISSING, $withcustomfields = false) {
215
    global $DB;
216
    $group = $DB->get_record('groups', ['id' => $groupid], $fields, $strictness);
217
    if ($withcustomfields) {
218
        $customfieldsdata = get_group_custom_fields_data([$groupid]);
219
        $group->customfields = $customfieldsdata[$groupid] ?? [];
220
    }
221
    return $group;
222
}
223
 
224
/**
225
 * Get the grouping object
226
 *
227
 * @category group
228
 * @param int $groupingid ID of the group.
229
 * @param string $fields
230
 * @param int $strictness (IGNORE_MISSING - default)
231
 * @return stdClass group object
232
 */
233
function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING, $withcustomfields = false) {
234
    global $DB;
235
    $grouping = $DB->get_record('groupings', ['id' => $groupingid], $fields, $strictness);
236
    if ($withcustomfields) {
237
        $customfieldsdata = get_grouping_custom_fields_data([$groupingid]);
238
        $grouping->customfields = $customfieldsdata[$groupingid] ?? [];
239
    }
240
    return $grouping;
241
}
242
 
243
/**
244
 * Gets array of all groups in a specified course (subject to the conditions imposed by the other arguments).
245
 *
246
 * If a user does not have moodle/course:viewhiddengroups, the list of groups and members will be restricted based on the
247
 * visibility setting of each group.
248
 *
249
 * @category group
250
 * @param int $courseid The id of the course.
251
 * @param int|int[] $userid optional user id or array of ids, returns only groups continaing one or more of those users.
252
 * @param int $groupingid optional returns only groups in the specified grouping.
253
 * @param string $fields defaults to g.*. This allows you to vary which fields are returned.
254
 *      If $groupingid is specified, the groupings_groups table will be available with alias gg.
255
 *      If $userid is specified, the groups_members table will be available as gm.
256
 * @param bool $withmembers if true return an extra field members (int[]) which is the list of userids that
257
 *      are members of each group. For this to work, g.id (or g.*) must be included in $fields.
258
 *      In this case, the final results will always be an array indexed by group id.
259
 * @param bool $participationonly Only return groups where the participation field is true.
260
 * @return array returns an array of the group objects (unless you have done something very weird
261
 *      with the $fields option).
262
 */
263
function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*', $withmembers=false, $participationonly = false) {
264
    global $DB, $USER;
265
 
266
    // We need to check that we each field in the fields list belongs to the group table and that it has not being
267
    // aliased. If its something else we need to avoid the cache and run the query as who knows whats going on.
268
    $knownfields = true;
269
    if ($fields !== 'g.*') {
270
        // Quickly check if the first field is no longer g.id as using the
271
        // cache will return an array indexed differently than when expect
272
        if (strpos($fields, 'g.*') !== 0 && strpos($fields, 'g.id') !== 0) {
273
            $knownfields = false;
274
        } else {
275
            $fieldbits = explode(',', $fields);
276
            foreach ($fieldbits as $bit) {
277
                $bit = trim($bit);
278
                if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) {
279
                    $knownfields = false;
280
                    break;
281
                }
282
            }
283
        }
284
    }
285
 
286
    if (empty($userid) && $knownfields && !$withmembers && \core_group\visibility::can_view_all_groups($courseid)) {
287
        // We can use the cache.
288
        $data = groups_get_course_data($courseid);
289
        if (empty($groupingid)) {
290
            // All groups.. Easy!
291
            $groups = $data->groups;
292
        } else {
293
            $groups = array();
294
            foreach ($data->mappings as $mapping) {
295
                if ($mapping->groupingid != $groupingid) {
296
                    continue;
297
                }
298
                if (isset($data->groups[$mapping->groupid])) {
299
                    $groups[$mapping->groupid] = $data->groups[$mapping->groupid];
300
                }
301
            }
302
        }
303
        if ($participationonly) {
304
            $groups = array_filter($groups, fn($group) => $group->participation);
305
        }
306
        // Yay! We could use the cache. One more query saved.
307
        return $groups;
308
    }
309
 
310
    $params = [];
311
    $userfrom  = '';
312
    $userwhere = '';
313
    if (!empty($userid)) {
314
        list($usql, $params) = $DB->get_in_or_equal($userid);
315
        $userfrom  = "JOIN {groups_members} gm ON gm.groupid = g.id";
316
        $userwhere = "AND gm.userid $usql";
317
    }
318
 
319
    $groupingfrom  = '';
320
    $groupingwhere = '';
321
    if (!empty($groupingid)) {
322
        $groupingfrom  = "JOIN {groupings_groups} gg ON gg.groupid = g.id";
323
        $groupingwhere = "AND gg.groupingid = ?";
324
        $params[] = $groupingid;
325
    }
326
 
327
    array_unshift($params, $courseid);
328
 
329
    $visibilityfrom = '';
330
    $visibilitywhere = '';
331
    $viewhidden = has_capability('moodle/course:viewhiddengroups', context_course::instance($courseid));
332
    if (!$viewhidden) {
333
        // Apply group visibility restrictions. Only return groups where visibility is ALL, or the current user is a member and the
334
        // visibility is MEMBERS or OWN.
335
        $userids = [];
336
        if (empty($userid)) {
337
            $userids = [$USER->id];
338
            $visibilityfrom = "LEFT JOIN {groups_members} gm ON gm.groupid = g.id AND gm.userid = ?";
339
        }
340
        [$insql, $inparams] = $DB->get_in_or_equal([GROUPS_VISIBILITY_MEMBERS, GROUPS_VISIBILITY_OWN]);
341
        $visibilitywhere = " AND (g.visibility = ? OR (g.visibility $insql AND gm.id IS NOT NULL))";
342
        $params = array_merge(
343
            $userids,
344
            $params,
345
            [GROUPS_VISIBILITY_ALL],
346
            $inparams
347
        );
348
    }
349
 
350
    $participationwhere = '';
351
    if ($participationonly) {
352
        $participationwhere = "AND g.participation = ?";
353
        $params = array_merge($params, [1]);
354
    }
355
 
356
    $results = $DB->get_records_sql("
357
            SELECT $fields
358
              FROM {groups} g
359
              $userfrom
360
              $groupingfrom
361
              $visibilityfrom
362
             WHERE g.courseid = ?
363
               $userwhere
364
               $groupingwhere
365
               $visibilitywhere
366
               $participationwhere
367
          ORDER BY g.name ASC", $params);
368
 
369
    if (!$withmembers) {
370
        return $results;
371
    }
372
 
373
    // We also want group members. We do this in a separate query, becuse the above
374
    // query will return a lot of data (e.g. g.description) for each group, and
375
    // some groups may contain hundreds of members. We don't want the results
376
    // to contain hundreds of copies of long descriptions.
377
    $groups = [];
378
    foreach ($results as $row) {
379
        $groups[$row->id] = $row;
380
        $groups[$row->id]->members = [];
381
    }
382
 
383
    $gmvisibilityfrom = '';
384
    $gmvisibilitywhere = '';
385
    $gmvisibilityparams = [];
386
    if (!$viewhidden) {
387
        // Only return membership records where visibility is ALL, visibility is MEMBERS and the current user is a member,
388
        // or visibility is OWN and the record is for the current user.
389
        $gmvisibilityfrom = "
390
            JOIN {groups} g ON gm.groupid = g.id
391
        ";
392
        $gmvisibilitywhere = "
393
                AND (g.visibility = ?
394
                    OR (g.visibility = ?
395
                        AND g.id IN (SELECT gm2.groupid FROM {groups_members} gm2 WHERE gm2.groupid = g.id AND gm2.userid = ?))
396
                    OR (g.visibility = ?
397
                        AND gm.userid = ?))";
398
        $gmvisibilityparams = [
399
            GROUPS_VISIBILITY_ALL,
400
            GROUPS_VISIBILITY_MEMBERS,
401
            $USER->id,
402
            GROUPS_VISIBILITY_OWN,
403
            $USER->id
404
        ];
405
    }
406
 
407
    $groupmembers = [];
408
    if (!empty($groups)) {
409
        [$gmin, $gmparams] = $DB->get_in_or_equal(array_keys($groups));
410
        $params = array_merge($gmparams, $gmvisibilityparams);
411
        $gmsql = "
412
        SELECT gm.*
413
          FROM {groups_members} gm
414
               $gmvisibilityfrom
415
         WHERE gm.groupid $gmin
416
               $gmvisibilitywhere";
417
        $groupmembers = $DB->get_records_sql($gmsql, $params);
418
    }
419
 
420
    foreach ($groupmembers as $gm) {
421
        $groups[$gm->groupid]->members[$gm->userid] = $gm->userid;
422
    }
423
    return $groups;
424
}
425
 
426
/**
427
 * Gets array of all groups in current user.
428
 *
429
 * @since Moodle 2.5
430
 * @category group
431
 * @return array Returns an array of the group objects.
432
 */
433
function groups_get_my_groups() {
434
    global $DB, $USER;
435
 
436
    $params = ['userid' => $USER->id];
437
 
438
    $viewhidden = has_capability('moodle/course:viewhiddengroups', context_system::instance());
439
    $visibilitywhere = '';
440
    if (!$viewhidden) {
441
        $params['novisibility'] = GROUPS_VISIBILITY_NONE;
442
        $visibilitywhere = ' AND g.visibility != :novisibility';
443
    }
444
 
445
    return $DB->get_records_sql("SELECT *
446
                                   FROM {groups_members} gm
447
                                   JOIN {groups} g
448
                                    ON g.id = gm.groupid
449
                                  WHERE gm.userid = :userid
450
                                    $visibilitywhere
451
                                   ORDER BY name ASC", $params);
452
}
453
 
454
/**
455
 * Returns info about user's groups in course.
456
 *
457
 * @category group
458
 * @param int $courseid
459
 * @param int $userid $USER if not specified
460
 * @param bool $includehidden Include groups with GROUP_VISIBILITY_NONE that the user is a member of, but is not allowed to see
461
 *    themselves. Use this parameter with care - it is the responsibility of the calling code to ensure these groups are not exposed
462
 *    to the user, as this could have privacy implications.
463
 * @return array Array[groupingid][groupid] including grouping id 0 which means all groups
464
 */
465
function groups_get_user_groups(int $courseid, int $userid = 0, bool $includehidden = false): array {
466
    global $USER, $DB;
467
 
468
    if (empty($courseid)) {
469
        return ['0' => []];
470
    }
471
 
472
    if (empty($userid)) {
473
        $userid = $USER->id;
474
    }
475
 
476
    $usergroups = false;
477
    $viewhidden = $includehidden || has_capability('moodle/course:viewhiddengroups', context_course::instance($courseid));
478
    $viewall = \core_group\visibility::can_view_all_groups($courseid);
479
 
480
    $cache = cache::make('core', 'user_group_groupings');
481
 
482
    if ($viewall) {
483
        // Try to retrieve group ids from the cache.
484
        $usergroups = $cache->get($userid);
485
    }
486
 
487
    if ($usergroups === false) {
488
 
489
        $sql = "SELECT g.id, g.courseid, gg.groupingid, g.visibility
490
                  FROM {groups} g
491
                  JOIN {groups_members} gm ON gm.groupid = g.id
492
             LEFT JOIN {groupings_groups} gg ON gg.groupid = g.id
493
                 WHERE gm.userid = :userid";
494
 
495
        $params = ['userid' => $userid];
496
 
497
        if (!$viewhidden) {
498
            // Apply visibility restrictions.
499
            // Everyone can see who is in groups with ALL visibility.
500
            list($visibilitywhere, $visibilityparams) = \core_group\visibility::sql_group_visibility_where($userid);
501
            $sql .= " AND " . $visibilitywhere;
502
            $params = array_merge($params, $visibilityparams);
503
        }
504
 
505
        $sql .= ' ORDER BY g.id'; // To make results deterministic.
506
 
507
        $rs = $DB->get_recordset_sql($sql, $params);
508
 
509
        $usergroups = array();
510
        $allgroups  = array();
511
 
512
        foreach ($rs as $group) {
513
            if (!array_key_exists($group->courseid, $allgroups)) {
514
                $allgroups[$group->courseid] = array();
515
            }
516
            $allgroups[$group->courseid][$group->id] = $group->id;
517
            if (!array_key_exists($group->courseid, $usergroups)) {
518
                $usergroups[$group->courseid] = array();
519
            }
520
            if (is_null($group->groupingid)) {
521
                continue;
522
            }
523
            if (!array_key_exists($group->groupingid, $usergroups[$group->courseid])) {
524
                $usergroups[$group->courseid][$group->groupingid] = array();
525
            }
526
            $usergroups[$group->courseid][$group->groupingid][$group->id] = $group->id;
527
        }
528
        $rs->close();
529
 
530
        foreach (array_keys($allgroups) as $cid) {
531
            $usergroups[$cid]['0'] = array_keys($allgroups[$cid]); // All user groups in the course.
532
        }
533
 
534
        if ($viewall) {
535
            // Cache the data, if we got the full list of groups.
536
            $cache->set($userid, $usergroups);
537
        }
538
    }
539
 
540
    if (array_key_exists($courseid, $usergroups)) {
541
        return $usergroups[$courseid];
542
    } else {
543
        return array('0' => array());
544
    }
545
}
546
 
547
/**
548
 * Gets an array of all groupings in a specified course. This value is cached
549
 * for a single course (so you can call it repeatedly for the same course
550
 * without a performance penalty).
551
 *
552
 * @category group
553
 * @param int $courseid return all groupings from course with this courseid
554
 * @return array Returns an array of the grouping objects (empty if none)
555
 */
556
function groups_get_all_groupings($courseid) {
557
    $data = groups_get_course_data($courseid);
558
    return $data->groupings;
559
}
560
 
561
/**
562
 * Determines if the user is a member of the given group.
563
 *
564
 * If $userid is null, use the global object.
565
 *
566
 * @category group
567
 * @param int $groupid The group to check for membership.
568
 * @param int $userid The user to check against the group.
569
 * @return bool True if the user is a member, false otherwise.
570
 */
571
function groups_is_member($groupid, $userid=null) {
572
    global $USER, $DB;
573
 
574
    if (!$userid) {
575
        $userid = $USER->id;
576
    }
577
 
578
    $courseid = $DB->get_field('groups', 'courseid', ['id' => $groupid]);
579
    if (!$courseid) {
580
        return false;
581
    }
582
 
583
    if (\core_group\visibility::can_view_all_groups($courseid)) {
584
        return $DB->record_exists('groups_members', ['groupid' => $groupid, 'userid' => $userid]);
585
    }
586
 
587
    $sql = "SELECT *
588
              FROM {groups_members} gm
589
                   JOIN {groups} g ON gm.groupid = g.id
590
             WHERE g.id = :groupid
591
                   AND gm.userid = :userid";
592
    $params = ['groupid' => $groupid, 'userid' => $userid];
593
 
594
    list($visibilitywhere, $visibilityparams) = \core_group\visibility::sql_group_visibility_where($userid);
595
 
596
    $sql .= " AND " . $visibilitywhere;
597
    $params = array_merge($params, $visibilityparams);
598
 
599
    return $DB->record_exists_sql($sql, $params);
600
}
601
 
602
/**
603
 * Determines if current or specified is member of any active group in activity
604
 *
605
 * @category group
606
 * @staticvar array $cache
607
 * @param stdClass|cm_info $cm course module object
608
 * @param int $userid id of user, null means $USER->id
609
 * @return bool true if user member of at least one group used in activity
610
 */
611
function groups_has_membership($cm, $userid=null) {
612
    global $CFG, $USER, $DB;
613
 
614
    static $cache = array();
615
 
616
    if (empty($userid)) {
617
        $userid = $USER->id;
618
    }
619
 
620
    $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
621
    if (isset($cache[$cachekey])) {
622
        return($cache[$cachekey]);
623
    }
624
 
625
    if ($cm->groupingid) {
626
        // find out if member of any group in selected activity grouping
627
        $sql = "SELECT 'x'
628
                  FROM {groups_members} gm, {groupings_groups} gg
629
                 WHERE gm.userid = ? AND gm.groupid = gg.groupid AND gg.groupingid = ?";
630
        $params = array($userid, $cm->groupingid);
631
 
632
    } else {
633
        // no grouping used - check all groups in course
634
        $sql = "SELECT 'x'
635
                  FROM {groups_members} gm, {groups} g
636
                 WHERE gm.userid = ? AND gm.groupid = g.id AND g.courseid = ?";
637
        $params = array($userid, $cm->course);
638
    }
639
 
640
    $cache[$cachekey] = $DB->record_exists_sql($sql, $params);
641
 
642
    return $cache[$cachekey];
643
}
644
 
645
/**
646
 * Returns the users in the specified group.
647
 *
648
 * @category group
649
 * @param int $groupid The groupid to get the users for
650
 * @param int $fields The fields to return
651
 * @param int $sort optional sorting of returned users
652
 * @return array Returns an array of the users for the specified group
653
 */
654
function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
655
    global $DB;
656
 
657
    if (empty($groupid)) {
658
        return [];
659
    }
660
 
661
    $courseid = $DB->get_field('groups', 'courseid', ['id' => $groupid]);
662
    if ($courseid === false) {
663
        return [];
664
    }
665
 
666
    $select = "SELECT $fields";
667
    $from = "FROM {user} u
668
                  JOIN {groups_members} gm ON gm.userid = u.id";
669
    $where = "WHERE gm.groupid = :groupid";
670
    $order = "ORDER BY $sort";
671
 
672
    $params = ['groupid' => $groupid];
673
 
674
    if (!\core_group\visibility::can_view_all_groups($courseid)) {
675
        $from .= " JOIN {groups} g ON g.id = gm.groupid";
676
        // Can view memberships of visibility is ALL, visibility is MEMBERS and current user is a member,
677
        // or visibility is OWN and this is their membership.
678
        list($visibilitywhere, $visibilityparams) = \core_group\visibility::sql_member_visibility_where();
679
        $params = array_merge($params, $visibilityparams);
680
        $where .= ' AND ' . $visibilitywhere;
681
    }
682
 
683
    $sql = implode(PHP_EOL, [$select, $from, $where, $order]);
684
 
685
    return $DB->get_records_sql($sql, $params);
686
}
687
 
688
 
689
/**
690
 * Returns the users in the specified grouping.
691
 *
692
 * @category group
693
 * @param int $groupingid The groupingid to get the users for
694
 * @param string $fields The fields to return
695
 * @param string $sort optional sorting of returned users
696
 * @return array|bool Returns an array of the users for the specified
697
 * group or false if no users or an error returned.
698
 */
699
function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
700
    global $DB;
701
 
702
    return $DB->get_records_sql("SELECT $fields
703
                                   FROM {user} u
704
                                     INNER JOIN {groups_members} gm ON u.id = gm.userid
705
                                     INNER JOIN {groupings_groups} gg ON gm.groupid = gg.groupid
706
                                  WHERE  gg.groupingid = ?
707
                               ORDER BY $sort", array($groupingid));
708
}
709
 
710
/**
711
 * Returns effective groupmode used in course
712
 *
713
 * @category group
714
 * @param stdClass $course course object.
715
 * @return int group mode
716
 */
717
function groups_get_course_groupmode($course) {
718
    return $course->groupmode;
719
}
720
 
721
/**
722
 * Returns effective groupmode used in activity, course setting
723
 * overrides activity setting if groupmodeforce enabled.
724
 *
725
 * If $cm is an instance of cm_info it is easier to use $cm->effectivegroupmode
726
 *
727
 * @category group
728
 * @param cm_info|stdClass $cm the course module object. Only the ->course and ->groupmode need to be set.
729
 * @param stdClass $course object optional course object to improve perf
730
 * @return int group mode
731
 */
732
function groups_get_activity_groupmode($cm, $course=null) {
733
    if ($cm instanceof cm_info) {
734
        return $cm->effectivegroupmode;
735
    }
736
    if (isset($course->id) and $course->id == $cm->course) {
737
        //ok
738
    } else {
739
        // Get course object (reuse $COURSE if possible).
740
        $course = get_course($cm->course, false);
741
    }
742
 
743
    return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
744
}
745
 
746
/**
747
 * Print group menu selector for course level.
748
 *
749
 * @category group
750
 * @param stdClass $course course object
751
 * @param mixed $urlroot return address. Accepts either a string or a moodle_url
752
 * @param bool $return return as string instead of printing
753
 * @return mixed void or string depending on $return param
754
 */
755
function groups_print_course_menu($course, $urlroot, $return=false) {
756
    global $USER, $OUTPUT;
757
 
758
    if (!$groupmode = $course->groupmode) {
759
        if ($return) {
760
            return '';
761
        } else {
762
            return;
763
        }
764
    }
765
 
766
    $context = context_course::instance($course->id);
767
    $aag = has_capability('moodle/site:accessallgroups', $context);
768
 
769
    $usergroups = array();
770
    if ($groupmode == VISIBLEGROUPS or $aag) {
771
        $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
772
        // Get user's own groups and put to the top.
773
        $usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
774
    } else {
775
        $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
776
    }
777
 
778
    $activegroup = groups_get_course_group($course, true, $allowedgroups);
779
 
780
    $groupsmenu = array();
781
    if (!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) {
782
        $groupsmenu[0] = get_string('allparticipants');
783
    }
784
 
785
    $groupsmenu += groups_sort_menu_options($allowedgroups, $usergroups);
786
 
787
    if ($groupmode == VISIBLEGROUPS) {
788
        $grouplabel = get_string('groupsvisible');
789
    } else {
790
        $grouplabel = get_string('groupsseparate');
791
    }
792
 
793
    if ($aag and $course->defaultgroupingid) {
794
        if ($grouping = groups_get_grouping($course->defaultgroupingid)) {
795
            $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
796
        }
797
    }
798
 
799
    if (count($groupsmenu) == 1) {
800
        $groupname = reset($groupsmenu);
801
        $output = $grouplabel.': '.$groupname;
802
    } else {
803
        $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
804
        $select->label = $grouplabel;
805
        $output = $OUTPUT->render($select);
806
    }
807
 
808
    $output = '<div class="groupselector">'.$output.'</div>';
809
 
810
    if ($return) {
811
        return $output;
812
    } else {
813
        echo $output;
814
    }
815
}
816
 
817
/**
818
 * Turn an array of groups into an array of menu options.
819
 * @param array $groups of group objects.
820
 * @return array groupid => formatted group name.
821
 */
822
function groups_list_to_menu($groups) {
823
    $groupsmenu = array();
824
    foreach ($groups as $group) {
825
        $groupsmenu[$group->id] = format_string($group->name);
826
    }
827
    return $groupsmenu;
828
}
829
 
830
/**
831
 * Takes user's allowed groups and own groups and formats for use in group selector menu
832
 * If user has allowed groups + own groups will add to an optgroup
833
 * Own groups are removed from allowed groups
834
 * @param array $allowedgroups All groups user is allowed to see
835
 * @param array $usergroups Groups user belongs to
836
 * @return array
837
 */
838
function groups_sort_menu_options($allowedgroups, $usergroups) {
839
    $useroptions = array();
840
    if ($usergroups) {
841
        $useroptions = groups_list_to_menu($usergroups);
842
 
843
        // Remove user groups from other groups list.
844
        foreach ($usergroups as $group) {
845
            unset($allowedgroups[$group->id]);
846
        }
847
    }
848
 
849
    $allowedoptions = array();
850
    if ($allowedgroups) {
851
        $allowedoptions = groups_list_to_menu($allowedgroups);
852
    }
853
 
854
    if ($useroptions && $allowedoptions) {
855
        return array(
856
            1 => array(get_string('mygroups', 'group') => $useroptions),
857
            2 => array(get_string('othergroups', 'group') => $allowedoptions)
858
        );
859
    } else if ($useroptions) {
860
        return $useroptions;
861
    } else {
862
        return $allowedoptions;
863
    }
864
}
865
 
866
/**
867
 * Generates html to print menu selector for course level, listing all groups.
868
 * Note: This api does not do any group mode check use groups_print_course_menu() instead if you want proper checks.
869
 *
870
 * @param stdclass          $course  course object.
871
 * @param string|moodle_url $urlroot return address. Accepts either a string or a moodle_url.
872
 * @param bool              $update  set this to true to update current active group based on the group param.
873
 * @param int               $activegroup Change group active to this group if $update set to true.
874
 *
875
 * @return string html or void
876
 */
877
function groups_allgroups_course_menu($course, $urlroot, $update = false, $activegroup = 0) {
878
    global $SESSION, $OUTPUT, $USER;
879
 
880
    $groupmode = groups_get_course_groupmode($course);
881
    $context = context_course::instance($course->id);
882
    $groupsmenu = array();
883
 
884
    if (has_capability('moodle/site:accessallgroups', $context)) {
885
        $groupsmenu[0] = get_string('allparticipants');
886
        $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
887
    } else {
888
        $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
889
    }
890
 
891
    $groupsmenu += groups_list_to_menu($allowedgroups);
892
 
893
    if ($update) {
894
        // Init activegroup array if necessary.
895
        if (!isset($SESSION->activegroup)) {
896
            $SESSION->activegroup = array();
897
        }
898
        if (!isset($SESSION->activegroup[$course->id])) {
899
            $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS => array(), VISIBLEGROUPS => array(), 'aag' => array());
900
        }
901
        if (empty($groupsmenu[$activegroup])) {
902
            $activegroup = key($groupsmenu); // Force set to one of accessible groups.
903
        }
904
        $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = $activegroup;
905
    }
906
 
907
    $grouplabel = get_string('groups');
908
    if (count($groupsmenu) == 0) {
909
        return '';
910
    } else if (count($groupsmenu) == 1) {
911
        $groupname = reset($groupsmenu);
912
        $output = $grouplabel.': '.$groupname;
913
    } else {
914
        $select = new single_select(new moodle_url($urlroot), 'group', $groupsmenu, $activegroup, null, 'selectgroup');
915
        $select->label = $grouplabel;
916
        $output = $OUTPUT->render($select);
917
    }
918
 
919
    return $output;
920
 
921
}
922
 
923
/**
924
 * Print group menu selector for activity.
925
 *
926
 * @category group
927
 * @param stdClass|cm_info $cm course module object
928
 * @param string|moodle_url $urlroot return address that users get to if they choose an option;
929
 *   should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
930
 * @param bool $return return as string instead of printing
931
 * @param bool $hideallparticipants If true, this prevents the 'All participants'
932
 *   option from appearing in cases where it normally would. This is intended for
933
 *   use only by activities that cannot display all groups together. (Note that
934
 *   selecting this option does not prevent groups_get_activity_group from
935
 *   returning 0; it will still do that if the user has chosen 'all participants'
936
 *   in another activity, or not chosen anything.)
937
 * @return mixed void or string depending on $return param
938
 */
939
function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
940
    global $USER, $OUTPUT;
941
 
942
    if ($urlroot instanceof moodle_url) {
943
        // no changes necessary
944
 
945
    } else {
946
        if (strpos($urlroot, 'http') !== 0) { // Will also work for https
947
            // Display error if urlroot is not absolute (this causes the non-JS version to break)
948
            debugging('groups_print_activity_menu requires absolute URL for ' .
949
                      '$urlroot, not <tt>' . s($urlroot) . '</tt>. Example: ' .
950
                      'groups_print_activity_menu($cm, $CFG->wwwroot . \'/mod/mymodule/view.php?id=13\');',
951
                      DEBUG_DEVELOPER);
952
        }
953
        $urlroot = new moodle_url($urlroot);
954
    }
955
 
956
    if (!$groupmode = groups_get_activity_groupmode($cm)) {
957
        if ($return) {
958
            return '';
959
        } else {
960
            return;
961
        }
962
    }
963
 
964
    $context = context_module::instance($cm->id);
965
    $aag = has_capability('moodle/site:accessallgroups', $context);
966
 
967
    $usergroups = array();
968
    if ($groupmode == VISIBLEGROUPS or $aag) {
969
        $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid, 'g.*', false, true); // Any group in grouping.
970
        // Get user's own groups and put to the top.
971
        $usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid, 'g.*', false, true);
972
    } else {
973
        // Only assigned groups.
974
        $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid, 'g.*', false, true);
975
    }
976
 
977
    $activegroup = groups_get_activity_group($cm, true, $allowedgroups);
978
 
979
    $groupsmenu = array();
980
    if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or $aag) and !$hideallparticipants) {
981
        $groupsmenu[0] = get_string('allparticipants');
982
    }
983
 
984
    $groupsmenu += groups_sort_menu_options($allowedgroups, $usergroups);
985
 
986
    if ($groupmode == VISIBLEGROUPS) {
987
        $grouplabel = get_string('groupsvisible');
988
    } else {
989
        $grouplabel = get_string('groupsseparate');
990
    }
991
 
992
    if ($aag and $cm->groupingid) {
993
        if ($grouping = groups_get_grouping($cm->groupingid)) {
994
            $grouplabel = $grouplabel . ' (' . format_string($grouping->name) . ')';
995
        }
996
    }
997
 
998
    if (count($groupsmenu) == 1) {
999
        $groupname = reset($groupsmenu);
1000
        $output = $grouplabel.': '.$groupname;
1001
    } else {
1002
        $select = new single_select($urlroot, 'group', $groupsmenu, $activegroup, null, 'selectgroup');
1003
        $select->label = $grouplabel;
1004
        $output = $OUTPUT->render($select);
1005
    }
1006
 
1007
    $output = '<div class="groupselector">'.$output.'</div>';
1008
 
1009
    if ($return) {
1010
        return $output;
1011
    } else {
1012
        echo $output;
1013
    }
1014
}
1015
 
1016
/**
1017
 * Returns group active in course, changes the group by default if 'group' page param present
1018
 *
1019
 * @category group
1020
 * @param stdClass $course course bject
1021
 * @param bool $update change active group if group param submitted
1022
 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
1023
 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
1024
 */
1025
function groups_get_course_group($course, $update=false, $allowedgroups=null) {
1026
    global $USER, $SESSION;
1027
 
1028
    if (!$groupmode = $course->groupmode) {
1029
        // NOGROUPS used
1030
        return false;
1031
    }
1032
 
1033
    $context = context_course::instance($course->id);
1034
    if (has_capability('moodle/site:accessallgroups', $context)) {
1035
        $groupmode = 'aag';
1036
    }
1037
 
1038
    if (!is_array($allowedgroups)) {
1039
        if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
1040
            $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
1041
        } else {
1042
            $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
1043
        }
1044
    }
1045
 
1046
    _group_verify_activegroup($course->id, $groupmode, $course->defaultgroupingid, $allowedgroups);
1047
 
1048
    // set new active group if requested
1049
    $changegroup = optional_param('group', -1, PARAM_INT);
1050
    if ($update and $changegroup != -1) {
1051
 
1052
        if ($changegroup == 0) {
1053
            // do not allow changing to all groups without accessallgroups capability
1054
            if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
1055
                $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = 0;
1056
            }
1057
 
1058
        } else {
1059
            if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
1060
                $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid] = $changegroup;
1061
            }
1062
        }
1063
    }
1064
 
1065
    return $SESSION->activegroup[$course->id][$groupmode][$course->defaultgroupingid];
1066
}
1067
 
1068
/**
1069
 * Returns group active in activity, changes the group by default if 'group' page param present
1070
 *
1071
 * @category group
1072
 * @param stdClass|cm_info $cm course module object
1073
 * @param bool $update change active group if group param submitted
1074
 * @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
1075
 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
1076
 */
1077
function groups_get_activity_group($cm, $update=false, $allowedgroups=null) {
1078
    global $USER, $SESSION;
1079
 
1080
    if (!$groupmode = groups_get_activity_groupmode($cm)) {
1081
        // NOGROUPS used
1082
        return false;
1083
    }
1084
 
1085
    $context = context_module::instance($cm->id);
1086
    if (has_capability('moodle/site:accessallgroups', $context)) {
1087
        $groupmode = 'aag';
1088
    }
1089
 
1090
    if (!is_array($allowedgroups)) {
1091
        if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
1092
            $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid, 'g.*', false, true);
1093
        } else {
1094
            $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid, 'g.*', false, true);
1095
        }
1096
    }
1097
 
1098
    _group_verify_activegroup($cm->course, $groupmode, $cm->groupingid, $allowedgroups);
1099
 
1100
    // set new active group if requested
1101
    $changegroup = optional_param('group', -1, PARAM_INT);
1102
    if ($update and $changegroup != -1) {
1103
 
1104
        if ($changegroup == 0) {
1105
            // allgroups visible only in VISIBLEGROUPS or when accessallgroups
1106
            if ($groupmode == VISIBLEGROUPS or $groupmode === 'aag') {
1107
                $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
1108
            }
1109
 
1110
        } else {
1111
            if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
1112
                $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
1113
            }
1114
        }
1115
    }
1116
 
1117
    return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
1118
}
1119
 
1120
/**
1121
 * Gets a list of groups that the user is allowed to access within the
1122
 * specified activity.
1123
 *
1124
 * @category group
1125
 * @param stdClass|cm_info $cm Course-module
1126
 * @param int $userid User ID (defaults to current user)
1127
 * @return array An array of group objects, or false if none
1128
 */
1129
function groups_get_activity_allowed_groups($cm,$userid=0) {
1130
    // Use current user by default
1131
    global $USER;
1132
    if(!$userid) {
1133
        $userid=$USER->id;
1134
    }
1135
 
1136
    // Get groupmode for activity, taking into account course settings
1137
    $groupmode=groups_get_activity_groupmode($cm);
1138
 
1139
    // If visible groups mode, or user has the accessallgroups capability,
1140
    // then they can access all groups for the activity...
1141
    $context = context_module::instance($cm->id);
1142
    if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context, $userid)) {
1143
        return groups_get_all_groups($cm->course, 0, $cm->groupingid, 'g.*', false, true);
1144
    } else {
1145
        // ...otherwise they can only access groups they belong to
1146
        return groups_get_all_groups($cm->course, $userid, $cm->groupingid, 'g.*', false, true);
1147
    }
1148
}
1149
 
1150
/**
1151
 * Determine if a given group is visible to user or not in a given context.
1152
 *
1153
 * @since Moodle 2.6
1154
 * @param int      $groupid Group id to test. 0 for all groups.
1155
 * @param stdClass $course  Course object.
1156
 * @param stdClass $cm      Course module object.
1157
 * @param int      $userid  user id to test against. Defaults to $USER.
1158
 * @return boolean true if visible, false otherwise
1159
 */
1160
function groups_group_visible($groupid, $course, $cm = null, $userid = null) {
1161
    global $USER;
1162
 
1163
    if (empty($userid)) {
1164
        $userid = $USER->id;
1165
    }
1166
 
1167
    $groupmode = empty($cm) ? groups_get_course_groupmode($course) : groups_get_activity_groupmode($cm, $course);
1168
    if ($groupmode == NOGROUPS || $groupmode == VISIBLEGROUPS) {
1169
        // Groups are not used, or everything is visible, no need to go any further.
1170
        return true;
1171
    }
1172
 
1173
    $context = empty($cm) ? context_course::instance($course->id) : context_module::instance($cm->id);
1174
    if (has_capability('moodle/site:accessallgroups', $context, $userid)) {
1175
        // User can see everything. Groupid = 0 is handled here as well.
1176
        return true;
1177
    } else if ($groupid != 0) {
1178
        // Group mode is separate, and user doesn't have access all groups capability. Check if user can see requested group.
1179
        $groups = empty($cm) ? groups_get_all_groups($course->id, $userid) : groups_get_activity_allowed_groups($cm, $userid);
1180
        if (array_key_exists($groupid, $groups)) {
1181
            // User can see the group.
1182
            return true;
1183
        }
1184
    }
1185
    return false;
1186
}
1187
 
1188
/**
1189
 * Get sql and parameters that will return user ids for a group or groups
1190
 *
1191
 * @param int|array $groupids Where this is an array of multiple groups, it will match on members of any of the groups
1192
 * @param context $context Course context or a context within a course. Mandatory when $groupid = USERSWITHOUTGROUP
1193
 * @param int $groupsjointype Join type logic used. Defaults to 'Any' (logical OR).
1194
 * @return array($sql, $params)
1195
 * @throws coding_exception if empty or invalid context submitted when $groupid = USERSWITHOUTGROUP
1196
 */
1197
function groups_get_members_ids_sql($groupids, context $context = null, $groupsjointype = GROUPS_JOIN_ANY) {
1198
    if (!is_array($groupids)) {
1199
        $groupids = [$groupids];
1200
    }
1201
 
1202
    $groupjoin = groups_get_members_join($groupids, 'u.id', $context, $groupsjointype);
1203
 
1204
    $sql = "SELECT DISTINCT u.id
1205
              FROM {user} u
1206
            $groupjoin->joins
1207
             WHERE u.deleted = 0";
1208
    if (!empty($groupjoin->wheres)) {
1209
        $sql .= ' AND '. $groupjoin->wheres;
1210
    }
1211
 
1212
    return array($sql, $groupjoin->params);
1213
}
1214
 
1215
/**
1216
 * Returns array with SQL and parameters returning userids and concatenated group names for given course
1217
 *
1218
 * This function uses 'gn[0-9]+_' prefix for table names and parameters
1219
 *
1220
 * @param int $courseid
1221
 * @param string $separator
1222
 * @return array [$sql, $params]
1223
 */
1224
function groups_get_names_concat_sql(int $courseid, string $separator = ', '): array {
1225
    global $DB;
1226
 
1227
    // Use unique prefix just in case somebody makes some SQL magic with the result.
1228
    static $i = 0;
1229
    $i++;
1230
    $prefix = "gn{$i}_";
1231
 
1232
    $groupalias = $prefix . 'g';
1233
    $groupmemberalias = $prefix . 'gm';
1234
    $groupcourseparam = $prefix . 'courseid';
1235
 
1236
    $sqlgroupconcat = $DB->sql_group_concat("{$groupalias}.name", $separator, "{$groupalias}.name");
1237
 
1238
    $sql = "SELECT {$groupmemberalias}.userid, {$sqlgroupconcat} AS groupnames
1239
              FROM {groups} {$groupalias}
1240
              JOIN {groups_members} {$groupmemberalias} ON {$groupmemberalias}.groupid = {$groupalias}.id
1241
             WHERE {$groupalias}.courseid = :{$groupcourseparam}
1242
          GROUP BY {$groupmemberalias}.userid";
1243
 
1244
    return [$sql, [$groupcourseparam => $courseid]];
1245
};
1246
 
1247
/**
1248
 * Get sql join to return users in a group
1249
 *
1250
 * @param int|array $groupids The groupids, 0 or [] means all groups and USERSWITHOUTGROUP no group
1251
 * @param string $useridcolumn The column of the user id from the calling SQL, e.g. u.id
1252
 * @param context $context Course context or a context within a course. Mandatory when $groupids includes USERSWITHOUTGROUP
1253
 * @param int $jointype Join type logic used. Defaults to 'Any' (logical OR).
1254
 * @return \core\dml\sql_join Contains joins, wheres, params
1255
 * @throws coding_exception if empty or invalid context submitted when $groupid = USERSWITHOUTGROUP
1256
 */
1257
function groups_get_members_join($groupids, $useridcolumn, context $context = null, int $jointype = GROUPS_JOIN_ANY) {
1258
    global $DB;
1259
 
1260
    // Use unique prefix just in case somebody makes some SQL magic with the result.
1261
    static $i = 0;
1262
    $i++;
1263
    $prefix = 'gm' . $i . '_';
1264
 
1265
    if (!is_array($groupids)) {
1266
        $groupids = $groupids ? [$groupids] : [];
1267
    }
1268
 
1269
    $joins = [];
1270
    $where = '';
1271
    $param = [];
1272
 
1273
    $coursecontext = (!empty($context)) ? $context->get_course_context() : null;
1274
    if (in_array(USERSWITHOUTGROUP, $groupids) && empty($coursecontext)) {
1275
        // Throw an exception if $context is empty or invalid because it's needed to get the users without any group.
1276
        throw new coding_exception('Missing or wrong $context parameter in an attempt to get members without any group');
1277
    }
1278
    // Can we view hidden groups within a course?
1279
    [$ualias] = explode('.', $useridcolumn);
1280
    $viewhidden = false;
1281
    if (!empty($coursecontext)) {
1282
        $viewhidden = \core_group\visibility::can_view_all_groups($coursecontext->instanceid);
1283
    }
1284
 
1285
    // Handle cases where we need to include/exclude users not in any groups.
1286
    if (($nogroupskey = array_search(USERSWITHOUTGROUP, $groupids)) !== false) {
1287
        $visibilityjoin = '';
1288
        $visibilitywhere = '';
1289
 
1290
        if (!$viewhidden) {
1291
            $visibilityjoin = 'JOIN {user} u ON u.id = m.userid';
1292
            [$visibilitywhere, $visibilityparams] = \core_group\visibility::sql_member_visibility_where('g', 'm');
1293
            $param = array_merge($param, $visibilityparams);
1294
            $visibilitywhere = 'WHERE ' . $visibilitywhere;
1295
        }
1296
        // Get members without any group, or only in groups we cannot see membership of.
1297
        $joins[] = "LEFT JOIN (
1298
                     SELECT g.courseid, m.groupid, m.userid
1299
                       FROM {groups_members} m
1300
                       JOIN {groups} g ON g.id = m.groupid
1301
                       {$visibilityjoin}
1302
                       {$visibilitywhere}
1303
                  ) {$prefix}gm ON ({$prefix}gm.userid = {$useridcolumn} AND {$prefix}gm.courseid = :{$prefix}gcourseid)";
1304
 
1305
        // Join type 'None' when filtering by 'no groups' means match users in at least one group.
1306
        if ($jointype == GROUPS_JOIN_NONE) {
1307
            $where = "{$prefix}gm.userid IS NOT NULL";
1308
        } else {
1309
            // All other cases need to match users not in any group.
1310
            $where = "{$prefix}gm.userid IS NULL";
1311
        }
1312
 
1313
        $param["{$prefix}gcourseid"] = $coursecontext->instanceid;
1314
        unset($groupids[$nogroupskey]);
1315
    }
1316
 
1317
    // Handle any specified groups that need to be included.
1318
    if (!empty($groupids)) {
1319
        switch ($jointype) {
1320
            case GROUPS_JOIN_ALL:
1321
                // Handle matching all of the provided groups (logical AND).
1322
                $joinallwheres = [];
1323
                $aliaskey = 0;
1324
                foreach ($groupids as $groupid) {
1325
                    $gmalias = "{$prefix}gm{$aliaskey}";
1326
                    $aliaskey++;
1327
                    $joins[] = "LEFT JOIN {groups_members} {$gmalias}
1328
                                     ON ({$gmalias}.userid = {$useridcolumn} AND {$gmalias}.groupid = :{$gmalias}param)";
1329
                    $joinallwheres[] = "{$gmalias}.userid IS NOT NULL";
1330
                    $param["{$gmalias}param"] = $groupid;
1331
                    if (!$viewhidden) {
1332
                        $galias = "{$prefix}g{$aliaskey}";
1333
                        $joins[] = "LEFT JOIN {groups} {$galias} ON {$gmalias}.groupid = {$galias}.id";
1334
                        [$visibilitywhere, $visibilityparams] = \core_group\visibility::sql_member_visibility_where(
1335
                            $galias,
1336
                            $gmalias,
1337
                            $ualias,
1338
                            $prefix . $aliaskey . '_'
1339
                        );
1340
                        $joinallwheres[] = $visibilitywhere;
1341
                        $param = array_merge($param, $visibilityparams);
1342
                    }
1343
                }
1344
 
1345
                // Members of all of the specified groups only.
1346
                if (empty($where)) {
1347
                    $where = '(' . implode(' AND ', $joinallwheres) . ')';
1348
                } else {
1349
                    // Members of the specified groups and also no groups.
1350
                    // NOTE: This will always return no results, because you cannot be in specified groups and also be in no groups.
1351
                    $where = '(' . $where . ' AND ' . implode(' AND ', $joinallwheres) . ')';
1352
                }
1353
 
1354
                break;
1355
 
1356
            case GROUPS_JOIN_ANY:
1357
                // Handle matching any of the provided groups (logical OR).
1358
                list($groupssql, $groupsparams) = $DB->get_in_or_equal($groupids, SQL_PARAMS_NAMED, $prefix);
1359
 
1360
                $joins[] = "LEFT JOIN {groups_members} {$prefix}gm2
1361
                                 ON ({$prefix}gm2.userid = {$useridcolumn} AND {$prefix}gm2.groupid {$groupssql})";
1362
                $param = array_merge($param, $groupsparams);
1363
 
1364
                // Members of any of the specified groups only.
1365
                if (empty($where)) {
1366
                    $where = "{$prefix}gm2.userid IS NOT NULL";
1367
                } else {
1368
                    // Members of any of the specified groups or no groups.
1369
                    $where = "({$where} OR {$prefix}gm2.userid IS NOT NULL)";
1370
                }
1371
 
1372
                if (!$viewhidden) {
1373
                    $joins[] = "LEFT JOIN {groups} {$prefix}g2 ON {$prefix}gm2.groupid = {$prefix}g2.id";
1374
                    [$visibilitywhere, $visibilityparams] = \core_group\visibility::sql_member_visibility_where(
1375
                        $prefix . 'g2',
1376
                        $prefix . 'gm2',
1377
                        $ualias,
1378
                        $prefix . 'param_'
1379
                    );
1380
                    $where .= ' AND ' . $visibilitywhere;
1381
                    $param = array_merge($param, $visibilityparams);
1382
                }
1383
 
1384
                break;
1385
 
1386
            case GROUPS_JOIN_NONE:
1387
                // Handle matching none of the provided groups (logical NOT).
1388
                list($groupssql, $groupsparams) = $DB->get_in_or_equal($groupids, SQL_PARAMS_NAMED, $prefix);
1389
 
1390
                $joins[] = "LEFT JOIN {groups_members} {$prefix}gm2
1391
                                 ON ({$prefix}gm2.userid = {$useridcolumn} AND {$prefix}gm2.groupid {$groupssql})";
1392
                $param = array_merge($param, $groupsparams);
1393
 
1394
                // Members of none of the specified groups only.
1395
                if (empty($where)) {
1396
                    $where = "{$prefix}gm2.userid IS NULL";
1397
                } else {
1398
                    // Members of any unspecified groups (not a member of the specified groups, and not a member of no groups).
1399
                    $where = "({$where} AND {$prefix}gm2.userid IS NULL)";
1400
                }
1401
 
1402
                if (!$viewhidden) {
1403
                    $joins[] = "LEFT JOIN {groups} {$prefix}g2 ON {$prefix}gm2.groupid = {$prefix}g2.id";
1404
                    [$visibilitywhere, $visibilityparams] = \core_group\visibility::sql_member_visibility_where(
1405
                        $prefix . 'g2',
1406
                        $prefix . 'gm2',
1407
                        $ualias
1408
                    );
1409
                    $where .= ' OR NOT ' . $visibilitywhere;
1410
                    $param = array_merge($param, $visibilityparams);
1411
                }
1412
 
1413
                break;
1414
        }
1415
    }
1416
 
1417
    return new \core\dml\sql_join(implode("\n", $joins), $where, $param);
1418
}
1419
 
1420
/**
1421
 * Internal method, sets up $SESSION->activegroup and verifies previous value
1422
 *
1423
 * @param int $courseid
1424
 * @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
1425
 * @param int $groupingid 0 means all groups
1426
 * @param array $allowedgroups list of groups user can see
1427
 */
1428
function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
1429
    global $SESSION, $USER;
1430
 
1431
    // init activegroup array if necessary
1432
    if (!isset($SESSION->activegroup)) {
1433
        $SESSION->activegroup = array();
1434
    }
1435
    if (!array_key_exists($courseid, $SESSION->activegroup)) {
1436
        $SESSION->activegroup[$courseid] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
1437
    }
1438
 
1439
    // make sure that the current group info is ok
1440
    if (array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode]) and !array_key_exists($SESSION->activegroup[$courseid][$groupmode][$groupingid], $allowedgroups)) {
1441
        // active group does not exist anymore or is 0
1442
        if ($SESSION->activegroup[$courseid][$groupmode][$groupingid] > 0 or $groupmode == SEPARATEGROUPS) {
1443
            // do not do this if all groups selected and groupmode is not separate
1444
            unset($SESSION->activegroup[$courseid][$groupmode][$groupingid]);
1445
        }
1446
    }
1447
 
1448
    // set up defaults if necessary
1449
    if (!array_key_exists($groupingid, $SESSION->activegroup[$courseid][$groupmode])) {
1450
        if ($groupmode == 'aag') {
1451
            $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0; // all groups by default if user has accessallgroups
1452
 
1453
        } else if ($allowedgroups) {
1454
            if ($groupmode != SEPARATEGROUPS
1455
                    && $mygroups = groups_get_all_groups($courseid, $USER->id, $groupingid, 'g.*', false, true)) {
1456
                $firstgroup = reset($mygroups);
1457
            } else {
1458
                $firstgroup = reset($allowedgroups);
1459
            }
1460
            $SESSION->activegroup[$courseid][$groupmode][$groupingid] = $firstgroup->id;
1461
 
1462
        } else {
1463
            // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
1464
            // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
1465
            $SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0;
1466
        }
1467
    }
1468
}
1469
 
1470
/**
1471
 * Caches group data for a particular course to speed up subsequent requests.
1472
 *
1473
 * @param int $courseid The course id to cache data for.
1474
 * @param cache $cache The cache if it has already been initialised. If not a new one will be created.
1475
 * @return stdClass A data object containing groups, groupings, and mappings.
1476
 */
1477
function groups_cache_groupdata($courseid, cache $cache = null) {
1478
    global $DB;
1479
 
1480
    if ($cache === null) {
1481
        // Initialise a cache if we wern't given one.
1482
        $cache = cache::make('core', 'groupdata');
1483
    }
1484
 
1485
    // Get the groups that belong to the course.
1486
    $groups = $DB->get_records('groups', array('courseid' => $courseid), 'name ASC');
1487
    // Get the groupings that belong to the course.
1488
    $groupings = $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
1489
 
1490
    if (!is_array($groups)) {
1491
        $groups = array();
1492
    }
1493
 
1494
    if (!is_array($groupings)) {
1495
        $groupings = array();
1496
    }
1497
 
1498
    if (!empty($groupings)) {
1499
        // Finally get the mappings between the two.
1500
        list($insql, $params) = $DB->get_in_or_equal(array_keys($groupings));
1501
        $mappings = $DB->get_records_sql("
1502
                SELECT gg.id, gg.groupingid, gg.groupid
1503
                  FROM {groupings_groups} gg
1504
                  JOIN {groups} g ON g.id = gg.groupid
1505
                 WHERE gg.groupingid $insql
1506
              ORDER BY g.name ASC", $params);
1507
    } else {
1508
        $mappings = array();
1509
    }
1510
 
1511
    // Prepare the data array.
1512
    $data = new stdClass;
1513
    $data->groups = $groups;
1514
    $data->groupings = $groupings;
1515
    $data->mappings = $mappings;
1516
    // Cache the data.
1517
    $cache->set($courseid, $data);
1518
    // Finally return it so it can be used if desired.
1519
    return $data;
1520
}
1521
 
1522
/**
1523
 * Gets group data for a course.
1524
 *
1525
 * This returns an object with the following properties:
1526
 *   - groups : An array of all the groups in the course.
1527
 *   - groupings : An array of all the groupings within the course.
1528
 *   - mappings : An array of group to grouping mappings.
1529
 *
1530
 * @param int $courseid The course id to get data for.
1531
 * @param cache $cache The cache if it has already been initialised. If not a new one will be created.
1532
 * @return stdClass
1533
 */
1534
function groups_get_course_data($courseid, cache $cache = null) {
1535
    if ($cache === null) {
1536
        // Initialise a cache if we wern't given one.
1537
        $cache = cache::make('core', 'groupdata');
1538
    }
1539
    // Try to retrieve it from the cache.
1540
    $data = $cache->get($courseid);
1541
    if ($data === false) {
1542
        $data = groups_cache_groupdata($courseid, $cache);
1543
    }
1544
    return $data;
1545
}
1546
 
1547
/**
1548
 * Determine if the current user can see at least one of the groups of the specified user.
1549
 *
1550
 * @param stdClass $course  Course object.
1551
 * @param int $userid  user id to check against.
1552
 * @param stdClass $cm Course module object. Optional, just for checking at activity level instead course one.
1553
 * @return boolean true if visible, false otherwise
1554
 * @since Moodle 2.9
1555
 */
1556
function groups_user_groups_visible($course, $userid, $cm = null) {
1557
    global $USER;
1558
 
1559
    $groupmode = empty($cm) ? groups_get_course_groupmode($course) : groups_get_activity_groupmode($cm, $course);
1560
    if ($groupmode == NOGROUPS || $groupmode == VISIBLEGROUPS) {
1561
        // Groups are not used, or everything is visible, no need to go any further.
1562
        return true;
1563
    }
1564
 
1565
    $context = empty($cm) ? context_course::instance($course->id) : context_module::instance($cm->id);
1566
    if (has_capability('moodle/site:accessallgroups', $context)) {
1567
        // User can see everything.
1568
        return true;
1569
    } else {
1570
        // Group mode is separate, and user doesn't have access all groups capability.
1571
        if (empty($cm)) {
1572
            $usergroups = groups_get_all_groups($course->id, $userid);
1573
            $currentusergroups = groups_get_all_groups($course->id, $USER->id);
1574
        } else {
1575
            $usergroups = groups_get_activity_allowed_groups($cm, $userid);
1576
            $currentusergroups = groups_get_activity_allowed_groups($cm, $USER->id);
1577
        }
1578
 
1579
        $samegroups = array_intersect_key($currentusergroups, $usergroups);
1580
        if (!empty($samegroups)) {
1581
            // We share groups!
1582
            return true;
1583
        }
1584
    }
1585
    return false;
1586
}
1587
 
1588
/**
1589
 * Returns the users in the specified groups.
1590
 *
1591
 * This function does not return complete user objects by default. It returns the user_picture basic fields.
1592
 *
1593
 * @param array $groupsids The list of groups ids to check
1594
 * @param array $extrafields extra fields to be included in result
1595
 * @param int $sort optional sorting of returned users
1596
 * @return array|bool Returns an array of the users for the specified group or false if no users or an error returned.
1597
 * @since  Moodle 3.3
1598
 */
1599
function groups_get_groups_members($groupsids, $extrafields=null, $sort='lastname ASC') {
1600
    global $DB;
1601
 
1602
    $wheres = [];
1603
    $userfieldsapi = \core_user\fields::for_userpic()->including(...($extrafields ?? []));
1604
    $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
1605
    list($insql, $params) = $DB->get_in_or_equal($groupsids, SQL_PARAMS_NAMED);
1606
    $wheres[] = "gm.groupid $insql";
1607
 
1608
    $courseids = $DB->get_fieldset_sql("SELECT DISTINCT courseid FROM {groups} WHERE id $insql", $params);
1609
 
1610
    if (count($courseids) > 1) {
1611
        // Groups from multiple courses. Have to check permission in system context.
1612
        $context = context_system::instance();
1613
    } else {
1614
        $courseid = reset($courseids);
1615
        $context = context_course::instance($courseid);
1616
    }
1617
 
1618
    if (!has_capability('moodle/course:viewhiddengroups', $context)) {
1619
        list($visibilitywhere, $visibilityparams) = \core_group\visibility::sql_member_visibility_where();
1620
        $params = array_merge($params, $visibilityparams);
1621
        $wheres[] = $visibilitywhere;
1622
    }
1623
 
1624
    $where = implode(' AND ', $wheres);
1625
    return $DB->get_records_sql("SELECT $userfields
1626
                                   FROM {user} u
1627
                                        JOIN {groups_members} gm ON u.id = gm.userid
1628
                                        JOIN {groups} g ON g.id = gm.groupid
1629
                                  WHERE {$where}
1630
                               GROUP BY $userfields
1631
                               ORDER BY $sort", $params);
1632
}
1633
 
1634
/**
1635
 * Returns users who share group membership with the specified user in the given actiivty.
1636
 *
1637
 * @param stdClass|cm_info $cm course module
1638
 * @param int $userid user id (empty for current user)
1639
 * @return array a list of user
1640
 * @since  Moodle 3.3
1641
 */
1642
function groups_get_activity_shared_group_members($cm, $userid = null) {
1643
    global $USER;
1644
 
1645
    if (empty($userid)) {
1646
        $userid = $USER->id;
1647
    }
1648
 
1649
    $groupsids = array_keys(groups_get_activity_allowed_groups($cm, $userid));
1650
    // No groups no users.
1651
    if (empty($groupsids)) {
1652
        return [];
1653
    }
1654
    return groups_get_groups_members($groupsids);
1655
}