lib.php 6.95 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
14
15
16
17
18
19
20
21
22
23
24
 *
 */

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

class PluginBlocktypeInbox extends SystemBlocktype {

    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
35
36
37
38
39
        safe_require('module', 'multirecipientnotification');
        if (PluginModuleMultirecipientnotification::is_active()) {
            $data = get_config('wwwroot') . 'module/multirecipientnotification/inbox.php';
        }
        else {
            $data = get_config('wwwroot') . 'account/activity/index.php';
        }
40
41
42
        return sanitize_url($data);
    }

43
    public static function render_instance(BlockInstance $instance, $editing=false) {
44
        global $USER, $THEME;
45
46
47
        $configdata = $instance->get('configdata');

        $desiredtypes = array();
48
49
50
51
        foreach($configdata as $k => $v) {
            if (!empty($v) && $k != 'maxitems') {
                $type = preg_replace('/[^a-z]+/', '', $k);
                $desiredtypes[$type] = $type;
52
53
            }
        }
54
55
56
57

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

60
        $maxitems = isset($configdata['maxitems']) ? $configdata['maxitems'] : 5;
61

62
        // check if multirecipientnotification plugin is active or if we proceed here
63
        if (record_exists('module_installed', 'name', 'multirecipientnotification', 'active', '1') && safe_require_plugin('module', 'multirecipientnotification')) {
64
65
            global $USER;
            $userid = $USER->get('id');
66
67
            $activitylist = activityblocklistin(join(',', $desiredtypes), $maxitems);
            $records = $activitylist->records;
68
69
70
            $showmore = ($activitylist->count > $maxitems);
            // use a different template
            $smartytemplate = 'blocktype:inbox:inboxmr.tpl';
71
        }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
        else {
            $records = array();
            if ($desiredtypes) {
                $sql = "
                    SELECT n.id, n.subject, n.message, n.url, n.urltext, n.read, t.name AS type
                    FROM {notification_internal_activity} n JOIN {activity_type} t ON n.type = t.id
                    WHERE n.usr = ?
                    AND t.name IN (" . join(',', array_map('db_quote', $desiredtypes)) . ")
                    ORDER BY n.ctime DESC
                    LIMIT ?;";

                $records = get_records_sql_array($sql, array(
                    $USER->get('id'),
                    $maxitems + 1 // Hack to decide whether to show the More... link
                ));
            }
88

89
90
91
92
93
            // Hack to decide whether to show the More... link
            if ($showmore = count($records) > $maxitems) {
                unset($records[$maxitems]);
            }
            $smartytemplate = 'blocktype:inbox:inbox.tpl';
94
95
        }

96
97
98
        if ($records) {
            require_once('activity.php');
            foreach ($records as &$r) {
99
100
                $section = empty($r->plugintype) ? 'activity' : "{$r->plugintype}.{$r->pluginname}";
                $r->strtype = get_string('type' . $r->type, $section);
101
                $r->message = format_notification_whitespace($r->message, $r->type);
102
103
104
            }
        }

105
        $smarty = smarty_core();
106
107
108
        if ($showmore) {
            $smarty->assign('desiredtypes', implode(',', $desiredtypes));
        }
109
110
        $smarty->assign('blockid', 'blockinstance_' . $instance->get('id'));
        $smarty->assign('items', $records);
111

112
        return $smarty->fetch($smartytemplate);
113
114
115
116
117
118
    }

    public static function has_instance_config() {
        return true;
    }

Son Nguyen's avatar
Son Nguyen committed
119
    public static function instance_config_form(BlockInstance $instance) {
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        global $USER;
        $configdata = $instance->get('configdata');

        $types = get_records_array('activity_type', 'admin', 0, 'plugintype,pluginname,name', 'name,plugintype,pluginname');
        if ($USER->get('admin')) {
            $types[] = (object)array('name' => 'adminmessages');
        }

        $elements = array();
        $elements['types'] = array(
            'type' => 'fieldset',
            'legend' => get_string('messagetypes', 'blocktype.inbox'),
            'elements' => array(),
        );
        foreach($types as $type) {
            if (!empty($type->plugintype)) {
                $title = get_string('type' . $type->name, $type->plugintype . '.' . $type->pluginname);
            }
            else {
                $title = get_string('type' . $type->name, 'activity');
            }
            $elements['types']['elements'][$type->name] = array(
                'type' => 'checkbox',
                'title' => $title,
144
                'defaultvalue' => isset($configdata[$type->name]) ? $configdata[$type->name] : 0,
145
146
147
148
149
150
            );
        }
        $elements['maxitems'] = array(
            'type' => 'text',
            'title' => get_string('maxitems', 'blocktype.inbox'),
            'description' => get_string('maxitemsdescription', 'blocktype.inbox'),
151
            'defaultvalue' => isset($configdata['maxitems']) ? $configdata['maxitems'] : 5,
152
153
154
155
            'rules' => array(
                'minvalue' => 1,
                'maxvalue' => 100,
            ),
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        );

        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) {
        return $view->get('owner') != null;
    }

172
173
174
175
    /**
     * We need a default title for this block, so that the inbox blocks
     * on the dashboard are translatable.
     *
176
     * To maintain existing behaviour, use the 'inboxblocktitle' string unless
177
178
179
180
181
182
183
     * 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') {
184
                    return get_string('inboxblocktitle');
185
186
187
188
189
190
                }
            }
            if ($configdata['newpost']) {
                return get_string('topicsimfollowing');
            }
        }
191
        return get_string('inboxblocktitle');
192
193
    }

194
195
196
    public static function should_ajaxify() {
        return true;
    }
197
}