lib.php 6.14 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-myfriends
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
 *
 */

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

14
15
define('MAXFRIENDDISPLAY', 16);

16
class PluginBlocktypeMyfriends extends MaharaCoreBlocktype {
17
18
19
20
21
22
23
24
25

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

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

26
27
28
29
    public static function single_only() {
        return true;
    }

30
    public static function get_categories() {
31
        return array('internal' => 31000);
32
33
34
    }

    public static function get_viewtypes() {
35
        return array('profile', 'dashboard');
36
37
    }

38
    public static function build_myfriends_html(&$friends, $userid, BlockInstance $instance) {
39
40
        $friendarray = array_chunk($friends['data'], 4); // get the friends into a 4x4 array
        $smarty = smarty_core();
41
        $smarty->assign('friends', $friendarray);
42
        $friends['tablerows'] = $smarty->fetch('blocktype:myfriends:myfriendrows.tpl');
43
44
45
46
47

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

48
49
        $baseurl = $instance->get_view()->get_url();
        $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'block=' . $instance->get('id');
50
        $baseurl .= '&user=' . (int) $userid;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        $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'];
    }

68
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
69
70
        global $USER, $exporter;

71
        $userid = $instance->get_view()->get('owner');
72
73
74
75
76
        if (!$userid) {
            // 'My Friends' doesn't make sense for group/site views
            return '';
        }

77
78
79
        $limit = isset($exporter) ? false : MAXFRIENDDISPLAY;

        $friends = get_friends($userid, $limit, 0);
80
        if ($friends['count']) {
81
            self::build_myfriends_html($friends, $userid, $instance);
82
83
84
85
        }
        else {
            $friends = false;
        }
86
87

        $smarty = smarty_core();
88
        $smarty->assign('friends', $friends);
89
        $smarty->assign('searchingforfriends', array('<a href="' . get_config('wwwroot') . 'user/index.php">', '</a>'));
90

Aaron Wells's avatar
Aaron Wells committed
91
        // If the user has no friends, try and display something useful, such
92
        // as a 'request friendship' button
93
94
95
        if (!$friends) {
            $loggedinid = $USER->get('id');
            $is_friend = is_friend($userid, $loggedinid);
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
            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') {
                    $newfriendform = pieform(array(
                        'name' => 'myfriends_addfriend',
                        'successcallback' => 'addfriend_submit',
                        'autofocus' => false,
                        'renderer' => 'div',
                        'elements' => array(
                            'add' => array(
114
115
                                'type' => 'button',
                                'usebuttontag' => true,
116
                                'class' => 'btn-secondary',
117
                                'value' => '<span class="icon icon-user-plus icon-lg left" role="presentation" aria-hidden="true"></span>' .get_string('addtomyfriends', 'group')
118
119
120
121
122
                            ),
                            'id' => array(
                                'type' => 'hidden',
                                'value' => $userid
                            )
123
                        )
124
125
126
127
                    ));
                    $smarty->assign('newfriendform', $newfriendform);
                }
                $smarty->assign('friendscontrol', $friendscontrol);
128
            }
129
            $smarty->assign('relationship', $relationship);
130
        }
131

132
        $smarty->assign('loggedin', is_logged_in());
133
        $smarty->assign('lookingatownpage', $USER->get('id') == $userid);
134
135
        $smarty->assign('USERID', $userid);

136
137
138
139
140
141
        return $smarty->fetch('blocktype:myfriends:myfriends.tpl');
    }

    public static function has_instance_config() {
        return false;
    }
142
143
144
145
146

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

147
148
149
150
    /**
     * Myfriends only makes sense for personal views
     */
    public static function allowed_in_view(View $view) {
151
        return in_array($view->get('type'), self::get_viewtypes());
152
153
    }

154
155
156
    public static function override_instance_title(BlockInstance $instance) {
        global $USER;
        $ownerid = $instance->get_view()->get('owner');
157

158
        if ($ownerid === null || $ownerid == $USER->get('id')) {
159
            return get_string('title', 'blocktype.myfriends');
160
161
        }

162
        return get_string('otherusertitle', 'blocktype.myfriends', display_name($ownerid, null, true));
163
164
    }

165
166
167
168
169
170
171
172
173
    /**
     * Shouldn't be linked to any artefacts via the view_artefacts table.
     *
     * @param BlockInstance $instance
     * @return multitype:
     */
    public static function get_artefacts(BlockInstance $instance) {
        return array();
    }
174
}