MDL-46881 forum: Ensure that there are posts to remove
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 23 Jan 2019 03:15:20 +0000 (11:15 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Tue, 29 Jan 2019 23:48:38 +0000 (07:48 +0800)
mod/forum/classes/task/send_user_digests.php
mod/forum/classes/task/send_user_notifications.php

index 816ad76..395e61f 100644 (file)
@@ -307,6 +307,12 @@ class send_user_digests extends \core\task\adhoc_task {
         }
         $posts->close();
 
+        if (empty($discussionids)) {
+            // All posts have been removed since the task was queued.
+            $this->empty_queue($this->recipient->id, $timenow);
+            return;
+        }
+
         list($in, $params) = $DB->get_in_or_equal($discussionids);
         $this->discussions = $DB->get_records_select('forum_discussions', "id {$in}", $params);
 
@@ -321,7 +327,21 @@ class send_user_digests extends \core\task\adhoc_task {
 
         $this->fill_digest_cache();
 
-        $DB->delete_records_select('forum_queue', "userid = :userid AND timemodified < :timemodified", $queueparams);
+        $this->empty_queue($this->recipient->id, $timenow);
+    }
+
+    /**
+     * Empty the queue of posts for this user.
+     *
+     * @param   array $queueparams The list of 
+     */
+    protected function empty_queue(int $userid, int $timemodified) : void {
+        global $DB;
+
+        $DB->delete_records_select('forum_queue', "userid = :userid AND timemodified < :timemodified", [
+                'userid' => $userid,
+                'timemodified' => $timemodified,
+            ]);
     }
 
     /**
index cd146d5..0b6bd9e 100644 (file)
@@ -213,6 +213,11 @@ class send_user_notifications extends \core\task\adhoc_task {
         }
         $posts->close();
 
+        if (empty($discussionids)) {
+            // All posts have been removed since the task was queued.
+            return;
+        }
+
         // Fetch all discussions.
         list($in, $params) = $DB->get_in_or_equal(array_values($discussionids));
         $this->discussions = $DB->get_records_select('forum_discussions', "id {$in}", $params);