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

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

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

    public static function get_categories() {
25
        return array('general' => 19000);
26
27
28
29
30
31
    }

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

32
    public static function get_link(BlockInstance $instance) {
33
34
        $data = get_config('wwwroot') . 'module/multirecipientnotification/inbox.php';
        return $data;
35
36
    }

37
    public static function render_instance(BlockInstance $instance, $editing=false, $versioning=false) {
38
        global $USER, $THEME;
39
        $configdata = $instance->get('configdata');
40
        require_once('activity.php');
41
42

        $desiredtypes = array();
43
44
45
46
        foreach($configdata as $k => $v) {
            if (!empty($v) && $k != 'maxitems') {
                $type = preg_replace('/[^a-z]+/', '', $k);
                $desiredtypes[$type] = $type;
47
48
            }
        }
49
50
51
52

        if ($USER->get('admin') && !empty($desiredtypes['adminmessages'])) {
            unset($desiredtypes['adminmessages']);
            $desiredtypes += get_column('activity_type', 'name', 'admin', 1);
53
54
        }

55
        $maxitems = isset($configdata['maxitems']) ? $configdata['maxitems'] : 5;
56

57
58
59
60
        safe_require_plugin('module', 'multirecipientnotification');
        $activitylist = activityblocklistin(join(',', $desiredtypes), $maxitems);
        $records = $activitylist->records;
        $showmore = ($activitylist->count > $maxitems);
61

62
63
64
        if ($records) {
            require_once('activity.php');
            foreach ($records as &$r) {
65
66
                $section = empty($r->plugintype) ? 'activity' : "{$r->plugintype}.{$r->pluginname}";
                $r->strtype = get_string('type' . $r->type, $section);
67
68
69
            }
        }

70
        $smarty = smarty_core();
71
        if ($showmore) {
72
            $smarty->assign('morelink', self::get_link($instance) . '?type=' . implode(',', $desiredtypes));
73
        }
74
75
        $smarty->assign('blockid', 'blockinstance_' . $instance->get('id'));
        $smarty->assign('items', $records);
76

77
        return $smarty->fetch('blocktype:inbox:inboxmr.tpl');
78
79
80
81
82
83
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
84
    public static function instance_config_form(BlockInstance $instance) {
85
86
87
88
89
90
91
92
93
94
95
96
97
        global $USER;
        $configdata = $instance->get('configdata');

        $types = get_records_array('activity_type', 'admin', 0, 'plugintype,pluginname,name', 'name,plugintype,pluginname');

        $elements = array();
        $elements['types'] = array(
            'type' => 'fieldset',
            'legend' => get_string('messagetypes', 'blocktype.inbox'),
            'elements' => array(),
        );
        foreach($types as $type) {
            if (!empty($type->plugintype)) {
98
                $type->title = get_string('type' . $type->name, $type->plugintype . '.' . $type->pluginname);
99
100
            }
            else {
101
                $type->title = get_string('type' . $type->name, 'activity');
102
            }
103
104
105
106
107
108
109
110
111
            $type->class = '';
        }
        usort($types, function ($a, $b) { return strnatcasecmp($a->title, $b->title); });
        if ($USER->get('admin')) {
            $types[] = (object)array('name' => 'adminmessages',
                                     'title' => get_string('typeadminmessages', 'activity'),
                                     'class' => 'field-label-bold');
        }
        foreach($types as $type) {
112
            $elements['types']['elements'][$type->name] = array(
113
                'type' => 'switchbox',
114
115
                'title' => $type->title,
                'class' => $type->class,
116
                'defaultvalue' => isset($configdata[$type->name]) ? $configdata[$type->name] : 0,
117
118
119
120
121
122
            );
        }
        $elements['maxitems'] = array(
            'type' => 'text',
            'title' => get_string('maxitems', 'blocktype.inbox'),
            'description' => get_string('maxitemsdescription', 'blocktype.inbox'),
123
            'defaultvalue' => isset($configdata['maxitems']) ? $configdata['maxitems'] : 5,
124
125
126
127
            'rules' => array(
                'minvalue' => 1,
                'maxvalue' => 100,
            ),
128
129
130
131
132
133
134
135
136
137
138
139
140
        );

        return $elements;
    }

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

    /**
     * Inbox only makes sense for personal views
     */
    public static function allowed_in_view(View $view) {
141
        return in_array($view->get('type'), self::get_viewtypes());
142
143
    }

144
145
146
147
    /**
     * We need a default title for this block, so that the inbox blocks
     * on the dashboard are translatable.
     *
148
     * To maintain existing behaviour, use the 'inboxblocktitle' string unless
149
150
151
152
153
154
155
     * the block has only got forum post notifications in it, in which case
     * use 'topicsimfollowing'
     */
    public static function get_instance_title(BlockInstance $instance) {
        if ($configdata = $instance->get('configdata')) {
            foreach ($configdata as $k => $v) {
                if ($v && $k != 'newpost' && $k != 'maxitems') {
156
                    return get_string('inboxblocktitle');
157
158
159
160
161
162
                }
            }
            if ($configdata['newpost']) {
                return get_string('topicsimfollowing');
            }
        }
163
        return get_string('inboxblocktitle');
164
165
    }

166
167
168
    public static function should_ajaxify() {
        return true;
    }
169
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
}