community.php 9.81 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
31
32
33
34
35
36
37
<?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();


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

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

/**
 * removes a user from a community
 *
 * @param int $community id of community
 * @param int $user id of user to remove
 */
63
64
function community_remove_user($community, $userid) {    
    db_begin();
65
    delete_records('community_member', 'community', $community, 'member', $userid);
66
67
68
69
70
71
72
73
74
75
76
    delete_records('usr_watchlist_community', 'community', $community, 'usr', $userid);
    db_commit();
}

/**
 * all communities the user is a member of
 * 
 * @param int userid (optional, defaults to $USER id) 
 * @return array of community db rows
 */
function get_member_communities($userid=0, $offset=0, $limit=0) {
77

78
79
80
81
82
83
84
    $userid = optional_userid($userid);
    $prefix = get_config('dbprefix');

    return get_records_sql_array('SELECT c.*, cm.ctime, cm.tutor
              FROM ' . $prefix . 'community c 
              JOIN ' . $prefix . 'community_member cm ON cm.community = c.id
              WHERE c.owner != ? AND cm.member = ?', array($userid, $userid), $offset, $limit);
85
86
87
}


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

    $userid = optional_userid($userid);
    $prefix = get_config('dbprefix');

100
101
102
103
104
105
106
107
108
109
    $sql = 'SELECT c.* FROM ' . $prefix . 'community c 
             WHERE c.owner = ?';
    $values = array($userid);

    if (!empty($jointype)) {
        $sql .= ' AND jointype = ?';
        $values[] = $jointype;
    }
       
    return get_records_sql_array($sql, $values);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
}

/**
 * all communities the user has pending invites to
 * 
 * @param int userid (optional, defaults to $USER id)
 * @return array of community db rows
 */
function get_invited_communities($userid=0) {

    $userid = optional_userid($userid);
    $prefix = get_config('dbprefix');

    return get_records_sql_array('SELECT c.*, cmi.ctime, cmi.reason
             FROM ' . $prefix . 'community c 
             JOIN ' . $prefix . 'community_member_invite cmi ON cmi.community = c.id
             WHERE cmi.member = ?)', array($userid));
}

/**
 * all communities the user has pending requests for 
 * 
 * @param int $userid (optional, defaults to $USER id)
 * @return array of community db rows
 */

function get_requested_communities($userid=0) {

    $userid = optional_userid($userid);
    $prefix = get_config('dbprefix');

    return get_records_sql_array('SELECT c.*, cmr.ctime, cmr.reason 
              FROM ' . $prefix . 'community c 
              JOIN ' . $prefix . 'community_member_request cmr ON cmr.community = c.id
              WHERE cmr.member = ?', array($userid));
}

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
 * all communities this user is associated with at all
 * either member, invited or requested.
 * 
 * @param int $userid (optional, defaults to $USER id)
 * @return array of community db rows (with type=member|invite|request)
 */

function get_associated_communities($userid=0) {

    $userid = optional_userid($userid);
    $prefix = get_config('dbprefix');
    
    $sql = "SELECT c.*, a.type FROM " . $prefix . "community c JOIN (
    SELECT cm.community, 'invite' AS type
        FROM " . $prefix . "community_member_invite cm WHERE cm.member = ?
    UNION 
    SELECT cm.community, 'request' AS type
        FROM " . $prefix . "community_member_request cm WHERE cm.member = ?
    UNION 	
    SELECT cm.community, 'member' AS type
        FROM " . $prefix . "community_member cm WHERE cm.member = ?
    ) AS a ON a.community = c.id";
    
    return get_records_sql_assoc($sql, array($userid, $userid, $userid));
}


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

    $userid = optional_userid($userid);
    $prefix = get_config('dbprefix');

186
    $sql = 'SELECT DISTINCT c.*, cm.ctime
187
              FROM ' . $prefix . 'community c 
188
              LEFT JOIN ' . $prefix . 'community_member cm ON cm.community = c.id
189
              WHERE (c.owner = ? OR (cm.member = ? AND cm.tutor = ?))';
