view.php 11.8 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
4
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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 3 of the License, or
 * (at your option) any later version.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
25
26
27
 *
 * @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
 *
 */

define('INTERNAL', 1);
28
define('MENUITEM', 'groups');
29
require(dirname(dirname(__FILE__)) . '/init.php');
30
define('TITLE', get_string('groups'));
31
require_once('group.php');
32
require_once(get_config('docroot') . 'interaction/lib.php');
33
34

$id = param_integer('id');
35
$joincontrol = param_alpha('joincontrol', null);
36
$pending = param_integer('pending', 0);
37

38
39
if (!$group = get_record('group', 'id', $id)) {
    throw new GroupNotFoundException("Couldn't find group with id $id");
40
}
41
$group->ownername = display_name(get_record('usr', 'id', $group->owner));
42

43
$membership = user_can_access_group($id);
44
// $membership is a bit string summing all membership types
45
$ismember = (bool) ($membership & GROUP_MEMBERSHIP_MEMBER);
46

47
48
49
50
51
if (!empty($joincontrol)) {
    // leave, join, acceptinvite, request
    switch ($joincontrol) {
        case 'leave':
            // make sure they're a member and can leave
52
            if ($ismember && $group->jointype != 'controlled') {
53
                group_remove_user($id, $USER->get('id'));
54
                $SESSION->add_ok_msg(get_string('leftgroup', 'group'));
55
56
            } 
            else {
57
                $SESSION->add_error_msg(get_string('couldnotleavegroup', 'group'));
58
59
60
            }
            break;
        case 'join':
61
62
            if (!$ismember && $group->jointype == 'open') {
                group_add_member($id, $USER->get('id'));
63
                $SESSION->add_ok_msg(get_string('joinedgroup', 'group'));
64
65
            }
            else {
66
                $SESSION->add_error_msg(get_string('couldnotjoingroup', 'group'));
67
68
69
70
            }
            break;
        case 'acceptinvite':
        case 'declineinvite':
71
            if (!$request = get_record('group_member_invite', 'member', $USER->get('id'), 'group', $id)) {
72
                $SESSION->add_error_msg(get_string('groupnotinvited', 'group'));
73
74
75
                break;
            }
            if ($joincontrol == 'acceptinvite') {
76
                group_add_member($id, $USER->get('id'));
77
                $message = get_string('groupinviteaccepted', 'group');
78
79
            }
            else {
80
                $message = get_string('groupinvitedeclined', 'group');
81
            }
82
            delete_records('group_member_invite', 'member', $USER->get('id'), 'group', $id);
83
84
85
            $SESSION->add_ok_msg($message);
            break;
        case 'request':
86
87
88
89
90
91
92
93
94
95
            if (!$ismember && $group->jointype == 'request' 
                && !record_exists('group_member_request', 'group', $id, 'member', $USER->get('id'))) {
                $gmr = new StdClass;
                $gmr->reason = param_variable('reason', null);
                $gmr->group = $id;
                $gmr->member = $USER->get('id');
                $gmr->ctime = db_format_timestamp(time());
                $owner = get_record('usr', 'id', $group->owner);
                insert_record('group_member_request', $gmr);
                if (empty($gmr->reason)) {
96
                    $message = get_string('grouprequestmessage', 'group', 
97
                                          display_name($USER, $owner), $group->name);
98
99
                } 
                else {
100
                    $message = get_string('grouprequestmessagereason', 'group', 
101
                                          display_name($USER, $owner), $group->name, $gmr->reason);
102
103
104
                }
                require_once('activity.php');
                activity_occurred('maharamessage', 
105
                    array('users'   => array($group->owner), 
106
                          'subject' => get_string('grouprequestsubject', 'group'),
107
                          'message' => $message,
108
                          'url'     => get_config('wwwroot') . 'group/view.php?id=' . $id));
109
                $SESSION->add_ok_msg(get_string('grouprequestsent', 'group'));
110
111
            }
            else {
112
                $SESSION->add_error_msg(get_string('couldnotrequestgroup', 'group'));
113
114
115
116
            }
            break;
    }
    // redirect, stuff will have changed
117
    redirect('/group/view.php?id=' . $id);
118
119
    exit;
}
120

