activity.php 19.9 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
<?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 
 */
36
function activity_occurred($activitytype, $data) {
37
38
39
40
    if (!$at = get_record('activity_type', 'name', $activitytype)) {
        throw new Exception("Invalid activity type $activitytype");
    }

41
42
43
44
45
46
47
48
    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 {
49
        handle_activity($at, $data);
50
    }
51
52
53
54
55
56
57
58
59
}

/** 
 * 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
60
61
62
 * @param mixed $data must contain message to save.
 * it can also contain url.
 * each activity type has different requirements of $data - 
63
64
65
66
67
68
69
70
71
72
 *  - <b>contactus</b> must contain $message, $subject (optional), $fromname, $fromaddress, $userfrom (if a logged in user)
 *  - <b>objectionable</b> must contain $message, $view and $artefact if applicable
 *  - <b>maharamessage</b> must contain $users, an array of userids. $subject and $message (contents of message)
 *  - <b>usermessage</b> must contain $userto, id of recipient user, $userfrom, id of user from 
    -       and $subject and $message (contents of message)
 *  - <b>feedback (artefact)</b> must contain both $artefact (id) and $view (id) and $message 
 *  - <b>feedback (view)</b> must contain $view (id) and $message
 *  - <b>watchlist (view) </b> must contain $view (id of view) 
    -       and should also contain $subject (or a boring default will be used)
 *  - <b>newview</b> must contain $owner userid of view owner AND $view (id of new view)
73
 *  - <b>viewaccess</b> must contain $owner userid of view owner AND $view (id of view) and $oldusers array of userids before access change was committed.
74
 */
75
function handle_activity($activitytype, $data, $cron=false) {
76

77
78
79
80
81
82
    // mysql compatibility (sigh...)
    $casturl = 'CAST(? AS TEXT)';
    if (get_config('dbtype') == 'mysql') {
        $casturl = 'CAST(? AS CHAR)'; // note, NOT varchar
    }

83
    $data = (object)$data;
84
85
86
87
    if (is_string($activitytype)) {
        $activitytype = get_record('activity_type', 'name', $activitytype);
    }
    
88

89
90
91
92
    if (!is_object($activitytype)) {
        throw new InvalidArgumentException("Invalid activitytype $activitytype");
    }

93
94
95
96
97
98
99
    $classname = 'ActivityType' . ucfirst($activitytype->name);
    if (!empty($activitytype->plugintype)) {
        safe_require($activitytype->plugintype, $activitytype->pluginname);
        $classname = 'ActivityType' . 
            ucfirst($activitytype->plugintype) . 
            ucfirst($activitytype->pluginname) . 
            ucfirst($activitytype->name);
100
    }
101
102
103

    $activity = new $classname($data);
    if (!$activity->any_users()) {
104
105
        return;
    }
106
107

    $data = $activity->to_stdclass();
108
    safe_require('notification', 'internal', 'lib.php', 'require_once');
109
110
    $data->type = $activity->get_type();
    foreach ($activity->get_users() as $user) {
111
112
113
114
115
116
117
118
119
120
        $userdata = $data;
        // some stuff gets overridden by user specific stuff
        if (!empty($user->url)) {
            $userdata->url = $user->url;
        }
        if (!empty($user->message)) {
            $userdata->message = $user->message;
        }
        if (!empty($user->subject)) {
            $userdata->subject = $user->subject;
121
        }
122
123
124
        if (empty($user->method)) {
            $user->method = 'internal';
        }
125
        if ($user->method != 'internal') {
126
            $method = $user->method;
127
            safe_require('notification', $method, 'lib.php', 'require_once');
128
129
130
131
132
133
134
135
            try {
                call_static_method(generate_class_name('notification', $method), 'notify_user', $user, $userdata);
                $user->markasread = true; // if we're doing something else, don't generate unread internal ones.
            }
            catch (Exception $e) {
                $user->markasread = false; // if we fail (eg email falls over), don't mark it as read...
                // @todo penny notify them that their notification type failed....
            }
136
137
        }
        // always do internal
138
        call_static_method('PluginNotificationInternal', 'notify_user', $user, $userdata);
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    }
}

