lib.php 8.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-multirecipientnotification
 * @author     David Ballhausen, Tobias Zeuch
 * @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.
 *
 */

defined('INTERNAL') || die();
require_once(dirname(dirname(dirname(__FILE__))) . '/lib/group.php');
require_once(dirname(__FILE__) . '/lib/multirecipientnotification.php');
require_once(get_config('docroot') . '/artefact/multirecipientnotification/lib/activityextend.php');


/**
 * artefact plugin class. Used for registering the plugin and his functions.
 */
class PluginArtefactMultirecipientnotification extends PluginArtefact {
    /**
     * the maximum length of a userstring in the list of senders/recipients
     * in the inbox or outbox view
     */
    const MAX_USERNAME_IN_LIST_LENGTH = 30;
    /**
     * Is the plugin activated or not?
     *
     * @return boolean true, if the plugin is activated, otherwise false
     */
    public static function is_active() {
        $active = false;
        if (get_field('artefact_installed', 'active', 'name', 'multirecipientnotification')) {
            $active = true;
        }
        return $active;
    }

    /**
     * API-Function get the provided artefacttypes. Here is it 'Multirecipientnotification'.
     *
     * @return array array with the provided artefacttypes
     */
    public static function get_artefact_types() {
        return array('Multirecipientnotification');
    }

    /**
     * API-Function get the provided blocktypes. Here None.
     *
     * @return array array with the provided blocktypes
     */
    public static function get_block_types() {
        return array();
    }

