diff --git a/blocks/news_items/block_news_items.php b/blocks/news_items/block_news_items.php
index 9cd9406943d..482227cd332 100644
--- a/blocks/news_items/block_news_items.php
+++ b/blocks/news_items/block_news_items.php
@@ -115,9 +115,13 @@ class block_news_items extends block_base {
$discussion->subject = format_string($discussion->subject, true, $forum->course);
+ $posttime = $discussion->modified;
+ if (!empty($CFG->forum_enabletimedposts) && ($discussion->timestart > $posttime)) {
+ $posttime = $discussion->timestart;
+ }
$text .= '
'.
''.
- '
'.userdate($discussion->modified, $strftimerecent).'
'.
+ '
'.userdate($posttime, $strftimerecent).'
'.
'
'.fullname($discussion).'
'.
''.
"\n";
diff --git a/mod/forum/classes/local/exporters/post.php b/mod/forum/classes/local/exporters/post.php
index a1919f62939..6b361c5e895 100644
--- a/mod/forum/classes/local/exporters/post.php
+++ b/mod/forum/classes/local/exporters/post.php
@@ -27,6 +27,7 @@ namespace mod_forum\local\exporters;
defined('MOODLE_INTERNAL') || die();
use mod_forum\local\entities\post as post_entity;
+use mod_forum\local\entities\discussion as discussion_entity;
use mod_forum\local\exporters\author as author_exporter;
use mod_forum\local\factories\exporter as exporter_factory;
use core\external\exporter;
@@ -402,7 +403,7 @@ class post extends exporter {
if ($loadcontent) {
$subject = $post->get_subject();
- $timecreated = $post->get_time_created();
+ $timecreated = $this->get_start_time($discussion, $post);
$message = $this->get_message($post);
} else {
$subject = $isdeleted ? get_string('forumsubjectdeleted', 'forum') : get_string('forumsubjecthidden', 'forum');
@@ -642,4 +643,22 @@ class post extends exporter {
$date = userdate_htmltime($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
return get_string('bynameondate', 'mod_forum', ['name' => $name, 'date' => $date]);
}
+
+ /**
+ * Get the start time for a post.
+ *
+ * @param discussion_entity $discussion entity
+ * @param post_entity $post entity
+ * @return int The start time (timestamp) for a post
+ */
+ private function get_start_time(discussion_entity $discussion, post_entity $post) {
+ global $CFG;
+
+ $posttime = $post->get_time_created();
+ $discussiontime = $discussion->get_time_start();
+ if (!empty($CFG->forum_enabletimedposts) && ($discussiontime > $posttime)) {
+ return $discussiontime;
+ }
+ return $posttime;
+ }
}
diff --git a/mod/forum/tests/exporters_post_test.php b/mod/forum/tests/exporters_post_test.php
index 7f6e46321fd..c182e6ce4d2 100644
--- a/mod/forum/tests/exporters_post_test.php
+++ b/mod/forum/tests/exporters_post_test.php
@@ -46,8 +46,12 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
/**
* Test the export function returns expected values.
+ *
+ * @dataProvider export_post_provider
+ * @param bool $istimed True if this is a timed post
+ * @param int $addtime Seconds to be added to the current time
*/
- public function test_export_post() {
+ public function test_export_post($istimed = false, $addtime = 0) {
global $CFG, $PAGE;
$this->resetAfterTest();
@@ -61,12 +65,18 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
$forum = $datagenerator->create_module('forum', ['course' => $course->id]);
$coursemodule = get_coursemodule_from_instance('forum', $forum->id);
$context = context_module::instance($coursemodule->id);
- $discussion = $forumgenerator->create_discussion((object) [
+ $now = time();
+
+ $forumgenparams = [
'course' => $forum->course,
'userid' => $user->id,
- 'forum' => $forum->id
- ]);
- $now = time();
+ 'forum' => $forum->id,
+ ];
+ if ($istimed) {
+ $forumgenparams['timestart'] = $now + $addtime;
+ }
+ $discussion = $forumgenerator->create_discussion((object) $forumgenparams);
+
$post = $forumgenerator->create_post((object) [
'discussion' => $discussion->id,
'parent' => 0,
@@ -150,7 +160,11 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
$this->assertEquals($discussion->get_id(), $exportedpost->discussionid);
$this->assertEquals(false, $exportedpost->hasparent);
$this->assertEquals(null, $exportedpost->parentid);
- $this->assertEquals($now, $exportedpost->timecreated);
+ if ($istimed && ($addtime > 0)) {
+ $this->assertEquals($now + $addtime, $exportedpost->timecreated);
+ } else {
+ $this->assertEquals($now, $exportedpost->timecreated);
+ }
$this->assertEquals(null, $exportedpost->unread);
$this->assertEquals(false, $exportedpost->isdeleted);
$this->assertEquals($canview, $exportedpost->capabilities['view']);
@@ -179,6 +193,26 @@ class mod_forum_exporters_post_testcase extends advanced_testcase {
$this->assertNotEmpty($exportedpost->html['authorsubheading']);
}
+ /**
+ * Data provider for test_export_post().
+ *
+ * @return array
+ */
+ public function export_post_provider(): array {
+ return [
+ 'Simple export' => [
+ ],
+ 'Test timed post future' => [
+ true,
+ 1000
+ ],
+ 'Test timed post past' => [
+ true,
+ -1000
+ ],
+ ];
+ }
+
/**
* Test exporting of a deleted post.
*/
diff --git a/mod/forum/tests/externallib_test.php b/mod/forum/tests/externallib_test.php
index 657b4577823..62e33a3d504 100644
--- a/mod/forum/tests/externallib_test.php
+++ b/mod/forum/tests/externallib_test.php
@@ -2663,11 +2663,12 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$forum1context = context_module::instance($forum1->cmid);
// Add discussions to the forums.
+ $time = time();
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user1->id;
$record->forum = $forum1->id;
- $record->timemodified = 1;
+ $record->timemodified = $time + 100;
$discussion1 = $forumgenerator->create_discussion($record);
$discussion1firstpost = $postvault->get_first_post_for_discussion_ids([$discussion1->id]);
$discussion1firstpost = $discussion1firstpost[$discussion1->firstpost];
@@ -2677,7 +2678,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$record->course = $course1->id;
$record->userid = $user1->id;
$record->forum = $forum1->id;
- $record->timemodified = 2;
+ $record->timemodified = $time + 200;
$discussion2 = $forumgenerator->create_discussion($record);
$discussion2firstpost = $postvault->get_first_post_for_discussion_ids([$discussion2->id]);
$discussion2firstpost = $discussion2firstpost[$discussion2->firstpost];
@@ -2726,7 +2727,6 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher');
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
// Changed display period for the discussions in past.
- $time = time();
$discussion = new \stdClass();
$discussion->id = $discussion1->id;
$discussion->timestart = $time - 200;