lib.php 5 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 *
 * @package    mahara
 * @subpackage blocktype-myviews
 * @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 PluginBlocktypeMyviews extends MaharaCoreBlocktype {
15 16

    public static function get_title() {
17
        return get_string('title1', 'blocktype.myviews');
18 19 20
    }

    public static function get_description() {
21
        return get_string('description1', 'blocktype.myviews');
22
    }
Aaron Wells's avatar
Aaron Wells committed
23

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

    public static function get_categories() {
29
        return array('internal' => 33000);
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 views 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
        $smarty = smarty_core();
        $smarty->assign('options', $options);
        $smarty->assign('items', $items['data']);

        $items['tablerows'] = $smarty->fetch($template);

51
        if ($items['count'] && $items['limit'] && $pagination) {
52 53 54 55 56 57 58 59
            $pagination = build_pagination(array(
                'id' => $pagination['id'],
                'class' => 'center',
                'datatable' => $pagination['datatable'],
                'url' => $pagination['baseurl'],
                'jsonscript' => $pagination['jsonscript'],
                'count' => $items['count'],
                'limit' => $items['limit'],
60
                'setlimit' => true,
61 62 63 64 65 66 67 68 69 70
                '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'];
        }
    }

71
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
72
        $userid = $instance->get_view()->get('owner');
73 74 75 76
        if (!$userid) {
            return '';
        }

77 78 79
        $smarty = smarty_core();

        // Get viewable views
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        $views = View::view_search(
                null, // $query
                null, // $ownerquery
                (object) array('owner' => $userid), // $ownedby
                null, // $copyableby
                10, // $limit
                0, // $offset
                true, // $extra
                null, // $sort
                array('portfolio'), // $types
                null, // $collection
                null, // $accesstypes
                null, // $tag
                null, // $viewid
                null, // $excludeowner
                true // $groupbycollection
        );
97 98 99 100 101 102 103 104 105
        $views = (array)$views;
        $viewid = $instance->get_view()->get('id');
        $baseurl = $instance->get_view()->get_url();
        $baseurl .= (strpos($baseurl, '?') === false ? '?' : '&') . 'view=' . $viewid . '&editing=' . $editing;
        $pagination = array(
            'baseurl'    => $baseurl,
            'id'         => 'myviews_pagination',
            'datatable'  => 'myviewlist',
            'jsonscript' => 'blocktype/myviews/myviews.json.php',
106 107
            'resultcounttextsingular' => get_string('result'),
            'resultcounttextplural'   => get_string('results'),
108 109 110
        );
        self::render_items($views, 'blocktype:myviews:myviewspaginator.tpl', array(), $pagination);
        $smarty->assign('myviews', $views);
111 112 113 114 115 116
        return $smarty->fetch('blocktype:myviews:myviews.tpl');
    }

    public static function has_instance_config() {
        return false;
    }
117 118 119 120 121

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

122 123 124 125
    /**
     * Myviews only makes sense for personal views
     */
    public static function allowed_in_view(View $view) {
126
        return in_array($view->get('type'), self::get_viewtypes());
127 128
    }

129 130 131
    public static function override_instance_title(BlockInstance $instance) {
        global $USER;
        $ownerid = $instance->get_view()->get('owner');
132
        if ($ownerid === null || $ownerid == $USER->get('id')) {
133
            return get_string('title1', 'blocktype.myviews');
134
        }
135
        return get_string('otherusertitle1', 'blocktype.myviews', display_name($ownerid, null, true));
136 137
    }

138 139 140 141 142 143 144 145 146
    /**
     * 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();
    }
147
}