/**
 * 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);
155
    $sql = 'SELECT u.id,u.username,u.firstname,u.lastname,u.preferredname,u.email, p.method
156
157
                FROM {usr} u
                LEFT JOIN {usr_activity_preference} p
158
                    ON p.usr = u.id
159
                WHERE (p.activity = ? ' . (empty($adminonly) ? ' OR p.activity IS NULL' : '') . ')';
160
161
162
163
164
165
166
167
168
169
170
171
    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);
    }
172
    return get_records_sql_array($sql, $values);
173
174
}

175
176
177
178
179
180
181
/**
 * this function inserts a default set of activity preferences for a given user
 * id
 */
function activity_set_defaults($user_id) {
    $activitytypes = get_records_array('activity_type', 'admin', 0);
    foreach ($activitytypes as $type) {
182
183
184
185
186
        insert_record('usr_activity_preference', (object)array(
            'usr' => $user_id,
            'activity' => $type->name,
            'method' => 'internal',
        ));
187
188
189
190
    }
    
}

191
192
function activity_process_queue() {

193
    db_begin();
194
    if ($toprocess = get_records_array('activity_queue')) {
195
        foreach ($toprocess as $activity) {
196
            handle_activity($activity->type, unserialize($activity->data), true);
197
198
199
200
201
        }
        delete_records('activity_queue');
    }
    db_commit();
}
202

203
function activity_get_viewaccess_users($view, $owner, $type) {
204

205
206
207
    $sql = 'SELECT userid, u.*, p.method
                FROM (
                SELECT (CASE WHEN usr1 = ? THEN usr2 ELSE usr1 END) AS userid 
208
209
210
                    FROM {usr_friend} f
                    JOIN {view} v ON (v.owner = f.usr1 OR v.owner = f.usr2)
                    JOIN {view_access} vu ON vu.view = v.id
211
                        WHERE (usr1 = ? OR usr2 = ?) AND vu.accesstype = ? AND v.id = ? 
212
                UNION SELECT usr AS userid 
213
                    FROM {view_access_usr} u 
214
                        WHERE u.view = ?
215
                UNION SELECT m.member 
216
217
218
219
220
221
222
                    FROM {group_member} m
                    JOIN {view_access_group} g ON g.group = m.group
                        WHERE g.view = ? AND (g.tutoronly = ? OR m.tutor = ?)
                UNION SELECT g.owner
                    FROM {group} g
                    JOIN {view_access_group} ag ON ag.group = g.id
                        WHERE ag.view = ?
223
                ) AS userlist
224
225
                JOIN {usr} u ON u.id = userlist.userid
                LEFT JOIN {usr_activity_preference} p ON p.usr = u.id
226
            WHERE p.activity = ?';
227
    $values = array($owner, $owner, $owner, 'friends', $view, $view, $view, 0, 1, $view, $type);
228
    if (!$u = get_records_sql_assoc($sql, $values)) {
229
230
231
        $u = array();
    }
    return $u;
232
233
}

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

/** activity type classes **/
abstract class ActivityType {
    
    protected $subject;
    protected $message;
    protected $users = array();
    protected $url;
    
    public function get_type() {
        $prefix = 'ActivityType';
        return strtolower(substr(get_class($this), strlen($prefix)));
    }

    public function any_users() {
        return (is_array($this->users) && count($this->users) > 0);
    }

    public function get_users() {
        return $this->users;
    }

    public function __construct($data) {
        $this->set_parameters($data);
        $this->ensure_parameters();
    }

    private function set_parameters($data) {
        foreach ($data as $key => $value) {
            if (property_exists($this, $key)) {
                $this->{$key} = $value;
            }
        }   
    }

    private function ensure_parameters() {
        foreach ($this->get_required_parameters() as $param) {
            if (!isset($this->{$param})) {
                throw new ParamOutOfRangeException(get_string('missingparam', 'activity', $param, $this->get_type()));
            }
        }
    }

    public function to_stdclass() {
       return (object)get_object_vars($this); 
    }
        

    abstract function get_required_parameters();
}


abstract class ActivityTypeAdmin extends ActivityType { 

