4 * Groups not used in course or activity
9 * Groups used, users do not see other groups
11 define('SEPARATEGROUPS', 1);
14 * Groups used, students see other groups
16 define('VISIBLEGROUPS', 2);
20 * Determines if a group with a given groupid exists.
21 * @param int $groupid The groupid to check for
22 * @return boolean True if the group exists, false otherwise or if an error
25 function groups_group_exists($groupid) {
26 return record_exists('groups', 'id', $groupid);
30 * Gets the name of a group with a specified id
31 * @param int $groupid The id of the group
32 * @return string The name of the group
34 function groups_get_group_name($groupid) {
35 return get_field('groups', 'name', 'id', $groupid);
39 * Gets the name of a grouping with a specified id
40 * @param int $groupingid The id of the grouping
41 * @return string The name of the grouping
43 function groups_get_grouping_name($groupingid) {
44 return get_field('groupings', 'name', 'id', $groupingid);
48 * Returns the groupid of a group with the name specified for the course.
49 * Group names should be unique in course
50 * @param int $courseid The id of the course
51 * @param string $name name of group (without magic quotes)
52 * @return int $groupid
54 function groups_get_group_by_name($courseid, $name) {
55 if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
62 * Returns the groupingid of a grouping with the name specified for the course.
63 * Grouping names should be unique in course
64 * @param int $courseid The id of the course
65 * @param string $name name of group (without magic quotes)
66 * @return int $groupid
68 function groups_get_grouping_by_name($courseid, $name) {
69 if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) {
70 return key($groupings);
76 * Get the group object
77 * @param groupid ID of the group.
78 * @return group object
80 function groups_get_group($groupid) {
81 return get_record('groups', 'id', $groupid);
85 * Get the grouping object
86 * @param groupingid ID of the group.
87 * @return group object
89 function groups_get_grouping($groupingid) {
90 return get_record('groupings', 'id', $groupingid);
94 * Gets array of all groups in a specified course.
95 * @param int $courseid The id of the course.
96 * @param mixed $userid optional user id or array of ids, returns only groups of the user.
97 * @param int $groupingid optional returns only groups in the specified grouping.
98 * @return array | false Returns an array of the group objects or false if no records
99 * or an error occurred. (userid field returned if array in $userid)
101 function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
104 // groupings are ignored when not enabled
105 if (empty($CFG->enablegroupings
)) {
109 if (empty($userid)) {
113 } else if (is_array($userid)) {
114 $userids = implode(',', $userid);
115 $userfrom = ", {$CFG->prefix}groups_members gm";
116 $userwhere = "AND g.id = gm.groupid AND gm.userid IN ($userids)";
119 $userfrom = ", {$CFG->prefix}groups_members gm";
120 $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
123 if (!empty($groupingid)) {
124 $groupingfrom = ", {$CFG->prefix}groupings_groups gg";
125 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
131 return get_records_sql("SELECT $fields
132 FROM {$CFG->prefix}groups g $userfrom $groupingfrom
133 WHERE g.courseid = $courseid $userwhere $groupingwhere
138 * Returns info about user's groups in course.
139 * @param int $courseid
140 * @param int $userid $USER if not specified
141 * @return array[groupingid][groupid] including grouping id 0 which means all groups
143 function groups_get_user_groups($courseid, $userid=0) {
146 if (empty($userid)) {
150 if (!$rs = get_recordset_sql("SELECT g.id, gg.groupingid
151 FROM {$CFG->prefix}groups g
152 JOIN {$CFG->prefix}groups_members gm ON gm.groupid = g.id
153 LEFT JOIN {$CFG->prefix}groupings_groups gg ON gg.groupid = g.id
154 WHERE gm.userid = $userid AND g.courseid = $courseid")) {
155 return array('0' => array());
159 $allgroups = array();
161 while ($group = rs_fetch_next_record($rs)) {
162 $allgroups[$group->id
] = $group->id
;
163 if (is_null($group->groupingid
)) {
166 if (!array_key_exists($group->groupingid
, $result)) {
167 $result[$group->groupingid
] = array();
169 $result[$group->groupingid
][$group->id
] = $group->id
;
173 $result['0'] = array_keys($allgroups); // all groups
179 * Gets array of all groupings in a specified course.
180 * @param int $courseid return only groupings in this with this courseid
181 * @return array | false Returns an array of the grouping objects or false if no records
182 * or an error occurred.
184 function groups_get_all_groupings($courseid) {
187 // groupings are ignored when not enabled
188 if (empty($CFG->enablegroupings
)) {
191 return get_records_sql("SELECT *
192 FROM {$CFG->prefix}groupings
193 WHERE courseid = $courseid
200 * Determines if the user is a member of the given group.
202 * @uses $USER If $userid is null, use the global object.
203 * @param int $groupid The group to check for membership.
204 * @param int $userid The user to check against the group.
205 * @return boolean True if the user is a member, false otherwise.
207 function groups_is_member($groupid, $userid=null) {
214 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
218 * Determines if current or specified is member of any active group in activity
219 * @param object $cm coruse module object
220 * @param int $userid id of user, null menas $USER->id
221 * @return booelan true if user member of at least one group used in activity
223 function groups_has_membership($cm, $userid=null) {
226 static $cache = array();
228 // groupings are ignored when not enabled
229 if (empty($CFG->enablegroupings
)) {
233 if (empty($userid)) {
237 $cachekey = $userid.'|'.$cm->course
.'|'.$cm->groupingid
;
238 if (isset($cache[$cachekey])) {
239 return($cache[$cachekey]);
242 if ($cm->groupingid
) {
243 // find out if member of any group in selected activity grouping
245 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg
246 WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}";
249 // no grouping used - check all groups in course
251 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
252 WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
255 $cache[$cachekey] = record_exists_sql($sql);
257 return $cache[$cachekey];
261 * Returns the users in the specified group.
262 * @param int $groupid The groupid to get the users for
263 * @param int $fields The fields to return
264 * @param int $sort optional sorting of returned users
265 * @return array | false Returns an array of the users for the specified
266 * group or false if no users or an error returned.
268 function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
271 return get_records_sql("SELECT $fields
272 FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
273 WHERE u.id = gm.userid AND gm.groupid = '$groupid'
279 * Returns the users in the specified grouping.
280 * @param int $groupingid The groupingid to get the users for
281 * @param int $fields The fields to return
282 * @param int $sort optional sorting of returned users
283 * @return array | false Returns an array of the users for the specified
284 * group or false if no users or an error returned.
286 function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
289 return get_records_sql("SELECT $fields
290 FROM {$CFG->prefix}user u
291 INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
292 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
293 WHERE gg.groupingid = $groupingid
298 * Returns effective groupmode used in course
299 * @return integer group mode
301 function groups_get_course_groupmode($course) {
302 return $course->groupmode
;
306 * Returns effective groupmode used in activity, course setting
307 * overrides activity setting if groupmodeforce enabled.
308 * @param $cm the course module object. Only the ->course and ->groupmode need to be set.
309 * @param $course object optional course object to improve perf
310 * @return integer group mode
312 function groups_get_activity_groupmode($cm, $course=null) {
315 // get course object (reuse COURSE if possible)
316 if (isset($course->id
) and $course->id
== $cm->course
) {
318 } else if ($cm->course
== $COURSE->id
) {
321 if (!$course = get_record('course', 'id', $cm->course
)) {
322 error('Incorrect course id in cm');
326 return empty($course->groupmodeforce
) ?
$cm->groupmode
: $course->groupmode
;
330 * Print group menu selector for course level.
331 * @param object $course course object
332 * @param string $urlroot return address
333 * @param boolean $return return as string instead of printing
334 * @return mixed void or string depending on $return param
336 function groups_print_course_menu($course, $urlroot, $return=false) {
337 global $CFG, $USER, $SESSION;
339 if (!$groupmode = $course->groupmode
) {
347 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
348 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
349 $allowedgroups = groups_get_all_groups($course->id
, 0);
350 // detect changes related to groups and fix active group
351 if (!empty($SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][0])) {
352 if (!array_key_exists($SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][0], $allowedgroups)) {
353 // active does not exist anymore
354 unset($SESSION->activegroup
[$course->id
][VISIBLEGROUPS
][0]);
357 if (!empty($SESSION->activegroup
[$course->id
]['aag'][0])) {
358 if (!array_key_exists($SESSION->activegroup
[$course->id
]['aag'][0], $allowedgroups)) {
359 // active group does not exist anymore
360 unset($SESSION->activegroup
[$course->id
]['aag'][0]);
365 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
);
366 // detect changes related to groups and fix active group
367 if (isset($SESSION->activegroup
[$course->id
][SEPARATEGROUPS
][0])) {
368 if ($SESSION->activegroup
[$course->id
][SEPARATEGROUPS
][0] == 0) {
369 if ($allowedgroups) {
370 // somebody must have assigned at least one group, we can select it now - yay!
371 unset($SESSION->activegroup
[$course->id
][SEPARATEGROUPS
][0]);
374 if (!array_key_exists($SESSION->activegroup
[$course->id
][SEPARATEGROUPS
][0], $allowedgroups)) {
375 // active group not allowed or does not exist anymore
376 unset($SESSION->activegroup
[$course->id
][SEPARATEGROUPS
][0]);
382 $activegroup = groups_get_course_group($course, true);
384 $groupsmenu = array();
385 if (!$allowedgroups or $groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
386 $groupsmenu[0] = get_string('allparticipants');
389 if ($allowedgroups) {
390 foreach ($allowedgroups as $group) {
391 $groupsmenu[$group->id
] = format_string($group->name
);
395 if ($groupmode == VISIBLEGROUPS
) {
396 $grouplabel = get_string('groupsvisible');
398 $grouplabel = get_string('groupsseparate');
401 if (count($groupsmenu) == 1) {
402 $groupname = reset($groupsmenu);
403 $output = $grouplabel.': '.$groupname;
405 $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
408 $output = '<div class="groupselector">'.$output.'</div>';
418 * Print group menu selector for activity.
419 * @param object $cm course module object
420 * @param string $urlroot return address that users get to if they choose an option;
421 * should include any parameters needed, e.g. 'view.php?id=34'
422 * @param boolean $return return as string instead of printing
423 * @param boolean $hideallparticipants If true, this prevents the 'All participants'
424 * option from appearing in cases where it normally would. This is intended for
425 * use only by activities that cannot display all groups together. (Note that
426 * selecting this option does not prevent groups_get_activity_group from
427 * returning 0; it will still do that if the user has chosen 'all participants'
428 * in another activity, or not chosen anything.)
429 * @return mixed void or string depending on $return param
431 function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
432 global $CFG, $USER, $SESSION;
434 // groupings are ignored when not enabled
435 if (empty($CFG->enablegroupings
)) {
439 if (!$groupmode = groups_get_activity_groupmode($cm)) {
447 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
448 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
449 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
); // any group in grouping (all if groupings not used)
450 // detect changes related to groups and fix active group
451 if (!empty($SESSION->activegroup
[$cm->course
][VISIBLEGROUPS
][$cm->groupingid
])) {
452 if (!array_key_exists($SESSION->activegroup
[$cm->course
][VISIBLEGROUPS
][$cm->groupingid
], $allowedgroups)) {
453 // active group does not exist anymore
454 unset($SESSION->activegroup
[$cm->course
][VISIBLEGROUPS
][$cm->groupingid
]);
457 if (!empty($SESSION->activegroup
[$cm->course
]['aag'][$cm->groupingid
])) {
458 if (!array_key_exists($SESSION->activegroup
[$cm->course
]['aag'][$cm->groupingid
], $allowedgroups)) {
459 // active group does not exist anymore
460 unset($SESSION->activegroup
[$cm->course
]['aag'][$cm->groupingid
]);
465 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
); // only assigned groups
466 // detect changes related to groups and fix active group
467 if (isset($SESSION->activegroup
[$cm->course
][SEPARATEGROUPS
][$cm->groupingid
])) {
468 if ($SESSION->activegroup
[$cm->course
][SEPARATEGROUPS
][$cm->groupingid
] == 0) {
469 if ($allowedgroups) {
470 // somebody must have assigned at least one group, we can select it now - yay!
471 unset($SESSION->activegroup
[$cm->course
][SEPARATEGROUPS
][$cm->groupingid
]);
474 if (!array_key_exists($SESSION->activegroup
[$cm->course
][SEPARATEGROUPS
][$cm->groupingid
], $allowedgroups)) {
475 // active group not allowed or does not exist anymore
476 unset($SESSION->activegroup
[$cm->course
][SEPARATEGROUPS
][$cm->groupingid
]);
482 $activegroup = groups_get_activity_group($cm, true);
484 $groupsmenu = array();
485 if ((!$allowedgroups or $groupmode == VISIBLEGROUPS
or
486 has_capability('moodle/site:accessallgroups', $context)) and !$hideallparticipants) {
487 $groupsmenu[0] = get_string('allparticipants');
490 if ($allowedgroups) {
491 foreach ($allowedgroups as $group) {
492 $groupsmenu[$group->id
] = format_string($group->name
);
496 if ($groupmode == VISIBLEGROUPS
) {
497 $grouplabel = get_string('groupsvisible');
499 $grouplabel = get_string('groupsseparate');
502 if (count($groupsmenu) == 1) {
503 $groupname = reset($groupsmenu);
504 $output = $grouplabel.': '.$groupname;
506 $output = popup_form($urlroot.'&group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
509 $output = '<div class="groupselector">'.$output.'</div>';
519 * Returns group active in course, changes the group by default if 'group' page param present
521 * @param object $course course bject
522 * @param boolean $update change active group if group param submitted
523 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
525 function groups_get_course_group($course, $update=false) {
526 global $CFG, $USER, $SESSION;
528 if (!$groupmode = $course->groupmode
) {
533 // init activegroup array
534 if (!array_key_exists('activegroup', $SESSION)) {
535 $SESSION->activegroup
= array();
537 if (!array_key_exists($course->id
, $SESSION->activegroup
)) {
538 $SESSION->activegroup
[$course->id
] = array(SEPARATEGROUPS
=>array(), VISIBLEGROUPS
=>array(), 'aag'=>array());
541 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
542 if (has_capability('moodle/site:accessallgroups', $context)) {
546 // grouping used the first time - add first user group as default
547 if (!array_key_exists(0, $SESSION->activegroup
[$course->id
][$groupmode])) {
548 if ($groupmode == 'aag') {
549 $SESSION->activegroup
[$course->id
][$groupmode][0] = 0; // all groups by default if user has accessallgroups
551 } else if ($usergroups = groups_get_all_groups($course->id
, $USER->id
, 0)) {
552 $fistgroup = reset($usergroups);
553 $SESSION->activegroup
[$course->id
][$groupmode][0] = $fistgroup->id
;
556 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
557 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
558 $SESSION->activegroup
[$course->id
][$groupmode][0] = 0;
562 // set new active group if requested
563 $changegroup = optional_param('group', -1, PARAM_INT
);
564 if ($update and $changegroup != -1) {
566 if ($changegroup == 0) {
567 // do not allow changing to all groups without accessallgroups capability
568 if ($groupmode == VISIBLEGROUPS
or $groupmode == 'aag') {
569 $SESSION->activegroup
[$course->id
][$groupmode][0] = 0;
573 // first make list of allowed groups
574 if ($groupmode == VISIBLEGROUPS
or $groupmode == 'aag') {
575 $allowedgroups = groups_get_all_groups($course->id
, 0, 0);
577 $allowedgroups = groups_get_all_groups($course->id
, $USER->id
, 0);
580 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
581 $SESSION->activegroup
[$course->id
][$groupmode][0] = $changegroup;
586 return $SESSION->activegroup
[$course->id
][$groupmode][0];
590 * Returns group active in activity, changes the group by default if 'group' page param present
592 * @param object $cm course module object
593 * @param boolean $update change active group if group param submitted
594 * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
596 function groups_get_activity_group($cm, $update=false) {
597 global $CFG, $USER, $SESSION;
599 // groupings are ignored when not enabled
600 if (empty($CFG->enablegroupings
)) {
604 if (!$groupmode = groups_get_activity_groupmode($cm)) {
609 // init activegroup array
610 if (!array_key_exists('activegroup', $SESSION)) {
611 $SESSION->activegroup
= array();
613 if (!array_key_exists($cm->course
, $SESSION->activegroup
)) {
614 $SESSION->activegroup
[$cm->course
] = array(SEPARATEGROUPS
=>array(), VISIBLEGROUPS
=>array(), 'aag'=>array());
617 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
618 if (has_capability('moodle/site:accessallgroups', $context)) {
622 // grouping used the first time - add first user group as default
623 if (!array_key_exists($cm->groupingid
, $SESSION->activegroup
[$cm->course
][$groupmode])) {
624 if ($groupmode == 'aag') {
625 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0; // all groups by default if user has accessallgroups
627 } else if ($usergroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
)) {
628 $fistgroup = reset($usergroups);
629 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = $fistgroup->id
;
632 // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
633 // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
634 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0;
638 // set new active group if requested
639 $changegroup = optional_param('group', -1, PARAM_INT
);
640 if ($update and $changegroup != -1) {
642 if ($changegroup == 0) {
643 // allgroups visible only in VISIBLEGROUPS or when accessallgroups
644 if ($groupmode == VISIBLEGROUPS
or $groupmode == 'aag') {
645 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = 0;
649 // first make list of allowed groups
650 if ($groupmode == VISIBLEGROUPS
or $groupmode == 'aag') {
651 $allowedgroups = groups_get_all_groups($cm->course
, 0, $cm->groupingid
); // any group in grouping (all if groupings not used)
653 $allowedgroups = groups_get_all_groups($cm->course
, $USER->id
, $cm->groupingid
); // only assigned groups
656 if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
657 $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
] = $changegroup;
662 return $SESSION->activegroup
[$cm->course
][$groupmode][$cm->groupingid
];
666 * Gets a list of groups that the user is allowed to access within the
667 * specified activity.
668 * @param object $cm Course-module
669 * @param int $userid User ID (defaults to current user)
670 * @return array An array of group objects, or false if none
672 function groups_get_activity_allowed_groups($cm,$userid=0) {
673 // Use current user by default
679 // Get groupmode for activity, taking into account course settings
680 $groupmode=groups_get_activity_groupmode($cm);
682 // If visible groups mode, or user has the accessallgroups capability,
683 // then they can access all groups for the activity...
684 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
685 if ($groupmode == VISIBLEGROUPS
or has_capability('moodle/site:accessallgroups', $context)) {
686 return groups_get_all_groups($cm->course
, 0, $cm->groupingid
);
688 // ...otherwise they can only access groups they belong to
689 return groups_get_all_groups($cm->course
, $userid, $cm->groupingid
);
694 * Determine if a course module is currently visible to a user
695 * @uses $USER If $userid is null, use the global object.
696 * @param int $cm The course module
697 * @param int $userid The user to check against the group.
698 * @return boolean True if the user can view the course module, false otherwise.
700 function groups_course_module_visible($cm, $userid=null) {
703 if (empty($userid)) {
706 if (empty($CFG->enablegroupings
)) {
709 if (empty($cm->groupmembersonly
)) {
712 if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE
, $cm->id
), $userid) or groups_has_membership($cm, $userid)) {