5 * Determines if a group with a given groupid exists.
6 * @param int $groupid The groupid to check for
7 * @return boolean True if the group exists, false otherwise or if an error
10 function groups_group_exists($groupid) {
11 return record_exists('groups', 'id', $groupid);
15 * Gets the name of a group with a specified id
16 * @param int $groupid The id of the group
17 * @return string The name of the group
19 function groups_get_group_name($groupid) {
20 return get_field('groups', 'name', 'id', $groupid);
24 * Returns the groupid of a group with the name specified for the course.
25 * Group names should be unique in course
26 * @param int $courseid The id of the course
27 * @param string $name name of group (without magic quotes)
28 * @return int $groupid
30 function groups_get_group_by_name($courseid, $name) {
31 if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
38 * Returns the groupingid of a grouping with the name specified for the course.
39 * Grouping names should be unique in course
40 * @param int $courseid The id of the course
41 * @param string $name name of group (without magic quotes)
42 * @return int $groupid
44 function groups_get_grouping_by_name($courseid, $name) {
45 if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) {
46 return key($groupings);
52 * Get the group object
53 * @param groupid ID of the group.
54 * @return group object
56 function groups_get_group($groupid) {
57 return get_record('groups', 'id', $groupid);
61 * Gets array of all groups in a specified course.
62 * @param int $courseid The id of the course.
63 * @param int $userid optional user id, returns only groups of the user.
64 * @param int $groupingid optional returns only groups in the specified grouping.
65 * @return array | false Returns an array of the group IDs or false if no records
66 * or an error occurred.
68 function groups_get_all_groups($courseid, $userid=0, $groupingid=0) {
71 if (!empty($userid)) {
72 $userfrom = ", {$CFG->prefix}groups_members gm";
73 $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
79 if (!empty($groupingid)) {
80 $groupingfrom = ", {$CFG->prefix}groupings_groups gg";
81 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
87 return get_records_sql("SELECT g.*
88 FROM {$CFG->prefix}groups g $userfrom $groupingfrom
89 WHERE g.courseid = '$courseid' $userwhere $groupingwhere
94 * Determines if the user is a member of the given group.
96 * @uses $USER If $userid is null, use the global object.
97 * @param int $groupid The group to check for membership.
98 * @param int $userid The user to check against the group.
99 * @return boolean True if the user is a member, false otherwise.
101 function groups_is_member($groupid, $userid=null) {
108 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
112 * Determines if current or specified is member of any active group in activity
113 * @param object $cm coruse module object
114 * @param int $userid id of user, null menas $USER->id
115 * @return booelan true if user member of at least one group used in activity
117 function groups_has_membership($cm, $userid=null) {
120 if (empty($userid)) {
124 if ($cm->groupingid
) {
125 // find out if member of any group in selected activity grouping
127 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg
128 WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}";
131 // no grouping used - check all groups in course
133 FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
134 WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
137 return record_exists_sql($sql);
141 * Returns the users in the specified group.
142 * @param int $groupid The groupid to get the users for
143 * @param int $sort optional sorting of returned users
144 * @return array | false Returns an array of the users for the specified
145 * group or false if no users or an error returned.
147 function groups_get_members($groupid, $sort='lastname ASC') {
150 return get_records_sql("SELECT u.*
151 FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
152 WHERE u.id = gm.userid AND gm.groupid = '$groupid'