lib.php 21.7 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
 * @copyright  (C) 2010 Liip AG, http://www.liip.ch
 *
 */

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

require_once('group.php');
16
class PluginBlocktypeGroupViews extends MaharaCoreBlocktype {
17

18
19
20
21
    const SORTBY_TITLE = 0;
    const SORTBY_LASTUPDATE = 1;
    const SORTBY_TIMESUBMITTED = 2;

22
    public static function get_title() {
23
        return get_string('title1', 'blocktype.groupviews');
24
25
26
    }

    public static function get_description() {
27
        return get_string('description1', 'blocktype.groupviews');
28
29
30
    }

    public static function single_only() {
31
        return true;
32
33
34
    }

    public static function get_categories() {
35
        return array('general' => 18000);
36
37
38
39
40
41
    }

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

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

46
47
48
49
50
51
52
53
    /**
     * This function renders a list of items (views/collections) as html
     *
     * @param array items
     * @param string template
     * @param array options
     * @param array pagination
     */
54
    public static function render_items(&$items, $template, $options, $pagination) {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        $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'],
71
72
                'jumplinks'  => (!empty($pagination['jumplinks']) ? $pagination['jumplinks'] : 0),
                'numbersincludeprevnext' => (!empty($pagination['numbersincludeprevnext']) ? $pagination['numbersincludeprevnext'] : 1),
73
74
75
76
77
78
79
80
81
                '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'];
        }
    }

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

85
        $configdata = $instance->get('configdata');
86
        if (!isset($configdata['showgroupviews'])) {
87
88
89
            // If not set, use default
            $configdata['showgroupviews'] = 1;
        }
90
91
92
        if (!isset($configdata['showsharedviews'])) {
            $configdata['showsharedviews'] = 1;
        }
93
94
95
        if (!isset($configdata['showsharedcollections'])) {
            $configdata['showsharedcollections'] = 1;
        }
96
97
98
        if (!isset($configdata['showsubmitted'])) {
            $configdata['showsubmitted'] = 1;
        }
99
100
101
102
103
        $groupid = $instance->get_view()->get('group');
        if (!$groupid) {
            return '';
        }

104
        $data = self::get_data($groupid, $editing);
105
106
107
108

        $dwoo = smarty_core();
        $dwoo->assign('group', $data['group']);
        $dwoo->assign('groupid', $data['group']->id);
109
        $baseurl = $instance->get_view()->get_url();
110
        $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'group=' . $groupid . '&editing=' . $editing;
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

        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);
        }

126
        if (!empty($configdata['showsharedviews']) && isset($data['sharedviews'])) {
127
128
129
130
131
132
133
134
135
136
137
            $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);
138
        }
139
140
141
142
143
144
145
146
147
148
        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'),
            );
149
            self::render_items($sharedcollections, 'blocktype:groupviews:sharedviews.tpl', $configdata, $pagination);
150
151
            $dwoo->assign('sharedcollections', $sharedcollections);
        }
152
153
154
155
156
157
158
159
160
161
162
163
        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);
164
        }
165
166
        if (isset($data['mysubmitted'])) {
            $dwoo->assign('mysubmitted', $data['mysubmitted']);
167
        }
168
        if (!$editing && isset($data['group_view_submission_form'])) {
169
170
            $dwoo->assign('group_view_submission_form', $data['group_view_submission_form']);
        }
171
        // Get members who have no submitted work - only show to those allowed to see submitted work
172
        if ($USER->is_logged_in() && !empty($configdata['showsubmitted']) && group_user_can_assess_submitted_views($groupid, $USER->get('id')) && isset($data['nosubmissions'])) {
173
174
175
176
177
178
179
180
181
182
183
184
185
186
            $nosubmissions = $data['nosubmissions'];
            $pagination = array(
                'baseurl'    => $baseurl,
                'id'         => 'nosubmissions_pagination',
                'datatable'  => 'nosubmissionslist',
                'jsonscript' => 'blocktype/groupviews/nosubmissions.json.php',
                'jumplinks'  => 6,
                'numbersincludeprevnext' => 3,
                'resultcounttextsingular' => get_string('member', 'group'),
                'resultcounttextplural'   => get_string('members', 'group'),
            );
            self::render_items($nosubmissions, 'blocktype:groupviews:nosubmissions.tpl', $configdata, $pagination);
            $dwoo->assign('nosubmissions', $nosubmissions);
        }
