MDL-44087 mod_forum: Move forum digest to new processor
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 29 Jan 2016 07:16:23 +0000 (15:16 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Sun, 7 Feb 2016 23:54:14 +0000 (07:54 +0800)
mod/forum/db/messages.php
mod/forum/lang/en/forum.php
mod/forum/lib.php
mod/forum/tests/maildigest_test.php
mod/forum/version.php

index f195b01..1733de0 100644 (file)
  */
 
 $messageproviders = array (
+    // Ordinary single forum posts.
+    'posts' => array(
+    ),
 
-/// Ordinary single forum posts
-    'posts' => array (
-    )
-
+    // Forum digest messages.
+    'digests' => array(
+    ),
 );
-
-
-
index ae32f1e..f00cc11 100644 (file)
@@ -461,6 +461,7 @@ $string['shortpost'] = 'Short post';
 $string['showsubscribers'] = 'Show/edit current subscribers';
 $string['singleforum'] = 'A single simple discussion';
 $string['smallmessage'] = '{$a->user} posted in {$a->forumname}';
+$string['smallmessagedigest'] = 'Forum digest containing {$a} messages';
 $string['startedby'] = 'Started by';
 $string['subject'] = 'Subject';
 $string['subscribe'] = 'Subscribe to this forum';
index d993c41..121b2e6 100644 (file)
@@ -1063,6 +1063,7 @@ function forum_cron() {
 
                     $postsarray = $discussionposts[$discussionid];
                     sort($postsarray);
+                    $sentcount = 0;
 
                     foreach ($postsarray as $postid) {
                         $post = $posts[$postid];
@@ -1144,6 +1145,7 @@ function forum_cron() {
                                 $userto->markposts[$post->id] = $post->id;
                             }
                         }
+                        $sentcount++;
                     }
                     $footerlinks = array();
                     if ($canunsubscribe) {
@@ -1162,10 +1164,18 @@ function forum_cron() {
                     $posthtml = '';
                 }
 
-                $attachment = $attachname='';
-                // Directly email forum digests rather than sending them via messaging, use the
-                // site shortname as 'from name', the noreply address will be used by email_to_user.
-                $mailresult = email_to_user($userto, $site->shortname, $postsubject, $posttext, $posthtml, $attachment, $attachname);
+                $eventdata = new \core\message\message();
+                $eventdata->component           = 'mod_forum';
+                $eventdata->name                = 'digests';
+                $eventdata->userfrom            = core_user::get_noreply_user();
+                $eventdata->userto              = $userto;
+                $eventdata->subject             = $postsubject;
+                $eventdata->fullmessage         = $posttext;
+                $eventdata->fullmessageformat   = FORMAT_PLAIN;
+                $eventdata->fullmessagehtml     = $posthtml;
+                $eventdata->notification        = 1;
+                $eventdata->smallmessage        = get_string('smallmessagedigest', 'forum', $sentcount);
+                $mailresult = message_send($eventdata);
 
                 if (!$mailresult) {
                     mtrace("ERROR: mod/forum/cron.php: Could not send out digest mail to user $userto->id ".
index 12d24bb..b694007 100644 (file)
@@ -150,9 +150,10 @@ class mod_forum_maildigest_testcase extends advanced_testcase {
      * specified number of times.
      *
      * @param integer $expected The number of times that the post should have been sent
-     * @return array An array of the messages caught by the message sink
+     * @param integer $individualcount The number of individual messages sent
+     * @param integer $digestcount The number of digest messages sent
      */
-    protected function helper_run_cron_check_count($expected, $messagecount, $mailcount) {
+    protected function helper_run_cron_check_count($expected, $individualcount, $digestcount) {
         if ($expected === 0) {
             $this->expectOutputRegex('/(Email digests successfully sent to .* users.){0}/');
         } else {
@@ -162,15 +163,18 @@ class mod_forum_maildigest_testcase extends advanced_testcase {
 
         // Now check the results in the message sink.
         $messages = $this->helper->messagesink->get_messages();
-        // There should be the expected number of messages.
-        $this->assertEquals($messagecount, count($messages));
 
-        // Now check the results in the mail sink.
-        $messages = $this->helper->mailsink->get_messages();
-        // There should be the expected number of messages.
-        $this->assertEquals($mailcount, count($messages));
+        $counts = (object) array('digest' => 0, 'individual' => 0);
+        foreach ($messages as $message) {
+            if (strpos($message->subject, 'forum digest') !== false) {
+                $counts->digest++;
+            } else {
+                $counts->individual++;
+            }
+        }
 
-        return $messages;
+        $this->assertEquals($digestcount, $counts->digest);
+        $this->assertEquals($individualcount, $counts->individual);
     }
 
     public function test_set_maildigest() {
index 50277d1..cd4eaf1 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015120800;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2015120801;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015111000;       // Requires this Moodle version
 $plugin->component = 'mod_forum';      // Full name of the plugin (used for diagnostics)