view.php 14.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
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
Richard Mansfield's avatar
Richard Mansfield committed
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * 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
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * 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
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * 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
19
20
21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
Richard Mansfield's avatar
Richard Mansfield committed
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
Richard Mansfield's avatar
Richard Mansfield committed
25
26
27
 *
 */
define('INTERNAL', 1);
28
define('PUBLIC', 1);
29
30
31
32
33
// Technically these are lies, but we set them like this to hook in the right 
// plugin stylesheet. This file should be provided by artefact/internal anyway.
define('SECTION_PLUGINTYPE', 'artefact');
define('SECTION_PLUGINNAME', 'internal');
define('SECTION_PAGE', 'view');
34

35
require(dirname(dirname(__FILE__)).'/init.php');
36
require_once('group.php');
37
require_once('pieforms/pieform.php');
38
require_once(get_config('libroot') . 'view.php');
Richard Mansfield's avatar
Richard Mansfield committed
39

40
$loggedinid = $USER->get('id');
41
42
43
44
45
46
if (!empty($loggedinid)) {
    $userid = param_integer('id', $loggedinid);
}
else {
    $userid = param_integer('id');
}
47
48
49
if ($userid == 0) {
    redirect();
}
50
51
52
if ($userid == $loggedinid) {
    define('MENUITEM', 'profile/view');
}
53

54
55
// Get the user's details

56
if (!$user = get_record('usr', 'id', $userid, 'deleted', 0)) {
57
58
    throw new UserNotFoundException("User with id $userid not found");
}
Penny Leach's avatar
Penny Leach committed
59
$is_friend = is_friend($userid, $loggedinid);
60

61
62
63
$userobj = new User();
$userobj->find_by_id($userid);
$view = $userobj->get_profile_view();
64
$viewid = $view->get('id');
65
# access will either be logged in (always) or public as well
66
if (!$view || !can_view_view($viewid)) {
67
    throw new AccessDeniedException(get_string('youcannotviewthisusersprofile', 'error'));
68
69
}

70
// Set up theme
71
72
73
$viewtheme = $view->get('theme');
if ($viewtheme && $THEME->basename != $viewtheme) {
    $THEME = new Theme($viewtheme);
74
}
75
$stylesheets = array('<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'theme/views.css">');
76

77
$name = display_name($user);
Martyn Smith's avatar
Martyn Smith committed
78
define('TITLE', $name);
79
$smarty = smarty(
80
    array('paginator', 'lib/pieforms/static/core/pieforms.js', 'artefact/resume/resumeshowhide.js'),
81
82
83
84
85
86
    $stylesheets,
    array(),
    array(
        'stylesheets' => array('style/views.css'),
        'sidebars'    => false,
    )
87
);
88
89
90
91
92
93
94
95
96
97
98
99
100
101

$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
102
103
104
if (!$allusergroups = get_records_sql_assoc($sql, array($userid, $userid, $userid))) {
    $allusergroups = array();
}
105
if (!empty($loggedinid) && $loggedinid != $userid) {
106
107
108
109

    $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

110
    // Get the logged in user's "invite only" groups
Nigel McNie's avatar
Nigel McNie committed
111
112
113
114
115
116
117
    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))) {
118
        $invitelist = array();
119
        foreach ($groups as $group) {
120
            if (array_key_exists($group->id, $allusergroups)) {
121
                $invitedlist[$group->id] = $group->name;
122
123
                continue;
            }
124
            $invitelist[$group->id] = $group->name;
125
        }
126
        $smarty->assign('invitedlist', join(', ', $invitedlist));
127
128
129
130
131
        if (count($invitelist) > 0) {
            $default = array_keys($invitelist);
            $default = $default[0];
            $inviteform = pieform(array(
                'name'              => 'invite',
Clare Lenihan's avatar
Clare Lenihan committed
132
133
                'successcallback'   => 'invite_submit',
                'renderer'          => 'div',
134
                'elements'          => array(
135
                    'group' => array(
136
                        'type'                => 'select',
Clare Lenihan's avatar
Clare Lenihan committed
137
                        'title'               => get_string('inviteusertojoingroup', 'group'),
138
139
140
141
142
143
144
145
146
147
                        'collapseifoneoption' => false,
                        'options'             => $invitelist,
                        'defaultvalue'        => $default,
                    ),
                    'id' => array(
                        'type'  => 'hidden',
                        'value' => $userid,
                    ),
                    'submit' => array(
                        'type'  => 'submit',
Clare Lenihan's avatar
Clare Lenihan committed
148
                        'value' => get_string('sendinvitation', 'group'),
149
150
151
                    ),
                ),
            ));
Clare Lenihan's avatar
Clare Lenihan committed
152
            $smarty->assign('inviteform',$inviteform);
153
        }
154
    }