187
188
189
190
191

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

    public static function has_instance_config() {
192
193
194
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
195
    public static function instance_config_form(BlockInstance $instance) {
196
197
198
        $configdata = $instance->get('configdata');
        return array(
            'showgroupviews' => array(
199
                'type' => 'select',
200
201
                'description' => get_string('displaygroupviewsdesc1', 'blocktype.groupviews'),
                'title' => get_string('displaygroupviews1', 'blocktype.groupviews'),
202
203
204
205
206
207
                'options' => array(
                    1 => get_string('yes'),
                    0 => get_string('no'),
                ),
                'defaultvalue' => isset($configdata['showgroupviews']) ? $configdata['showgroupviews'] : 1,
            ),
208
209
            'sortgroupviewsby' => array(
                'type' => 'select',
210
                'title' => get_string('sortgroupviewstitle1', 'blocktype.groupviews'),
211
212
213
214
215
216
                'options' => array(
                    PluginBlocktypeGroupViews::SORTBY_TITLE => get_string('sortviewsbyalphabetical', 'blocktype.groupviews'),
                    PluginBlocktypeGroupViews::SORTBY_LASTUPDATE => get_string('sortviewsbylastupdate', 'blocktype.groupviews'),
                ),
                'defaultvalue' => isset($configdata['sortgroupviewsby']) ? (int) $configdata['sortgroupviewsby'] : 0
            ),
217
            'showsharedviews' => array(
218
                'type' => 'select',
219
                'title' => get_string('displaysharedviews', 'blocktype.groupviews'),
220
                'description' => get_string('displaysharedviewsdesc1', 'blocktype.groupviews'),
221
                'options' => array(
222
223
224
                    0 => get_string('shownone', 'blocktype.groupviews'),
                    1 => get_string('showbygroupmembers', 'blocktype.groupviews'),
                    2 => get_string('showbyanybody', 'blocktype.groupviews'),
225
                ),
226
                'defaultvalue' => isset($configdata['showsharedviews']) ? $configdata['showsharedviews'] : 1,
227
            ),
228
            'showsharedcollections' => array(
229
                'type' => 'select',
230
231
232
                'title' => get_string('displaysharedcollections', 'blocktype.groupviews'),
                'description' => get_string('displaysharedcollectionsdesc', 'blocktype.groupviews'),
                'options' => array(
233
234
235
                    0 => get_string('shownone', 'blocktype.groupviews'),
                    1 => get_string('showbygroupmembers', 'blocktype.groupviews'),
                    2 => get_string('showbyanybody', 'blocktype.groupviews'),
236
237
238
                ),
                'defaultvalue' => isset($configdata['showsharedcollections']) ? $configdata['showsharedcollections'] : 1,
            ),
239
240
241
242
243
244
245
246
247
            'sortsharedviewsby' => array(
                'type' => 'select',
                'title' => get_string('sortsharedviewstitle', 'blocktype.groupviews'),
                'options' => array(
                    PluginBlocktypeGroupViews::SORTBY_TITLE => get_string('sortviewsbyalphabetical', 'blocktype.groupviews'),
                    PluginBlocktypeGroupViews::SORTBY_LASTUPDATE => get_string('sortviewsbylastupdate', 'blocktype.groupviews'),
                ),
                'defaultvalue' => isset($configdata['sortsharedviewsby']) ? (int) $configdata['sortsharedviewsby'] : 0
            ),
248
            'showsubmitted' => array(
249
                'type' => 'select',
250
251
252
253
254
255
256
257
                'title' => get_string('displaysubmissions', 'blocktype.groupviews'),
                'description' => get_string('displaysubmissionsdesc', 'blocktype.groupviews'),
                'options' => array(
                    1 => get_string('yes'),
                    0 => get_string('no'),
                ),
                'defaultvalue' => isset($configdata['showsubmitted']) ? $configdata['showsubmitted'] : 1,
            ),
258
259
260
261
262
263
264
265
266
            'sortsubmittedby' => array(
                'type' => 'select',
                'title' => get_string('sortsubmittedtitle', 'blocktype.groupviews'),
                'options' => array(
                    PluginBlocktypeGroupViews::SORTBY_TITLE => get_string('sortviewsbyalphabetical', 'blocktype.groupviews'),
                    PluginBlocktypeGroupViews::SORTBY_TIMESUBMITTED => get_string('sortviewsbytimesubmitted', 'blocktype.groupviews'),
                ),
                'defaultvalue' => isset($configdata['sortsubmittedby']) ? (int) $configdata['sortsubmittedby'] : 0
            ),
267
268
269
270
271
272
273
274
            '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),
            ),
275
        );
276
277
278
279
280
281
    }

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

