2c386f82 |
1 | <?php //$Id$ |
2 | |
5bf243d1 |
3 | |
5bf243d1 |
4 | /** |
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 |
8 | * occurred. |
9 | */ |
10 | function groups_group_exists($groupid) { |
11 | return record_exists('groups', 'id', $groupid); |
12 | } |
13 | |
14 | /** |
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 |
18 | */ |
19 | function groups_get_group_name($groupid) { |
20 | return get_field('groups', 'name', 'id', $groupid); |
21 | } |
2c386f82 |
22 | |
23 | /** |
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 |
29 | */ |
30 | function groups_get_group_by_name($courseid, $name) { |
ddff2fa8 |
31 | if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) { |
32 | return key($groups); |
2c386f82 |
33 | } |
ddff2fa8 |
34 | return false; |
35 | } |
2c386f82 |
36 | |
ddff2fa8 |
37 | /** |
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 |
43 | */ |
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); |
47 | } |
48 | return false; |
2c386f82 |
49 | } |
50 | |
51 | /** |
52 | * Get the group object |
53 | * @param groupid ID of the group. |
54 | * @return group object |
55 | */ |
56 | function groups_get_group($groupid) { |
57 | return get_record('groups', 'id', $groupid); |
58 | } |
59 | |
60 | /** |
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. |
62d63838 |
64 | * @param int $groupingid optional returns only groups in the specified grouping. |
2c386f82 |
65 | * @return array | false Returns an array of the group IDs or false if no records |
66 | * or an error occurred. |
67 | */ |
62d63838 |
68 | function groups_get_all_groups($courseid, $userid=0, $groupingid=0) { |
2c386f82 |
69 | global $CFG; |
70 | |
62d63838 |
71 | if (!empty($userid)) { |
72 | $userfrom = ", {$CFG->prefix}groups_members gm"; |
73 | $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'"; |
74 | } else { |
75 | $userfrom = ""; |
76 | $userwhere = ""; |
77 | } |
2c386f82 |
78 | |
62d63838 |
79 | if (!empty($groupingid)) { |
80 | $groupingfrom = ", {$CFG->prefix}groupings_groups gg"; |
81 | $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'"; |
2c386f82 |
82 | } else { |
62d63838 |
83 | $groupingfrom = ""; |
84 | $groupingwhere = ""; |
2c386f82 |
85 | } |
62d63838 |
86 | |
87 | return get_records_sql("SELECT g.* |
88 | FROM {$CFG->prefix}groups g $userfrom $groupingfrom |
89 | WHERE g.courseid = '$courseid' $userwhere $groupingwhere |
90 | ORDER BY name ASC"); |
2c386f82 |
91 | } |
92 | |
93 | /** |
94 | * Determines if the user is a member of the given group. |
95 | * |
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. |
100 | */ |
101 | function groups_is_member($groupid, $userid=null) { |
102 | global $USER; |
103 | |
104 | if (!$userid) { |
105 | $userid = $USER->id; |
106 | } |
107 | |
108 | return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid); |
109 | } |
110 | |
f8e3d5f0 |
111 | /** |
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 |
116 | */ |
117 | function groups_has_membership($cm, $userid=null) { |
118 | global $CFG, $USER; |
119 | |
120 | if (empty($userid)) { |
121 | $userid = $USER->id; |
122 | } |
123 | |
124 | if ($cm->groupingid) { |
125 | // find out if member of any group in selected activity grouping |
126 | $sql = "SELECT 'x' |
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}"; |
129 | |
130 | } else { |
131 | // no grouping used - check all groups in course |
132 | $sql = "SELECT 'x' |
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}"; |
135 | } |
136 | |
137 | return record_exists_sql($sql); |
138 | } |
139 | |
62d63838 |
140 | /** |
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. |
146 | */ |
147 | function groups_get_members($groupid, $sort='lastname ASC') { |
148 | global $CFG; |
149 | |
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' |
153 | ORDER BY $sort"); |
154 | } |
155 | |
156 | |
2c386f82 |
157 | |
158 | ?> |