MDL-65792 mod_forum: show release time on timed posts
authorBas Brands <bas@moodle.com>
Wed, 14 Oct 2020 11:24:14 +0000 (13:24 +0200)
committerBas Brands <bas@moodle.com>
Tue, 27 Oct 2020 07:58:26 +0000 (08:58 +0100)
blocks/news_items/block_news_items.php
mod/forum/classes/local/exporters/post.php
mod/forum/tests/exporters_post_test.php
mod/forum/tests/externallib_test.php

index 9cd9406..482227c 100644 (file)
@@ -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 .= '<li class="post">'.
                          '<div class="head clearfix">'.
-                         '<div class="date">'.userdate($discussion->modified, $strftimerecent).'</div>'.
+                         '<div class="date">'.userdate($posttime, $strftimerecent).'</div>'.
                          '<div class="name">'.fullname($discussion).'</div></div>'.
                          '<div class="info"><a href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->discussion.'">'.$discussion->subject.'</a></div>'.
                          "</li>\n";
index a1919f6..6b361c5 100644 (file)
@@ -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;
+    }
 }
index 7f6e463..c182e6c 100644 (file)
@@ -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.
      */
index 657b457..62e33a3 100644 (file)
@@ -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;