    public function __construct($data) {
        parent::__construct($data);
        $this->users = activity_get_users($this->get_type(), null, null, true);
        log_debug($this->users);
    }
}

class ActivityTypeContactus extends ActivityTypeAdmin {
    
    protected $fromname;
    protected $fromemail;
    protected $userfrom;

    function __construct($data) { 
        parent::__construct($data);
        $this->subject = get_string('newcontactusfrom', 'activity') . ' ' .$this->fromname 
            . '<' . $this->fromemail .'>' . (isset($this->subject) ? ': ' . $this->subject : '');
        $this->message = $this->subject . "\n\n" . $this->message;
        $this->subject = get_string('newcontactus', 'activity');
        if (!empty($this->userfrom)) {
            $this->url = get_config('wwwroot') . 'user/view.php?id=' . $this->userfrom;
        }
    }
    
    public function get_required_parameters() {
        return array('message', 'fromname', 'fromemail');
    }
}

class ActivityTypeObjectionable extends ActivityTypeAdmin {

    protected $view;
    protected $artefact;

    function __construct($data) { 
        parent::__construct($data);
        if (!$viewtitle = get_field('view', 'title', 'id', $this->view)) {
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }
        if (empty($this->artefact)) {
            $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;
            $this->subject = get_string('objectionablecontentview', 'activity') 
                . ' ' . get_string('onview', 'activity') . ' ' . $viewtitle;
        }
        else {
            if (!$artefacttitle = get_field('artefact', 'title', 'id', $this->artefact)) {
                throw new ArtefactNotFoundException(get_string('artefactnotfound', 'error', $this->artefact));
            }
            $this->url = get_config('wwwroot') . 'view/view.php?artefact=' . $this->artefact . '&id=' . $this->view;
            $this->subject = get_string('objectionablecontentartefact', 'activity') 
                . ' '  . get_string('onartefact', 'activity') . ' ' . $artefacttitle;
        }
    }

    public function get_required_parameters() {
        return array('message', 'view');
    }

}

class ActivityTypeVirusRepeat extends ActivityTypeAdmin {

    protected $username;
    protected $fullname;
    protected $userid;

    public function __construct($data) { 
        parent::__construct($data);
        $userstring = $this->username . ' (' . $this->fullname . ') (userid:' . $this->userid . ')' ;
        $this->subject = get_string('virusrepeatsubject', 'mahara', $userstring);
        $this->message = get_string('virusrepeatmessage');
    }

    public function get_required_parameters() {
        return array('username', 'fullname', 'userid');
    }
}

class ActivityTypeVirusRelease extends ActivityTypeAdmin {

    public function __construct($data) { 
        parent::__construct($data);
    }

    public function get_required_parameters() {
        return array();
    }
}

class ActivityTypeMaharamessage extends ActivityType {

    public function __construct($data) { 
        parent::__construct($data);
        $this->users = activity_get_users('maharamessage', $this->users);
    }

    public function get_required_parameters() {
        return array('message', 'subject', 'users');
    }
}

class ActivityTypeUsermessage extends ActivityType { 

    protected $userto;
    protected $userfrom;

    public function __construct($data) { 
        parent::__construct($data);
        if (empty($this->subject)) {
            $this->subject = get_string('newusermessage', 'mahara', display_name($this->userfrom));
        }
        $this->users = activity_get_users('usermessage', array($this->userto));
        if (empty($this->url)) {
            $this->url = get_config('wwwroot') . 'user/view.php?id=' . $this->userfrom;
        }
    } 

    public function get_required_parameters() {
        return array('message', 'userto', 'userfrom');
    }
    
}

class ActivityTypeFeedback extends ActivityType { 

    protected $view;
    protected $artefact;

