Hello,
Coming to paragraph 3 of yours, here is the code:
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
/// out to all subscribers
global $CFG, $USER;
if (!empty($USER)) { // Remember real USER account if necessary
$realuser = $USER;
}
$cutofftime = time() - $CFG->maxeditingtime;
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 "\n";
if (! $userfrom = get_record("user", "id", "$post->userid")) {
echo "Could not find user $post->userid\n";
continue;
}
$userfrom->precedence = "bulk"; // This gets added to the email header
if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
echo "Could not find discussion $post->discussion\n";
continue;
}
if (! $forum = get_record("forum", "id", "$discussion->forum")) {
echo "Could not find forum $discussion->forum\n";
continue;
}
if (! $course = get_record("course", "id", "$forum->course")) {
echo "Could not find course $forum->course\n";
continue;
}
if (!empty($course->lang)) {
$CFG->courselang = $course->lang;
} else {
unset($CFG->courselang);
}
$groupmode = false;
if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
if (!$group = get_record("groups", "id", $discussion->groupid)) { // Can't find group
continue; // Be safe and don't send it to anyone
}
}
} else {
$cm->id = 0;
}
if ($users = forum_subscribed_users($course, $forum)) {
$canunsubscribe = ! forum_is_forcesubscribed($forum->id);
$mailcount=0;
$errorcount=0;
foreach ($users as $userto) {
if ($groupmode) { // Look for a reason not to send this email
if (!isteacheredit($course->id, $userto->id)) {
if (!ismember($group->id, $userto->id)) {
continue;
}
}
}
/// GWD: reset timelimit so that script does not get timed out when posting to many users
@set_time_limit(0);
/// Override the language and timezone of the "current" user, so that
/// mail is customised for the receiver.
$USER->lang = $userto->lang;
$USER->timezone = $userto->timezone;
$canreply = forum_user_can_post($forum, $userto);
$by->name = fullname($userfrom, isteacher($course->id, $userto->id));
$by->date = userdate($post->modified, "", $userto->timezone);
$strbynameondate = get_string("bynameondate", "forum", $by);
$strforums = get_string("forums", "forum");
$postsubject = "$course->shortname: $post->subject";
$posttext = "$course->shortname -> $strforums -> $forum->name";
if ($discussion->name == $forum->name) {
$posttext .= "\n";
} else {
$posttext .= " -> $discussion->name\n";
}
$posttext .= "---------------------------------------------------------------------\n";
$posttext .= "$post->subject\n";
$posttext .= $strbynameondate."\n";
$posttext .= "---------------------------------------------------------------------\n";
$posttext .= format_text_email($post->message, $post->format);
$posttext .= "\n\n";
if ($post->attachment) {
$post->course = $course->id;
$post->forum = $forum->id;
$posttext .= forum_print_attachments($post, "text");
}
if ($canreply) {
$posttext .= "---------------------------------------------------------------------\n";
$posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
$posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
}
if ($canunsubscribe) {
$posttext .= "\n---------------------------------------------------------------------\n";
$posttext .= get_string("unsubscribe", "forum");
$posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
}
if ($userto->mailformat == 1) { // HTML
$posthtml = "<p><font face=\"sans-serif\">".
"<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
"<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ".
"<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</a>";
if ($discussion->name == $forum->name) {
$posthtml .= "</font></p>";
} else {
$posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</a></font></p>";
}
$posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
if ($canunsubscribe) {
$posthtml .= "\n<br /><hr size=\"1\" noshade /><p align=\"right\"><font size=\"1\"><a href=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</a></font></p>";
}
} else {
$posthtml = "";
}
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) .. not trying again.\n";
add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#$post->id", substr($post->subject,0,15), $cm->id, $userto->id);
$errorcount++;
} else {
$mailcount++;
}
}
echo ".... mailed to $mailcount users.\n";
if ($errorcount) {
set_field("forum_posts", "mailed", "2", "id", "$post->id");
}
}
}
}
if (!empty($realuser)) { // Restore real USER if necessary
$USER = $realuser;
}
return true;
}
As I am not into php coding, it will take a lot of time for me to come out with something that will take away the ability to send email to the students for a particular course.
However, there is one thing I noticed that I've shown in bold. Does it mean that if group feature is activated for a course but group is not defined, no email will be sent?
Also, do you think that Martin's suggestion will stop emails?
What happens if this whole code that is shown here is deleted?
An alternate solution could be to increase the maximum editing time. If it can be made say 24 hrs. or so by which admin can surely check the post before it gets emailed. However, the maximum time allowed is 1 hour and it is a common practice to keep it 30 minutes.
Please comment!