MDL-65574 forum: fix return type for get_first_post_for_discussion_ids
authorRyan Wyllie <ryan@moodle.com>
Tue, 14 May 2019 02:43:37 +0000 (10:43 +0800)
committerRyan Wyllie <ryan@moodle.com>
Tue, 14 May 2019 02:43:37 +0000 (10:43 +0800)
mod/forum/classes/local/builders/exported_discussion_summaries.php
mod/forum/classes/local/vaults/post.php
mod/forum/externallib.php
mod/forum/tests/vaults_post_test.php

index 3687b32..6e49a62 100644 (file)
@@ -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();
         });
 
index d1ae66f..ecbbf6f 100644 (file)
@@ -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);
     }
 }
index 258c8ad..8fe06cc 100644 (file)
@@ -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();
index 8004033..3d3efad 100644 (file)
@@ -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());
     }
 
     /**