From edffca1558d14450f0139c5a7db3784fb2797c1b Mon Sep 17 00:00:00 2001
From: moodler
Date: Thu, 1 May 2003 12:56:03 +0000
Subject: [PATCH] Changes to make forum mailouts a little more robust, and
avoid duplicates I hope, at least the ones caused by overlapping cron jobs.
---
mod/forum/lib.php | 46 ++++++++++++++++++++++++++++------------------
1 file changed, 28 insertions(+), 18 deletions(-)
diff --git a/mod/forum/lib.php b/mod/forum/lib.php
index d8a8c58a33..538c46d15f 100644
--- a/mod/forum/lib.php
+++ b/mod/forum/lib.php
@@ -143,8 +143,9 @@ function forum_delete_instance($id) {
function forum_cron () {
-// Function to be run periodically according to the moodle cron
-// Finds all posts that have yet to be mailed out, and mails them
+/// Function to be run periodically according to the moodle cron
+/// Finds all posts that have yet to be mailed out, and mails them
+/// out to all subscribers
global $CFG, $USER;
@@ -152,12 +153,25 @@ function forum_cron () {
if ($posts = forum_get_unmailed_posts($cutofftime)) {
+ /// Mark them all now as being mailed. It's unlikely but possible there
+ /// might be an error later so that a post is NOT actually mailed out,
+ /// but since mail isn't crucial, we can accept this risk. Doing it now
+ /// prevents the risk of duplicated mails, which is a worse problem.
+
+ foreach ($posts as $key => $post) {
+ if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
+ echo "Error marking post id post->id as being mailed. This post will not be mailed.\n";
+ unset($posts[$key]);
+ }
+ }
+
$timenow = time();
foreach ($posts as $post) {
+ echo "\n";
print_string("processingpost", "forum", $post->id);
- echo " ... ";
+ echo "\n";
if (! $userfrom = get_record("user", "id", "$post->userid")) {
echo "Could not find user $post->userid\n";
@@ -184,10 +198,10 @@ function forum_cron () {
$mailcount=0;
foreach ($users as $userto) {
- $USER->lang = $userto->lang; // Affects the language of get_string
+ /// Override the language of get_string, so that mail is in correct language for the receiver.
+ $USER->lang = $userto->lang;
$canreply = forum_user_can_post($forum, $userto);
-
$by->name = "$userfrom->firstname $userfrom->lastname";
$by->date = userdate($post->modified, "", $userto->timezone);
$strbynameondate = get_string("bynameondate", "forum", $by);
@@ -225,19 +239,19 @@ function forum_cron () {
}
if ($userto->mailformat == 1) { // HTML
- $posthtml = "".
- "wwwroot/course/view.php?id=$course->id\">$course->shortname -> ".
- "wwwroot/mod/forum/index.php?id=$course->id\">$strforums -> ".
- "wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name";
+ $posthtml = "".
+ "wwwroot/course/view.php?id=$course->id\">$course->shortname -> ".
+ "wwwroot/mod/forum/index.php?id=$course->id\">$strforums -> ".
+ "wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name";
if ($discussion->name == $forum->name) {
- $posthtml .= "
";
+ $posthtml .= "
";
} else {
- $posthtml .= " -> wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name
";
+ $posthtml .= " -> wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name";
}
$posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
if ($canunsubscribe) {
- $posthtml .= "\n
wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."
";
+ $posthtml .= "\n
wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."
";
}
} else {
@@ -245,18 +259,14 @@ function forum_cron () {
}
if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
- echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
+ echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email) .. not trying again.\n";
} else {
$mailcount++;
}
}
- echo "mailed to $mailcount users ...";
- }
- if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
- echo "Could not update the mailed field for id $post->id\n";
+ echo ".... mailed to $mailcount users.\n";
}
- echo "\n";
}
}
--
2.39.5