/
mod_forumng_mail_list.php
377 lines (337 loc) · 14.1 KB
/
mod_forumng_mail_list.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
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
<?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/>.
require_once(dirname(__FILE__).'/mod_forumng.php');
/**
* Manages a list (based on a database recordset, so not all stored in memory)
* of posts which need to be emailed to users.
*
* The list only includes posts which are due to be mailed.
* It does not include:
* - Posts older than 48 hours (this is to avoid cron sending out a million old
* posts if it has never been run before), with exception of timed posts that
* have just become due
* - Posts which have been deleted, or old versions of edited posts.
* - Any posts which don't belong to a valid discussion, forum, and
* course-module
* - Posts which are timed and not yet due - even if user has permission to
* see them (it is more useful for the timed posts to be mailed out at the
* 'right time' even to these users, plus is easier)
*
* When used with shared forums, this will return multiple copies of each
* message (one from each shared forum including the original one).
* @package mod
* @subpackage forumng
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_forumng_mail_list {
/** Config flag used to prevent sending mails twice */
const PENDING_MARK_MAILED = 'pending_mark_mailed';
private $rs;
private $time;
private $forum, $discussion;
private $storedrecord;
private $postcount;
/**
* Creates the mail queue and runs query to obtain list of posts that should
* be mailed.
* @param bool $tracetimes True if it should call mtrace to display
* performance information
*/
function __construct($tracetimes) {
global $DB, $CFG;
$this->time = time();
$this->forum = null;
$this->discussion = null;
$this->storedrecord = null;
$this->postcount = 0;
// Check if an earlier run got aborted. In that case we mark all
// messages as mailed anyway because it's better to skip some than
// to send out double-posts.
if ($pending = get_config('forumng', $this->get_pending_flag_name())) {
$this->mark_mailed($pending);
}
// Note that we are mid-run
set_config($this->get_pending_flag_name(), $this->time, 'forumng');
list ($wheresql, $whereparams) = $this->get_query_where($this->time);
$querychunk = $this->get_query_from() . $wheresql;
$this->rs = $DB->get_recordset_sql($sql="
SELECT
".mod_forumng_utils::select_mod_forumng_fields('f').",
".mod_forumng_utils::select_discussion_fields('fd').",
".mod_forumng_utils::select_post_fields('discussionpost').",
".mod_forumng_utils::select_post_fields('fp').",
".mod_forumng_utils::select_post_fields('reply').",
".mod_forumng_utils::select_course_module_fields('cm').",
".mod_forumng_utils::select_context_fields('x').",
".mod_forumng_utils::select_username_fields('u', true).",
".mod_forumng_utils::select_username_fields('eu').",
".mod_forumng_utils::select_username_fields('replyu').",
".mod_forumng_utils::select_username_fields('replyeu').",
".mod_forumng_utils::select_course_fields('c').",
clonecm.id AS cloneid
$querychunk
ORDER BY
clonecm.course, f.id, fd.id, fp.id", $whereparams);
if (!empty($CFG->forumng_cronultradebug)) {
$easyread = mod_forumng_utils::debug_query_for_reading($sql, $whereparams);
mtrace("\n\n" . $easyread . "\n\n");
}
}
/**
* Obtains the next post in current forum.
* @param mod_forumng_post &$post Output variable: Receives the post object
* @param mod_forumng_post &$inreplyto Output variable: Receives the post this one was
* replying to
* @return bool True if a post could be retrieved, false if there are
* no more posts in this forum (call next_forum)
*/
function next_post(&$post, &$inreplyto) {
// Make sure we have a forum/discussion setup
if ($this->forum==null || $this->discussion==null) {
throw new coding_exception("Cannot call next_post when not inside
forum and discussion");
}
// Get record
if ($this->storedrecord) {
$record = $this->storedrecord;
$this->storedrecord = null;
} else {
if (!$this->rs->valid()) {
// End of the line. Mark everything as mailed
$this->mark_mailed($this->time);
$this->rs->close();
$this->rs = null;
$this->discussion = null;
return false;
}
$record = $this->rs->current();
$this->rs->next();
}
// If record discussion is not the same as current discussion
if ($record->fd_id != $this->discussion->get_id()
|| $record->cloneid != $this->forum->get_course_module_id()) {
$this->storedrecord = $record;
$this->discussion = null;
return false;
}
// Get post details including the joined user info
$postfields = mod_forumng_utils::extract_subobject($record, 'fp_');
mod_forumng_utils::copy_subobject($postfields, $record, 'u_');
mod_forumng_utils::copy_subobject($postfields, $record, 'eu_');
$post = new mod_forumng_post($this->discussion, $postfields);
if ($record->reply_id) {
$postfields = mod_forumng_utils::extract_subobject($record, 'reply_');
mod_forumng_utils::copy_subobject($postfields, $record, 'replyu_', 'u_');
mod_forumng_utils::copy_subobject($postfields, $record, 'replyeu_', 'eu_');
$inreplyto = new mod_forumng_post($this->discussion, $postfields);
} else {
$inreplyto = null;
}
$this->postcount++;
return true;
}
/**
* Obtains the next discussion in the list.
* @param mod_forumng_discussion $discussion Discussion
*/
function next_discussion(&$discussion) {
// Make sure we have a forum setup but no discussion
if ($this->forum==null) {
throw new coding_exception("Cannot call next_discussion when not inside
forum");
}
// Skip if required to get to new discussion
while ($this->discussion!=null) {
$this->next_post($post, $inreplyto);
}
// Get record
if ($this->storedrecord) {
$record = $this->storedrecord;
$this->storedrecord = null;
} else if (!$this->rs) {
// Already used entire list and closed recordset
$this->forum = null;
return false;
} else {
if (!$this->rs->valid()) {
// End of the line. Mark everything as mailed
$this->mark_mailed($this->time);
$this->rs->close();
$this->forum = null;
$this->rs = null;
return false;
}
$record = $this->rs->current();
$this->rs->next();
}
// If record forums are not the same as current forum
if ($record->cloneid != $this->forum->get_course_module_id()) {
$this->storedrecord = $record;
$this->forum = null;
return false;
}
// Store record and check discussion
$this->storedrecord = clone($record);
$discussionfields = mod_forumng_utils::extract_subobject($record, 'fd_');
$discussionfields->subject = $record->discussionpost_subject;
$discussion = new mod_forumng_discussion($this->forum,
$discussionfields, false, -1);
$this->discussion = $discussion;
return true;
}
/**
* Obtains the next forum from the list.
* @param mod_forumng &$forum Forum (out variable)
* @param object &$cm Course-module object (out variable)
* @param object &$context Context object (out variable)
* @param object &$course Course object (out variable)
*/
function next_forum(&$forum, &$cm, &$context, &$course) {
// Skip if required to get to new forum
while ($this->forum!=null) {
$this->next_discussion($discussion);
}
// Get record
if ($this->storedrecord) {
$record = $this->storedrecord;
$this->storedrecord = null;
} else if (!$this->rs) {
// Already used entire list and closed recordset
return false;
} else {
if (!$this->rs->valid()) {
// End of the line. Mark everything as mailed
$this->mark_mailed($this->time);
$this->rs->close();
$this->rs = null;
return false;
}
$record = $this->rs->current();
$this->rs->next();
}
// Set data
$this->storedrecord = clone($record);
$cm = mod_forumng_utils::extract_subobject($record, 'cm_');
$course = mod_forumng_utils::extract_subobject($record, 'c_');
context_helper::preload_from_record(
mod_forumng_utils::extract_subobject($record, 'x_'));
$context = context_module::instance($cm->id);
$forum = new mod_forumng($course, $cm, $context,
mod_forumng_utils::extract_subobject($record, 'f_'));
if ($forum->is_shared()) {
$forum->set_clone_reference($record->cloneid);
$cm = $forum->get_course_module();
$course = $forum->get_course();
$context = $forum->get_context();
}
$this->forum = $forum;
return true;
}
private function mark_mailed($time) {
global $DB;
list ($wheresql, $whereparams) = $this->get_query_where($time, 'forumng_posts');
$querychunk = $this->get_query_from() . $wheresql;
$before = microtime(true);
mtrace('Marking processed posts: ', '');
mod_forumng_utils::update_with_subquery_grrr_mysql("
UPDATE
{forumng_posts}
SET
mailstate = " . $this->get_target_mail_state() . "
WHERE
id %'IN'%", "SELECT fp.id $querychunk", $whereparams);
mtrace(round(microtime(true)-$before, 1) . 's.');
unset_config($this->get_pending_flag_name(), 'forumng');
}
public function get_post_count_so_far() {
return $this->postcount;
}
protected function get_pending_flag_name() {
return self::PENDING_MARK_MAILED;
}
protected function get_target_mail_state() {
return mod_forumng::MAILSTATE_MAILED;
}
/**
* Safety net is to prevent the forum sending out very old emails if cron
* is down for a long time, potentially causing a mail flood.
* @param int $time Current/base time (seconds)
* @return int Oldest time (seconds) of messages to process
*/
protected function get_safety_net($time) {
global $CFG;
$hours = isset($CFG->forumng_donotmailafter)
? $CFG->forumng_donotmailafter : 48;
return $time - $hours * 3600;
}
protected function get_query_from() {
return "
FROM
{forumng_posts} fp
INNER JOIN {user} u ON fp.userid = u.id
LEFT JOIN {user} eu ON fp.edituserid = eu.id
LEFT JOIN {forumng_posts} reply ON fp.parentpostid = reply.id
LEFT JOIN {user} replyu ON reply.userid = replyu.id
LEFT JOIN {user} replyeu ON reply.edituserid = replyeu.id
INNER JOIN {forumng_discussions} fd ON fp.discussionid = fd.id
INNER JOIN {forumng_posts} discussionpost ON fd.postid = discussionpost.id
INNER JOIN {forumng} f ON fd.forumngid = f.id
INNER JOIN {course_modules} cm ON f.id = cm.instance
AND cm.module = (SELECT id FROM {modules} WHERE name='forumng')
INNER JOIN {context} x ON x.instanceid = cm.id
INNER JOIN {course} c ON c.id = f.course
INNER JOIN {forumng} clonef
ON (clonef.originalcmid = cm.id OR (f.originalcmid IS NULL AND clonef.id = f.id))
INNER JOIN {course_modules} clonecm ON clonef.id = clonecm.instance
AND clonecm.module = (SELECT id FROM {modules} WHERE name='forumng')";
}
protected function get_query_where($time) {
global $CFG;
// We usually only mail out posts after a delay of maxeditingtime.
$mailtime = $time - $CFG->maxeditingtime;
// In case cron has not run for a while
$safetynet = $this->get_safety_net($time);
$sql = "
WHERE
-- Skip future posts (this is more relevant when using the set state
-- version of the query)...
fp.created < ?
-- Post must not have been mailed yet, also wait for editing delay if
-- not set to mailnow
AND ((fp.mailstate = " . mod_forumng::MAILSTATE_NOT_MAILED . "
AND fp.created < ?)
OR fp.mailstate = " . mod_forumng::MAILSTATE_NOW_NOT_MAILED . ")
-- Don't mail out really old posts (unless they were previously hidden)
AND (fp.created > ? OR fd.timestart > ?)
-- Group mode posts on non-group forums are not shown in the UI so let's
-- not mail them either
AND (fd.groupid IS NULL OR (c.groupmodeforce = 0 AND cm.groupmode <> 0) OR
(c.groupmodeforce <> 0 AND c.groupmode <> 0))
-- Discussion must meet time requirements
AND fd.timestart < ?
AND (fd.timeend = 0 OR fd.timeend > ?)
-- Post and discussion must not have been deleted and we're only looking
-- at original posts not edited old ones
AND fp.deleted = 0
AND fd.deleted = 0
AND fp.oldversion = 0
-- Context limitation
AND x.contextlevel = 70";
$params = array($time, $mailtime, $safetynet, $safetynet, $time, $time);
return array($sql, $params);
}
}