44
43
* Determines if a group with a given groupid exists.
47
46
* @param int $groupid The groupid to check for
48
* @return boolean True if the group exists, false otherwise or if an error
47
* @return bool True if the group exists, false otherwise or if an error
51
50
function groups_group_exists($groupid) {
81
80
* Returns the groupid of a group with the name specified for the course.
82
81
* Group names should be unique in course
85
84
* @param int $courseid The id of the course
86
85
* @param string $name name of group (without magic quotes)
87
86
* @return int $groupid
89
88
function groups_get_group_by_name($courseid, $name) {
91
if ($groups = $DB->get_records('groups', array('courseid'=>$courseid, 'name'=>$name))) {
89
$data = groups_get_course_data($courseid);
90
foreach ($data->groups as $group) {
91
if ($group->name == $name) {
99
* Returns the groupid of a group with the idnumber specified for the course.
100
* Group idnumbers should be unique within course
103
* @param int $courseid The id of the course
104
* @param string $idnumber idnumber of group
105
* @return group object
107
function groups_get_group_by_idnumber($courseid, $idnumber) {
108
if (empty($idnumber)) {
111
$data = groups_get_course_data($courseid);
112
foreach ($data->groups as $group) {
113
if ($group->idnumber == $idnumber) {
98
121
* Returns the groupingid of a grouping with the name specified for the course.
99
122
* Grouping names should be unique in course
102
125
* @param int $courseid The id of the course
103
126
* @param string $name name of group (without magic quotes)
104
127
* @return int $groupid
106
129
function groups_get_grouping_by_name($courseid, $name) {
108
if ($groupings = $DB->get_records('groupings', array('courseid'=>$courseid, 'name'=>$name))) {
109
return key($groupings);
130
$data = groups_get_course_data($courseid);
131
foreach ($data->groupings as $grouping) {
132
if ($grouping->name == $name) {
133
return $grouping->id;
140
* Returns the groupingid of a grouping with the idnumber specified for the course.
141
* Grouping names should be unique within course
144
* @param int $courseid The id of the course
145
* @param string $idnumber idnumber of the group
146
* @return grouping object
148
function groups_get_grouping_by_idnumber($courseid, $idnumber) {
149
if (empty($idnumber)) {
152
$data = groups_get_course_data($courseid);
153
foreach ($data->groupings as $grouping) {
154
if ($grouping->idnumber == $idnumber) {
115
162
* Get the group object
117
165
* @param int $groupid ID of the group.
118
* @return object group object
166
* @param string $fields (default is all fields)
167
* @param int $strictness (IGNORE_MISSING - default)
168
* @return stdGlass group object
120
170
function groups_get_group($groupid, $fields='*', $strictness=IGNORE_MISSING) {
126
176
* Get the grouping object
128
179
* @param int $groupingid ID of the group.
129
180
* @param string $fields
130
* @return object group object
181
* @param int $strictness (IGNORE_MISSING - default)
182
* @return stdClass group object
132
184
function groups_get_grouping($groupingid, $fields='*', $strictness=IGNORE_MISSING) {
146
199
function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
202
// We need to check that we each field in the fields list belongs to the group table and that it has not being
203
// aliased. If its something else we need to avoid the cache and run the query as who knows whats going on.
205
if ($fields !== 'g.*') {
206
$fieldbits = explode(',', $fields);
207
foreach ($fieldbits as $bit) {
209
if (strpos($bit, 'g.') !== 0 or stripos($bit, ' AS ') !== false) {
210
$knownfields = false;
216
if (empty($userid) && $knownfields) {
217
// We can use the cache.
218
$data = groups_get_course_data($courseid);
219
if (empty($groupingid)) {
220
// All groups.. Easy!
221
$groups = $data->groups;
224
foreach ($data->mappings as $mapping) {
225
if ($mapping->groupingid != $groupingid) {
228
if (isset($data->groups[$mapping->groupid])) {
229
$groups[$mapping->groupid] = $data->groups[$mapping->groupid];
233
// Yay! We could use the cache. One more query saved.
149
238
if (empty($userid)) {
174
263
ORDER BY name ASC", $params);
268
* Gets array of all groups in current user.
272
* @return array Returns an array of the group objects.
274
function groups_get_my_groups() {
276
return $DB->get_records_sql("SELECT *
277
FROM {groups_members} gm
281
ORDER BY name ASC", array($USER->id));
178
285
* Returns info about user's groups in course.
180
288
* @param int $courseid
181
289
* @param int $userid $USER if not specified
182
290
* @return array Array[groupingid][groupid] including grouping id 0 which means all groups
226
* Gets array of all groupings in a specified course.
334
* Gets an array of all groupings in a specified course. This value is cached
335
* for a single course (so you can call it repeatedly for the same course
336
* without a performance penalty).
230
* @param int $courseid return only groupings in this with this courseid
231
* @return array|bool Returns an array of the grouping objects or false if no records
232
* or an error occurred.
339
* @param int $courseid return all groupings from course with this courseid
340
* @return array Returns an array of the grouping objects (empty if none)
234
342
function groups_get_all_groupings($courseid) {
237
return $DB->get_records_sql("SELECT *
240
ORDER BY name ASC", array($courseid));
343
$data = groups_get_course_data($courseid);
344
return $data->groupings;
246
348
* Determines if the user is a member of the given group.
248
350
* If $userid is null, use the global object.
252
353
* @param int $groupid The group to check for membership.
253
354
* @param int $userid The user to check against the group.
254
* @return boolean True if the user is a member, false otherwise.
355
* @return bool True if the user is a member, false otherwise.
256
357
function groups_is_member($groupid, $userid=null) {
257
358
global $USER, $DB;
267
368
* Determines if current or specified is member of any active group in activity
272
371
* @staticvar array $cache
273
* @param object $cm coruse module object
274
* @param int $userid id of user, null menas $USER->id
275
* @return booelan true if user member of at least one group used in activity
372
* @param cm_info $cm course module object
373
* @param int $userid id of user, null means $USER->id
374
* @return bool true if user member of at least one group used in activity
277
376
function groups_has_membership($cm, $userid=null) {
278
377
global $CFG, $USER, $DB;
332
431
* Returns the users in the specified grouping.
335
434
* @param int $groupingid The groupingid to get the users for
336
* @param int $fields The fields to return
337
* @param int $sort optional sorting of returned users
435
* @param string $fields The fields to return
436
* @param string $sort optional sorting of returned users
338
437
* @return array|bool Returns an array of the users for the specified
339
438
* group or false if no users or an error returned.
362
463
* Returns effective groupmode used in activity, course setting
363
464
* overrides activity setting if groupmodeforce enabled.
367
* @param object $cm the course module object. Only the ->course and ->groupmode need to be set.
368
* @param object $course object optional course object to improve perf
369
* @return integer group mode
467
* @param cm_info $cm the course module object. Only the ->course and ->groupmode need to be set.
468
* @param stdClass $course object optional course object to improve perf
469
* @return int group mode
371
471
function groups_get_activity_groupmode($cm, $course=null) {
372
472
global $COURSE, $DB;
389
489
* Print group menu selector for course level.
391
492
* @param stdClass $course course object
392
* @param string|moodle_url $urlroot return address
393
* @param boolean $return return as string instead of printing
493
* @param mixed $urlroot return address. Accepts either a string or a moodle_url
494
* @param bool $return return as string instead of printing
394
495
* @return mixed void or string depending on $return param
396
497
function groups_print_course_menu($course, $urlroot, $return=false) {
460
561
* Print group menu selector for activity.
462
564
* @param stdClass $cm course module object
463
565
* @param string|moodle_url $urlroot return address that users get to if they choose an option;
464
566
* should include any parameters needed, e.g. "$CFG->wwwroot/mod/forum/view.php?id=34"
465
* @param boolean $return return as string instead of printing
466
* @param boolean $hideallparticipants If true, this prevents the 'All participants'
567
* @param bool $return return as string instead of printing
568
* @param bool $hideallparticipants If true, this prevents the 'All participants'
467
569
* option from appearing in cases where it normally would. This is intended for
468
570
* use only by activities that cannot display all groups together. (Note that
469
571
* selecting this option does not prevent groups_get_activity_group from
552
654
* Returns group active in course, changes the group by default if 'group' page param present
554
657
* @param stdClass $course course bject
555
* @param boolean $update change active group if group param submitted
658
* @param bool $update change active group if group param submitted
556
659
* @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_course_menu())
557
660
* @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
603
706
* Returns group active in activity, changes the group by default if 'group' page param present
605
709
* @param stdClass $cm course module object
606
* @param boolean $update change active group if group param submitted
710
* @param bool $update change active group if group param submitted
607
711
* @param array $allowedgroups list of groups user may access (INTERNAL, to be used only from groups_print_activity_menu())
608
712
* @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
671
776
// If visible groups mode, or user has the accessallgroups capability,
672
777
// then they can access all groups for the activity...
673
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
778
$context = context_module::instance($cm->id);
674
779
if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
675
780
return groups_get_all_groups($cm->course, 0, $cm->groupingid);
685
790
* $USER If $userid is null, use the global object.
687
* @param int $cm The course module
793
* @param stdClass $cm The course module
688
794
* @param int $userid The user to check against the group.
689
* @return boolean True if the user can view the course module, false otherwise.
795
* @return bool True if the user can view the course module, false otherwise.
691
797
function groups_course_module_visible($cm, $userid=null) {
692
798
global $CFG, $USER;
700
806
if (empty($cm->groupmembersonly)) {
703
if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
809
if (has_capability('moodle/site:accessallgroups', context_module::instance($cm->id), $userid) or groups_has_membership($cm, $userid)) {
710
816
* Internal method, sets up $SESSION->activegroup and verifies previous value
713
818
* @param int $courseid
714
819
* @param int|string $groupmode SEPARATEGROUPS, VISIBLEGROUPS or 'aag' (access all groups)
715
820
* @param int $groupingid 0 means all groups
716
* @param all $allowedgroups list of groups user can see
821
* @param array $allowedgroups list of groups user can see
719
823
function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $allowedgroups) {
720
824
global $SESSION, $USER;
755
859
$SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0;
b'\\ No newline at end of file'
865
* Caches group data for a particular course to speed up subsequent requests.
867
* @param int $courseid The course id to cache data for.
868
* @param cache $cache The cache if it has already been initialised. If not a new one will be created.
869
* @return stdClass A data object containing groups, groupings, and mappings.
871
function groups_cache_groupdata($courseid, cache $cache = null) {
874
if ($cache === null) {
875
// Initialise a cache if we wern't given one.
876
$cache = cache::make('core', 'groupdata');
879
// Get the groups that belong to the course.
880
$groups = $DB->get_records('groups', array('courseid' => $courseid), 'name ASC');
881
// Get the groupings that belong to the course.
882
$groupings = $DB->get_records('groupings', array('courseid' => $courseid), 'name ASC');
884
if (!is_array($groups)) {
888
if (!is_array($groupings)) {
889
$groupings = array();
892
if (!empty($groupings)) {
893
// Finally get the mappings between the two.
894
$mappings = $DB->get_records_list('groupings_groups', 'groupingid', array_keys($groupings), '', 'id,groupingid,groupid');
899
// Prepare the data array.
900
$data = new stdClass;
901
$data->groups = $groups;
902
$data->groupings = $groupings;
903
$data->mappings = $mappings;
905
$cache->set($courseid, $data);
906
// Finally return it so it can be used if desired.
911
* Gets group data for a course.
913
* This returns an object with the following properties:
914
* - groups : An array of all the groups in the course.
915
* - groupings : An array of all the groupings within the course.
916
* - mappings : An array of group to grouping mappings.
918
* @param int $courseid The course id to get data for.
919
* @param cache $cache The cache if it has already been initialised. If not a new one will be created.
922
function groups_get_course_data($courseid, cache $cache = null) {
923
if ($cache === null) {
924
// Initialise a cache if we wern't given one.
925
$cache = cache::make('core', 'groupdata');
927
// Try to retrieve it from the cache.
928
$data = $cache->get($courseid);
929
if ($data === false) {
930
$data = groups_cache_groupdata($courseid, $cache);