MDL-46881 forum: Defer post visibility check to send time
authorAndrew Nicols <andrew@nicols.co.uk>
Wed, 23 Jan 2019 02:08:53 +0000 (10:08 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Tue, 29 Jan 2019 23:48:38 +0000 (07:48 +0800)
mod/forum/classes/task/cron_task.php
mod/forum/tests/mail_test.php
mod/forum/tests/qanda_test.php

index ee2d296..2d0fd60 100644 (file)
@@ -173,10 +173,6 @@ class cron_task extends \core\task\scheduled_task {
         $this->fill_digest_cache();
         $this->log_finish("Done", 1);
 
-        $this->log_start("Filling user posted cache for Q&A forums", 1);
-        $this->fetch_user_has_posted();
-        $this->log_finish("Done", 1);
-
         $this->log_start("Queueing user tasks.");
         $this->queue_user_tasks();
         $this->log_finish("All tasks queued.", 1);
@@ -311,44 +307,6 @@ class cron_task extends \core\task\scheduled_task {
         }
     }
 
-    /**
-     * Fill the cache for Q&A forums.
-     */
-    protected function fetch_user_has_posted() {
-        global $DB;
-
-        $forums = array_filter($this->forums, function($forum) {
-            return $forum->type === 'qanda';
-        });
-
-        if (empty($forums)) {
-            return;
-        }
-
-        list($in, $params) = $DB->get_in_or_equal(array_keys($forums));
-
-        $sql = "SELECT d.forum, d.firstpost, p.userid
-                 FROM {forum} f
-           INNER JOIN {forum_discussions} d ON d.forum = f.id
-            LEFT JOIN {forum_posts} p ON p.discussion = d.id
-                WHERE f.type = 'qanda'
-                  AND f.id {$in}
-             GROUP BY p.userid, d.forum, d.firstpost";
-
-        $rs = $DB->get_recordset_sql($sql, $params);
-        foreach ($rs as $row) {
-            if (empty($this->qandametadata[$row->forum])) {
-                $this->qandametadata[$row->forum] = (object) [
-                        'users' => [],
-                        'firstpost' => [$row->firstpost],
-                    ];
-            }
-            $this->qandametadata[$row->forum]->users[$row->userid] = true;
-        }
-        $rs->close();
-
-    }
-
     /**
      * Queue the user tasks.
      */
@@ -500,22 +458,6 @@ class cron_task extends \core\task\scheduled_task {
                             }
                         }
 
-                        if ($forum->type === 'qanda' && $postid != $discussion->firstpost) {
-                            $hasqandapost = isset($this->qandametadata[$forumid]);
-                            $hasqandapost = $hasqandapost && isset($this->qandametadata[$forumid]->users[$user->id]);
-                            if (!$hasqandapost) {
-                                // The user has not posted to this qanda forum.
-                                unset($poststructure[$courseid][$forumid][$discussionid]);
-                                continue;
-                            }
-                        }
-
-                        if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
-                            // The user is not allowed to see the post for some other reason.
-                            unset($poststructure[$courseid][$forumid][$discussionid][$postid]);
-                            continue;
-                        }
-
                         if ($maildigest > 0) {
                             // This user wants the mails to be in digest form.
                             $digeststructure[] = (object) [
index 957a4ed..8bf818f 100644 (file)
@@ -1215,11 +1215,11 @@ class mod_forum_mail_testcase extends advanced_testcase {
         $expect = [
             'author' => (object) [
                 'userid' => $author->id,
-                'messages' => 0,
+                'messages' => 1,
             ],
             'recipient' => (object) [
                 'userid' => $recipient->id,
-                'messages' => 0,
+                'messages' => 1,
             ],
             'editor' => (object) [
                 'userid' => $editor->id,
@@ -1229,8 +1229,8 @@ class mod_forum_mail_testcase extends advanced_testcase {
         $this->queue_tasks_and_assert($expect);
 
         // No notifications should be queued.
-        $this->send_notifications_and_assert($author, []);
-        $this->send_notifications_and_assert($recipient, []);
+        $this->send_notifications_and_assert($author, [], true);
+        $this->send_notifications_and_assert($recipient, [], true);
         $this->send_notifications_and_assert($editor, [$post], true);
     }
 
index 417ea2a..3e47952 100644 (file)
@@ -134,7 +134,7 @@ class mod_forum_qanda_testcase extends advanced_testcase {
             ],
             'otheruser' => (object) [
                 'userid' => $otheruser->id,
-                'messages' => 1,
+                'messages' => 3,
             ],
             'editingteacher' => (object) [
                 'userid' => $editingteacher->id,