Skip to content

Commit

Permalink
MDL-46881 forum: Defer post visibility check to send time
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jan 29, 2019
1 parent c0284cc commit af48d8b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 63 deletions.
58 changes: 0 additions & 58 deletions mod/forum/classes/task/cron_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ protected function process_post_data($posts) {
$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);
Expand Down Expand Up @@ -311,44 +307,6 @@ protected function fill_user_subscription_cache() {
}
}

/**
* 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.
*/
Expand Down Expand Up @@ -500,22 +458,6 @@ protected function fetch_posts_for_user($user) {
}
}

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) [
Expand Down
8 changes: 4 additions & 4 deletions mod/forum/tests/mail_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1215,11 +1215,11 @@ public function test_access_coursemodule_hidden() {
$expect = [
'author' => (object) [
'userid' => $author->id,
'messages' => 0,
'messages' => 1,
],
'recipient' => (object) [
'userid' => $recipient->id,
'messages' => 0,
'messages' => 1,
],
'editor' => (object) [
'userid' => $editor->id,
Expand All @@ -1229,8 +1229,8 @@ public function test_access_coursemodule_hidden() {
$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);
}

Expand Down
2 changes: 1 addition & 1 deletion mod/forum/tests/qanda_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function test_user_has_not_posted() {
],
'otheruser' => (object) [
'userid' => $otheruser->id,
'messages' => 1,
'messages' => 3,
],
'editingteacher' => (object) [
'userid' => $editingteacher->id,
Expand Down

0 comments on commit af48d8b

Please sign in to comment.