lib.php 4.27 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
 *
 * @package    mahara
Penny Leach's avatar
Penny Leach committed
21
 * @subpackage notification-internal
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
29
 *
 */

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

30
31
class PluginNotificationInternal extends PluginNotification {

32
33
    static $userdata = array('urltext', 'subject', 'message');

34
    public static function notify_user($user, $data) {
35
        $toinsert = new StdClass;
36
        $toinsert->type = $data->type;
37
38
39
40
41
42
43
        $toinsert->usr = $user->id;
        if (!empty($user->markasread)) {
            $toinsert->read = 1;
        } 
        else {
            $toinsert->read = 0;
        }
44
        $toinsert->message = $data->message;
45
        $toinsert->subject = $data->subject;
Evan Goldenberg's avatar
Evan Goldenberg committed
46
        $toinsert->parent = $data->parent;
47
        $toinsert->ctime = db_format_timestamp(time());
48

49
50
51
        if (!empty($data->url)) {
            $toinsert->url = $data->url;
        }
52
53
54
        if (!empty($data->urltext)) {
            $toinsert->urltext = $data->urltext;
        }
55
56
57
58
        if (!empty($data->fromuser)) {
            $toinsert->from = $data->fromuser;
        }

59
        return insert_record('notification_internal_activity', $toinsert, 'id', true);
60
    }
61
62
63
64
65
66
    
    /** 
     * this method is only implemented in internal & is used for the header
     */

    public static function unread_count($userid) {
67
68
69
70
71
        static $unreadcount = array();
        if (!isset($unreadcount[$userid])) {
            $unreadcount[$userid] = count_records('notification_internal_activity', 'usr', $userid, 'read', 0);
        }
        return $unreadcount[$userid];
72
    }
73

74
75
76
77
78
79
80
81
82
83
84
85
86
    public static function get_event_subscriptions() {
        $subscriptions = array(
            (object)array(
                'plugin'       => 'internal',
                'event'        => 'deleteuser',
                'callfunction' => 'deleteuser',
            ),
        );
        return $subscriptions;
    }

    public static function deleteuser($event, $user) {
        delete_records('notification_internal_activity', 'usr', $user['id']);
87
88
89
90
91
92
93
        // Delete system messages from this user where the url points to their
        // missing profile.  They're mostly friend requests, which are now useless.
        delete_records_select(
            'notification_internal_activity',
            '"from" = ? AND type = (SELECT id FROM {activity_type} WHERE name = ?) AND url = ?',
            array($user['id'], 'maharamessage', get_config('wwwroot') . 'user/view.php?id=' . $user['id'])
        );
94
    }
95
96
97
98
99
100
101
102
103
104
105
106
107
108

    /**
     * A method that does housekeeping on the notification_internal_activity table
     * @param $types string|array the activity types to be cleaned
     * @param $olderthandays integer the age an entry should at least be, before cleaning
     */
    public static function clean_notifications($types, $olderthandays=182) {
        $staletime = db_format_timestamp(time() - ($olderthandays * 24 * 60 * 60));

        if (!is_array($types)) {
            // We're potentially dealing with just one type
            $types = array($types);
        }

109
        $select = '
110
            ctime < ?
111
            AND "read" = 1
112
113
            AND type IN(
                SELECT id FROM {activity_type}
114
                WHERE name IN (' . join(",", array_map(create_function('$a', 'return db_quote($a);'), $types)) . '))';
115
116
117
118

        delete_records_select('notification_internal_activity', $select, array(db_format_timestamp($staletime)));
    }

119
}