From 2256bb74afcbd2babf0b5768f7a359997e6f6299 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 30 Mar 2017 11:55:38 +0200 Subject: [PATCH] MDL-58444 mod_forum: Return number of unread posts in WS --- mod/forum/externallib.php | 5 +++++ mod/forum/lib.php | 7 ++++++- mod/forum/tests/externallib_test.php | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 04cde18a1d8..a6745c2a6d8 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -98,6 +98,9 @@ class mod_forum_external extends external_api { $forum->cmid = $forum->coursemodule; $forum->cancreatediscussions = forum_user_can_post_discussion($forum, null, -1, $cm, $context); $forum->istracked = forum_tp_is_tracked($forum); + if ($forum->istracked) { + $forum->unreadpostscount = forum_tp_count_forum_unread_posts($cm, $course); + } // Add the forum to the array to return. $arrforums[$forum->id] = $forum; @@ -146,6 +149,8 @@ class mod_forum_external extends external_api { 'cancreatediscussions' => new external_value(PARAM_BOOL, 'If the user can create discussions', VALUE_OPTIONAL), 'lockdiscussionafter' => new external_value(PARAM_INT, 'After what period a discussion is locked', VALUE_OPTIONAL), 'istracked' => new external_value(PARAM_BOOL, 'If the user is tracking the forum', VALUE_OPTIONAL), + 'unreadpostscount' => new external_value(PARAM_INT, 'The number of unread posts for tracked forums', + VALUE_OPTIONAL), ), 'forum' ) ); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 8c5ec559005..f1d3ee24d91 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -6360,13 +6360,18 @@ function forum_tp_get_course_unread_posts($userid, $courseid) { * @global object * @param object $cm * @param object $course + * @param bool $resetreadcache optional, true to reset the function static $readcache var * @return int */ -function forum_tp_count_forum_unread_posts($cm, $course) { +function forum_tp_count_forum_unread_posts($cm, $course, $resetreadcache = false) { global $CFG, $USER, $DB; static $readcache = array(); + if ($resetreadcache) { + $readcache = array(); + } + $forumid = $cm->instance; if (!isset($readcache[$course->id])) { diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index ea1ca6d0354..3726b342892 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -94,6 +94,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $forum1->numdiscussions = 1; $forum1->cancreatediscussions = true; $forum1->istracked = true; + $forum1->unreadpostscount = 0; $forum1->introfiles = []; $record = new stdClass(); @@ -215,6 +216,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $record = new stdClass(); $record->course = $course1->id; $forum2 = self::getDataGenerator()->create_module('forum', $record); + $forum2cm = get_coursemodule_from_id('forum', $forum2->cmid); $forum2context = context_module::instance($forum2->cmid); // Add discussions to the forums. @@ -352,6 +354,16 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { array_pop($posts['posts']); $this->assertEquals($expectedposts, $posts); + // Check we receive the unread count correctly on tracked forum. + forum_tp_count_forum_unread_posts($forum2cm, $course1, true); // Reset static cache. + $result = mod_forum_external::get_forums_by_courses(array($course1->id)); + $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result); + foreach ($result as $f) { + if ($f['id'] == $forum2->id) { + $this->assertEquals(1, $f['unreadpostscount']); + } + } + // Test discussion without additional posts. There should be only one post (the one created by the discussion). $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC'); $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); @@ -382,6 +394,16 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { foreach ($posts['posts'] as $post) { $this->assertTrue($post['postread']); } + + // Check we receive 0 unread posts. + forum_tp_count_forum_unread_posts($forum2cm, $course1, true); // Reset static cache. + $result = mod_forum_external::get_forums_by_courses(array($course1->id)); + $result = external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $result); + foreach ($result as $f) { + if ($f['id'] == $forum2->id) { + $this->assertEquals(0, $f['unreadpostscount']); + } + } } /**