282
    protected static function get_data($groupid, $editing=false) {
283
284
285
286
287
288
289
290
        global $USER;

        if(!defined('GROUP')) {
            define('GROUP', $groupid);
        }
        // get the currently requested group
        $group = group_current_group();
        $role = group_user_access($group->id);
291
292
293
294
295
296
297
298
        $bi = group_get_homepage_view_groupview_block($group->id);
        $configdata = $bi->get('configdata');
        if (!isset($configdata['sortsubmittedby']) || $configdata['sortsubmittedby'] == PluginBlocktypeGroupViews::SORTBY_TITLE) {
            $sortsubmittedby = 'c.name, v.title';
        }
        else {
            $sortsubmittedby = 'c.submittedtime DESC, v.submittedtime DESC';
        }
299
        if ($role) {
300
301
302
            $limit = isset($configdata['count']) ? intval($configdata['count']) : 5;
            $limit = ($limit > 0) ? $limit : 5;

303
            // Get all views created in the group
304
305
306
307
308
309
310
311
312
313
314
315
316
317
            // Sortorder: Group homepage should be first, then sort by sortorder
            $sort = array(
                    array(
                            'column' => "type='grouphomepage'",
                            'desc' => true
                    )
            );
            // Find out what order to sort them by (default is titles)
            if (!isset($configdata['sortgroupviewsby']) || $configdata['sortgroupviewsby'] == PluginBlocktypeGroupViews::SORTBY_TITLE) {
                $sort[] = array('column' => 'title');
            }
            else {
                $sort[] = array('column' => 'mtime', 'desc' => true);
            }
318
319
320
            $data['groupviews'] = View::view_search(null, null, (object) array('group' => $group->id),
                                                    null, $limit, 0, true, $sort, null, false, null, null,
                                                    null, null, true);
321
            foreach ($data['groupviews']->data as &$view) {
322
323
324
                if (empty($view['displaytitle'])) {
                    $view['displaytitle'] = $view['title']; // add for collections
                }
325
                if (!$editing && isset($view['template']) && $view['template']) {
326
327
                    $collid = !empty($view['collid']) ? $view['collid'] : null;
                    $view['form'] = pieform(create_view_form(null, null, $view['viewid'], $collid, $collid));
328
329
                }
            }
330

331
332
333
            // Find out what order to sort them by (default is titles)
            if (!isset($configdata['sortsharedviewsby']) || $configdata['sortsharedviewsby'] == PluginBlocktypeGroupViews::SORTBY_TITLE) {
                $sortsharedviewsby = 'v.title';
334
                $sortsharedcollectionsby = array(array('type' => 'name'));
335
336
337
338
339
            }
            else {
                $sortsharedviewsby = 'v.mtime DESC';
                $sortsharedcollectionsby = array(
                        array(
340
                                'type' => 'lastchanged',
341
342
343
344
345
                                'desc' => true
                        )
                );
            }

346
347
            // For group members, display a list of views that others have
            // shared to the group
348
            $hidesubmitted = group_user_can_assess_submitted_views($group->id, $USER->get('id'));
349
350
351
352
353
354
355
356
357
358
359
360
361
            if (empty($configdata['showsharedviews'])) {
                $data['sharedviews'] = (object) array(
                    'data'   => array(),
                    'count'  => 0,
                    'limit'  => $limit,
                    'offset' => 0
                );
            }
            else {
                $data['sharedviews'] = View::get_sharedviews_data(
                        $limit,
                        0,
                        $group->id,
362
                        ($configdata['showsharedviews'] == 2 ? false : true),
363
364
                        $sortsharedviewsby,
                        $hidesubmitted
365
366
367
368
369
                );
                foreach ($data['sharedviews']->data as &$view) {
                    if (!$editing && isset($view['template']) && $view['template']) {
                        $view['form'] = pieform(create_view_form($group, null, $view->id));
                    }
370
371
                }
            }
372

373
374
375
376
377
378
379
380
381
382
383
384
385
            if (empty($configdata['showsharedcollections'])) {
                $data['sharedcollections'] = (object) array(
                    'data'   => array(),
                    'count'  => 0,
                    'limit'  => $limit,
                    'offset' => 0
                );
            }
            else {
                $data['sharedcollections'] = View::get_sharedcollections_data(
                        $limit,
                        0,
                        $group->id,
386
                        ($configdata['showsharedcollections'] == 2 ? false : true),
387
388
                        $sortsharedcollectionsby,
                        $hidesubmitted
389
390
                );
            }
391
392
            if (!empty($configdata['showsubmitted'])
                && group_user_can_assess_submitted_views($group->id, $USER->get('id'))) {
393
                // Display a list of views submitted to the group
394
                list($collections, $views) = View::get_views_and_collections(null, null, null, null, false, $group->id, $sortsubmittedby);
395
396
397
398
399
400
401
                $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,
                );
402
403
404
405
406
407
408
409
410
411

                // Display a list of members who are yet to submit views to the group
                $nosubmissions = self::find_members_without_submissions($group->id);

                $data['nosubmissions'] = array(
                    'data'   => array_slice($nosubmissions, 0, $limit),
                    'count'  => count($nosubmissions),
                    'limit'  => $limit,
                    'offset' => 0,
                );
412
            }
413

414
415
        }

