view.php 13.2 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
# access will either be logged in (always) or public as well
68
69
if (!$view) {
    // No access, so restrict profile view
70
    throw new AccessDeniedException(get_string('youcannotviewthisusersprofile', 'error'));
71
72
}

73
$viewid = $view->get('id');
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
121
122
    // Get all groups that the logged in user is an admin of, or where the logged in user
    // has a role in the list of roles who are allowed to assess submitted views for the
    // group's grouptype
Nigel McNie's avatar
Nigel McNie committed
123
124
125
    if ($groups = get_records_sql_array("SELECT g.*
        FROM {group} g
        JOIN {group_member} gm ON (gm.group = g.id)
126
        JOIN {grouptype_roles} gtr ON gtr.grouptype = g.grouptype AND gtr.role = gm.role
Nigel McNie's avatar
Nigel McNie committed
127
        WHERE gm.member = ?
128
        AND (gm.role = 'admin' OR gtr.see_submitted_views = 1)
Nigel McNie's avatar
Nigel McNie committed
129
        AND g.deleted = 0", array($loggedinid))) {
130
131
132
133

        $invitelist     = array(); // List of groups the displayed user can be invited to join
        $controlledlist = array(); // List of groups the displayed user can be directly added to

134
        foreach ($groups as $group) {
135
            if (array_key_exists($group->id, $allusergroups)) {
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                if ($allusergroups[$group->id]->type == 'invite') {
                    $invitedlist[$group->id] = $group->name;
                }
                else if ($allusergroups[$group->id]->type == 'request') {
                    $requestedlist[$group->id] = $group->name;
                    $controlledlist[$group->id] = $group->name;
                    continue;
                }
                else {
                    continue; // Already a member
                }
            }
            if ($group->jointype == 'controlled') {
                $controlledlist[$group->id] = $group->name;
            }
            if (!isset($invitedlist[$group->id])) {
                $invitelist[$group->id] = $group->name;
153
154
            }
        }
155
        $smarty->assign('invitedlist', join(', ', $invitedlist));
156
157
158
159
160
        if (count($invitelist) > 0) {
            $default = array_keys($invitelist);
            $default = $default[0];
            $inviteform = pieform(array(
                'name'              => 'invite',
Clare Lenihan's avatar
Clare Lenihan committed
161
162
                'successcallback'   => 'invite_submit',
                'renderer'          => 'div',
163
                'elements'          => array(
164
                    'group' => array(
165
                        'type'                => 'select',
Clare Lenihan's avatar
Clare Lenihan committed
166
                        'title'               => get_string('inviteusertojoingroup', 'group'),
167
168
169
170
171
172
173
174
175
176
                        'collapseifoneoption' => false,
                        'options'             => $invitelist,
                        'defaultvalue'        => $default,
                    ),
                    'id' => array(
                        'type'  => 'hidden',
                        'value' => $userid,
                    ),
                    'submit' => array(
                        'type'  => 'submit',
Clare Lenihan's avatar
Clare Lenihan committed
177
                        'value' => get_string('sendinvitation', 'group'),
178
179
180
                    ),
                ),
            ));
Clare Lenihan's avatar
Clare Lenihan committed
181
            $smarty->assign('inviteform',$inviteform);
182
        }
183

184
        $smarty->assign('requestedlist', join(', ', $requestedlist));
185
186
187
188
189
        if (count($controlledlist) > 0) {
            $default = array_keys($controlledlist);
            $default = $default[0];
            $addform = pieform(array(
                'name'                => 'addmember',
Clare Lenihan's avatar
Clare Lenihan committed
190
191
                'successcallback'     => 'addmember_submit',
                'renderer'            => 'div',
192
                'autofocus'           => false,
193
                'elements'            => array(
194
                    'group' => array(
195
                        'type'    => 'select',
Clare Lenihan's avatar
Clare Lenihan committed
196
                        'title'   => get_string('addusertogroup', 'group'),
197
198
199
200
                        'collapseifoneoption' => false,
                        'options' => $controlledlist,
                        'defaultvalue' => $default,
                    ),
201
202
203
204
                    'member' => array(
                        'type'  => 'hidden',
                        'value' => $userid, 
                    ),
205
206
207
208
209
                    'submit' => array(
                        'type'  => 'submit',
                        'value' => get_string('add'),
                    ),
                ),
210
            ));
Clare Lenihan's avatar
Clare Lenihan committed
211
            $smarty->assign('addform',$addform);
212
        } 
213
    }
214

Penny Leach's avatar
Penny Leach committed
215
    if ($is_friend) {
Clare Lenihan's avatar
Clare Lenihan committed
216
        $relationship = 'existingfriend';
217
    }
Clare Lenihan's avatar
Clare Lenihan committed
218
219
    else if (record_exists('usr_friend_request', 'requester', $loggedinid, 'owner', $userid)) {
        $relationship = 'requestedfriendship';
220
    }
Clare Lenihan's avatar
Clare Lenihan committed
221
222
223
224
225
226
    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
227
            'elements' => array(
Clare Lenihan's avatar
Clare Lenihan committed
228
229
230
                'approve' => array(
                    'type' => 'submit',
                    'value' => get_string('approverequest', 'group'),
Penny Leach's avatar
Penny Leach committed
231
                ),
Clare Lenihan's avatar
Clare Lenihan committed
232
                'deny' => array(
Penny Leach's avatar
Penny Leach committed
233
                    'type' => 'submit',
Clare Lenihan's avatar
Clare Lenihan committed
234
                    'value' => get_string('denyrequest', 'group')
Clare Lenihan's avatar
Clare Lenihan committed
235
236
237
238
                ),
                'id' => array(
                    'type' => 'hidden',
                    'value' => $userid
Clare Lenihan's avatar
Clare Lenihan committed
239
240
241
                )
            )
        ));
