MDL-64034 core_message: return conversationid in search_messages()
authorJake Dallimore <jake@moodle.com>
Fri, 16 Nov 2018 04:27:24 +0000 (12:27 +0800)
committerJake Dallimore <jake@moodle.com>
Mon, 19 Nov 2018 02:23:15 +0000 (10:23 +0800)
message/classes/api.php
message/classes/helper.php
message/classes/output/messagearea/contact.php
message/externallib.php
message/tests/api_test.php
message/tests/externallib_test.php

index de7e08e..2eb4ab9 100644 (file)
@@ -100,8 +100,8 @@ class api {
         $ufields2 = \user_picture::fields('u2', array('lastaccess'), 'userto_id', 'userto_');
 
         $sql = "SELECT m.id, m.useridfrom, mcm.userid as useridto, m.subject, m.fullmessage, m.fullmessagehtml, m.fullmessageformat,
-                       m.smallmessage, m.timecreated, 0 as isread, $ufields, mub.id as userfrom_blocked, $ufields2,
-                       mub2.id as userto_blocked
+                       m.smallmessage, m.conversationid, m.timecreated, 0 as isread, $ufields, mub.id as userfrom_blocked,
+                       $ufields2, mub2.id as userto_blocked
                   FROM {messages} m
             INNER JOIN {user} u
                     ON u.id = m.useridfrom
index d2c4ece..2e9c99a 100644 (file)
@@ -326,6 +326,7 @@ class helper {
         $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false;
         $data->isread = isset($contact->isread) ? (bool) $contact->isread : false;
         $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
+        $data->conversationid = $contact->conversationid ?? null;
 
         return $data;
     }
index 3a138c1..364940c 100644 (file)
@@ -108,6 +108,11 @@ class contact implements templatable, renderable {
      */
     public $unreadcount;
 
+    /**
+     * @var int The id of the conversation to which to message belongs.
+     */
+    public $conversationid;
+
     /**
      * Constructor.
      *
@@ -127,6 +132,7 @@ class contact implements templatable, renderable {
         $this->isblocked = $contact->isblocked;
         $this->isread = $contact->isread;
         $this->unreadcount = $contact->unreadcount;
+        $this->conversationid = $contact->conversationid ?? null;
     }
 
     public function export_for_template(\renderer_base $output) {
@@ -152,6 +158,7 @@ class contact implements templatable, renderable {
         $contact->isblocked = $this->isblocked;
         $contact->isread = $this->isread;
         $contact->unreadcount = $this->unreadcount;
+        $contact->conversationid = $this->conversationid;
 
         return $contact;
     }
index cccdf07..c0046d2 100644 (file)
@@ -1049,6 +1049,7 @@ class core_message_external extends external_api {
                 'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'),
                 'unreadcount' => new external_value(PARAM_INT, 'The number of unread messages in this conversation',
                     VALUE_DEFAULT, null),
+                'conversationid' => new external_value(PARAM_INT, 'The id of the conversation', VALUE_DEFAULT, null),
             )
         );
     }
index 69841af..8dbc15c 100644 (file)
@@ -732,6 +732,9 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
         $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
 
+        $convid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
+        $conv2id = \core_message\api::get_conversation_between_users([$user1->id, $user3->id]);
+
         // Block user 3.
         \core_message\api::block_user($user1->id, $user3->id);
 
@@ -755,6 +758,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertFalse($message1->isread);
         $this->assertFalse($message1->isblocked);
         $this->assertNull($message1->unreadcount);
+        $this->assertEquals($convid, $message1->conversationid);
 
         $this->assertEquals($user2->id, $message2->userid);
         $this->assertEquals($user1->id, $message2->useridfrom);
@@ -766,6 +770,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertTrue($message2->isread);
         $this->assertFalse($message2->isblocked);
         $this->assertNull($message2->unreadcount);
+        $this->assertEquals($convid, $message2->conversationid);
 
         $this->assertEquals($user3->id, $message3->userid);
         $this->assertEquals($user3->id, $message3->useridfrom);
@@ -777,6 +782,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertFalse($message3->isread);
         $this->assertTrue($message3->isblocked);
         $this->assertNull($message3->unreadcount);
+        $this->assertEquals($conv2id, $message3->conversationid);
     }
 
     /**
index 1f23331..3e00d62 100644 (file)
@@ -2653,13 +2653,13 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1);
         $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2);
         $this->send_message($user2, $user1, 'Word.', 0, $time + 3);
+        $convid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
 
         // Perform a search.
         $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o');
 
         // We need to execute the return values cleaning process to simulate the web service server.
-        $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(),
-            $result);
+        $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), $result);
 
         // Confirm the data is correct.
         $messages = $result['contacts'];
@@ -2678,6 +2678,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->assertFalse($message1['isread']);
         $this->assertFalse($message1['isblocked']);
         $this->assertNull($message1['unreadcount']);
+        $this->assertEquals($convid, $message1['conversationid']);
 
         $this->assertEquals($user2->id, $message2['userid']);
         $this->assertEquals(fullname($user2), $message2['fullname']);
@@ -2689,6 +2690,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->assertTrue($message2['isread']);
         $this->assertFalse($message2['isblocked']);
         $this->assertNull($message2['unreadcount']);
+        $this->assertEquals($convid, $message2['conversationid']);
     }
 
     /**