activity.php 27.3 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
4
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 *
 * @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
37
function activity_occurred($activitytype, $data, $plugintype=null, $pluginname=null) {
    $at = activity_locate_typerecord($activitytype, $plugintype, $pluginname);
38
39
    if (!empty($at->delay)) {
        $delayed = new StdClass;
40
        $delayed->type = $at->id;
41
42
43
44
45
        $delayed->data = serialize($data);
        $delayed->ctime = db_format_timestamp(time());
        insert_record('activity_queue', $delayed);
    }
    else {
46
        handle_activity($at, $data);
47
    }
48
49
50
51
52
53
54
55
56
}

/** 
 * 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
57
58
59
 * @param mixed $data must contain message to save.
 * it can also contain url.
 * each activity type has different requirements of $data - 
60
61
62
63
64
65
66
 *  - <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
67
 *  - <b>watchlist (view) </b> must contain $view (id of view) as $message
68
 *  - <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.
69
 */
70
function handle_activity($activitytype, $data, $cron=false) {
71
    $data = (object)$data;
72
    $activitytype = activity_locate_typerecord($activitytype);
73

74
75
76
77
78
79
80
    $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);
81
    }
82

83
    $activity = new $classname($data, $cron);
84
    if (!$activity->any_users()) {
85
86
        return;
    }
87

88
    $activity->notify_users();
89
90
91
}

/**
92
93
 * this function returns an array of users who subsribe to a particular activitytype 
 * including the notification method they are using to subscribe to it.
94
 *
95
 * @param int $activitytype the id of the activity type
96
97
98
 * @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
99
 * @param array $admininstitutions list of institution names to get admins for
100
101
 * @return array of users
 */
102
103
function activity_get_users($activitytype, $userids=null, $userobjs=null, $adminonly=false,
                            $admininstitutions = array()) {
104
    $values = array($activitytype);
105
106
    $sql = '
        SELECT
107
108
            u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff, 
            p.method, ap.value AS lang
109
110
111
112
113
114
        FROM {usr} u
        LEFT JOIN {usr_activity_preference} p
            ON p.usr = u.id ' . (empty($admininstitutions) ? '' : '
        LEFT OUTER JOIN {usr_institution} ui
            ON (u.id = ui.usr
                AND ui.institution IN ('.join(',',array_map('db_quote',$admininstitutions)).'))') . '
115
116
        LEFT OUTER JOIN {usr_account_preference} ap
            ON (ap.usr = u.id AND ap.field = \'lang\')
117
        WHERE (p.activity = ? ' . ($adminonly ? '' : ' OR p.activity IS NULL') . ')';
118
119
120
121
122
123
124
125
    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);
    }
126
127
128
    if (!empty($admininstitutions)) {
        $sql .= '
        GROUP BY
129
130
            u.id, u.username, u.firstname, u.lastname, u.preferredname, u.email, u.admin, u.staff,
            p.method, ap.value
131
132
133
134
        HAVING (u.admin = 1 OR SUM(ui.admin) > 0)';
    } else if ($adminonly) {
        $sql .= ' AND u.admin = 1';
    }
135
    return get_records_sql_array($sql, $values);
136
137
}

