activity.php 7.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
 * This program is part of Mahara
 *
 *  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 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

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

/**
 * This is the function to call whenever anything happens
 * that is going to end up on a user's activity page.
 * 
 * @param string $activitytype type of activity
 * @param mixed $data data 
 */
function activity_occured($activitytype, $data) {
    if (!$at = get_record('activity_type', 'name', $activitytype)) {
        throw new Exception("Invalid activity type $activitytype");
    }

    if (!empty($at->delay)) {
        $delayed = new StdClass;
        $delayed->type = $activitytype;
        $delayed->data = serialize($data);
        $delayed->ctime = db_format_timestamp(time());
        insert_record('activity_queue', $delayed);
    }
    else {
        handle_activity($at, $data);
    }
}

/** 
 * This function dispatches all the activity stuff
 * to whatever notification plugin it needs to
 * and figures out all the implications of 
 * activity and who needs to know about it.
 * 
 * @param object $activitytype record from activity_type
 * @param mixed $data
 */
function handle_activity($activitytype, $data) {

    $users = array();
    $prefix = get_config('dbprefix');

    if (!empty($activitytype->admin)) {
        $users = activity_get_users($activitytype->name, null, null, true);
    }
    else {
        switch ($activitytype->name) {
            // easy ones first :)
            case 'maharamessage':
                $users = activity_get_users($activitytype->name, null, $data->users);
                break;
            case 'usermessage':
                $users = activity_get_users($activitytype->name, array($data->userto));
                break;
            case 'feedback':
                if ($data->view) {
                    $userid = get_field('view', 'owner', 'id', $data->view);
                } 
                else if ($data->artefact) {
                    $userid = get_field('artefact', 'owner', 'id', $data->artefact);
                }
                $users = activity_get_users($activitytype->name, array($userid));
                break;
            // and now the harder ones
            case 'watchlist':
                if ($data->view) {
                    $sql = 'SELECT u.*, p.method
                                FROM ' . $prefix . 'watchlist_view wv
                                JOIN ' . $prefix . 'usr u
                                    ON wa.user = u.id
                                JOIN ' . $prefix . 'usr_preference p
                                    ON p.user = u.id
                                WHERE pc.activity = ?
                                AND wv.view = ?
                           ';
                    $users = get_records_sql($sql, array('watchlist', $data->view));
                } 
                else if ($data->artefact) {
                    $sql = 'SELECT DISTINCT u.*, p.method
                                FROM ' . $prefix . 'watchlist_artefact wa
                                JOIN ' . $prefix . 'artefact_parent_cache pc
                                    ON (pc.parent = wa.artefact OR pc.artefact = wa.artefact)
                                JOIN ' . $prefix . 'usr u 
                                    ON wa.user = u.id
                                JOIN ' . $prefix . 'usr_preference p
                                    ON p.user = u.id
                                WHERE pc.activity = ?
                                AND (pc.parent = ? OR wa.artefact = ?)
                            ';
                    $users = get_records_sql($sql, array('watchlist', $data->artefact));
                }
                else {
                    throw new InvalidArgumentException("Invalid watchlist type");
                }
                break;
            case 'newview':
                // add users on friendslist, userlist or grouplist...
                $sql = 'SELECT userid, u.*, p.method
                        FROM (
                            SELECT (CASE WHEN usr1 = ? THEN usr2 ELSE usr1 END) AS userid 
                                FROM ' . $prefix . 'usr_friend 
                                WHERE (usr1 = ? OR usr2 = ?)
                            UNION SELECT member AS userid 
                                FROM ' . $prefix . 'usr_group_member m
                                JOIN ' . $prefix . 'view_access_group g ON m.group = g.group 
                            WHERE g.view = ?
                            UNION SELECT usr AS userid 
                                  FROM ' . $prefix . 'view_access_usr u 
                                  WHERE u.view = ?
                        ) AS userlist
                            JOIN ' . $prefix . 'usr u ON u.id = userlist.userid
                            JOIN ' . $prefix . 'usr_activity_preference p ON p.usr = u.id';
                $users = get_records_sql($sql, array($data->owner, $data->owner, $data->owner,  
                                                     $data->view, $data->view));
                break;
        }
    }
    safe_require('notification', 'internal', 'lib.php', 'require_once');
    foreach ($users as $user) {
        if ($user->method != 'internal') {
            safe_require('notification', $method, 'lib.php', 'require_once');
            call_static_method(generate_class_name('notification', $method), 'notify_user', $user, $data);
        }
        // always do internal
        call_static_method('PluginNotificationInternal', 'notify_user', $user, $data);
    }
}

/**
 * this function returns an array of users
 * for a particular activitytype
 * including the notification method.
 *
 * @param string $activitytype the name of the activity type
 * @param array $userids an array of userids to filter by
 * @param array $userobjs an array of user objects to filterby
 * @param bool $adminonly whether to filter by admin flag
 * @return array of users
 */
function activity_get_users($activitytype, $userids=null, $userobjs=null, $adminonly=false) {
    $values = array($activitytype);
    $sql = 'SELECT u.*, p.method
                FROM ' . get_config('dbprefix') .'usr u
                JOIN ' . get_config('dbprefix') . 'usr_activity_preference p
                    ON p.user = u.id
                WHERE p.activity = ? ';
    if (!empty($adminonly)) {
        $sql .= ' AND u.admin = ? ';
        $values[] = 1;
    }
    if (!empty($userobjs) && is_array($userobjs)) {
        $sql .= ' AND u.id IN (' . implode(',',db_array_to_ph($userobjs)) . ')';
        $values = array_merge($values, array_to_fields($userobjs));
    } 
    else if (!empty($userids) && is_array($userids)) {
        $sql .= ' AND u.id IN (' . implode(',',db_array_to_ph($userids)) . ')';
        $values = array_merge($values, $userids);
    }
}


?>