242
        $smarty->assign('message', $record->message);
Clare Lenihan's avatar
Clare Lenihan committed
243
        $smarty->assign('requestform', $requestform);
244
    }
Clare Lenihan's avatar
Clare Lenihan 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
    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);
269

Penny Leach's avatar
Penny Leach committed
270
271
}

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

279
$smarty->assign('institutions', get_institution_string_for_user($userid));
280
$smarty->assign('canmessage', $loggedinid != $userid && can_send_message($loggedinid, $userid));
281
$smarty->assign('USERID', $userid);
282
$smarty->assign('viewtitle', get_string('usersprofile', 'mahara', display_name($user, null, true)));
283
$smarty->assign('viewtype', 'profile');
284

285
$smarty->assign('user', $user);
286
287
288
289
290
291
if (get_config('viewmicroheaders')) {
    $smarty->assign('microheaders', true);
    $smarty->assign('microheadertitle', $view->display_title(true, false));
    if ($loggedinid && $loggedinid == $userid) {
        $microheaderlinks = array(
            array(
292
                'name' => get_string('editthisview', 'view'),
293
294
295
296
297
                'url' => get_config('wwwroot') . 'view/blocks.php?profile=1',
            ),
        );
        $smarty->assign('microheaderlinks', $microheaderlinks);
    }
298
}
299
else {
300
301
302
    if ($loggedinid && $loggedinid == $userid) {
        $smarty->assign('ownprofile', true);
    }
303
    $smarty->assign('pageheadinghtml', $view->display_title(false));
304
}
305

306
307
308
309
if (!$restrictedview) {
    $smarty->assign('viewcontent', $view->build_columns());
}

310
311
$smarty->display('user/view.tpl');

312
mahara_log('views', "$viewid"); // Log view visits
313

314
// Send an invitation to the user to join a group
315
function invite_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
316
317
    global $userid;
    redirect('/group/invite.php?id=' . $values['group'] . '&user=' . $userid);
318
319
}

320
// Add the user as a member of a group
321
function addmember_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
322
    global $USER, $SESSION, $userid;
323

324
    $data = new StdClass;
325
    $data->group  = $values['group'];
Clare Lenihan's avatar
Clare Lenihan committed
326
    $data->member = $userid;
327
    $data->ctime  = db_format_timestamp(time());
Nigel McNie's avatar
Nigel McNie committed
328
    $data->role  = 'member'; // TODO: modify the dropdown to allow the role to be chosen
329
    $ctitle = get_field('group', 'name', 'id', $data->group);
330
331
    $adduser = get_record('usr', 'id', $data->member);

332
    try {
333
        insert_record('group_member', $data);
334
        delete_records('group_member_request', 'member', $userid, 'group', $data->group);
Clare Lenihan's avatar
Clare Lenihan committed
335
        $lang = get_user_language($userid);
336
        require_once(get_config('libroot') . 'activity.php');
337
338
339
340
341
342
343
        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
344
        $SESSION->add_ok_msg(get_string('useradded', 'group'));
345
346
    }
    catch (SQLException $e) {
347
        $SESSION->add_error_msg(get_string('adduserfailed', 'group'));
348
    }
Clare Lenihan's avatar
Clare Lenihan committed
349
    redirect('/user/view.php?id=' . $userid);
350
351
}

Clare Lenihan's avatar
Clare Lenihan committed
352
353
function approve_deny_friendrequest_submit(Pieform $form, $values) {
    if (isset($values['deny'])) {
354
        redirect('/user/denyrequest.php?id=' . $values['id'] . '&returnto=view');
Clare Lenihan's avatar
Clare Lenihan committed
355
356
357
358
359
    }
    else {
        acceptfriend_submit($form, $values);
    }
}