155

156
157
158
159
    // 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
160
161
162
163
164
    // 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)
165
          LEFT JOIN {group_member_request} gmr ON (gmr.member = ? AND gmr.group = g.id)
Nigel McNie's avatar
Nigel McNie committed
166
          WHERE gm.member = ?
167
          AND (g.jointype = 'controlled' OR (g.jointype = 'request' AND gmr.member = ?))
Nigel McNie's avatar
Nigel McNie committed
168
          AND (gm.role = 'admin' OR gtr.see_submitted_views = 1)
169
          AND g.deleted = 0", array($userid,$loggedinid,$userid))) {
170
        $controlledlist = array();
171
        foreach ($groups as $group) {
172
            if (array_key_exists($group->id, $allusergroups)) {
173
174
                continue;
            }
175
176
177
            if ($group->jointype == 'request') {
                $requestedlist[$group->id] = $group->name;
            }
178
179
180
            else {
                $controlledlist[$group->id] = $group->name;
            }
181
        }
182
        $smarty->assign('requestedlist', join(', ', $requestedlist));
183
184
185
186
187
        if (count($controlledlist) > 0) {
            $default = array_keys($controlledlist);
            $default = $default[0];
            $addform = pieform(array(
                'name'                => 'addmember',
Clare Lenihan's avatar
Clare Lenihan committed
188
189
                'successcallback'     => 'addmember_submit',
                'renderer'            => 'div',
190
                'autofocus'           => false,
191
                'elements'            => array(
192
                    'group' => array(
193
                        'type'    => 'select',
Clare Lenihan's avatar
Clare Lenihan committed
194
                        'title'   => get_string('addusertogroup', 'group'),
195
196
197
198
                        'collapseifoneoption' => false,
                        'options' => $controlledlist,
                        'defaultvalue' => $default,
                    ),
199
200
201
202
                    'member' => array(
                        'type'  => 'hidden',
                        'value' => $userid, 
                    ),
203
204
205
206
207
                    'submit' => array(
                        'type'  => 'submit',
                        'value' => get_string('add'),
                    ),
                ),
208
            ));
Clare Lenihan's avatar
Clare Lenihan committed
209
            $smarty->assign('addform',$addform);
210
        } 
211
    }
212

Penny Leach's avatar
Penny Leach committed
213
    if ($is_friend) {
Clare Lenihan's avatar
Clare Lenihan committed
214
        $relationship = 'existingfriend';
215
    }
Clare Lenihan's avatar
Clare Lenihan committed
216
217
    else if (record_exists('usr_friend_request', 'requester', $loggedinid, 'owner', $userid)) {
        $relationship = 'requestedfriendship';
218
    }
Clare Lenihan's avatar
Clare Lenihan committed
219
220
221
222
223
224
    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
225
            'elements' => array(
Clare Lenihan's avatar
Clare Lenihan committed
226
227
228
                'approve' => array(
                    'type' => 'submit',
                    'value' => get_string('approverequest', 'group'),
Penny Leach's avatar
Penny Leach committed
229
                ),
Clare Lenihan's avatar
Clare Lenihan committed
230
                'deny' => array(
Penny Leach's avatar
Penny Leach committed
231
                    'type' => 'submit',
Clare Lenihan's avatar
Clare Lenihan committed
232
                    'value' => get_string('denyrequest', 'group')
Clare Lenihan's avatar
Clare Lenihan committed
233
234
235
236
                ),
                'id' => array(
                    'type' => 'hidden',
                    'value' => $userid
Clare Lenihan's avatar
Clare Lenihan committed
237
238
239
                )
            )
        ));
240
        $smarty->assign('message', $record->message);
Clare Lenihan's avatar
Clare Lenihan committed
241
        $smarty->assign('requestform', $requestform);
242
    }
Clare Lenihan's avatar
Clare Lenihan committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    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);
267