138
139
140
141
142
143
/**
 * 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);
144
145
    $haveemail = in_array('email', array_map(create_function('$a', 'return $a->name;'),
                                             plugins_installed('notification')));
146
    foreach ($activitytypes as $type) {
147
148
149
150
151
        if ($type->name == 'institutionmessage' && $haveemail) {
            $method = 'email';
        } else {
            $method = 'internal';
        }
152
153
        insert_record('usr_activity_preference', (object)array(
            'usr' => $user_id,
Richard Mansfield's avatar
Richard Mansfield committed
154
            'activity' => $type->id,
155
            'method' => $method,
156
        ));
157
158
159
160
    }
    
}

161
162
163
164
function activity_add_admin_defaults($userids) {
    $activitytypes = get_records_array('activity_type', 'admin', 1);
    foreach ($activitytypes as $type) {
        foreach ($userids as $id) {
165
            if (!record_exists('usr_activity_preference', 'usr', $id, 'activity', $type->id)) {
166
167
                insert_record('usr_activity_preference', (object)array(
                    'usr' => $id,
168
                    'activity' => $type->id,
169
170
171
172
173
174
175
176
                    'method' => 'internal',
                ));
            }
        }
    }
}


177
178
function activity_process_queue() {

179
    db_begin();
180
    if ($toprocess = get_records_array('activity_queue')) {
181
182
183
        // Hack to avoid duplicate watchlist notifications on the same view
        $watchlist = activity_locate_typerecord('watchlist');
        $viewsnotified = array();
184
        foreach ($toprocess as $activity) {
185
186
187
188
189
190
191
192
            $data = unserialize($activity->data);
            if ($activity->type == $watchlist->id && !empty($data->view)) {
                if (isset($viewsnotified[$data->view])) {
                    continue;
                }
                $viewsnotified[$data->view] = true;
            }
            handle_activity($activity->type, $data, true);
193
194
195
196
197
        }
        delete_records('activity_queue');
    }
    db_commit();
}
198

199
function activity_get_viewaccess_users($view, $owner, $type) {
200
    $type = activity_locate_typerecord($type);
201
    $sql = 'SELECT userid, u.*, p.method, ap.value AS lang
202
203
                FROM (
                SELECT (CASE WHEN usr1 = ? THEN usr2 ELSE usr1 END) AS userid 
204
205
206
                    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
207
                        WHERE (usr1 = ? OR usr2 = ?) AND vu.accesstype = ? AND v.id = ? 
208
                UNION SELECT usr AS userid 
209
                    FROM {view_access_usr} u 
210
                        WHERE u.view = ?
211
                UNION SELECT m.member 
212
                    FROM {group_member} m
213
214
215
                    JOIN {view_access_group} vg ON vg.group = m.group
                    JOIN {group} g ON (g.id = vg.group AND g.deleted = 0)
                        WHERE vg.view = ? AND (vg.tutoronly = ? OR m.tutor = ?)
216
217
218
                UNION SELECT g.owner
                    FROM {group} g
                    JOIN {view_access_group} ag ON ag.group = g.id
219
                        WHERE ag.view = ? AND g.deleted = 0
220
                ) AS userlist
221
222
                JOIN {usr} u ON u.id = userlist.userid
                LEFT JOIN {usr_activity_preference} p ON p.usr = u.id
223
                LEFT OUTER JOIN {usr_account_preference} ap ON (ap.usr = u.id AND ap.field = \'lang\')
224
            WHERE p.activity = ?';
225
    $values = array($owner, $owner, $owner, 'friends', $view, $view, $view, 0, 1, $view, $type->id);
226
    if (!$u = get_records_sql_assoc($sql, $values)) {
227
228
229
        $u = array();
    }
    return $u;
230
231
}

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
function activity_locate_typerecord($activitytype, $plugintype=null, $pluginname=null) {
    if (is_object($activitytype)) {
        return $activitytype;
    }
    if (is_numeric($activitytype)) {
        $at = get_record('activity_type', 'id', $activitytype);
    }
    else {
        if (empty($plugintype) && empty($pluginname)) {
            $at = get_record_select('activity_type', 
                'name = ? AND plugintype IS NULL AND pluginname IS NULL', 
                array($activitytype));
        } 
        else {
            $at = get_record('activity_type', 'name', $activitytype, 'plugintype', $plugintype, 'pluginname', $pluginname);
        }
    }
    if (empty($at)) {
        throw new Exception("Invalid activity type $activitytype");
    }
    return $at;
}
254
255
256
257
258
259
260
261

/** activity type classes **/
abstract class ActivityType {
    
    protected $subject;
    protected $message;
    protected $users = array();
    protected $url;
262
    protected $id;
263
264
    protected $type;
    protected $activityname;
265
    protected $cron;
266
267
268
269
270
271
272
273
   
    public function get_id() {
        if (!isset($this->id)) {
            $tmp = activity_locate_typerecord($this->get_type());
            $this->id = $tmp->id;
        }
        return $this->id;
    }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
    
    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;
    }

288
289
    public function __construct($data, $cron=false) {
        $this->cron = $cron;
290
291
        $this->set_parameters($data);
        $this->ensure_parameters();
292
        $this->activityname = strtolower(substr(get_class($this), strlen('ActivityType')));
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    }

    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); 
    }
314
315
316
317

    public function get_message($user) {
        return $this->message;
    }
318
        
319
320
321
    public function get_subject($user) {
        return $this->subject;
    }