121
122
$invited   = get_record('group_member_invite', 'group', $id, 'member', $USER->get('id'));
$requested = get_record('group_member_request', 'group', $id, 'member', $USER->get('id'));
123
124

$userview = get_config('wwwroot') . 'user/view.php?id=';
125
$viewview = get_config('wwwroot') . 'view/view.php?id=';
126
$commview = get_config('wwwroot') . 'group/view.php';
127

128
// strings that are used in the js
129
130
131
132
133
134
135
136
$releaseviewstr  = get_string('releaseview', 'group');
$tutorstr        = get_string('tutor', 'group');
$memberstr       = get_string('member', 'group');
$removestr       = get_string('remove', 'group');
$declinestr      = get_string('declinerequest', 'group');
$updatefailedstr = get_string('updatefailed', 'group');
$requeststr      = get_string('sendrequest', 'group');
$reasonstr       = get_string('reason', 'group');
137

138
// all the permissions stuff
139
140
141
142
143
144
145
//$tutor          = (int)($membership && ($membership != GROUP_MEMBERSHIP_MEMBER));
$controlled     = (int)($group->jointype == 'controlled');
$request        = (int)($group->jointype == 'request');
$tutor          = (int)(bool)($membership & GROUP_MEMBERSHIP_TUTOR);
$admin          = (int)(bool)($membership & GROUP_MEMBERSHIP_ADMIN);
$staff          = (int)(bool)($membership & GROUP_MEMBERSHIP_STAFF);
$owner          = (int)(bool)($membership & GROUP_MEMBERSHIP_OWNER);
146
147
148
$canupdate      = (int)(bool)($tutor || $staff || $admin || $owner);
$canpromote     = (int)(bool)(($staff || $admin) && $controlled);
$canremove      = (int)(bool)(($tutor && $controlled) || $staff || $admin || $owner);
Richard Mansfield's avatar
Richard Mansfield committed
149
$canleave       = ($ismember && !$controlled);
150
$canrequestjoin = (!$ismember && empty($invited) && empty($requested) && $group->jointype == 'request');
151
$canjoin        = (!$ismember && $group->jointype == 'open' && !$owner);
152