190
191
192
193
194
195
196
197
198
199
    $values = array($userid, $userid, 1);
    
    if (!empty($jointype)) {
        $sql .= ' AND c.jointype = ? ';
        $values[] = $jointype;
    }
    return get_records_sql_array($sql, $values);
}


200
201
202
203
204
205
// constants for community membership type
define('COMMUNITY_MEMBERSHIP_ADMIN', 1);
define('COMMUNITY_MEMBERSHIP_STAFF', 2);
define('COMMUNITY_MEMBERSHIP_OWNER', 3);
define('COMMUNITY_MEMBERSHIP_TUTOR', 4);
define('COMMUNITY_MEMBERSHIP_MEMBER', 5);
206

207

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * Can a user access a given community?
 * 
 * @param mixed $community id of community or db record (object)
 * @param mixed $user optional (object or id), defaults to logged in user
 *
 * @returns constant access level or FALSE
 */
function user_can_access_community($community, $user=null) {

    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) {
        throw new InvalidArgumentException("not useful user arg given to user_can_access_community: $user");
    }

    if (is_int($community)) {
        $community = get_record('community', 'id', $community);
    }

    if (!is_object($community)) {
        throw new InvalidArgumentException("not useful community arg given to user_can_access_community: $community");
    }

    if ($user->get('admin')) {
        return COMMUNITY_MEMBERSHIP_ADMIN;
    }
    if ($user->get('staff')) {
        return COMMUNITY_MEMBERSHIP_STAFF;
    }
    if ($community->owner == $user->get('id')) {
        return COMMUNITY_MEMBERSHIP_OWNER;
    }

    if (!$membership = get_record('community_member', 'community', $community->id, 'member', $user->get('id'))) {
        return false;
    }

    if ($membership->tutor) {
        return COMMUNITY_MEMBERSHIP_TUTOR;
    }
    
    return COMMUNITY_MEMBERSHIP_MEMBER;
}

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

/**
 * helper function to remove a user from a community
 * also deletes the community from their watchlist, 
 * and deletes any views they can only access through this community
 * from their watchlist. 
 *
 * @param int $communityid
 * @param int $userid
 */
function community_remove_member($communityid, $userid) {

    $prefix = get_config('dbprefix');

    delete_records('usr_watchlist_community', 'usr', $userid, 'community', $communityid);
    // get all the views in this user's watchlist associated with this community.
    $views = get_column_sql('SELECT v.id 
                             FROM ' . $prefix . 'view v JOIN ' . $prefix . 'usr_watchlist_view va on va.view = v.id
                             JOIN ' . $prefix . 'view_access_community c ON c.view = v.id');
    // @todo this is probably a retarded way to do it and should be changed later.
    foreach ($views as $view) {
        db_begin();
        delete_record('usr_watchlist_view', 'view', $view, 'usr', $userid);
        if (can_view_view($view, $userid)) {
            db_rollback();
        }
        else {
            db_commit();
        }
    }
    delete_records('community_member', 'member', $userid, 'community', $communityid);
Penny Leach's avatar
Penny Leach committed
293
294
295
296
    $user = optional_userobj($userid);
    activity_occurred('watchlist', 
                      array('community' => $communityid, 
                            'subject' => get_string('removedcommunitymembersubj', 'activity', display_default_name($user))));
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
}

/**
 * function to add a member to a community
 * doesn't do any jointype checking, that should be handled by the caller
 *
 * @param int $communityid
 * @param int $userid
 */
function community_add_member($communityid, $userid) {
    $cm = new StdClass;
    $cm->member = $userid;
    $cm->community = $communityid;
    $cm->ctime =  db_format_timestamp(time());
    $cm->tutor = 0;
    insert_record('community_member', $cm);
Penny Leach's avatar
Penny Leach committed
313
314
315
316
    $user = optional_userobj($userid);
    activity_occurred('watchlist', 
                      array('community' => $communityid, 
                            'subject' => get_string('newcommunitymembersubj', 'activity', display_default_name($user))));
317
318
}

319
?>