Skip to content

Commit

Permalink
Merge branch 'MDL-63892-36-rework' of git://github.com/mickhawkins/mo…
Browse files Browse the repository at this point in the history
…odle into MOODLE_36_STABLE
  • Loading branch information
andrewnicols committed Feb 13, 2019
2 parents d64e812 + 8c28651 commit 8bc84e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
7 changes: 2 additions & 5 deletions mod/forum/lib.php
Expand Up @@ -4029,7 +4029,7 @@ function forum_print_discussion_header(&$post, $forum, $group = -1, $datestring
}

echo '<td class="lastpost">';
$usedate = (empty($post->created)) ? $post->timemodified : $post->created;
$usedate = (empty($post->timemodified)) ? $post->created : $post->timemodified;
$parenturl = '';
$usermodified = new stdClass();
$usermodified->id = $post->usermodified;
Expand Down Expand Up @@ -4662,10 +4662,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;
Expand All @@ -4678,6 +4674,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);
Expand Down
2 changes: 1 addition & 1 deletion mod/forum/post.php
Expand Up @@ -593,7 +593,7 @@

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 = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
fullname($USER).'</a>';
Expand Down
43 changes: 42 additions & 1 deletion mod/forum/tests/lib_test.php
Expand Up @@ -3298,6 +3298,17 @@ public function test_forum_update_post_keeps_discussions_usermodified() {
// 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,
Expand All @@ -3310,6 +3321,30 @@ public function test_forum_update_post_keeps_discussions_usermodified() {
// 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)[
Expand All @@ -3319,8 +3354,14 @@ public function test_forum_update_post_keeps_discussions_usermodified() {
];
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() {
Expand Down

0 comments on commit 8bc84e2

Please sign in to comment.