Commit db3c9ff8 authored by Pau Ferrer's avatar Pau Ferrer
Browse files

MDL-57554 forum: Mark as read on view_forum_discussion calls

parent d97582fd
......@@ -678,7 +678,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(),
......@@ -700,6 +700,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;
......
......@@ -206,6 +206,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;
......@@ -219,6 +225,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;
......@@ -340,6 +352,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']);
}
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment