From 4e190d7eae9d069790fc8a972dbc17dbdb34c927 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Thu, 22 Nov 2018 17:39:14 +0800 Subject: [PATCH 1/3] MDL-63892 mod_forum: Correction to timestamp added to edit strings --- mod/forum/post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod/forum/post.php b/mod/forum/post.php index 4f79679b41b..ae1af721634 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -574,7 +574,7 @@ file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment' if ($USER->id != $post->userid) { // Not the original author, so add a message to the end $data = new stdClass(); - $data->date = userdate($post->modified); + $data->date = userdate($post->created); if ($post->messageformat == FORMAT_HTML) { $data->name = ''. fullname($USER).''; From 11fc8b3d02561b26315b4537184cd019647e1ddb Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Fri, 14 Dec 2018 17:21:24 +0800 Subject: [PATCH 2/3] MDL-63892 mod_forum: Fix last post details in forum view --- mod/forum/lib.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mod/forum/lib.php b/mod/forum/lib.php index cc873aec530..04b519578a2 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -3925,7 +3925,7 @@ function forum_print_discussion_header(&$post, $forum, $group = -1, $datestring } echo ''; - $usedate = (empty($post->created)) ? $post->timemodified : $post->created; + $usedate = (empty($post->timemodified)) ? $post->created : $post->timemodified; $parenturl = ''; $usermodified = new stdClass(); $usermodified->id = $post->usermodified; @@ -4558,10 +4558,6 @@ function forum_update_post($newpost, $mform, $unused = null) { } $post->modified = time(); - // Last post modified tracking. - $discussion->timemodified = $post->modified; - $discussion->usermodified = $post->userid; - if (!$post->parent) { // Post is a discussion starter - update discussion title and times too $discussion->name = $post->subject; $discussion->timestart = $post->timestart; @@ -4574,6 +4570,7 @@ function forum_update_post($newpost, $mform, $unused = null) { $post->message = file_save_draft_area_files($newpost->itemid, $context->id, 'mod_forum', 'post', $post->id, mod_forum_post_form::editor_options($context, $post->id), $post->message); $DB->update_record('forum_posts', $post); + // Note: Discussion modified time/user are intentionally not updated, to enable them to track the latest new post. $DB->update_record('forum_discussions', $discussion); forum_add_attachment($post, $forum, $cm, $mform); From 4fccbafa46a3090e46e67a825b57ac5b4bf6cdca Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Mon, 21 Jan 2019 17:03:49 +0800 Subject: [PATCH 3/3] MDL-63892 mod_forum: Updating lib unit test --- mod/forum/tests/lib_test.php | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index cce78611b44..54e8bba2514 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -3298,6 +3298,17 @@ class mod_forum_lib_testcase extends advanced_testcase { // On this freshly created discussion, the teacher is the author of the last post. $this->assertEquals($teacher->id, $DB->get_field('forum_discussions', 'usermodified', ['id' => $discussion->id])); + // Fetch modified timestamp of the discussion. + $discussionmodified = $DB->get_field('forum_discussions', 'timemodified', ['id' => $discussion->id]); + $pasttime = $discussionmodified - 3600; + + // Adjust the discussion modified timestamp back an hour, so it's in the past. + $adjustment = (object)[ + 'id' => $discussion->id, + 'timemodified' => $pasttime, + ]; + $DB->update_record('forum_discussions', $adjustment); + // Let the student reply to the teacher's post. $reply = $generator->create_post((object)[ 'course' => $course->id, @@ -3310,6 +3321,30 @@ class mod_forum_lib_testcase extends advanced_testcase { // The student should now be the last post's author. $this->assertEquals($student->id, $DB->get_field('forum_discussions', 'usermodified', ['id' => $discussion->id])); + // Fetch modified timestamp of the discussion and student's post. + $discussionmodified = $DB->get_field('forum_discussions', 'timemodified', ['id' => $discussion->id]); + $postmodified = $DB->get_field('forum_posts', 'modified', ['id' => $reply->id]); + + // Discussion modified time should be updated to be equal to the newly created post's time. + $this->assertEquals($discussionmodified, $postmodified); + + // Adjust the discussion and post timestamps, so they are in the past. + $adjustment = (object)[ + 'id' => $discussion->id, + 'timemodified' => $pasttime, + ]; + $DB->update_record('forum_discussions', $adjustment); + + $adjustment = (object)[ + 'id' => $reply->id, + 'modified' => $pasttime, + ]; + $DB->update_record('forum_posts', $adjustment); + + // The discussion and student's post time should now be an hour in the past. + $this->assertEquals($pasttime, $DB->get_field('forum_discussions', 'timemodified', ['id' => $discussion->id])); + $this->assertEquals($pasttime, $DB->get_field('forum_posts', 'modified', ['id' => $reply->id])); + // Let the teacher edit the student's reply. $this->setUser($teacher->id); $newpost = (object)[ @@ -3319,8 +3354,14 @@ class mod_forum_lib_testcase extends advanced_testcase { ]; forum_update_post($newpost, null); - // The student should be still the last post's author. + // The student should still be the last post's author. $this->assertEquals($student->id, $DB->get_field('forum_discussions', 'usermodified', ['id' => $discussion->id])); + + // The discussion modified time should not have changed. + $this->assertEquals($pasttime, $DB->get_field('forum_discussions', 'timemodified', ['id' => $discussion->id])); + + // The post time should be updated. + $this->assertGreaterThan($pasttime, $DB->get_field('forum_posts', 'modified', ['id' => $reply->id])); } public function test_forum_core_calendar_provide_event_action() {