322
323

    abstract function get_required_parameters();
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

    public function notify_user($user) {
        $userdata = $this->to_stdclass();
        // some stuff gets overridden by user specific stuff
        if (!empty($user->url)) {
            $userdata->url = $user->url;
        }
        if (empty($user->lang) || $user->lang == 'default') {
            $user->lang = get_config('lang');
        }
        $userdata->message = $this->get_message($user);
        $userdata->subject = $this->get_subject($user);
        if (empty($user->method)) {
            $user->method = 'internal';
        }
        if ($user->method != 'internal') {
            $method = $user->method;
            safe_require('notification', $method, 'lib.php', 'require_once');
            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....
            }
        }
        // always do internal
        call_static_method('PluginNotificationInternal', 'notify_user', $user, $userdata);
    }

    public function notify_users() {
        safe_require('notification', 'internal', 'lib.php', 'require_once');
        $this->type = $this->get_id();

        foreach ($this->get_users() as $user) {
            $this->notify_user($user);
        }
    }
363
364
365
366
367
}


abstract class ActivityTypeAdmin extends ActivityType { 

368
369
    public function __construct($data, $cron=false) {
        parent::__construct($data, $cron);
370
        $this->users = activity_get_users($this->get_id(), null, null, true);
371
372
373
374
375
376
377
378
379
    }
}

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

380
381
    function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
382
383
384
385
        if (!empty($this->userfrom)) {
            $this->url = get_config('wwwroot') . 'user/view.php?id=' . $this->userfrom;
        }
    }
386
387
388
389
390
391
392
393
394
395
396

    function get_subject($user) {
        return get_string_from_language($user->lang, 'newcontactus', 'activity');
    }

    function get_message($user) {
        return get_string_from_language($user->lang, 'newcontactusfrom', 'activity') . ' ' . $this->fromname 
            . '<' . $this->fromemail .'>' . (isset($this->subject) ? ': ' . $this->subject : '')
            . "\n\n" . $this->message;
    }

397
398
399
400
401
402
403
404
405
406
    public function get_required_parameters() {
        return array('message', 'fromname', 'fromemail');
    }
}

class ActivityTypeObjectionable extends ActivityTypeAdmin {

    protected $view;
    protected $artefact;

407
408
    function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
409
410
411
412
413
414
415
416
417
        if (empty($this->artefact)) {
            $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;
        }
        else {
            $this->url = get_config('wwwroot') . 'view/artefact.php?artefact=' . $this->artefact . '&view=' . $this->view;
        }
    }

    function get_subject($user) {
418
419
420
421
        if (!$viewtitle = get_field('view', 'title', 'id', $this->view)) {
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }
        if (empty($this->artefact)) {
422
423
            return get_string_from_language($user->lang, 'objectionablecontentview', 'activity') 
                . ' ' . get_string_from_language($user->lang, 'onview', 'activity') . ' ' . $viewtitle;
424
425
426
427
428
        }
        else {
            if (!$artefacttitle = get_field('artefact', 'title', 'id', $this->artefact)) {
                throw new ArtefactNotFoundException(get_string('artefactnotfound', 'error', $this->artefact));
            }
429
430
            return get_string_from_language($user->lang, 'objectionablecontentartefact', 'activity') 
                . ' '  . get_string_from_language($user->lang, 'onartefact', 'activity') . ' ' . $artefacttitle;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
        }
    }

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

}

class ActivityTypeVirusRepeat extends ActivityTypeAdmin {

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

446
447
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
448
449
450
    }

    public function get_subject($user) {
451
        $userstring = $this->username . ' (' . $this->fullname . ') (userid:' . $this->userid . ')' ;
452
453
454
455
456
        return get_string_from_language($user->lang, 'virusrepeatsubject', 'mahara', $userstring);
    }

    public function get_message($user) {
        return get_string_from_language($user->lang, 'virusrepeatmessage');
457
458
459
460
461
462
463
464
465
    }

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

class ActivityTypeVirusRelease extends ActivityTypeAdmin {

466
467
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
468
469
470
471
472
473
474
475
476
    }

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

class ActivityTypeMaharamessage extends ActivityType {

477
478
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
479
        $this->users = activity_get_users($this->get_id(), $this->users);
480
481
482
483
484
485
486
    }

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

487
488
489
490
491
492
493
class ActivityTypeInstitutionmessage extends ActivityType {

