view.php 12.9 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
Richard Mansfield's avatar
Richard Mansfield committed
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.
Richard Mansfield's avatar
Richard Mansfield committed
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.
Richard Mansfield's avatar
Richard Mansfield committed
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/>.
Richard Mansfield's avatar
Richard Mansfield committed
18
19
20
 *
 * @package    mahara
 * @subpackage core
21
 * @author     Catalyst IT Ltd
Richard Mansfield's avatar
Richard Mansfield committed
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
Richard Mansfield's avatar
Richard Mansfield committed
24
25
26
 *
 */
define('INTERNAL', 1);
27
define('PUBLIC', 1);
28
require(dirname(dirname(__FILE__)).'/init.php');
29
require_once('group.php');
30
require_once('pieforms/pieform.php');
31
require_once(get_config('libroot') . 'view.php');
Richard Mansfield's avatar
Richard Mansfield committed
32

33
$loggedinid = $USER->get('id');
34
35
36
37
38
39
40
41
42
if (!empty($loggedinid)) {
    $userid = param_integer('id', $loggedinid);
}
else {
    $userid = param_integer('id');
}
if ($userid == $loggedinid) {
    define('MENUITEM', 'profile/view');
}
43

44
45
// Get the user's details

46
if (!$user = get_record('usr', 'id', $userid, 'deleted', 0)) {
47
48
    throw new UserNotFoundException("User with id $userid not found");
}
Penny Leach's avatar
Penny Leach committed
49
$is_friend = is_friend($userid, $loggedinid);
50

51
52
53
54
55
56
57
$view = View::profile_view($userid);
# access will either be logged in (always) or public as well
if (!can_view_view($view->get('id'))) {
    log_debug('throwing access denied exception');
    throw new AccessDeniedException();
}

