forked from isuru89/moodle-local_reminders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminder.class.php
245 lines (208 loc) · 8.99 KB
/
reminder.class.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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.
//
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Abstract class for reminder object.
*
* @package local
* @subpackage reminders
* @copyright 2012 Isuru Madushanka Weerarathna
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class reminder {
/**
* @var int number of days in advance to actual event.
*/
protected $aheaddays;
/**
* @var int indicates immediate sending of message as a notification.
*/
protected $notification = 1;
/**
*
* @var object event object correspond to this reminder.
*/
protected $event;
protected $tbodycssstyle = 'width:100%;font-family:Tahoma,Arial,Sans-serif;border-width:1px 2px 2px 1px;border:1px Solid #ccc';
protected $titlestyle = 'padding:0 0 6px 0;margin:0;font-family:Arial,Sans-serif;font-size:16px;font-weight:bold;color:#222';
protected $footerstyle = 'background-color:#f6f6f6;color:#888;border-top:1px Solid #ccc;font-family:Arial,Sans-serif;font-size:11px';
/**
*
* @var object cahced reminder message object. This will be reused for other users too.
*/
public $eventobject;
public function __construct($event, $aheaddays = 1) {
$this->event = $event;
$this->aheaddays = $aheaddays;
}
/**
* Gets the header content of the e-mail message.
*/
protected function get_html_header() {
return html_writer::tag('head', '');
}
/**
* Gets the footer content of the e-mail message.
*/
protected function get_html_footer() {
global $CFG;
$footer = html_writer::start_tag('tr');
$footer .= html_writer::start_tag('td', array('style' => $this->footerstyle, 'colspan' => 2));
$footer .= get_string('reminderfrom', 'local_reminders').' ';
$footer .= html_writer::link($CFG->wwwroot.'/calendar/index.php', 'Moodle Calendar', array('target' => '_blank'));
$footer .= html_writer::end_tag('td').html_writer::end_tag('tr');
return $footer;
}
/**
* Returns the correct link for the calendar event.
*
* @return string complete url for the event
*/
protected function generate_event_link() {
$params = array('view' => 'day', 'cal_d' => date('j', $this->event->timestart),
'cal_m' => date('n', $this->event->timestart), 'cal_y' => date('Y', $this->event->timestart));
$calurl = new moodle_url('/calendar/view.php', $params);
$calurl->set_anchor('event_'.$this->event->id);
return $calurl->out(false);
}
/**
* This function formats the due time of the event appropiately. If this event
* has a duration then formatted time will be [starttime]-[endtime].
*
* @param object $user user object
* @return string formatted time string
*/
protected function format_event_time_duration($user) {
$followedtimeformat = get_string('strftimedatetime', 'langconfig');
$tzone = 99;
if (isset($user) && !empty($user)) {
$tzone = $user->timezone;
}
$formattedtime = userdate($this->event->timestart, '', $tzone);
$sdate = usergetdate($this->event->timestart, $tzone);
if ($this->event->timeduration > 0) {
$ddate = usergetdate($this->event->timestart + $this->event->timeduration, $tzone);
if ($sdate['year'] == $ddate['year'] && $sdate['mon'] == $ddate['mon'] && $sdate['mday'] == $ddate['mday']) {
$followedtimeformat = get_string('strftimetime', 'langconfig');
}
$formattedtime .= ' - '.userdate($this->event->timestart + $this->event->timeduration, $followedtimeformat, $tzone);
}
return $formattedtime;
}
/**
* This function setup the corresponding message provider for each
* reminder type. It would be called everytime at the constructor.
*
* @return string Message provider name
*/
protected abstract function get_message_provider();
/**
* Generates a message content as a HTML. Suitable for email messages.
*
* @param object $event The event object
* @return string Message content as HTML text.
*/
public abstract function get_message_html($user=null);
/**
* Generates a message content as a plain-text. Suitable for popup messages.
*
* @param object $event The event object
* @return string Message content as plain-text.
*/
public abstract function get_message_plaintext($user=null);
/**
* Generates a message title for the reminder. Used for all message types.
*
* @return string Message title as a plain-text.
*/
public abstract function get_message_title();
/**
* Gets an array of custom headers for the reminder message, specially
* for e-mails. For e-mails they will be easier to track when
* several e-mail reminders are received for a particular event. <br>
* If no header is wanted, just simply returns an empty array.
*
* @return array array of strings containing header attributes.
*/
public function get_custom_headers() {
global $CFG;
$urlinfo = parse_url($CFG->wwwroot);
$hostname = $urlinfo['host'];
mtrace(" [Local Reminders] host [ $hostname ]");
return array('Message-ID: <moodlereminder'.$this->event->id.'@'.$hostname.'>');
}
/**
* Creates the final reminder message object from given information.
*
* @param object $name impersonated user for sending messages. This
* name will display in 'from' field in every reminder message.
*
* @return object a message object which will be sent to the messaging API
*/
public function create_reminder_message_object($admin=null) {
global $CFG;
if ($admin == null) {
$admin = get_admin();
//$admin->maildisplay = false;
}
$contenthtml = $this->get_message_html();
$titlehtml = $this->get_message_title();
$subjectprefix = get_string('titlesubjectprefix', 'local_reminders');
if (isset($CFG->local_reminders_messagetitleprefix) && !empty($CFG->local_reminders_messagetitleprefix)) {
$subjectprefix = $CFG->local_reminders_messagetitleprefix;
}
$cheaders = $this->get_custom_headers();
if (!empty($cheaders)) {
$admin->customheaders = $cheaders;
}
$eventdata = new stdClass();
$eventdata->component = 'local_reminders'; // plugin name
$eventdata->name = $this->get_message_provider(); // message interface name
$eventdata->userfrom = $admin;
//$eventdata->userto = 3;
$eventdata->subject = '['.$subjectprefix.'] '.$titlehtml; // message title
$eventdata->fullmessage = $this->get_message_plaintext();
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = $contenthtml;
$eventdata->smallmessage = $titlehtml . ' - ' . $contenthtml;
$eventdata->notification = $this->notification;
// save created object with reminder object
$this->eventobject = $eventdata;
return $eventdata;
}
/**
* Assign user which the reminder message is sent to
*
* @param object $user user object (id field must contain)
* @param boolean $refreshcontent indicates whether content of message should
* be refresh based on given user
*
* @return event object
*/
public function set_sendto_user($user, $refreshcontent=true) {
if (!isset($this->eventobject) || empty($this->eventobject)) {
$this->create_reminder_message_object();
}
$this->eventobject->userto = $user;
if ($refreshcontent) {
$contenthtml = $this->get_message_html($user);
$titlehtml = $this->get_message_title();
$this->eventobject->fullmessagehtml = $contenthtml;
$this->eventobject->smallmessage = $titlehtml . ' - ' . $contenthtml;
$this->eventobject->fullmessage = $this->get_message_plaintext($user);
}
return $this->eventobject;
}
}