    protected $messagetype;
    protected $institution;
    protected $username;
    protected $fullname;

494
495
    public function __construct($data, $cron=false) {
        parent::__construct($data, $cron);
496
497
498
499
500
501
502
503
504
505
        if ($this->messagetype == 'request') {
            $this->url = get_config('wwwroot') . 'admin/users/institutionusers.php';
            $this->users = activity_get_users($this->get_id(), null, null, null,
                                              array($this->institution->name));
        } else if ($this->messagetype == 'invite') {
            $this->url = get_config('wwwroot') . 'account/index.php';
            $this->users = activity_get_users($this->get_id(), $this->users);
        }
    }

506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    public function get_subject($user) {
        if ($this->messagetype == 'request') {
            $userstring = $this->fullname . ' (' . $this->username . ')';
            return get_string_from_language($user->lang, 'institutionrequestsubject', 'activity', $userstring, 
                                            $this->institution->displayname);
        } else if ($this->messagetype == 'invite') {
            return get_string_from_language($user->lang, 'institutioninvitesubject', 'activity', 
                                            $this->institution->displayname);
        }
    }

    public function get_message($user) {
        if ($this->messagetype == 'request') {
            return get_string_from_language($user->lang, 'institutionrequestmessage', 'activity');
        } else if ($this->messagetype == 'invite') {
            return get_string_from_language($user->lang, 'institutioninvitemessage', 'activity');
        }
    }

525
526
527
528
529
    public function get_required_parameters() {
        return array('messagetype', 'institution');
    }
}

530
531
532
533
534
class ActivityTypeUsermessage extends ActivityType { 

    protected $userto;
    protected $userfrom;

535
536
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
537
        $this->users = activity_get_users($this->get_id(), array($this->userto));
538
539
540
541
542
        if (empty($this->url)) {
            $this->url = get_config('wwwroot') . 'user/view.php?id=' . $this->userfrom;
        }
    } 

543
544
    public function get_subject($user) {
        if (empty($this->subject)) {
Clare Lenihan's avatar
Clare Lenihan committed
545
            return get_string_from_language($user->lang, 'newusermessage', 'group',
546
547
548
549
550
                                            display_name($this->userfrom));
        }
        return $this->subject;
    }

551
552
553
554
555
556
557
558
559
560
561
    public function get_required_parameters() {
        return array('message', 'userto', 'userfrom');
    }
    
}

class ActivityTypeFeedback extends ActivityType { 

    protected $view;
    protected $artefact;

562
563
564
    private $viewrecord;
    private $artefactinstance;

565
566
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
567

568
        if (!empty($this->artefact)) { // feedback on artefact
569
            $userid = null;
570
571
572
573
            require_once(get_config('docroot') . 'artefact/lib.php');
            $this->artefactinstance = artefact_instance_from_id($this->artefact);
            if ($this->artefactinstance->feedback_notify_owner()) {
                $userid = $this->artefactinstance->get('owner');
574
575
            }
            if (empty($this->url)) {
576
577
                $this->url = get_config('wwwroot') . 'view/artefact.php?artefact=' 
                    . $this->artefact . '&view=' . $this->view;
578
579
580
            }
        } 
        else { // feedback on view.
581
            if (!$this->viewrecord = get_record('view', 'id', $this->view)) {
582
583
                throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
            }
584
            $userid = $this->viewrecord->owner;
585
586
587
588
589
            if (empty($this->url)) {
                $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;
            }
        }
        if ($userid) {
590
            $this->users = activity_get_users($this->get_id(), array($userid));
591
592
593
        } 
    }

594
595
596
597
598
599
600
601
602
603
604
    public function get_subject($user) {
        if (!empty($this->artefact)) { // feedback on artefact
            return get_string_from_language($user->lang, 'newfeedbackonartefact', 'activity')
                . ' ' . $this->artefactinstance->get('title');
        }
        else {
            return get_string_from_language($user->lang, 'newfeedbackonview', 'activity')
                . ' ' . $this->viewrecord->title;
        }
    }

605
606
607
608
609
610
611
612
613
    public function get_required_parameters() {
        return array('message', 'view');
    }
}

class ActivityTypeWatchlist extends ActivityType { 

