view.php 13.5 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
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
require(dirname(dirname(__FILE__)).'/init.php');
35
require_once('group.php');
36
require_once('pieforms/pieform.php');
37
require_once(get_config('libroot') . 'view.php');
Richard Mansfield's avatar
Richard Mansfield committed
38

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

53
54
// Get the user's details

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

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

68
$name = display_name($user);
Martyn Smith's avatar
Martyn Smith committed
69
define('TITLE', $name);
70
71
72
73
74
75
76
$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'),
    )
77
);
78
79
80
81
82
83
84
85
86
87
88
89
90
91

$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
92
93
94
if (!$allusergroups = get_records_sql_assoc($sql, array($userid, $userid, $userid))) {
    $allusergroups = array();
}
95
if (!empty($loggedinid) && $loggedinid != $userid) {
96
97
98
99

    $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

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

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

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

Penny Leach's avatar
Penny Leach committed
258
}
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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',
278
                    'value' => ($public) ? get_string('loggedinusersonly') : get_string('allowpublicaccess'),
279
280
281
282
283
284
                ),
            ),
        ));
        $smarty->assign('togglepublic', $togglepublic);
    }
}
Penny Leach's avatar
Penny Leach committed
285

286
if ($userid != $USER->get('id') && $USER->is_admin_for_user($user) && is_null($USER->get('parentuser'))) {
287
    $loginas = get_string('loginasuser', 'admin', hsc($user->username));
288
289
290
291
292
} else {
    $loginas = null;
}
$smarty->assign('loginas', $loginas);

Clare Lenihan's avatar
Clare Lenihan committed
293
294
295
if (isset($introduction)) {
    $smarty->assign('introduction', $introduction);
}
296
$smarty->assign('institutions', get_institution_string_for_user($userid));
297
$smarty->assign('canmessage', can_send_message($loggedinid, $userid));
298
$smarty->assign('NAME',$name);
299
$smarty->assign('USERID', $userid);
300
$smarty->assign('viewcontent', $view->build_columns());
301
$smarty->assign('PAGEHEADING', hsc(TITLE));
302
303
$smarty->display('user/view.tpl');

304
// Send an invitation to the user to join a group
305
function invite_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
306
307
    global $userid;
    redirect('/group/invite.php?id=' . $values['group'] . '&user=' . $userid);
308
309
}

310
// Add the user as a member of a group
311
function addmember_submit(Pieform $form, $values) {
Clare Lenihan's avatar
Clare Lenihan committed
312
    global $USER, $SESSION, $userid;
313

314
    $data = new StdClass;
315
    $data->group  = $values['group'];
Clare Lenihan's avatar
Clare Lenihan committed
316
    $data->member = $userid;
317
    $data->ctime  = db_format_timestamp(time());
Nigel McNie's avatar
Nigel McNie committed
318
    $data->role  = 'member'; // TODO: modify the dropdown to allow the role to be chosen
319
    $ctitle = get_field('group', 'name', 'id', $data->group);
320
321
    $adduser = get_record('usr', 'id', $data->member);

322
    try {
323
        insert_record('group_member', $data);
324
        delete_records('group_member_request', 'member', $userid, 'group', $data->group);
Clare Lenihan's avatar
Clare Lenihan committed
325
        $lang = get_user_language($userid);
326
        require_once(get_config('libroot') . 'activity.php');
327
        activity_occurred('maharamessage', 
Clare Lenihan's avatar
Clare Lenihan committed
328
329
330
331
332
            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'));
333
334
    }
    catch (SQLException $e) {
335
        $SESSION->add_error_msg(get_string('adduserfailed', 'group'));
336
    }
Clare Lenihan's avatar
Clare Lenihan committed
337
    redirect('/user/view.php?id=' . $userid);
338
339
}

Clare Lenihan's avatar
Clare Lenihan committed
340
341
function approve_deny_friendrequest_submit(Pieform $form, $values) {
    if (isset($values['deny'])) {
342
        redirect('/user/denyrequest.php?id=' . $values['id'] . '&returnto=view');
Clare Lenihan's avatar
Clare Lenihan committed
343
344
345
346
347
348
    }
    else {
        acceptfriend_submit($form, $values);
    }
}

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
372
?>