Skip to content

Commit

Permalink
Merge branch 'MDL-65792-310' of git://github.com/bmbrands/moodle into…
Browse files Browse the repository at this point in the history
… MOODLE_310_STABLE
  • Loading branch information
snake committed Oct 28, 2020
2 parents 461bf54 + a5fa563 commit b1ef67b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
6 changes: 5 additions & 1 deletion blocks/news_items/block_news_items.php
Expand Up @@ -115,9 +115,13 @@ function get_content() {

$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";
Expand Down
21 changes: 20 additions & 1 deletion mod/forum/classes/local/exporters/post.php
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -402,7 +403,7 @@ protected function get_other_values(renderer_base $output) {

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');
Expand Down Expand Up @@ -642,4 +643,22 @@ private function get_author_subheading_html(stdClass $exportedauthor, int $timec
$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;
}
}
46 changes: 40 additions & 6 deletions mod/forum/tests/exporters_post_test.php
Expand Up @@ -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();

Expand All @@ -61,12 +65,18 @@ public function test_export_post() {
$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,
Expand Down Expand Up @@ -150,7 +160,11 @@ public function test_export_post() {
$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']);
Expand Down Expand Up @@ -179,6 +193,26 @@ public function test_export_post() {
$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.
*/
Expand Down
6 changes: 3 additions & 3 deletions mod/forum/tests/externallib_test.php
Expand Up @@ -2663,11 +2663,12 @@ public function test_mod_forum_get_discussion_posts_by_userid() {
$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];
Expand All @@ -2677,7 +2678,7 @@ public function test_mod_forum_get_discussion_posts_by_userid() {
$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];
Expand Down Expand Up @@ -2726,7 +2727,6 @@ public function test_mod_forum_get_discussion_posts_by_userid() {
$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;
Expand Down

0 comments on commit b1ef67b

Please sign in to comment.