lib.php 4.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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
Penny Leach's avatar
Penny Leach committed
20
 * @subpackage notification-emaildigest
21
22
23
24
25
26
27
28
 * @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();

29
require_once(get_config('docroot') . 'notification/lib.php');
30

31
32
33
class PluginNotificationEmaildigest extends PluginNotification {

    public static function notify_user($user, $data) {
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
        $toinsert = new StdClass;
        $toinsert->type = $data->type;
        $toinsert->usr = $user->id;
        $toinsert->message = $data->message;
        $toinsert->ctime = db_format_timestamp(time());
        if (!empty($data->url)) {
            $toinsert->url = $data->url;
        }
        
        insert_record('notification_emaildigest_queue', $toinsert);
    }

    public static function get_cron() {
        $emaildigest = new StdClass;
        $emaildigest->callfunction = 'send_digest';
        $emaildigest->hour = '6';
        return array($emaildigest);
    }

    public static function send_digest() {
        $users = array();

        $sql = 'SELECT u.firstname,u.lastname,u.prefname,u.email,q.*,' . db_format_tsfield('ctime').'
                FROM ' . get_config('dbprefix') . 'usr u 
                    JOIN ' . get_config('dbprefix') . 'notification_emaildigest_queue q
                        ON q.usr = u.id
                ORDER BY usr,type,ctime';

        if ($tosend = get_records_sql($sql);
            foreach ($tosend as $queue) {
                if (!isset($users[$queue->usr])) {
                    $users[$queue->usr] = new StdClass;

                    $users[$queue->usr]->user = new StdClass;
                    $users[$queue->usr]->user->firstname = $queue->firstname;
                    $users[$queue->usr]->user->lastname  = $queue->lastname;
                    $users[$queue->usr]->user->prefname  = $queue->prefname;
                    $users[$queue->usr]->user->email     = $queue->email;
                    $users[$queue->usr]->user->id        = $queue->usr;
                    
                    $users[$queue->usr]->entries = array();
                }
                $queue->nicetype = get_string('type' . $queue->type, 'activity');
                $users[$queue->usr]->entries[$queue->id] = $queue;
            }
        }
        
        foreach ($users as $user) {
Penny Leach's avatar
Penny Leach committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            $subject = get_string('emailsubject', 'notification.emaildigest');
            $body = get_string('emailbodynoreply', 'notification.emaildigest');
            foreach ($user->entries as $entry) {
                $body .= get_string('type', 'activity') . $entry->nicetype 
                    . get_string('attime')  . $entry->ctime . "\n"
                    . $queue->message;
                if (!empty($queue->url)) {
                    $body .= "\n" . $queue->url;
                }
                $prefurl = get_config('wwwroot') . 'account/activityprefs.php';
                $body .= "\n\n" . get_string('emailbodyending', 'notification.emaildigest', $prefurl););
                
            }
            try {
                email_user($user->user, null, $subject, $body);
                // only delete them if the email succeeded! 
                delete_records_select('notification_emaildigest_queue', 
                                      'id IN (' . db_array_to_ph($entries) . ')', 
                                      array_keys($user->entries));
            } 
            catch (Exception $e) {
                // @todo
            }
105
        }
106
    }
107
108
109
}

?>