416
        if ($group->submittableto && $USER->is_logged_in()) {
417
418
419
420
421
422
            // 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.

423
            list($collections, $views) = View::get_views_and_collections($USER->get('id'), null, null, null, false, $group->id, $sortsubmittedby);
424
            $data['mysubmitted'] = array_merge(array_values($collections), array_values($views));
425

426
427
428
429
            // Only render the submission form in viewing mode
            if (!$editing) {
                $data['group_view_submission_form'] = group_view_submission_form($group->id);
            }
430
431
432
433
434
        }
        $data['group'] = $group;
        return $data;
    }

435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    /**
     * Return list of members that have not submitted any pages/collections to the group
     *
     * @param integer $groupid The group to check
     *
     * @return array $members
     */
    public static function find_members_without_submissions($groupid) {
        if ($nosubmissions = get_records_sql_array("SELECT u.id FROM {usr} u JOIN {group_member} gm ON gm.member = u.id
                                                    WHERE gm.group = ? AND u.deleted = 0 AND gm.member NOT IN (
                                                        SELECT DISTINCT m.member FROM {group} g
                                                        JOIN {group_member} m ON m.group = g.id
                                                        JOIN {view} v ON v.owner = m.member
                                                        WHERE g.id = ?
                                                        AND g.submittableto = 1
                                                        AND v.submittedgroup = g.id
                                                    ) ORDER BY u.lastname, u.firstname", array($groupid, $groupid))) {
            foreach ($nosubmissions as $member) {
                $member->displayname = display_name($member->id);
                $member->url = profile_url($member->id);
            }
            return $nosubmissions;
        }
        return array();
    }

461
    public static function get_instance_title() {
462
        return get_string('title1', 'blocktype.groupviews');
463
    }
464
465
466
467
468
469
470
471
472
473

    /**
     * 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();
    }
474
}