MDL-10383 - group edit form refactoring and minor bugfixing
[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');
5require_once($CFG->dirroot.'/group/lib/groupinglib.php');
6require_once($CFG->dirroot.'/group/lib/utillib.php');
7require_once($CFG->dirroot.'/group/lib/legacylib.php');
8
9/**
10 * Returns the groupid of a group with the name specified for the course.
11 * Group names should be unique in course
12 * @param int $courseid The id of the course
13 * @param string $name name of group (without magic quotes)
14 * @return int $groupid
15 */
16function groups_get_group_by_name($courseid, $name) {
17 if (!$group = get_record('groups', 'courseid', $courseid, 'name', addslashes($name))) {
18 return false;
19 }
20
21 return $group->id;
22}
23
24/**
25 * Get the group object
26 * @param groupid ID of the group.
27 * @return group object
28 */
29function groups_get_group($groupid) {
30 return get_record('groups', 'id', $groupid);
31}
32
33/**
34 * Gets array of all groups in a specified course.
35 * @param int $courseid The id of the course.
36 * @param int $userid optional user id, returns only groups of the user.
37 * @return array | false Returns an array of the group IDs or false if no records
38 * or an error occurred.
39 */
40function groups_get_all_groups($courseid, $userid=0) {
41 global $CFG;
42
43 if (empty($userdi)) {
44 return get_records('groups', 'courseid', $courseid, 'name ASC');
45
46 } else {
47 return get_records_sql("SELECT g.*
48 FROM {$CFG->prefix}groups g,
49 {$CFG->prefix}groups_members m
50 WHERE g.courseid = '$courseid'
51 AND g.id = m.groupid
52 AND m.userid = '$userid'
53 ORDER BY name ASC");
54 }
55}
56
57/**
58 * Determines if the user is a member of the given group.
59 *
60 * @uses $USER If $userid is null, use the global object.
61 * @param int $groupid The group to check for membership.
62 * @param int $userid The user to check against the group.
63 * @return boolean True if the user is a member, false otherwise.
64 */
65function groups_is_member($groupid, $userid=null) {
66 global $USER;
67
68 if (!$userid) {
69 $userid = $USER->id;
70 }
71
72 return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
73}
74
75
76?>