diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 9b3daffcb5e..b3ab20ec4fc 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -689,7 +689,7 @@ class mod_forum_external extends external_api { * @throws moodle_exception */ public static function view_forum_discussion($discussionid) { - global $DB, $CFG; + global $DB, $CFG, $USER; require_once($CFG->dirroot . "/mod/forum/lib.php"); $params = self::validate_parameters(self::view_forum_discussion_parameters(), @@ -711,6 +711,11 @@ class mod_forum_external extends external_api { // Call the forum/lib API. forum_discussion_view($modcontext, $forum, $discussion); + // Mark as read if required. + if (!$CFG->forum_usermarksread && forum_tp_is_tracked($forum)) { + forum_tp_mark_discussion_read($USER, $discussion->id); + } + $result = array(); $result['status'] = true; $result['warnings'] = $warnings; diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php index 455abcaa5ee..30b8ff8a943 100644 --- a/mod/forum/tests/externallib_test.php +++ b/mod/forum/tests/externallib_test.php @@ -204,6 +204,12 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1context = context_module::instance($forum1->cmid); + // Forum with tracking enabled. + $record = new stdClass(); + $record->course = $course1->id; + $forum2 = self::getDataGenerator()->create_module('forum', $record); + $forum2context = context_module::instance($forum2->cmid); + // Add discussions to the forums. $record = new stdClass(); $record->course = $course1->id; @@ -217,6 +223,12 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $record->forum = $forum1->id; $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); + $record = new stdClass(); + $record->course = $course1->id; + $record->userid = $user2->id; + $record->forum = $forum2->id; + $discussion3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); + // Add 2 replies to the discussion 1 from different users. $record = new stdClass(); $record->discussion = $discussion1->id; @@ -314,6 +326,31 @@ class mod_forum_external_testcase extends externallib_advanced_testcase { $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); $this->assertEquals(1, count($posts['posts'])); + // Test discussion tracking on not tracked forum. + $result = mod_forum_external::view_forum_discussion($discussion1->id); + $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result); + $this->assertTrue($result['status']); + $this->assertEmpty($result['warnings']); + + // Test posts have not been marked as read. + $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC'); + $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); + foreach ($posts['posts'] as $post) { + $this->assertFalse($post['postread']); + } + + // Test discussion tracking on tracked forum. + $result = mod_forum_external::view_forum_discussion($discussion3->id); + $result = external_api::clean_returnvalue(mod_forum_external::view_forum_discussion_returns(), $result); + $this->assertTrue($result['status']); + $this->assertEmpty($result['warnings']); + + // Test posts have been marked as read. + $posts = mod_forum_external::get_forum_discussion_posts($discussion3->id, 'modified', 'DESC'); + $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); + foreach ($posts['posts'] as $post) { + $this->assertTrue($post['postread']); + } } /**