diff --git a/mod/forum/classes/local/builders/exported_discussion_summaries.php b/mod/forum/classes/local/builders/exported_discussion_summaries.php index 3687b327e52..6e49a629aba 100644 --- a/mod/forum/classes/local/builders/exported_discussion_summaries.php +++ b/mod/forum/classes/local/builders/exported_discussion_summaries.php @@ -165,7 +165,7 @@ class exported_discussion_summaries { $firstposts = $postvault->get_first_post_for_discussion_ids($discussionids); array_walk($exportedposts['summaries'], function($summary) use ($firstposts, $latestposts) { - $summary->discussion->times['created'] = (int) $firstposts[$summary->discussion->firstpostid]->created; + $summary->discussion->times['created'] = (int) $firstposts[$summary->discussion->firstpostid]->get_time_created(); $summary->discussion->times['modified'] = (int) $latestposts[$summary->discussion->id]->get_time_created(); }); diff --git a/mod/forum/classes/local/vaults/discussion_list.php b/mod/forum/classes/local/vaults/discussion_list.php index 9bd2aca9e65..6cfcebf2f58 100644 --- a/mod/forum/classes/local/vaults/discussion_list.php +++ b/mod/forum/classes/local/vaults/discussion_list.php @@ -137,9 +137,9 @@ class discussion_list extends db_table_vault { $issortbyreplies = in_array($sortsql, $sortkeys); $tables = $thistable->get_from_sql(); - $tables .= ' JOIN {user} fa ON fa.id = ' . $alias . '.userid'; - $tables .= ' JOIN {user} la ON la.id = ' . $alias . '.usermodified'; $tables .= ' JOIN ' . $posttable->get_from_sql() . ' ON fp.id = ' . $alias . '.firstpost'; + $tables .= ' JOIN {user} fa ON fa.id = fp.userid'; + $tables .= ' JOIN {user} la ON la.id = ' . $alias . '.usermodified'; $tables .= $favsql; if ($issortbyreplies) { diff --git a/mod/forum/classes/local/vaults/post.php b/mod/forum/classes/local/vaults/post.php index d1ae66fb0dd..ecbbf6fac39 100644 --- a/mod/forum/classes/local/vaults/post.php +++ b/mod/forum/classes/local/vaults/post.php @@ -35,6 +35,9 @@ use stdClass; * * This should be the only place that accessed the database. * + * This class should not return any objects other than post_entity objects. The class + * may contain some utility count methods which return integers. + * * This uses the repository pattern. See: * https://designpatternsphp.readthedocs.io/en/latest/More/Repository/README.html * @@ -428,8 +431,8 @@ class post extends db_table_vault { /** * Get a mapping of the first post in each discussion based on post creation time. * - * @param int[] $discussionids The list of discussions to fetch counts for - * @return stdClass[] The post object of the first post for each discussions returned in an associative array + * @param int[] $discussionids The list of discussions to fetch counts for + * @return post_entity[] The post object of the first post for each discussions returned in an associative array */ public function get_first_post_for_discussion_ids(array $discussionids) : array { @@ -449,6 +452,7 @@ class post extends db_table_vault { GROUP BY mp.discussion ) lp ON lp.discussion = p.discussion AND lp.created = p.created"; - return $this->get_db()->get_records_sql($sql, $params); + $records = $this->get_db()->get_records_sql($sql, $params); + return $this->transform_db_records_to_entities($records); } } diff --git a/mod/forum/externallib.php b/mod/forum/externallib.php index 0cab4ad56c3..200d05ae339 100644 --- a/mod/forum/externallib.php +++ b/mod/forum/externallib.php @@ -858,6 +858,7 @@ class mod_forum_external extends external_api { $discussionids = array_keys($alldiscussions); $postvault = $vaultfactory->get_post_vault(); + $postdatamapper = $legacydatamapperfactory->get_post_data_mapper(); // Return the reply count for each discussion in a given forum. $replies = $postvault->get_reply_count_for_discussion_ids($USER, $discussionids, $canseeanyprivatereply); // Return the first post for each discussion in a given forum. @@ -896,7 +897,12 @@ class mod_forum_external extends external_api { continue; } - $discussionobject = $firstposts[$discussion->get_first_post_id()]; + $firstpost = $firstposts[$discussion->get_first_post_id()]; + $discussionobject = $postdatamapper->to_legacy_object($firstpost); + // Fix up the types for these properties. + $discussionobject->mailed = $discussionobject->mailed ? 1 : 0; + $discussionobject->messagetrust = $discussionobject->messagetrust ? 1 : 0; + $discussionobject->mailnow = $discussionobject->mailnow ? 1 : 0; $discussionobject->groupid = $discussion->get_group_id(); $discussionobject->timemodified = $discussion->get_time_modified(); $discussionobject->usermodified = $discussion->get_user_modified(); diff --git a/mod/forum/tests/behat/split_forum_discussion.feature b/mod/forum/tests/behat/split_forum_discussion.feature index e664f8a56d9..51bae11ec85 100644 --- a/mod/forum/tests/behat/split_forum_discussion.feature +++ b/mod/forum/tests/behat/split_forum_discussion.feature @@ -9,6 +9,7 @@ Feature: Forum discussions can be split | username | firstname | lastname | email | | teacher1 | Teacher | 1 | teacher1@example.com | | student1 | Student | 1 | student1@example.com | + | student2 | Student | 2 | student2@example.com | And the following "courses" exist: | fullname | shortname | category | | Science 101 | C1 | 0 | @@ -16,6 +17,7 @@ Feature: Forum discussions can be split | user | course | role | | teacher1 | C1 | editingteacher | | student1 | C1 | student | + | student2 | C1 | student | And I log in as "teacher1" And I am on "Science 101" course homepage with editing mode on And I add a "Forum" to section "1" and I fill the form with: @@ -31,6 +33,16 @@ Feature: Forum discussions can be split And I reply "Photosynethis discussion" post from "Study discussions" forum with: | Message | Can anyone tell me which number is the mass number in the periodic table? | And I log out + And I log in as "student2" + And I am on "Science 101" course homepage + And I follow "Study discussions" + And I follow "Photosynethis discussion" + And I click on "Reply" "link" in the "//div[contains(concat(' ', normalize-space(@class), ' '), ' forumpost ')][contains(., 'Can anyone tell me which number is the mass number in the periodic table?')]" "xpath_element" + And I wait to be redirected + And I set the following fields to these values: + | Message | I would also like to know this | + And I press "Post to forum" + And I log out Scenario: Split a forum discussion Given I log in as "teacher1" @@ -44,4 +56,9 @@ Feature: Forum discussions can be split Then I should see "Mass number in periodic table" And I follow "Study discussions" And I should see "Teacher 1" in the "Photosynethis" "table_row" + # Confirm that the last post author has been updated. + And I should not see "Student 2" in the "Photosynethis" "table_row" + # Confirm that the corrent author has been shown for the new split discussion. And I should see "Student 1" in the "Mass number in periodic table" "table_row" + # Confirm that the last post author has been updated for the new discussion. + And I should see "Student 2" in the "Mass number in periodic table" "table_row" diff --git a/mod/forum/tests/vaults_post_test.php b/mod/forum/tests/vaults_post_test.php index 80040330ba5..3d3efadf192 100644 --- a/mod/forum/tests/vaults_post_test.php +++ b/mod/forum/tests/vaults_post_test.php @@ -836,18 +836,18 @@ class mod_forum_vaults_post_testcase extends advanced_testcase { $firstposts = $this->vault->get_first_post_for_discussion_ids([$discussion1->id]); $this->assertCount(1, $firstposts); - $this->assertEquals($post1->id, reset($firstposts)->id); + $this->assertEquals($post1->id, reset($firstposts)->get_id()); $firstposts = $this->vault->get_first_post_for_discussion_ids([$discussion1->id, $discussion2->id]); $this->assertCount(2, $firstposts); - $this->assertEquals($post1->id, $firstposts[$post1->id]->id); - $this->assertEquals($post5->id, $firstposts[$post5->id]->id); + $this->assertEquals($post1->id, $firstposts[$post1->id]->get_id()); + $this->assertEquals($post5->id, $firstposts[$post5->id]->get_id()); $firstposts = $this->vault->get_first_post_for_discussion_ids([$discussion1->id, $discussion2->id, $discussion3->id]); $this->assertCount(3, $firstposts); - $this->assertEquals($post1->id, $firstposts[$post1->id]->id); - $this->assertEquals($post5->id, $firstposts[$post5->id]->id); - $this->assertEquals($post7->id, $firstposts[$post7->id]->id); + $this->assertEquals($post1->id, $firstposts[$post1->id]->get_id()); + $this->assertEquals($post5->id, $firstposts[$post5->id]->get_id()); + $this->assertEquals($post7->id, $firstposts[$post7->id]->get_id()); $firstposts = $this->vault->get_first_post_for_discussion_ids([ $discussion1->id, @@ -856,9 +856,9 @@ class mod_forum_vaults_post_testcase extends advanced_testcase { $discussion3->id + 1000 ]); $this->assertCount(3, $firstposts); - $this->assertEquals($post1->id, $firstposts[$post1->id]->id); - $this->assertEquals($post5->id, $firstposts[$post5->id]->id); - $this->assertEquals($post7->id, $firstposts[$post7->id]->id); + $this->assertEquals($post1->id, $firstposts[$post1->id]->get_id()); + $this->assertEquals($post5->id, $firstposts[$post5->id]->get_id()); + $this->assertEquals($post7->id, $firstposts[$post7->id]->get_id()); } /**