    protected $view;

614
615
    private $viewinfo;

616
617
    public function __construct($data, $cron) { 
        parent::__construct($data, $cron); 
618
619
        //$oldsubject = $this->subject;
        if (!$this->viewinfo = get_record_sql('SELECT u.*, v.title FROM {usr} u
620
621
                                         JOIN {view} v ON v.owner = u.id
                                         WHERE v.id = ?', array($this->view))) {
622
            if (!empty($this->cron)) { // probably deleted already
623
624
625
626
                return;
            }
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }
627
628
629
630
631
        // mysql compatibility (sigh...)
        $casturl = 'CAST(? AS TEXT)';
        if (get_config('dbtype') == 'mysql') {
            $casturl = 'CAST(? AS CHAR)'; // note, NOT varchar
        }
632
        $sql = 'SELECT u.*, p.method, ap.value AS lang, ' . $casturl . ' AS url
633
634
635
636
637
                    FROM {usr_watchlist_view} wv
                    JOIN {usr} u
                        ON wv.usr = u.id
                    LEFT JOIN {usr_activity_preference} p
                        ON p.usr = u.id
638
639
                    LEFT OUTER JOIN {usr_account_preference} ap
                        ON (ap.usr = u.id AND ap.field = \'lang\')
640
641
642
643
644
                    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=' 
645
                                             . $this->view, $this->get_id(), $this->view));
646
647
648
649
650
651
652
    }

    public function get_subject($user) {
        return get_string_from_language($user->lang, 'newwatchlistmessage', 'activity');
    }

    public function get_message($user) {
653
654
        return get_string_from_language($user->lang, 'newwatchlistmessageview', 'activity', 
                                        display_name($this->viewinfo, $user), $this->viewinfo->title);
655
656
657
    }

    public function get_required_parameters() {
658
        return array('view');
659
660
661
662
663
664
665
666
    }
}

class ActivityTypeNewview extends ActivityType { 

    protected $owner;
    protected $view;

667
668
    private $viewinfo;

669
670
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
671
        if (!$this->viewinfo = get_record_sql('SELECT u.*, v.title FROM {usr} u
672
673
                                         JOIN {view} v ON v.owner = u.id
                                         WHERE v.id = ?', array($this->view))) {
674
            if (!empty($this->cron)) { //probably deleted already
675
676
677
678
679
680
681
682
                return;
            }
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }

        $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;

        // add users on friendslist or userlist...
683
        $this->users = activity_get_viewaccess_users($this->view, $this->owner, $this->get_id()); 
684
685
686
687
688
689
690
    }

    public function get_subject($user) {
        return get_string_from_language($user->lang, 'newviewsubject', 'activity');
    }
    
    public function get_message($user) {
691
692
        return get_string_from_language($user->lang, 'newviewmessage', 'activity', 
                                        display_name($this->viewinfo, $user), $this->viewinfo->title);
693
694
695
696
697
698
699
700
701
702
703
704
705
    }
    
    public function get_required_parameters() {
        return array('owner', 'view');
    }
}

class ActivityTypeViewaccess extends ActivityType { 

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

706
707
    private $viewinfo;

708
709
    public function __construct($data, $cron=false) { 
        parent::__construct($data, $cron);
710
        if (!$this->viewinfo = get_record_sql('SELECT u.*, v.title FROM {usr} u
711
712
                                         JOIN {view} v ON v.owner = u.id
                                         WHERE v.id = ?', array($this->view))) {
713
            if (!empty($this->cron)) { // probably deleted already
714
715
716
717
718
719
                return;
            }
            throw new ViewNotFoundException(get_string('viewnotfound', 'error', $this->view));
        }
        $this->url = get_config('wwwroot') . 'view/view.php?id=' . $this->view;
        $this->users = array_diff_key(
720
            activity_get_viewaccess_users($this->view, $this->owner, $this->get_id()),
721
722
            $this->oldusers
        );
723
    }
724

725
726
727
728
729
    public function get_subject($user) {
        return get_string('newviewaccesssubject', 'activity');
    }
    
    public function get_message($user) {
730
731
        return get_string_from_language($user->lang, 'newviewaccessmessage', 'activity',
                                        $this->viewinfo->title, display_name($this->viewinfo, $user));
732
733
    }
    
734
735
736
737
738
    public function get_required_parameters() {
        return array('view', 'owner', 'oldusers');
    }
}

739
740
741
742
743
744
abstract class ActivityTypePlugin extends ActivityType {

    abstract public function get_plugintype();

    abstract public function get_pluginname();

745
746
747
748
749
    public function get_type() {
        $prefix = 'ActivityType' . $this->get_plugintype() . $this->get_pluginname();
        return strtolower(substr(get_class($this), strlen($prefix)));
    }

750
751
752
753
754
755
756
757
758
    public function get_id() {
        if (!isset($this->id)) {
            $tmp = activity_locate_typerecord($this->get_type(), $this->get_plugintype(), $this->get_pluginname());
            $this->id = $tmp->id;
        }
        return $this->id;
    }
}

759
?>