3
* Legacy groups functions - these were in moodlelib.php, datalib.php, weblib.php
5
* @@@ Don't look at this file - still tons to do!
7
* TODO: For the moment these functions are in /lib/deprecatedlib.php
11
* @copyright © 2006 The Open University
12
* @author J.White AT open.ac.uk and others
13
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
19
* Returns the groupid of a group with the name specified for the course
20
* specified. If there's more than one with the name specified it returns the
21
* first one it finds it the database, so you need to be careful how you use it!
22
* This is needed to support upload of users into the database
23
* @param int $courseid The id of the course
24
* @param string $groupname
25
* @return int $groupid
27
function groups_get_group_by_name($courseid, $groupname) {
28
//uploaduser.php, get_record("groups","courseid",$course[$i]->id,"name",$addgroup[$i])
29
$groupids = groups_db_get_groups($courseid);
33
foreach ($groupids as $id) {
34
if (groups_get_group_name($id) == $groupname) {
42
* Returns an array of group objects that the user is a member of
43
* in the given course. If userid isn't specified, then return a
44
* list of all groups in the course.
47
* @param int $courseid The id of the course in question.
48
* @param int $userid The id of the user in question as found in the 'user'
52
function get_groups($courseid, $userid=0) {
54
$groupids = groups_get_groups_for_user($userid, $courseid);
56
$groupids = groups_get_groups($courseid);
59
return groups_groupids_to_groups($groupids, $courseid, $alldata=true);
64
* Returns the user's group in a particular course
67
* @param int $courseid The course in question.
68
* @param int $userid The id of the user as found in the 'user' table.
69
* @param int $groupid The id of the group the user is in.
72
function user_group($courseid, $userid) {
73
$groupids = groups_get_groups_for_user($userid, $courseid);
74
return groups_groupids_to_groups($groupids);
79
* Determines if the user is a member of the given group.
80
* TODO: replace all calls with 'groups_is_member'.
82
* @param int $groupid The group to check for membership.
83
* @param int $userid The user to check against the group.
84
* @return boolean True if the user is a member, false otherwise.
86
function ismember($groupid, $userid = null) {
87
return groups_is_member($groupid, $userid);
91
* Returns an array of user objects
94
* @param int $groupid The group in question.
95
* @param string $sort ?
96
* @param string $exceptions ?
98
* @todo Finish documenting this function
100
function get_group_users($groupid, $sort='u.lastaccess DESC', $exceptions='',
103
if (!empty($exceptions)) {
104
$except = ' AND u.id NOT IN ('. $exceptions .') ';
108
// in postgres, you can't have things in sort that aren't in the select, so...
109
$extrafield = str_replace('ASC','',$sort);
110
$extrafield = str_replace('DESC','',$extrafield);
111
$extrafield = trim($extrafield);
112
if (!empty($extrafield)) {
113
$extrafield = ','.$extrafield;
115
return get_records_sql("SELECT DISTINCT $fields $extrafield
116
FROM {$CFG->prefix}user u,
117
{$CFG->prefix}groups_members m
118
WHERE m.groupid = '$groupid'
119
AND m.userid = u.id $except
127
* Add a user to a group, return true upon success or if user already a group
130
* @param int $groupid The group id to add user to
131
* @param int $userid The user id to add to the group
134
function add_user_to_group($groupid, $userid) {
135
return groups_add_member($groupid, $userid);
140
* Get the IDs for the user's groups in the given course.
143
* @param int $courseid The course being examined - the 'course' table id field.
144
* @return array An _array_ of groupids.
145
* (Was return $groupids[0] - consequences!)
147
function mygroupid($courseid) {
149
$groupids = groups_get_groups_for_user($USER->id, $courseid);
154
* Returns the current group mode for a given course or activity module
156
* Could be false, SEPARATEGROUPS or VISIBLEGROUPS (<-- Martin)
158
function groupmode($course, $cm=null) {
160
if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
161
return $cm->groupmode;
163
return $course->groupmode;
165
/*if ($cm and !$course->groupingid) {
166
//TODO: was $coursemodule
167
return groups_has_groups_setup_for_instance($cm);
169
return groups_has_groups_setup($course->id);
175
* Sets the current group in the session variable
176
* When $SESSION->currentgroup[$courseid] is set to 0 it means, show all groups.
177
* Sets currentgroup[$courseid] in the session variable appropriately.
178
* Does not do any permission checking.
180
* @param int $courseid The course being examined - relates to id field in
182
* @param int $groupid The group being examined.
183
* @return int Current group id which was set by this function
185
function set_current_group($courseid, $groupid) {
187
return $SESSION->currentgroup[$courseid] = $groupid;
192
* Gets the current group - either from the session variable or from the database.
196
* @param int $courseid The course being examined - relates to id field in
198
* @param bool $full If true, the return value is a full record object.
199
* If false, just the id of the record.
201
function get_current_group($courseid, $full = false) {
204
if (isset($SESSION->currentgroup[$courseid])) {
206
return groups_get_group($SESSION->currentgroup[$courseid], false);
208
return $SESSION->currentgroup[$courseid];
212
$mygroupid = mygroupid($courseid);
213
if (is_array($mygroupid)) {
214
$mygroupid = array_shift($mygroupid);
215
set_current_group($courseid, $mygroupid);
217
return groups_get_group($mygroupid, false);
232
* A combination function to make it easier for modules
235
* It will use a given "groupid" parameter and try to use
236
* that to reset the current group for the user.
238
* @uses VISIBLEGROUPS
239
* @param course $course A {@link $COURSE} object
240
* @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
241
* @param int $groupid Will try to use this optional parameter to
242
* reset the current group for the user
243
* @return int|false Returns the current group id or false if error.
245
function get_and_set_current_group($course, $groupmode, $groupid=-1) {
246
//TODO: ?? groups_has_permission($userid, $groupingid, $courseid, $groupid, $permissiontype);
248
// Sets to the specified group, provided the current user has view permission
249
if (!$groupmode) { // Groups don't even apply
253
$currentgroupid = get_current_group($course->id);
255
if ($groupid < 0) { // No change was specified
256
return $currentgroupid;
259
$context = get_context_instance(CONTEXT_COURSE, $course->id);
260
if ($groupid) { // Try to change the current group to this groupid
261
if (groups_group_belongs_to_course($groupid, $course->id)) { // Exists TODO:check.
262
if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
263
$currentgroupid = set_current_group($course->id, $groupid);
265
} elseif ($groupmode == VISIBLEGROUPS) {
266
// All groups are visible
267
//if (ismember($group->id)){
268
$currentgroupid = set_current_group($course->id, $groupid); //set this since he might post
270
$currentgroupid = $group->id;*/
271
} elseif ($groupmode == SEPARATEGROUPS) { // student in separate groups switching
272
if (ismember($groupid)) { //check if is a member
273
$currentgroupid = set_current_group($course->id, $groupid); //might need to set_current_group?
276
notify('You do not belong to this group! ('.$groupid.')', 'error');
280
} else { // When groupid = 0 it means show ALL groups
281
// this is changed, non editting teacher needs access to group 0 as well,
282
// for viewing work in visible groups (need to set current group for multiple pages)
283
if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
284
$currentgroupid = set_current_group($course->id, 0);
286
} else if ($groupmode == VISIBLEGROUPS) { // All groups are visible
287
$currentgroupid = set_current_group($course->id, 0);
291
return $currentgroupid;
296
* A big combination function to make it easier for modules
299
* Terminates if the current user shouldn't be looking at this group
300
* Otherwise returns the current group if there is one
301
* Otherwise returns false if groups aren't relevant
303
* @uses SEPARATEGROUPS
304
* @uses VISIBLEGROUPS
305
* @param course $course A {@link $COURSE} object
306
* @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
307
* @param string $urlroot ?
310
function setup_and_print_groups($course, $groupmode, $urlroot) {
312
global $USER, $SESSION; //needs his id, need to hack his groups in session
314
$changegroup = optional_param('group', -1, PARAM_INT);
316
$currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
317
if ($currentgroup === false) {
321
$context = get_context_instance(CONTEXT_COURSE, $course->id);
323
if ($groupmode == SEPARATEGROUPS and !$currentgroup and !has_capability('moodle/site:accessallgroups', $context)) {
324
//we are in separate groups and the current group is group 0, as last set.
325
//this can mean that either, this guy has no group
326
//or, this guy just came from a visible all forum, and he left when he set his current group to 0 (show all)
328
if ($usergroups = user_group($course->id, $USER->id)){
329
//for the second situation, we need to perform the trick and get him a group.
330
$first = reset($usergroups);
331
$currentgroup = get_and_set_current_group($course, $groupmode, $first->id);
334
//else he has no group in this course
335
print_heading(get_string('notingroup'));
336
print_footer($course);
341
if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
343
if ($groups = get_groups($course->id)) {
345
echo '<div class="groupselector">';
346
print_group_menu($groups, $groupmode, $currentgroup, $urlroot, 1);
350
} else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', $context)) {
351
//get all the groups this guy is in in this course
352
if ($usergroups = user_group($course->id, $USER->id)){
353
echo '<div class="groupselector">';
354
//print them in the menu
355
print_group_menu($usergroups, $groupmode, $currentgroup, $urlroot, 0);
360
return $currentgroup;
365
function oldgroups_print_user_group_info($currentgroup, $isseparategroups, $courseid) {
367
$context = get_context_instance(CONTEXT_COURSE, $courseid);
369
if ($currentgroup and (!$isseparategroups or has_capability('moodle/site:accessallgroups', $context))) { /// Display info about the group
370
if ($group = get_record('groups', 'id', $currentgroup)) {
371
if (!empty($group->description) or (!empty($group->picture) and empty($group->hidepicture))) {
372
echo '<table class="groupinfobox"><tr><td class="left side picture">';
373
print_group_picture($group, $course->id, true, false, false);
374
echo '</td><td class="content">';
375
echo '<h3>'.$group->name;
376
if (has_capability('moodle/site:accessallgroups', $context)) {
377
echo ' <a title="'.get_string('editgroupprofile').'" href="../course/groups.php?id='.$course->id.'&group='.$group->id.'">';
378
echo '<img src="'.$CFG->pixpath.'/t/edit.gif" alt="" border="0">';
382
echo format_text($group->description);
383
echo '</td></tr></table>';
390
* Get the group object, including the course ID by default.
391
* @param groupid ID of the group.
392
* @param getcourse (default true), include the course ID in the return.
393
* @return group object, optionally including 'courseid'.
395
function groups_get_group($groupid, $getcourse=true) {
396
$group = groups_db_get_group_settings($groupid);
397
if ($group && $getcourse) {
398
$group->courseid = groups_get_course($groupid);
405
* Get an array of groups, as id => name.
406
* Replaces, get_records_menu("groups", "courseid", $course->id, "name ASC", "id,name")
407
* (For /user/index.php)
409
function groups_get_groups_names($courseid) {
410
$groupids = groups_db_get_groups($courseid);
414
$groups_names = array();
415
foreach ($groupids as $id) {
416
$groups_names[$id] = groups_get_group_name($id);
419
return $groups_names;
423
* Get the groups that a number of users are in.
424
* (For block_quiz_results.php)
426
function groups_get_groups_users($userids, $courseid) {
428
$groups_users = get_records_sql(
429
'SELECT gm.userid, gm.groupid, g.name FROM '.$CFG->prefix.'groups g LEFT JOIN '.$CFG->prefix.'groups_members gm ON g.id = gm.groupid '.
430
'WHERE g.courseid = '.$courseid.' AND gm.userid IN ('.implode(',', $userids).')'
432
return $groups_users;