view.php 13.4 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
// Get the user's details

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

58
59
60
61
62
63
64
65
66
if ($loggedinid == $userid) {
    $view = $USER->get_profile_view();
}
else {
    $userobj = new User();
    $userobj->find_by_id($userid);
    $view = $userobj->get_profile_view();
}

67
$viewid = $view->get('id');
68
# access will either be logged in (always) or public as well
69
70
if (!$view) {
    // No access, so restrict profile view
71
    throw new AccessDeniedException(get_string('youcannotviewthisusersprofile', 'error'));
72
73
}

74
75
$restrictedview = !can_view_view($viewid);

76
$javascript = array('paginator', 'jquery', 'lib/pieforms/static/core/pieforms.js', 'artefact/resume/resumeshowhide.js');
77
$javascript = array_merge($javascript, $view->get_blocktype_javascript());
78

79
// Set up theme
80
81
82
$viewtheme = $view->get('theme');
if ($viewtheme && $THEME->basename != $viewtheme) {
    $THEME = new Theme($viewtheme);
83
}
84
$stylesheets = array('<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'theme/views.css">');
85

86
$name = display_name($user);
Martyn Smith's avatar
Martyn Smith committed
87
define('TITLE', $name);
88
$smarty = smarty(
89
    $javascript,
90
91
92
93
94
95
    $stylesheets,
    array(),
    array(
        'stylesheets' => array('style/views.css'),
        'sidebars'    => false,
    )
96
);
97
$smarty->assign('restrictedview', $restrictedview);
98
99
100
101
102
103
104
105
106
107
108
109
110
111

$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
112
113
114
if (!$allusergroups = get_records_sql_assoc($sql, array($userid, $userid, $userid))) {
    $allusergroups = array();
}
115
if (!empty($loggedinid) && $loggedinid != $userid) {
116
117
118
119

    $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

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

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

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

Penny Leach's avatar
Penny Leach committed
278
279
}

280
if ($userid != $USER->get('id') && $USER->is_admin_for_user($user) && is_null($USER->get('parentuser'))) {
281
    $loginas = get_string('loginasuser', 'admin', display_username($user));
282
283
284
285
286
} else {
    $loginas = null;
}
$smarty->assign('loginas', $loginas);

287
$smarty->assign('institutions', get_institution_string_for_user($userid));
288
$smarty->assign('canmessage', $loggedinid != $userid && can_send_message($loggedinid, $userid));
289
$smarty->assign('USERID', $userid);
290
$smarty->assign('viewtitle', get_string('usersprofile', 'mahara', display_name($user, null, true)));
291
$smarty->assign('viewtype', 'profile');
292

293
$smarty->assign('user', $user);
294
295
296
297
298
299
if (get_config('viewmicroheaders')) {
    $smarty->assign('microheaders', true);
    $smarty->assign('microheadertitle', $view->display_title(true, false));
    if ($loggedinid && $loggedinid == $userid) {
        $microheaderlinks = array(
            array(
300
                'name' => get_string('editthisview', 'view'),
301
302
303
304
305
                'url' => get_config('wwwroot') . 'view/blocks.php?profile=1',
            ),
        );
        $smarty->assign('microheaderlinks', $microheaderlinks);
    }
306
}
307
else {
308
309
310
    if ($loggedinid && $loggedinid == $userid) {
        $smarty->assign('ownprofile', true);
    }
311
    $smarty->assign('pageheadinghtml', $view->display_title(false));
312
}
313

314
315
316
317
if (!$restrictedview) {
    $smarty->assign('viewcontent', $view->build_columns());
}

318
319
$smarty->display('user/view.tpl');

320
mahara_log('views', "$viewid"); // Log view visits
321

322
// Send an invitation to the user to join a group
323
function invite_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
324
325
    global $userid;
    redirect('/group/invite.php?id=' . $values['group'] . '&user=' . $userid);
326
327
}

328
// Add the user as a member of a group
329
function addmember_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
330
    global $USER, $SESSION, $userid;
331

332
    $data = new StdClass;
333
    $data->group  = $values['group'];
Clare Lenihan's avatar
Clare Lenihan committed
334
    $data->member = $userid;
335
    $data->ctime  = db_format_timestamp(time());
Nigel McNie's avatar
Nigel McNie committed
336
    $data->role  = 'member'; // TODO: modify the dropdown to allow the role to be chosen
337
    $ctitle = get_field('group', 'name', 'id', $data->group);
338
339
    $adduser = get_record('usr', 'id', $data->member);

340
    try {
341
        insert_record('group_member', $data);
342
        delete_records('group_member_request', 'member', $userid, 'group', $data->group);
Clare Lenihan's avatar
Clare Lenihan committed
343
        $lang = get_user_language($userid);
344
        require_once(get_config('libroot') . 'activity.php');
345
346
347
348
349
350
351
        activity_occurred('maharamessage', 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'],
            'urltext' => $ctitle,
        ));
Clare Lenihan's avatar
Clare Lenihan committed
352
        $SESSION->add_ok_msg(get_string('useradded', 'group'));
353
354
    }
    catch (SQLException $e) {
355
        $SESSION->add_error_msg(get_string('adduserfailed', 'group'));
356
    }
Clare Lenihan's avatar
Clare Lenihan committed
357
    redirect('/user/view.php?id=' . $userid);
358
359
}

Clare Lenihan's avatar
Clare Lenihan committed
360
361
function approve_deny_friendrequest_submit(Pieform $form, $values) {
    if (isset($values['deny'])) {
362
        redirect('/user/denyrequest.php?id=' . $values['id'] . '&returnto=view');
Clare Lenihan's avatar
Clare Lenihan committed
363
364
365
366
367
    }
    else {
        acceptfriend_submit($form, $values);
    }
}