153
154
155
$javascript = '';
if ($membership) {
    $javascript .= <<<EOF
156

157
viewlist = new TableRenderer(
158
    'group_viewlist',
159
160
161
162
163
164
165
166
167
    'view.json.php',
    [
     function (r) {
         return TD(null, A({'href': '{$viewview}' + r.id}, r.title));
     },
     function (r) {
         return TD(null, A({'href': '{$userview}' + r.owner}, r.ownername));
     },
     function (r,d) {
168
         if (r.submittedto && {$tutor} == 1) {
169
             return TD(null, A({'href': '', 'onclick': 'return releaseView(' + r.id + ');'}, '{$releaseviewstr}'));
170
171
172
173
174
175
         }
         return TD(null);
     }
    ]
);

176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
viewlist.type = 'views';
viewlist.submitted = 0;
viewlist.id = $id;
viewlist.statevars.push('type');
viewlist.statevars.push('id');
viewlist.statevars.push('submitted');
viewlist.updateOnLoad();

memberlist = new TableRenderer(
    'memberlist',
    'view.json.php',
    [
     function (r) {
         return TD(null, A({'href': '{$userview}' + r.id}, r.displayname));
     },
EOF;
193
if ($canupdate) {
194
    $javascript .= <<<EOF
195
    'reason',
196
197
198
     function (r) {
         var options = new Array();
         var member = OPTION({'value': 'member'}, '{$memberstr}');
199
         if (r.request != 1) {
200
201
202
             member.selected = true;
         }
         options.push(member);
203
204
205
206
207
         if (r.request) {
             var nonmember = OPTION({'value': 'declinerequest'}, '{$declinestr}');
             nonmember.selected = true;
             options.push(nonmember);
         }
208
EOF;
209
210
211
212
213
214
215
216
217
218
219
    if ($canpromote) {
    $javascript .= <<<EOF
         var tutor = OPTION({'value': 'tutor'}, '{$tutorstr}');
         if (r.tutor == 1) {
             member.selected = false;
             tutor.selected = true;
         }
         options.push(tutor);
EOF;
    }
    if ($canremove) {
220
        $javascript .= <<<EOF
221
222
223
224
        if (!r.request) {
            var remove = OPTION({'value': 'remove'}, '{$removestr}');
            options.push(remove);
        }
225
226
227
228
229
230
231
232
233
234
235
236
EOF;
    }
    $javascript .= <<<EOF

         return TD(null, SELECT({'name': 'member-' + r.id, 'class': 'member'}, options));
     }
EOF;
}
$javascript .= <<<EOF
    ]
);
memberlist.id = $id;
237
memberlist.type='members';
238
239
240
241
242
243
memberlist.pending = 0;
memberlist.statevars.push('type');
memberlist.statevars.push('pending');
memberlist.statevars.push('id');
memberlist.updateOnLoad();

244
245
addLoadEvent(function () { hideElement($('pendingreasonheader')); });

246
247
248
249
250
251
252
253
254
255
256
257
function switchPending(force) {
    if (force) {
        pending = force;
        var theOption = filter(
            function (o) { if ( o.value == pending ) return true; return false; },
            $('pendingselect').options
        );
        theOption[0].selected = true;
    } 
    else {
        var pending = $('pendingselect').options[$('pendingselect').selectedIndex].value;
    }
258
259
260
261
262
263
264
265
    if (pending == 0) {
        hideElement($('pendingreasonheader'));
    }
    else {
        showElement($('pendingreasonheader'));
    }
    memberlist.pending = pending;
    memberlist.doupdate();
266
267
268
}

function releaseView(id) {
269
    var pd = {'type': 'release', 'id': '{$group->id}', 'view': id};
Richard Mansfield's avatar
Richard Mansfield committed
270
    sendjsonrequest('view.json.php', pd, 'GET', function (data) {
271
272
        viewlist.doupdate();
    });
273
274
275
276
    return false;
}

function updateMembership() {
277
    var pd = {'type': 'membercontrol', 'id': '{$group->id}'};
278
279
280
281
    var e = getElementsByTagAndClassName(null, 'member');
    for (s in e) {
        pd[e[s].name] = e[s].options[e[s].selectedIndex].value;
    }
Richard Mansfield's avatar
Richard Mansfield committed
282
    sendjsonrequest('view.json.php', pd, 'GET', function (data) {
283
284
285
        if (memberlist.pending == 1) {
            memberlist.offset = 0;
        }
286
        memberlist.doupdate();
287
    });
288
289
290
}
EOF;

291
292
293
294
295
296
297
298
299
300
}// end of membership only javascript (tablerenderers etc)
$javascript .= <<<EOF

function joinRequestControl() {
    var form = P({'id': 'joinrequestextras'},
                 '{$reasonstr}: ', 
                 FORM({'method': 'post', 'action': '{$commview}'}, 
                      INPUT({'type': 'hidden', 'name': 'id', 'value': {$id}}),
                      INPUT({'type': 'hidden', 'name': 'joincontrol', 'value': 'request'}),
                      INPUT({'type': 'text', 'name': 'reason'}),
301
302
                      ' ',
                      INPUT({'type': 'submit', 'class': 'submit', 'value': '{$requeststr}'})));
303
304
305
306
307
308
    insertSiblingNodesAfter('joinrequest', form);
    return false;
}

EOF;

309
310
311
312
313
if (!empty($pending) && $canupdate && $request) {
    $javascript .= <<<EOF
addLoadEvent(function () { switchPending(1) });
EOF;
}
314

315
$smarty = smarty(array('tablerenderer'), array(), array(), array('sideblocks' => array(interaction_sideblock($id))));
316

317
318
319
$smarty->assign('INLINEJAVASCRIPT', $javascript);
$smarty->assign('member', $membership);
$smarty->assign('tutor', $tutor);
320
321
$smarty->assign('staff', $staff);
$smarty->assign('admin', $admin);
322
$smarty->assign('controlled', $controlled);
323
$smarty->assign('request', $request);
324
325
326
$smarty->assign('canjoin', $canjoin);
$smarty->assign('canrequestjoin', $canrequestjoin);
$smarty->assign('canleave', $canleave);
327
328
$smarty->assign('canpromote', $canpromote);
$smarty->assign('canupdate', $canupdate);
329
$smarty->assign('canacceptinvite', $invited);
330
$smarty->assign('group', $group);
331
$smarty->assign('hasmembers', group_has_members($group->id));
332
$smarty->display('group/view.tpl');
333
334


335
?>