lib.php 6.35 KB
Newer Older
1
2
3
<?php
/**
 * 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
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage blocktype-myfriends
 * @author     Catalyst IT Ltd
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
 *
 */

defined('INTERNAL') || die();

30
31
define('MAXFRIENDDISPLAY', 16);

32
33
34
35
36
37
38
39
40
41
class PluginBlocktypeMyfriends extends SystemBlocktype {

    public static function get_title() {
        return get_string('title', 'blocktype.myfriends');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.myfriends');
    }

42
43
44
45
    public static function single_only() {
        return true;
    }

46
47
48
49
50
    public static function get_categories() {
        return array('internal');
    }

    public static function get_viewtypes() {
51
        return array('profile', 'dashboard');
52
53
    }

54
    public static function build_myfriends_html(&$friends, $userid, BlockInstance $instance) {
55
56
57
58
        $friendarray = array_chunk($friends['data'], 4); // get the friends into a 4x4 array
        $smarty = smarty_core();
        $smarty->assign_by_ref('friends', $friendarray);
        $friends['tablerows'] = $smarty->fetch('blocktype:myfriends:myfriendrows.tpl');
59
60
61
62
63

        if ($friends['limit'] === false) {
            return;
        }

64
65
        $baseurl = $instance->get_view()->get_url() . '&block=' . $instance->get('id');
        $baseurl .= '&user=' . (int) $userid;
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        $pagination = build_pagination(array(
            'id' => 'userfriendstable_pagination',
            'class' => 'center nojs-hidden-block',
            'datatable' => 'userfriendstable',
            'url' => $baseurl,
            'jsonscript' => 'blocktype/myfriends/myfriends.json.php',
            'count' => $friends['count'],
            'limit' => $friends['limit'],
            'offset' => $friends['offset'],
            'numbersincludefirstlast' => false,
            'resultcounttextsingular' => get_string('friend', 'group'),
            'resultcounttextplural' => get_string('friends', 'group'),
        ));
        $friends['pagination'] = $pagination['html'];
        $friends['pagination_js'] = $pagination['javascript'];
    }

83
    public static function render_instance(BlockInstance $instance, $editing=false) {
84
85
        global $USER, $exporter;

86
        $userid = $instance->get_view()->get('owner');
87
88
89
90
91
        if (!$userid) {
            // 'My Friends' doesn't make sense for group/site views
            return '';
        }

92
93
94
        $limit = isset($exporter) ? false : MAXFRIENDDISPLAY;

        $friends = get_friends($userid, $limit, 0);
95
        if ($friends['count']) {
96
            self::build_myfriends_html($friends, $userid, $instance);
97
98
99
100
        }
        else {
            $friends = false;
        }
101
102

        $smarty = smarty_core();
103
        $smarty->assign('friends', $friends);
104
        $smarty->assign('searchingforfriends', array('<a href="' . get_config('wwwroot') . 'user/find.php">', '</a>'));
105
106
107

        // If the user has no friends, try and display something useful, such 
        // as a 'request friendship' button
108
109
110
        if (!$friends) {
            $loggedinid = $USER->get('id');
            $is_friend = is_friend($userid, $loggedinid);
111

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            if ($is_friend) {
                $relationship = 'existingfriend';
            }
            else if (record_exists('usr_friend_request', 'requester', $loggedinid, 'owner', $userid)) {
                $relationship = 'requestedfriendship';
            }
            else {
                $relationship = 'none';
                $friendscontrol = get_account_preference($userid, 'friendscontrol');
                if ($friendscontrol == 'auto') {
                    require_once('pieforms/pieform.php');
                    $newfriendform = pieform(array(
                        'name' => 'myfriends_addfriend',
                        'successcallback' => 'addfriend_submit',
                        'autofocus' => false,
                        'renderer' => 'div',
                        'elements' => array(
                            'add' => array(
                                'type' => 'submit',
                                'value' => get_string('addtomyfriends', 'group')
                            ),
                            'id' => array(
                                'type' => 'hidden',
                                'value' => $userid
                            )
137
                        )
138
139
140
141
                    ));
                    $smarty->assign('newfriendform', $newfriendform);
                }
                $smarty->assign('friendscontrol', $friendscontrol);
142
            }
143
            $smarty->assign('relationship', $relationship);
144
        }
145
146

        $smarty->assign('lookingatownpage', $USER->get('id') == $userid);
147
148
        $smarty->assign('USERID', $userid);

149
150
151
152
153
154
        return $smarty->fetch('blocktype:myfriends:myfriends.tpl');
    }

    public static function has_instance_config() {
        return false;
    }
155
156
157
158
159

    public static function default_copy_type() {
        return 'shallow';
    }

160
161
162
163
164
165
166
    /**
     * Myfriends only makes sense for personal views
     */
    public static function allowed_in_view(View $view) {
        return $view->get('owner') != null;
    }

167
168
169
    public static function override_instance_title(BlockInstance $instance) {
        global $USER;
        $ownerid = $instance->get_view()->get('owner');
170

171
        if ($ownerid === null || $ownerid == $USER->get('id')) {
172
            return get_string('title', 'blocktype.myfriends');
173
174
        }

175
        return get_string('otherusertitle', 'blocktype.myfriends', display_name($ownerid, null, true));
176
177
    }

178
179
180
}

?>