    /**
     * API-Function get the Plugin ShortName
     *
     * @return string ShortName of the plugin
     */
    public static function get_plugin_name() {
        return 'multirecipientnotification';
    }

67
68
69
70
71
72
73
74
    /**
     * API-Function to allow the artefact to be counted in progress bar
     * @return  bool
     */
    public static function has_progressbar_options() {
        return false;
    }

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    /**
     * API-Function get the provided Menus. It is possible to overwrite existing menuentries
     * by redefining them with the same path, title and weight.
     *
     * @return array fully descripted new menuitems with menupath, title, url, etc.
     */
    public static function menu_items() {
        $menuExtensions = array();
        // Templates
        if (PluginArtefactMultirecipientnotification::is_active()) {
            // search for path
            $searchFor = 'lib/activity.php';
            if (!(strpos($_SERVER['REQUEST_URI'], $searchFor) === false)) {
                // set new path
                $redirTarget = get_config('wwwroot') . 'artefact/multirecipientnotification/lib/activity.php';
90
                if (!empty($_SERVER['QUERY_STRING'])) {
91
92
93
94
95
96
97
98
99
100
101
                    // change path
                    $redirTarget .= '?' . $_SERVER['QUERY_STRING'];
                }
                redirect($redirTarget);
                exit;
            }
            // search for path
             $searchFor = '/account\/activity\/($|index.php)/';
            if ((preg_match($searchFor, $_SERVER['REQUEST_URI'])) == 1) {
                // set new path
                $redirTarget = get_config('wwwroot') . 'artefact/multirecipientnotification/inbox.php';
102
                if (!empty($_SERVER['QUERY_STRING'])) {
103
104
105
106
107
108
109
110
111
112
113
                    // change path
                    $redirTarget .='?' . $_SERVER['QUERY_STRING'];
                }
                redirect($redirTarget);
                exit;
            }
            // search for path
            $searchFor = '/user\/sendmessage.php/';
            if ((preg_match($searchFor, $_SERVER['REQUEST_URI'])) == 1) {
                // set new path
                $redirTarget = get_config('wwwroot') . 'artefact/multirecipientnotification/sendmessage.php';
114
                if (!empty($_SERVER['QUERY_STRING'])) {
115
116
117
118
119
120
121
122
123
124
                    // change path
                    $redirTarget .= '?' . $_SERVER['QUERY_STRING'];
                }
                redirect($redirTarget);
                exit;
            }
        }
        return $menuExtensions;
    }

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    /**
     * API-Function get the provided submenu tabs.
     *
     * @return array fully described new SUBPAGENAV tab items with title, url, etc.
     */
    public static function submenu_items() {
        $tabs = array(
            'inbox' => array(
                'url' => 'artefact/multirecipientnotification/inbox.php',
                'title' => get_string('labelinbox', 'artefact.multirecipientnotification'),
                'tooltip' => get_string('inboxdesc', 'artefact.multirecipientnotification'),
            ),
            'outbox' => array(
                'url' => 'artefact/multirecipientnotification/outbox.php',
                'title' => get_string('labeloutbox1', 'artefact.multirecipientnotification'),
                'tooltip' => get_string('outboxdesc', 'artefact.multirecipientnotification'),
            ),
142
143
144
145
146
            'compose' => array(
                'url' => 'artefact/multirecipientnotification/sendmessage.php',
                'title' => get_string('composemessage', 'artefact.multirecipientnotification'),
                'tooltip' => get_string('composemessagedesc', 'artefact.multirecipientnotification'),
            ),
147
148
149
150
151
152
153
        );
        if (defined('NOTIFICATION_SUBPAGE') && isset($tabs[NOTIFICATION_SUBPAGE])) {
            $tabs[NOTIFICATION_SUBPAGE]['selected'] = true;
        }
        return $tabs;
    }

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    public static function postinst($prevversion) {
        if ($prevversion < 20131010) {
            // Add triggers to update user unread message count when updating
            // artefact_multirecipient_userrelation
            db_create_trigger(
                'update_unread_insert2',
                'AFTER', 'INSERT', 'artefact_multirecipient_userrelation', '
                IF NEW.role = \'recipient\' AND NEW.read = \'0\' THEN
                    UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
                END IF;'
            );
            db_create_trigger(
                'update_unread_update2',
                'AFTER', 'UPDATE', 'artefact_multirecipient_userrelation', '
                IF OLD.read = \'0\' AND NEW.read = \'1\' AND NEW.role = \'recipient\' THEN
                    UPDATE {usr} SET unread = unread - 1 WHERE id = NEW.usr;
                ELSEIF OLD.read = \'1\' AND NEW.read = \'0\' AND NEW.role = \'recipient\' THEN
                    UPDATE {usr} SET unread = unread + 1 WHERE id = NEW.usr;
                END IF;'
            );
            db_create_trigger(
                'update_unread_delete2',
                'AFTER', 'DELETE', 'artefact_multirecipient_userrelation', '
                IF OLD.read = \'0\' AND OLD.role = \'recipient\' THEN
                    UPDATE {usr} SET unread = unread - 1 WHERE id = OLD.usr;
                END IF;'
            );
        }
    }

    /**
     * hooks the eventlistener_save_on_commit-method into the event-listener
     * is called upon installation or update
     *
     * @return array
     */
    public static function get_event_subscriptions() {
        return array(
            (object) array(
                'plugin'        => 'multirecipientnotification',
                'event'         => 'deleteuser',
                'callfunction'  => 'eventlistener_on_deleteuser',
            ),
        );
    }

    /**
     * deletes a users messages, when a user is deleted
     *
     * @param type $event
     * @param type $user
     */
    public static function eventlistener_on_deleteuser($event, $user) {
        if ('deleteuser' !== $event) {
            return;
        }
        $userid = $user['id'];

        db_begin();
        $recievedmessageids = get_message_ids_mr($userid, 'recipient', null, null, null);
        if (count($recievedmessageids) > 0) {
            delete_messages_mr($recievedmessageids, $userid);
        }

        $sentmessageids = get_message_ids_mr($userid, 'sender', null, null, 100);
        if (count($sentmessageids) > 0) {
            delete_messages_mr($sentmessageids, $userid);
        }
        db_commit();
    }

} // PluginArtefactNotficationmultirecipientnotification

// Class ArtefactTypeNotficationmultirecipientnotification
228
require('artefacttypemultirecipientnotification.php');