lib.php 8.3 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
25
26
27
28
29
30
31
32
 *
 */

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() {
        return array('general');
    }

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

    public static function render_instance(BlockInstance $instance, $editing=false) {
33
        global $USER, $THEME;
34
        $configdata = $instance->get('configdata');
35
        require_once('activity.php');
36
37

        $desiredtypes = array();
38
39
40
41
        foreach($configdata as $k => $v) {
            if (!empty($v) && $k != 'maxitems') {
                $type = preg_replace('/[^a-z]+/', '', $k);
                $desiredtypes[$type] = $type;
42
43
            }
        }
44
45
46
47

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

50
        $maxitems = isset($configdata['maxitems']) ? $configdata['maxitems'] : 5;
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        // check if multirecipientnotification plugin is active or if we proceed here
        if (record_exists('artefact_installed', 'name', 'multirecipientnotification', 'active', '1')) {
            global $USER;
            $userid = $USER->get('id');
            safe_require('artefact', 'multirecipientnotification');
            $activitylist = activitylistin(join(',', $desiredtypes), $maxitems);
            $records = array();

            foreach ($activitylist->msgidrecords as $msgidrecord) {
                if ($msgidrecord->msgtable == 'notification_internal_activity') {
                    $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.id = ?";
                    $notificationrecords = get_records_sql_array($sql, array($msgidrecord->id));
                    if (count($notificationrecords) === 1) {
                        $record = $notificationrecords[0];
69
70
                        // need to adjust the url to avoid it clashing with multiplerecipient url
                        $record->url = preg_replace('/replyto/', 'oldreplyto', $record->url);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
                        $record->msgtable = $msgidrecord->msgtable;
                        $records[] = $record;
                    }
                }
                else {
                    $record = get_message_mr($userid, $msgidrecord->id);
                    if (null === $record) {
                        continue;
                    }
                    $record->url = 'artefact/multirecipientnotification/sendmessage.php?replyto=' . $msgidrecord->id . '&returnto=outbox';
                    if (count($record->userids) > 1) {
                        $record->urltext = get_string('replyurltext', 'artefact.multirecipientnotification');
                    }
                    else {
                        $record->urltext = get_string('returnurltext', 'artefact.multirecipientnotification');
                    }
                    $record->msgtable = $msgidrecord->msgtable;
                    $records[] = $record;
                }
            }
            $showmore = ($activitylist->count > $maxitems);
            // use a different template
            $smartytemplate = 'blocktype:inbox:inboxmr.tpl';
94
        }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        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
                ));
            }
111

112
113
114
115
116
            // Hack to decide whether to show the More... link
            if ($showmore = count($records) > $maxitems) {
                unset($records[$maxitems]);
            }
            $smartytemplate = 'blocktype:inbox:inbox.tpl';
117
118
        }

119
120
121
        if ($records) {
            require_once('activity.php');
            foreach ($records as &$r) {
122
123
                $section = empty($r->plugintype) ? 'activity' : "{$r->plugintype}.{$r->pluginname}";
                $r->strtype = get_string('type' . $r->type, $section);
124
                $r->message = format_notification_whitespace($r->message, $r->type);
125
126
127
            }
        }

128
        $smarty = smarty_core();
129
130
131
        if ($showmore) {
            $smarty->assign('desiredtypes', implode(',', $desiredtypes));
        }
132
133
        $smarty->assign('blockid', 'blockinstance_' . $instance->get('id'));
        $smarty->assign('items', $records);
134
        $smarty->assign('readicon', $THEME->get_url('images/readusermessage.png'));
135

136
        return $smarty->fetch($smartytemplate);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    }

    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
        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,
168
                'defaultvalue' => isset($configdata[$type->name]) ? $configdata[$type->name] : 0,
169
170
171
172
173
174
            );
        }
        $elements['maxitems'] = array(
            'type' => 'text',
            'title' => get_string('maxitems', 'blocktype.inbox'),
            'description' => get_string('maxitemsdescription', 'blocktype.inbox'),
175
            'defaultvalue' => isset($configdata['maxitems']) ? $configdata['maxitems'] : 5,
176
177
178
179
            'rules' => array(
                'minvalue' => 1,
                'maxvalue' => 100,
            ),
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
        );

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

196
197
198
199
    /**
     * We need a default title for this block, so that the inbox blocks
     * on the dashboard are translatable.
     *
200
     * To maintain existing behaviour, use the 'inboxblocktitle' string unless
201
202
203
204
205
206
207
     * 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') {
208
                    return get_string('inboxblocktitle');
209
210
211
212
213
214
                }
            }
            if ($configdata['newpost']) {
                return get_string('topicsimfollowing');
            }
        }
215
        return get_string('inboxblocktitle');
216
217
218
    }

}