MDL-10383 - groups/groupings refactoring and improvements - basic UI for groupings...
[moodle.git] / lib / grouplib.php
CommitLineData
2c386f82 1<?php //$Id$
2
3// folowing files will be removed soon
4require_once($CFG->dirroot.'/group/lib/basicgrouplib.php');
2c386f82 5require_once($CFG->dirroot.'/group/lib/utillib.php');
6require_once($CFG->dirroot.'/group/lib/legacylib.php');
7
8/**
9 * Returns the groupid of a group with the name specified for the course.
10 * Group names should be unique in course
11 * @param int $courseid The id of the course
12 * @param string $name name of group (without magic quotes)
13 * @return int $groupid
14 */
15function groups_get_group_by_name($courseid, $name) {
ddff2fa8 16 if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
17 return key($groups);
2c386f82 18 }
ddff2fa8 19 return false;
20}
2c386f82 21
ddff2fa8 22/**
23 * Returns the groupingid of a grouping with the name specified for the course.
24 * Grouping names should be unique in course
25 * @param int $courseid The id of the course
26 * @param string $name name of group (without magic quotes)
27 * @return int $groupid
28 */
29function groups_get_grouping_by_name($courseid, $name) {
30 if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) {
31 return key($groupings);
32 }
33 return false;
2c386f82 34}
35
36/**
37 * Get the group object
38 * @param groupid ID of the group.
39 * @return group object
40 */
41function groups_get_group($groupid) {
42 return get_record('groups', 'id', $groupid);
43}
44
45/**
46 * Gets array of all groups in a specified course.
47 * @param int $courseid The id of the course.
48 * @param int $userid optional user id, returns only groups of the user.
62d63838 49 * @param int $groupingid optional returns only groups in the specified grouping.
2c386f82 50 * @return array | false Returns an array of the group IDs or false if no records
51 * or an error occurred.
52 */
62d63838 53function groups_get_all_groups($courseid, $userid=0, $groupingid=0) {
2c386f82 54 global $CFG;
55
62d63838 56 if (!empty($userid)) {
57 $userfrom = ", {$CFG->prefix}groups_members gm";
58 $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
59 } else {
60 $userfrom = "";
61 $userwhere = "";
62 }
2c386f82 63
62d63838 64 if (!empty($groupingid)) {
65 $groupingfrom = ", {$CFG->prefix}groupings_groups gg";
66 $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
2c386f82 67 } else {
62d63838 68 $groupingfrom = "";
69 $groupingwhere = "";
2c386f82 70 }
62d63838 71
72 return get_records_sql("SELECT g.*
73 FROM {$CFG->prefix}groups g $userfrom $groupingfrom
74 WHERE g.courseid = '$courseid' $userwhere $groupingwhere
75 ORDER BY name ASC");
2c386f82 76}
77
78/**
79 * Determines if the user is a member of the given group.
80 *
81 * @uses $USER If $userid is null, use the global object.
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.
85 */
86function groups_is_member($groupid, $userid=null) {
87 global $USER;
88
89 if (!$userid) {
90 $userid = $USER->id;
91 }
92
93 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
94}
95
62d63838 96/**
97 * Returns the users in the specified group.
98 * @param int $groupid The groupid to get the users for
99 * @param int $sort optional sorting of returned users
100 * @return array | false Returns an array of the users for the specified
101 * group or false if no users or an error returned.
102 */
103function groups_get_members($groupid, $sort='lastname ASC') {
104 global $CFG;
105
106 return get_records_sql("SELECT u.*
107 FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
108 WHERE u.id = gm.userid AND gm.groupid = '$groupid'
109 ORDER BY $sort");
110}
111
112
2c386f82 113
114?>