    public function __construct($data) { 
        parent::__construct($data);
        if (!empty($this->artefact)) { // feedback on artefact
            $this->subject = get_string('newfeedbackonartefact', 'activity');
            require_once(get_config('docroot') . 'artefact/lib.php');
            $artefact = artefact_instance_from_id($this->artefact);
            $this->subject .= ' ' .$artefact->get('title');

            $userid = null;
            if ($artefact->feedback_notify_owner()) {
                $userid = $artefact->get('owner');
            }
            if (empty($this->url)) {
                $this->url = get_config('wwwroot') . 'view/view.php?artefact=' 
                    . $this->artefact . '&id=' . $this->view;
            }
        } 
        else { // feedback on view.
            $this->subject = get_string('newfeedbackonview', 'activity');
            if (!$view = get_record('view', 'id', $this->view)) {
                throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
            }
            $userid = $view->owner;
            $this->subject .= ' ' .$view->title;
            if (empty($this->url)) {
                $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;
            }
        }
        if ($userid) {
            $this->users = activity_get_users('feedback', array($userid));
        } 
    }

    public function get_required_parameters() {
        return array('message', 'view');
    }
}

class ActivityTypeWatchlist extends ActivityType { 

    protected $view;

    public function __construct($data) { 
        parent::__construct($data); 
        $oldsubject = $this->subject;
        $this->subject = get_string('newwatchlistmessage', 'activity');
        if (!$viewinfo = get_record_sql('SELECT u.*, v.title FROM {usr} u
                                         JOIN {view} v ON v.owner = u.id
                                         WHERE v.id = ?', array($this->view))) {
            if (!empty($cron)) { // probably deleted already
                return;
            }
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }
        $this->message = $oldsubject . ' ' . $viewinfo->title;
        $sql = 'SELECT u.*, p.method, ' . $casturl . ' AS url
                    FROM {usr_watchlist_view} wv
                    JOIN {usr} u
                        ON wv.usr = u.id
                    LEFT JOIN {usr_activity_preference} p
                        ON p.usr = u.id
                    WHERE (p.activity = ? OR p.activity IS NULL)
                    AND wv.view = ?
               ';
        $this->users = get_records_sql_array($sql, 
                                       array(get_config('wwwroot') . 'view/view.php?id=' 
                                             . $this->view, 'watchlist', $this->view));
        foreach ($this->users as &$user) {
            $user->message = display_name($viewinfo, $user) . ' ' . $this->message;
        }
    }

    public function get_required_parameters() {
        return array('subject', 'view');
    }
}

class ActivityTypeNewview extends ActivityType { 

    protected $owner;
    protected $view;

    public function __construct($data) { 
        parent::__construct($data);
        if (!$viewinfo = get_record_sql('SELECT u.*, v.title FROM {usr} u
                                         JOIN {view} v ON v.owner = u.id
                                         WHERE v.id = ?', array($this->view))) {
            if (!empty($cron)) { //probably deleted already
                return;
            }
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }

        $this->message = get_string('newviewmessage', 'activity', $viewinfo->title);
        $this->subject = get_string('newviewsubject', 'activity');
        $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;

        // add users on friendslist or userlist...
        $this->users = activity_get_viewaccess_users($this->view, $this->owner, 'newview');
        // ick
        foreach ($this->users as &$user) {
            $user->message = display_name($viewinfo, $user) . ' ' . $this->message;
        }
    }
    
    public function get_required_parameters() {
        return array('owner', 'view');
    }
}

class ActivityTypeViewaccess extends ActivityType { 

    protected $view;
    protected $owner;
    protected $oldusers; // this can be empty though

    public function __construct($data) { 
        parent::__construct($data);
        if (!$viewinfo = get_record_sql('SELECT u.*, v.title FROM {usr} u
                                         JOIN {view} v ON v.owner = u.id
                                         WHERE v.id = ?', array($this->view))) {
            if (!empty($cron)) { // probably deleted already
                return;
            }
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }
        $this->message = get_string('newviewaccessmessage', 'activity')
            . ' "' . $viewinfo->title . '" ' . get_string('ownedby', 'activity');
        $this->subject = get_string('newviewaccesssubject', 'activity');
        $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;
        $this->users = array_diff_key(
            activity_get_viewaccess_users($this->view, $this->owner, 'viewaccess'), 
            $this->oldusers
        );

        // ick
        foreach ($this->users as &$user) {
            $user->message = $this->message . ' ' . display_name($viewinfo, $user);
        }
   }

    public function get_required_parameters() {
        return array('view', 'owner', 'oldusers');
    }
}

562
?>