58
$name = display_name($user);
Martyn Smith's avatar
Martyn Smith committed
59
define('TITLE', $name);
60
61
62
63
64
65
66
$smarty = smarty(
    array('tablerenderer'),
    array('<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'theme/views.css">'),
    array(),
    array(
        'stylesheets' => array('style/views.css'),
    )
67
);
68
69
70
71
72
73
74
75
76
77
78
79
80
81

$sql = "SELECT g.*, a.type FROM {group} g JOIN (
SELECT gm.group, 'invite' AS type
    FROM {group_member_invite} gm WHERE gm.member = ?
UNION
SELECT gm.group, 'request' AS type
    FROM {group_member_request} gm WHERE gm.member = ?
UNION
SELECT gm.group, gm.role AS type
    FROM {group_member} gm
    WHERE gm.member = ?
) AS a ON a.group = g.id
WHERE g.deleted = 0
ORDER BY g.name";
Nigel McNie's avatar
Nigel McNie committed
82
83
84
if (!$allusergroups = get_records_sql_assoc($sql, array($userid, $userid, $userid))) {
    $allusergroups = array();
}
85
if (!empty($loggedinid) && $loggedinid != $userid) {
86
87
88
89

    $invitedlist = array();   // Groups admin'ed by the logged in user that the displayed user has been invited to
    $requestedlist = array(); // Groups admin'ed by the logged in user that the displayed user has requested membership of

90
    // Get the logged in user's "invite only" groups
Nigel McNie's avatar
Nigel McNie committed
91
92
93
94
95
96
97
    if ($groups = get_records_sql_array("SELECT g.*
        FROM {group} g
        JOIN {group_member} gm ON (gm.group = g.id)
        WHERE gm.member = ?
        AND g.jointype = 'invite'
        AND gm.role = 'admin'
        AND g.deleted = 0", array($loggedinid))) {
98
        $invitelist = array();
99
        foreach ($groups as $group) {
100
            if (array_key_exists($group->id, $allusergroups)) {
101
                $invitedlist[$group->id] = $group->name;
102
103
                continue;
            }
104
            $invitelist[$group->id] = $group->name;
105
        }
106
        $smarty->assign('invitedlist', join(', ', $invitedlist));
107
108
109
110
111
        if (count($invitelist) > 0) {
            $default = array_keys($invitelist);
            $default = $default[0];
            $inviteform = pieform(array(
                'name'              => 'invite',
Clare Lenihan's avatar
Clare Lenihan committed
112
113
                'successcallback'   => 'invite_submit',
                'renderer'          => 'div',
114
                'elements'          => array(
115
                    'group' => array(
116
                        'type'                => 'select',
Clare Lenihan's avatar
Clare Lenihan committed
117
                        'title'               => get_string('inviteusertojoingroup', 'group'),
118
119
120
121
122
123
124
125
126
127
                        'collapseifoneoption' => false,
                        'options'             => $invitelist,
                        'defaultvalue'        => $default,
                    ),
                    'id' => array(
                        'type'  => 'hidden',
                        'value' => $userid,
                    ),
                    'submit' => array(
                        'type'  => 'submit',
Clare Lenihan's avatar
Clare Lenihan committed
128
                        'value' => get_string('sendinvitation', 'group'),
129
130
131
                    ),
                ),
            ));
Clare Lenihan's avatar
Clare Lenihan committed
132
            $smarty->assign('inviteform',$inviteform);
133
        }
134
    }
135

136
137
138
139
    // Get (a) controlled membership groups,
    //     (b) request membership groups where the displayed user has requested membership,
    // where the logged in user either:
    // 1. is a group admin, or;
Nigel McNie's avatar
Nigel McNie committed
140
141
142
143
144
    // 2. has a role in the list of roles who are allowed to assess submitted views for the given grouptype
    if ($groups = get_records_sql_array("SELECT g.*, gm.ctime
          FROM {group} g
          JOIN {group_member} gm ON (gm.group = g.id)
          JOIN {grouptype_roles} gtr ON (gtr.grouptype = g.grouptype AND gtr.role = gm.role)
145
          LEFT JOIN {group_member_request} gmr ON (gmr.member = ? AND gmr.group = g.id)
Nigel McNie's avatar
Nigel McNie committed
146
          WHERE gm.member = ?
147
          AND (g.jointype = 'controlled' OR (g.jointype = 'request' AND gmr.member = ?))
Nigel McNie's avatar
Nigel McNie committed
148
          AND (gm.role = 'admin' OR gtr.see_submitted_views = 1)
149
          AND g.deleted = 0", array($userid,$loggedinid,$userid))) {
150
        $controlledlist = array();
151
152
        foreach ($groups as $group) {
            if (array_key_exists($group->id, $userassocgroups)) {
153
154
                continue;
            }
155
156
157
            if ($group->jointype == 'request') {
                $requestedlist[$group->id] = $group->name;
            }
158
            $controlledlist[$group->id] = $group->name;
159
        }
160
        $smarty->assign('requestedlist', join(', ', $requestedlist));
161
162
163
164
165
        if (count($controlledlist) > 0) {
            $default = array_keys($controlledlist);
            $default = $default[0];
            $addform = pieform(array(
                'name'                => 'addmember',
Clare Lenihan's avatar
Clare Lenihan committed
166
167
                'successcallback'     => 'addmember_submit',
                'renderer'            => 'div',
168
                'elements'            => array(
169
                    'group' => array(
170
                        'type'    => 'select',
Clare Lenihan's avatar
Clare Lenihan committed
171
                        'title'   => get_string('addusertogroup', 'group'),
172
173
174
175
                        'collapseifoneoption' => false,
                        'options' => $controlledlist,
                        'defaultvalue' => $default,
                    ),
176
177
178
179
                    'member' => array(
                        'type'  => 'hidden',
                        'value' => $userid, 
                    ),
180
181
182
183
184
                    'submit' => array(
                        'type'  => 'submit',
                        'value' => get_string('add'),
                    ),
                ),
185
            ));
Clare Lenihan's avatar
Clare Lenihan committed
186
            $smarty->assign('addform',$addform);
187
        } 
188
    }
189

Penny Leach's avatar
Penny Leach committed
190
    if ($is_friend) {
Clare Lenihan's avatar
Clare Lenihan committed
191
        $relationship = 'existingfriend';
192
    }
Clare Lenihan's avatar
Clare Lenihan committed
193
194
    else if (record_exists('usr_friend_request', 'requester', $loggedinid, 'owner', $userid)) {
        $relationship = 'requestedfriendship';
195
    }
Clare Lenihan's avatar
Clare Lenihan committed
196
197
198
199
200
201
    else if ($record = get_record('usr_friend_request', 'requester', $userid, 'owner', $loggedinid)) {
        $relationship = 'pending';
        $requestform = pieform(array(
            'name' =>'approve_deny_friendrequest',
            'renderer' => 'oneline',
            'autofocus' => false,
Penny Leach's avatar
Penny Leach committed
202
            'elements' => array(
Clare Lenihan's avatar
Clare Lenihan committed
203
204
205
                'approve' => array(
                    'type' => 'submit',
                    'value' => get_string('approverequest', 'group'),
Penny Leach's avatar
Penny Leach committed
206
                ),
Clare Lenihan's avatar
Clare Lenihan committed
207
                'deny' => array(
Penny Leach's avatar
Penny Leach committed
208
                    'type' => 'submit',
Clare Lenihan's avatar
Clare Lenihan committed
209
                    'value' => get_string('denyrequest', 'group')
Clare Lenihan's avatar
Clare Lenihan committed
210
211
212
213
                ),
                'id' => array(
                    'type' => 'hidden',
                    'value' => $userid
Clare Lenihan's avatar
Clare Lenihan committed
214
215
216
                )
            )
        ));
217
        $smarty->assign('message', $record->message);
Clare Lenihan's avatar
Clare Lenihan committed
218
        $smarty->assign('requestform', $requestform);
219
    }
Clare Lenihan's avatar
Clare Lenihan committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    else {
        $relationship = 'none';
        $friendscontrol = get_account_preference($userid, 'friendscontrol');
        if ($friendscontrol == 'auto') {
            $newfriendform = pieform(array(
                'name' => 'addfriend',
                'autofocus' => false,
                'renderer' => 'div',
                'elements' => array(
                    'add' => array(
                        'type' => 'submit',
                        'value' => get_string('addtomyfriends', 'group')
                    ),
                    'id' => array(
                        'type' => 'hidden',
                        'value' => $userid
                    )
                )
            ));
            $smarty->assign('newfriendform', $newfriendform);
        }
        $smarty->assign('friendscontrol', $friendscontrol);
    }
    $smarty->assign('relationship', $relationship);
244

Penny Leach's avatar
Penny Leach committed
245
}
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
else if (!empty($loggedinid)) {
    if (get_config('allowpublicprofiles')) {
        $public = array_filter($view->get_access(), 
            create_function(
                '$item', 
                'return $item[\'type\'] == \'public\';'
            )
        );
        $togglepublic = pieform(array(
            'name'      => 'togglepublic',
            'autofocus' => false,
            'renderer'  => 'div',
            'elements'  => array(
                'changeto' => array(
                    'type'  => 'hidden',
                    'value' => ($public) ? 'loggedin' : 'public'
                ),
                'submit' => array(
                    'type' => 'submit',
                    'value' => ($public) ? 'Logged in users only' : 'Allow public (non logged in) access',
                ),
            ),
        ));
        $smarty->assign('togglepublic', $togglepublic);
    }
}
Penny Leach's avatar
Penny Leach committed
272

273
if ($userid != $USER->get('id') && $USER->is_admin_for_user($user) && is_null($USER->get('parentuser'))) {
274
275
276
277
278
279
    $loginas = get_string('loginasuser', 'admin', $user->username);
} else {
    $loginas = null;
}
$smarty->assign('loginas', $loginas);

Clare Lenihan's avatar
Clare Lenihan committed
280
281
282
if (isset($introduction)) {
    $smarty->assign('introduction', $introduction);
}
283
$smarty->assign('canmessage', can_send_message($loggedinid, $userid));
284
$smarty->assign('NAME',$name);
285
$smarty->assign('USERID', $userid);
286
$smarty->assign('viewcontent', $view->build_columns());
287
288
$smarty->display('user/view.tpl');

289
// Send an invitation to the user to join a group
290
function invite_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
291
292
    global $userid;
    redirect('/group/invite.php?id=' . $values['group'] . '&user=' . $userid);
293
294
}

295
// Add the user as a member of a group
296
function addmember_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
297
    global $USER, $SESSION, $userid;
298

299
    $data = new StdClass;
300
    $data->group  = $values['group'];
Clare Lenihan's avatar
Clare Lenihan committed
301
    $data->member = $userid;
302
    $data->ctime  = db_format_timestamp(time());
Nigel McNie's avatar
Nigel McNie committed
303
    $data->role  = 'member'; // TODO: modify the dropdown to allow the role to be chosen
304
    $ctitle = get_field('group', 'name', 'id', $data->group);
305
306
    $adduser = get_record('usr', 'id', $data->member);

307
    try {
308
        insert_record('group_member', $data);
309
        delete_records('group_member_request', 'member', $userid, 'group', $data->group);
Clare Lenihan's avatar
Clare Lenihan committed
310
        $lang = get_user_language($userid);
311
        activity_occurred('maharamessage', 
Clare Lenihan's avatar
Clare Lenihan committed
312
313
314
315
316
            array('users'   => array($userid),
                  'subject' => get_string_from_language($lang, 'addedtogroupsubject', 'group'),
                  'message' => get_string_from_language($lang, 'addedtogroupmessage', 'group', display_name($USER, $adduser), $ctitle),
                  'url'     => get_config('wwwroot') . 'group/view.php?id=' . $values['group']));
        $SESSION->add_ok_msg(get_string('useradded', 'group'));
317
318
    }
    catch (SQLException $e) {
319
        $SESSION->add_error_msg(get_string('adduserfailed', 'group'));
320
    }
Clare Lenihan's avatar
Clare Lenihan committed
321
    redirect('/user/view.php?id=' . $userid);
322
323
}

Clare Lenihan's avatar
Clare Lenihan committed
324
325
function approve_deny_friendrequest_submit(Pieform $form, $values) {
    if (isset($values['deny'])) {
326
        redirect('/user/denyrequest.php?id=' . $values['id'] . '&returnto=view');
Clare Lenihan's avatar
Clare Lenihan committed
327
328
329
330
331
332
    }
    else {
        acceptfriend_submit($form, $values);
    }
}

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
function togglepublic_submit(Pieform $form, $values) {
    global $SESSION, $userid, $view;
    $access = array(
        array(
            'type'      => 'loggedin',
            'startdate' => null,
            'stopdate'  => null,
        ),
    );

    if ($values['changeto'] == 'public') {
        $access[] = array(
            'type'      => 'public',
            'startdate' => null,
            'stopdate'  => null,
        );
    }
    $view->set_access($access);
    $SESSION->add_ok_msg(get_string('viewaccesseditedsuccessfully', 'view'));

    redirect('/user/view.php?id=' . $userid);
}

Richard Mansfield's avatar
Richard Mansfield committed
356
?>