lib.php 4.7 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
        $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();
55
        $sitename = get_config('sitename');
56

57
        $sql = 'SELECT q.id, u.firstname,u.lastname,u.preferredname,u.email,q.*,' . db_format_tsfield('ctime').'
58
59
60
                FROM ' . get_config('dbprefix') . 'usr u 
                    JOIN ' . get_config('dbprefix') . 'notification_emaildigest_queue q
                        ON q.usr = u.id
Penny Leach's avatar
Penny Leach committed
61
                ORDER BY usr,type,q.ctime';
62

63
        if ($tosend = get_records_sql_array($sql, array())) {
64
65
66
            foreach ($tosend as $queue) {
                if (!isset($users[$queue->usr])) {
                    $users[$queue->usr] = new StdClass;
Penny Leach's avatar
Penny Leach committed
67
                    
68
                    $users[$queue->usr]->user = new StdClass;
69
70
71
72
73
                    $users[$queue->usr]->user->firstname     = $queue->firstname;
                    $users[$queue->usr]->user->lastname      = $queue->lastname;
                    $users[$queue->usr]->user->preferredname = $queue->preferredname;
                    $users[$queue->usr]->user->email         = $queue->email;
                    $users[$queue->usr]->user->id            = $queue->usr;
74
75
76
77
78
79
80
81
                    
                    $users[$queue->usr]->entries = array();
                }
                $queue->nicetype = get_string('type' . $queue->type, 'activity');
                $users[$queue->usr]->entries[$queue->id] = $queue;
            }
        }
        foreach ($users as $user) {
82
83
            $subject = get_string('emailsubject', 'notification.emaildigest', $sitename);
            $body = get_string('emailbodynoreply', 'notification.emaildigest', $sitename);
Penny Leach's avatar
Penny Leach committed
84
85
            foreach ($user->entries as $entry) {
                $body .= get_string('type', 'activity') . $entry->nicetype 
86
87
88
89
90
91
92
                    . ' ' . get_string('attime', 'activity')  . ' ' . format_date($entry->ctime) . "\n";
                if (!empty($queue->subject)) {
                    $body .= get_string('subject') . $queue->subject ."\n";
                }
                if (!empty($queue->message)) {
                    $body .= "\n" . $queue->message;
                }
Penny Leach's avatar
Penny Leach committed
93
94
95
                if (!empty($queue->url)) {
                    $body .= "\n" . $queue->url;
                }
Penny Leach's avatar
Penny Leach committed
96
                $body .= "\n\n";
Penny Leach's avatar
Penny Leach committed
97
            }
98
            $prefurl = get_config('wwwroot') . 'account/activity/preferences/';
Penny Leach's avatar
Penny Leach committed
99
            $body .= "\n\n" . get_string('emailbodyending', 'notification.emaildigest', $prefurl);
Penny Leach's avatar
Penny Leach committed
100
101
            try {
                email_user($user->user, null, $subject, $body);
Penny Leach's avatar
Penny Leach committed
102
103
                //only delete them if the email succeeded! 
                $in = db_array_to_ph($user->entries);
Penny Leach's avatar
Penny Leach committed
104
                delete_records_select('notification_emaildigest_queue', 
Penny Leach's avatar
Penny Leach committed
105
                                      'id IN (' . implode(', ', $in) . ')', 
Penny Leach's avatar
Penny Leach committed
106
107
108
109
110
                                      array_keys($user->entries));
            } 
            catch (Exception $e) {
                // @todo
            }
111
        }
112
    }
113
114
115
}

?>