group.php 7.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/**
 * This program is part of Mahara
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();


/**
31
 * is a user allowed to leave a group? 
32
33
 * checks if they're the owner and the membership type
 *
34
 * @param object $group (corresponds to db record). if an id is given, record will be fetched.
35
36
 * @param int $userid (optional, will default to logged in user)
 */
37
function group_user_can_leave($group, $userid=null) {
38
39

    $userid = optional_userid($userid);
40
    
41
42
    if (is_numeric($group)) {
        if (!$group = get_record('group', 'id', $group)) {
43
44
45
46
            return false;
        }
    }
    
47
    if ($group->owner == $userid) {
48
49
50
        return false;
    }
    
51
    if ($group->jointype == 'controlled') {
52
53
54
55
56
57
        return false;
    }
    return true;
}

/**
58
 * removes a user from a group
59
 *
60
 * @param int $group id of group
61
62
 * @param int $user id of user to remove
 */
63
function group_remove_user($group, $userid) {    
64
    db_begin();
65
    delete_records('group_member', 'group', $group, 'member', $userid);
66
67
68
69
    db_commit();
}

/**
70
 * all groups the user is a member of
71
72
 * 
 * @param int userid (optional, defaults to $USER id) 
73
 * @return array of group db rows
74
 */
75
function get_member_groups($userid=0, $offset=0, $limit=0) {
76

77
78
    $userid = optional_userid($userid);

79
    return get_records_sql_array('SELECT g.id, g.name, g.description, g.jointype, g.owner, g.ctime, g.mtime, gm.ctime, gm.tutor, COUNT(v.view) AS hasviews
80
81
82
              FROM {group} g 
              JOIN {group_member} gm ON gm.group = g.id
              LEFT JOIN {view_access_group} v ON v.group = g.id
83
              WHERE g.owner != ? AND gm.member = ?
84
              GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9', array($userid, $userid), $offset, $limit);
85
86
87
}


88
/**
89
 * all groups the user owns
90
91
 * 
 * @param int userid (optional, defaults to $USER id) 
92
 * @param string $jointype (optional), will filter by jointype.
93
 * @return array of group db rows
94
 */
95
function get_owned_groups($userid=0, $jointype=null) {
96
97
98

    $userid = optional_userid($userid);

99
    $sql = 'SELECT g.* FROM {group} g 
100
             WHERE g.owner = ?';
101
102
103
104
105
106
107
108
    $values = array($userid);

    if (!empty($jointype)) {
        $sql .= ' AND jointype = ?';
        $values[] = $jointype;
    }
       
    return get_records_sql_array($sql, $values);
109
110
111
}

/**
112
 * all groups the user has pending invites to
113
114
 * 
 * @param int userid (optional, defaults to $USER id)
115
 * @return array of group db rows
116
 */
117
function get_invited_groups($userid=0) {
118
119
120

    $userid = optional_userid($userid);

121
    return get_records_sql_array('SELECT g.*, gmi.ctime, gmi.reason
122
123
             FROM {group} g 
             JOIN {group_member_invite} gmi ON gmi.group = g.id
124
             WHERE gmi.member = ?)', array($userid));
125
126
127
}

/**
128
 * all groups the user has pending requests for 
129
130
 * 
 * @param int $userid (optional, defaults to $USER id)
131
 * @return array of group db rows
132
133
 */

134
function get_requested_group($userid=0) {
135
136
137

    $userid = optional_userid($userid);

138
    return get_records_sql_array('SELECT g.*, gmr.ctime, gmr.reason 
139
              FROM {group} g 
140
              JOIN {group_member_request} gmr ON gmr.group = g.id
141
              WHERE gmr.member = ?', array($userid));
142
143
}

144
/**
145
 * all groups this user is associated with at all
146
147
148
 * either member, invited or requested.
 * 
 * @param int $userid (optional, defaults to $USER id)
149
 * @return array of group db rows (with type=member|invite|request)
150
151
 */

152
function get_associated_groups($userid=0) {
153
154
155

    $userid = optional_userid($userid);
    
156
    $sql = "SELECT g.*, a.type FROM {group} g JOIN (
157
    SELECT gm.group, 'invite' AS type
158
        FROM {group_member_invite} gm WHERE gm.member = ?
159
    UNION 
160
    SELECT gm.group, 'request' AS type
161
        FROM {group_member_request} gm WHERE gm.member = ?
162
    UNION 	
163
    SELECT gm.group, 'member' AS type
164
165
166
167
        FROM {group_member} gm WHERE gm.member = ? AND gm.tutor = 0
    UNION
    SELECT gm.group, 'tutor' AS type
        FROM {group_member} gm WHERE gm.member = ? AND gm.tutor = 1
168
    ) AS a ON a.group = g.id";
169
    
170
    return get_records_sql_assoc($sql, array($userid, $userid, $userid, $userid));
171
172
173
174
}


/**
175
 * gets groups the user is a tutor in, or the user owns
176
177
178
179
 * 
 * @param int $userid (optional, defaults to $USER id)
 * @param string $jointype (optional, will filter by jointype
 */
180
function get_tutor_groups($userid=0, $jointype=null) {
181
182
183

    $userid = optional_userid($userid);

184
    $sql = 'SELECT DISTINCT g.*, gm.ctime
185
186
              FROM {group} g 
              LEFT JOIN {group_member} gm ON gm.group = g.id
187
              WHERE (g.owner = ? OR (gm.member = ? AND gm.tutor = ?))';
188
189
190
    $values = array($userid, $userid, 1);
    
    if (!empty($jointype)) {
191
        $sql .= ' AND g.jointype = ? ';
192
193
194
195
196
197
        $values[] = $jointype;
    }
    return get_records_sql_array($sql, $values);
}


198
199
200
201
202
203
// constants for group membership type
define('GROUP_MEMBERSHIP_ADMIN', 1);
define('GROUP_MEMBERSHIP_STAFF', 2);
define('GROUP_MEMBERSHIP_OWNER', 4);
define('GROUP_MEMBERSHIP_TUTOR', 8);
define('GROUP_MEMBERSHIP_MEMBER', 16);
204

205

206
/**
207
 * Can a user access a given group?
208
 * 
209
 * @param mixed $group id of group or db record (object)
210
211
212
213
 * @param mixed $user optional (object or id), defaults to logged in user
 *
 * @returns constant access level or FALSE
 */
214
function user_can_access_group($group, $user=null) {
215
216
217
218
219
220
221
222
223
224
225
226
227

    if (empty($userid)) {
        global $USER;
        $user = $USER;
    }
    else if (is_int($user)) {
        $user = get_user($user);
    }
    else if (is_object($user) && !$user instanceof User) {
        $user = get_user($user->get('id'));
    }

    if (!$user instanceof User) {
228
        throw new InvalidArgumentException("not useful user arg given to user_can_access_group: $user");
229
230
    }

231
232
    if (is_int($group)) {
        $group = get_record('group', 'id', $group);
233
234
    }

235
236
    if (!is_object($group)) {
        throw new InvalidArgumentException("not useful group arg given to user_can_access_group: $group");
237
238
    }

239
240
    $membertypes = 0;

241
    if ($user->get('admin')) {
242
        $membertypes = GROUP_MEMBERSHIP_ADMIN;
243
244
    }
    if ($user->get('staff')) {
245
        $membertypes = $membertypes | GROUP_MEMBERSHIP_STAFF;
246
    }
247
248
    if ($group->owner == $user->get('id')) {
        $membertypes = $membertypes | GROUP_MEMBERSHIP_OWNER;
249
250
    }

251
    if (!$membership = get_record('group_member', 'group', $group->id, 'member', $user->get('id'))) {
252
        return $membertypes;
253
254
255
    }

    if ($membership->tutor) {
256
        $membertypes = $membertypes | GROUP_MEMBERSHIP_TUTOR;
257
258
    }
    
259
    return ($membertypes | GROUP_MEMBERSHIP_MEMBER);
260
261
}

262
263

/**
264
 * helper function to remove a user from a group
265
 *
266
 * @param int $groupid
267
268
 * @param int $userid
 */
269
270
function group_remove_member($groupid, $userid) {
    delete_records('group_member', 'member', $userid, 'group', $groupid);
271
272
273
}

/**
274
 * function to add a member to a group
275
276
 * doesn't do any jointype checking, that should be handled by the caller
 *
277
 * @param int $groupid
278
279
 * @param int $userid
 */
280
function group_add_member($groupid, $userid) {
281
282
    $cm = new StdClass;
    $cm->member = $userid;
283
    $cm->group = $groupid;
284
285
    $cm->ctime =  db_format_timestamp(time());
    $cm->tutor = 0;
286
    insert_record('group_member', $cm);
Penny Leach's avatar
Penny Leach committed
287
    $user = optional_userobj($userid);
288
289
}

290
?>