lib.php 13.1 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-groupviews
 * @author     Liip AG
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
14
15
16
17
18
19
20
21
22
23
24
25
26
 * @copyright  (C) 2010 Liip AG, http://www.liip.ch
 *
 */

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

require_once('group.php');
class PluginBlocktypeGroupViews extends SystemBlocktype {

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

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

    public static function single_only() {
27
        return true;
28
29
30
31
32
33
34
35
36
37
    }

    public static function get_categories() {
        return array('general');
    }

    public static function get_viewtypes() {
        return array('grouphomepage');
    }

38
39
40
41
    public static function hide_title_on_empty_content() {
        return true;
    }

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    /**
     * This function renders a list of items (views/collections) as html
     *
     * @param array items
     * @param string template
     * @param array options
     * @param array pagination
     */
    public function render_items(&$items, $template, $options, $pagination) {
        $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'];
        }
    }

76
    public static function render_instance(BlockInstance $instance, $editing=false) {
77
        $configdata = $instance->get('configdata');
78
        if (!isset($configdata['showgroupviews'])) {
79
80
81
            // If not set, use default
            $configdata['showgroupviews'] = 1;
        }
82
83
84
        if (!isset($configdata['showsharedviews'])) {
            $configdata['showsharedviews'] = 1;
        }
85
86
87
        if (!isset($configdata['showsharedcollections'])) {
            $configdata['showsharedcollections'] = 1;
        }
88
89
90
        if (!isset($configdata['showsubmitted'])) {
            $configdata['showsubmitted'] = 1;
        }
91
92
93
94
95
        $groupid = $instance->get_view()->get('group');
        if (!$groupid) {
            return '';
        }

96
        $data = self::get_data($groupid, $editing);
97
98
99
100

        $dwoo = smarty_core();
        $dwoo->assign('group', $data['group']);
        $dwoo->assign('groupid', $data['group']->id);
101
102
        $baseurl = $instance->get_view()->get_url();
        $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'group=' . $groupid;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

        if (!empty($configdata['showgroupviews']) && isset($data['groupviews'])) {
            $groupviews = (array)$data['groupviews'];
            $pagination = array(
                'baseurl'    => $baseurl,
                'id'         => 'groupviews_pagination',
                'datatable'  => 'groupviewlist',
                'jsonscript' => 'blocktype/groupviews/groupviews.json.php',
                'resultcounttextsingular' => get_string('view', 'view'),
                'resultcounttextplural'   => get_string('views', 'view'),
            );
            self::render_items($groupviews, 'blocktype:groupviews:groupviewssection.tpl', $configdata, $pagination);
            $dwoo->assign('groupviews', $groupviews);
        }

118
        if (!empty($configdata['showsharedviews']) && isset($data['sharedviews'])) {
119
120
121
122
123
124
125
126
127
128
129
            $sharedviews = (array)$data['sharedviews'];
            $pagination = array(
                'baseurl'    => $baseurl,
                'id'         => 'sharedviews_pagination',
                'datatable'  => 'sharedviewlist',
                'jsonscript' => 'blocktype/groupviews/sharedviews.json.php',
                'resultcounttextsingular' => get_string('view', 'view'),
                'resultcounttextplural'   => get_string('views', 'view'),
            );
            self::render_items($sharedviews, 'blocktype:groupviews:sharedviews.tpl', $configdata, $pagination);
            $dwoo->assign('sharedviews', $sharedviews);
130
        }
131
132
133
134
135
136
137
138
139
140
141
142
143
        if (!empty($configdata['showsharedcollections']) && isset($data['sharedcollections'])) {
            $sharedcollections = (array)$data['sharedcollections'];
            $pagination = array(
                'baseurl'    => $baseurl,
                'id'         => 'sharedcollections_pagination',
                'datatable'  => 'sharedcollectionlist',
                'jsonscript' => 'blocktype/groupviews/sharedcollections.json.php',
                'resultcounttextsingular' => get_string('collection', 'collection'),
                'resultcounttextplural'   => get_string('collections', 'collection'),
            );
            self::render_items($sharedcollections, 'blocktype:groupviews:sharedcollections.tpl', $configdata, $pagination);
            $dwoo->assign('sharedcollections', $sharedcollections);
        }
144
145
146
147
148
149
150
151
152
153
154
155
        if (!empty($configdata['showsubmitted']) && isset($data['allsubmitted'])) {
            $allsubmitted = $data['allsubmitted'];
            $pagination = array(
                'baseurl'    => $baseurl,
                'id'         => 'allsubmitted_pagination',
                'datatable'  => 'allsubmissionlist',
                'jsonscript' => 'blocktype/groupviews/allsubmissions.json.php',
                'resultcounttextsingular' => get_string('vieworcollection', 'view'),
                'resultcounttextplural'   => get_string('viewsandcollections', 'view'),
            );
            self::render_items($allsubmitted, 'blocktype:groupviews:allsubmissions.tpl', $configdata, $pagination);
            $dwoo->assign('allsubmitted', $allsubmitted);
156
        }
157
158
        if (isset($data['mysubmitted'])) {
            $dwoo->assign('mysubmitted', $data['mysubmitted']);
159
        }
160
        if (!$editing && isset($data['group_view_submission_form'])) {
161
162
163
164
165
166
167
            $dwoo->assign('group_view_submission_form', $data['group_view_submission_form']);
        }

        return $dwoo->fetch('blocktype:groupviews:groupviews.tpl');
    }

    public static function has_instance_config() {
168
169
170
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
171
    public static function instance_config_form(BlockInstance $instance) {
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
        $configdata = $instance->get('configdata');
        return array(
            'showgroupviews' => array(
                'type' => 'radio',
                'description' => get_string('displaygroupviewsdesc', 'blocktype.groupviews'),
                'title' => get_string('displaygroupviews', 'blocktype.groupviews'),
                'options' => array(
                    1 => get_string('yes'),
                    0 => get_string('no'),
                ),
                'separator' => '<br>',
                'defaultvalue' => isset($configdata['showgroupviews']) ? $configdata['showgroupviews'] : 1,
            ),
            'showsharedviews' => array(
                'type' => 'radio',
                'title' => get_string('displaysharedviews', 'blocktype.groupviews'),
188
                'description' => get_string('displaysharedviewsdesc1', 'blocktype.groupviews'),
189
190
191
192
193
                'options' => array(
                    1 => get_string('yes'),
                    0 => get_string('no'),
                ),
                'separator' => '<br>',
194
                'defaultvalue' => isset($configdata['showsharedviews']) ? $configdata['showsharedviews'] : 1,
195
            ),
196
197
198
199
200
201
202
203
204
205
206
            'showsharedcollections' => array(
                'type' => 'radio',
                'title' => get_string('displaysharedcollections', 'blocktype.groupviews'),
                'description' => get_string('displaysharedcollectionsdesc', 'blocktype.groupviews'),
                'options' => array(
                    1 => get_string('yes'),
                    0 => get_string('no'),
                ),
                'separator' => '<br>',
                'defaultvalue' => isset($configdata['showsharedcollections']) ? $configdata['showsharedcollections'] : 1,
            ),
207
208
209
210
211
212
213
214
215
216
217
            'showsubmitted' => array(
                'type' => 'radio',
                'title' => get_string('displaysubmissions', 'blocktype.groupviews'),
                'description' => get_string('displaysubmissionsdesc', 'blocktype.groupviews'),
                'options' => array(
                    1 => get_string('yes'),
                    0 => get_string('no'),
                ),
                'separator' => '<br>',
                'defaultvalue' => isset($configdata['showsubmitted']) ? $configdata['showsubmitted'] : 1,
            ),
218
219
220
221
222
223
224
225
            'count' => array(
                'type' => 'text',
                'title' => get_string('itemstoshow', 'blocktype.groupviews'),
                'description'  => get_string('itemstoshowdesc', 'blocktype.groupviews'),
                'defaultvalue' => isset($configdata['count']) ? $configdata['count'] : 5,
                'size'         => 3,
                'rules'        => array('integer' => true, 'minvalue' => 1, 'maxvalue' => 100),
            ),
226
        );
227
228
229
230
231
232
    }

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

233
    protected static function get_data($groupid, $editing=false) {
234
235
236
237
238
239
240
241
242
        global $USER;

        if(!defined('GROUP')) {
            define('GROUP', $groupid);
        }
        // get the currently requested group
        $group = group_current_group();
        $role = group_user_access($group->id);
        if ($role) {
243
244
245
246
247
            $bi = group_get_homepage_view_groupview_block($group->id);
            $configdata = $bi->get('configdata');
            $limit = isset($configdata['count']) ? intval($configdata['count']) : 5;
            $limit = ($limit > 0) ? $limit : 5;

248
            // Get all views created in the group
249
            $sort = array(array('column' => 'type=\'grouphomepage\'', 'desc' => true));
250
            $data['groupviews'] = View::view_search(null, null, (object) array('group' => $group->id), null, $limit, 0, true, $sort);
251
            foreach ($data['groupviews']->data as &$view) {
252
                if (!$editing && isset($view['template']) && $view['template']) {
253
                    $view['form'] = pieform(create_view_form(null, null, $view['id']));
254
255
                }
            }
256

257
258
            // For group members, display a list of views that others have
            // shared to the group
259
            $data['sharedviews'] = View::get_sharedviews_data($limit, 0, $group->id);
260
            foreach ($data['sharedviews']->data as &$view) {
261
                if (!$editing && isset($view['template']) && $view['template']) {
262
                    $view['form'] = pieform(create_view_form($group, null, $view->id));
263
264
                }
            }
265

266
267
            $data['sharedcollections'] = View::get_sharedcollections_data($limit, 0, $group->id);

268
269
            if (group_user_can_assess_submitted_views($group->id, $USER->get('id'))) {
                // Display a list of views submitted to the group
270
                list($collections, $views) = View::get_views_and_collections(null, null, null, null, false, $group->id);
271
272
273
274
275
276
277
                $allsubmitted = array_merge(array_values($collections), array_values($views));
                $data['allsubmitted'] = array(
                    'data'   => array_slice($allsubmitted, 0, $limit),
                    'count'  => count($allsubmitted),
                    'limit'  => $limit,
                    'offset' => 0,
                );
278
            }
279

280
281
        }

282
        if ($group->submittableto) {
283
            require_once('pieforms/pieform.php');
284
285
286
287
288
289
            // A user can submit more than one view to the same group, but no view can be
            // submitted to more than one group.

            // Display a list of views this user has submitted to this group, and a submission
            // form containing drop-down of their unsubmitted views.

290
291
            list($collections, $views) = View::get_views_and_collections($USER->get('id'), null, null, null, false, $group->id);
            $data['mysubmitted'] = array_merge(array_values($collections), array_values($views));
292

293
294
295
296
            // Only render the submission form in viewing mode
            if (!$editing) {
                $data['group_view_submission_form'] = group_view_submission_form($group->id);
            }
297
298
299
300
301
        }
        $data['group'] = $group;
        return $data;
    }

302
303
304
305
    public static function get_instance_title() {
        return get_string('title', 'blocktype.groupviews');
    }
}