Penny Leach's avatar
Penny Leach committed
268
}
269
else if (!empty($loggedinid)) {
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    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',
288
                    'value' => ($public) ? get_string('loggedinusersonly') : get_string('allowpublicaccess'),
289
290
291
292
293
                ),
            ),
        ));
        $smarty->assign('togglepublic', $togglepublic);
    }
294
}
Penny Leach's avatar
Penny Leach committed
295

296
if ($userid != $USER->get('id') && $USER->is_admin_for_user($user) && is_null($USER->get('parentuser'))) {
297
    $loginas = get_string('loginasuser', 'admin', hsc($user->username));
298
299
300
301
302
} else {
    $loginas = null;
}
$smarty->assign('loginas', $loginas);

303
$smarty->assign('institutions', get_institution_string_for_user($userid));
304
$smarty->assign('canmessage', can_send_message($loggedinid, $userid));
305
$smarty->assign('USERID', $userid);
306
$smarty->assign('userdisplayname', display_name($USER, null, true));
307
$smarty->assign('viewtitle', get_string('usersprofile', 'mahara', display_name($user, null, true)));
308
$smarty->assign('viewtype', 'profile');
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

if (get_config('viewmicroheaders')) {
    $smarty->assign('microheaders', true);
    $smarty->assign('microheadertitle', $view->display_title(true, false));
    if ($loggedinid && $loggedinid == $userid) {
        $microheaderlinks = array(
            array(
                'name' => get_string('editmyprofilepage'),
                'url' => get_config('wwwroot') . 'view/blocks.php?profile=1',
                'type' => 'edit',
            ),
            array(
                'name' => get_string('editmyprofile', 'artefact.internal'),
                'url' => get_config('wwwroot') . 'artefact/internal/index.php',
                'type' => 'edit',
            ),
        );
        $smarty->assign('microheaderlinks', $microheaderlinks);
    }
    if (isset($_SERVER['HTTP_REFERER'])) {
        $smarty->assign('backurl', $_SERVER['HTTP_REFERER']);
    }
331
}
332
333
else {
    $smarty->assign('PAGEHEADING', $view->display_title(false));
334
}
335

336
$smarty->assign('viewcontent', $view->build_columns());
337
338
$smarty->display('user/view.tpl');

339
mahara_log('views', "$viewid"); // Log view visits
340

341
// Send an invitation to the user to join a group
342
function invite_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
343
344
    global $userid;
    redirect('/group/invite.php?id=' . $values['group'] . '&user=' . $userid);
345
346
}

347
// Add the user as a member of a group
348
function addmember_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
349
    global $USER, $SESSION, $userid;
350

351
    $data = new StdClass;
352
    $data->group  = $values['group'];
Clare Lenihan's avatar
Clare Lenihan committed
353
    $data->member = $userid;
354
    $data->ctime  = db_format_timestamp(time());
Nigel McNie's avatar
Nigel McNie committed
355
    $data->role  = 'member'; // TODO: modify the dropdown to allow the role to be chosen
356
    $ctitle = get_field('group', 'name', 'id', $data->group);
357
358
    $adduser = get_record('usr', 'id', $data->member);

359
    try {
360
        insert_record('group_member', $data);
361
        delete_records('group_member_request', 'member', $userid, 'group', $data->group);
Clare Lenihan's avatar
Clare Lenihan committed
362
        $lang = get_user_language($userid);
363
        require_once(get_config('libroot') . 'activity.php');
364
        activity_occurred('maharamessage', 
Clare Lenihan's avatar
Clare Lenihan committed
365
366
367
368
369
            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'));
370
371
    }
    catch (SQLException $e) {
372
        $SESSION->add_error_msg(get_string('adduserfailed', 'group'));
373
    }
Clare Lenihan's avatar
Clare Lenihan committed
374
    redirect('/user/view.php?id=' . $userid);
375
376
}

Clare Lenihan's avatar
Clare Lenihan committed
377
378
function approve_deny_friendrequest_submit(Pieform $form, $values) {
    if (isset($values['deny'])) {
379
        redirect('/user/denyrequest.php?id=' . $values['id'] . '&returnto=view');
Clare Lenihan's avatar
Clare Lenihan committed
380
381
382
383
384
385
    }
    else {
        acceptfriend_submit($form, $values);
    }
}

386
function togglepublic_submit(Pieform $form, $values) {
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    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);
407
}
408

Richard Mansfield's avatar
Richard Mansfield committed
409
?>