lib.php 6.56 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-mygroups
 * @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
class PluginBlocktypeMyGroups extends MaharaCoreBlocktype {
15
16
17
18
19
20
21
22
23

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

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

24
25
26
27
    public static function single_only() {
        return true;
    }

28
    public static function get_categories() {
29
        return array('internal' => 32000);
30
31
32
    }

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

36
37
38
39
40
41
42
43
    /**
     * This function renders a list of items as html
     *
     * @param array items
     * @param string template
     * @param array options
     * @param array pagination
     */
44
    public static function render_items(&$items, $template, $options, $pagination) {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        $smarty = smarty_core();
        $smarty->assign('options', $options);
        $smarty->assign('items', $items['data']);
        $items['tablerows'] = $smarty->fetch($template);
        if ($items['limit'] && $pagination) {
            $pagination = build_pagination(array(
                'id' => $pagination['id'],
                'class' => 'center',
                'datatable' => $pagination['datatable'],
                'url' => $pagination['baseurl'],
                'jsonscript' => $pagination['jsonscript'],
                'count' => $items['count'],
                'limit' => $items['limit'],
                'offset' => $items['offset'],
                'numbersincludefirstlast' => false,
                'resultcounttextsingular' => $pagination['resultcounttextsingular'] ? $pagination['resultcounttextsingular'] : get_string('result'),
                'resultcounttextplural' => $pagination['resultcounttextplural'] ? $pagination['resultcounttextplural'] :get_string('results'),
            ));
            $items['pagination'] = $pagination['html'];
            $items['pagination_js'] = $pagination['javascript'];
        }
    }

68
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
69
70
71
72
73
74
75
76
        $configdata = $instance->get('configdata');
        $sort = !empty($configdata['sort']) ? $configdata['sort'] : null;
        $limit = !empty($configdata['limitto']) ? $configdata['limitto'] : null;
        $view = $instance->get_view();
        $baseurl = ($view->get('type') == 'dashboard') ? $view->get_url() . '?id=' . $view->get('id') : $view->get_url();
        $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'block=' . $instance->get('id');

        $userid = $view->get('owner');
77
78
79
80
        if (!$userid) {
            return '';
        }

81
82
83
        $smarty = smarty_core();
        require_once('group.php');
        // Group stuff
84
85
86
87
88
89
90
        if (!empty($limit)) {
            list($usergroups, $count) = group_get_user_groups($userid, null, $sort, $limit, 0);
        }
        else {
            $usergroups = group_get_user_groups($userid, null, $sort);
            $count = count($usergroups);
        }
91
        foreach ($usergroups as $group) {
92
            $group->roledisplay = get_string($group->role, 'grouptype.'.$group->grouptype);
93
        }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
        $groups = array('data' => $usergroups,
                        'count' => $count,
                        'limit' => $limit,
                        'offset' => 0,
                        );
        $pagination = array(
            'baseurl' => $baseurl,
            'id' => 'mygroups_pagination',
            'datatable' => 'usergroupstable',
            'jsonscript' => 'blocktype/mygroups/mygroups.json.php',
            'resultcounttextsingular' => get_string('group', 'group'),
            'resultcounttextplural' => get_string('groups', 'group'),
        );
        self::render_items($groups, 'blocktype:mygroups:mygroupslist.tpl', $configdata, $pagination);
108

109
        $smarty->assign('USERGROUPS', $groups);
110
        $smarty->assign('userid', $userid);
111
112
113
114
        return $smarty->fetch('blocktype:mygroups:mygroups.tpl');
    }

    public static function has_instance_config() {
115
116
117
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
118
    public static function instance_config_form(BlockInstance $instance) {
119
120
121
122
123
        $configdata = $instance->get('configdata');

        return array(
            'sort' => array(
                'type'  => 'select',
124
                'title' => get_string('sortgroups', 'blocktype.mygroups'),
125
126
127
128
129
130
131
132
133
                'options' => array(
                    'latest' => get_string('latest', 'blocktype.mygroups'),
                    'earliest' => get_string('earliest', 'blocktype.mygroups'),
                    'alphabetical'  => get_string('alphabetical', 'blocktype.mygroups'),
                ),
                'defaultvalue' => isset($configdata['sort']) ? $configdata['sort'] : 'alphabetical',
            ),
            'limitto' => array(
                'type'  => 'text',
134
                'title' => get_string('limitto1', 'blocktype.mygroups'),
135
                'description' => get_string('limittodesc', 'blocktype.mygroups'),
136
                'size' => 3,
137
138
139
140
141
142
143
144
145
146
147
                'defaultvalue' => isset($configdata['limitto']) ? $configdata['limitto'] : 20,
                'rules' => array(
                    'maxlength' => 4,
                ),
            ),
        );
    }

    public static function instance_config_save($values) {
        $values['limitto'] = !empty($values['limitto']) ? (int)$values['limitto'] : '';
        return $values;
148
    }
149
150
151
152
153

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

154
155
156
157
    /**
     * Mygroups only makes sense for personal views
     */
    public static function allowed_in_view(View $view) {
158
        return in_array($view->get('type'), self::get_viewtypes());
159
160
    }

161
162
163
    public static function override_instance_title(BlockInstance $instance) {
        global $USER;
        $ownerid = $instance->get_view()->get('owner');
164
        if ($ownerid === null || $ownerid == $USER->get('id')) {
165
166
167
168
169
            return get_string('title', 'blocktype.mygroups');
        }
        return get_string('otherusertitle', 'blocktype.mygroups', display_name($ownerid, null, true));
    }

170
171
172
173
174
175
176
177
178
    /**
     * 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();
    }
179
}