MDL-58444 mod_forum: Return number of unread posts in WS

This commit is contained in:
Juan Leyva 2017-03-30 11:55:38 +02:00
parent 216ea39be7
commit 2256bb74af
3 changed files with 33 additions and 1 deletions

View File

@ -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'
)
);

View File

@ -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])) {

View File

@ -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']);
}
}
}
/**