MDL-63466 core_message: deprecated methods and services
[moodle.git] / message / index.php
index ac33533..45cf605 100644 (file)
@@ -135,7 +135,52 @@ if (!empty($user2->id)) {
         $conversations[$user2->id]->isread = 1;
     }
 
-    $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 20, 'timecreated DESC');
+    // Get the conversationid.
+    if (!isset($conversationid)) {
+        if (!$conversationid = self::get_conversation_between_users($userids)) {
+            // If the conversationid doesn't exist, throw an exception.
+            throw new moodle_exception('conversationdoesntexist', 'core_message');
+        }
+    }
+
+    $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversationid, 0, 20, 'timecreated DESC');
+    $messages = $convmessages['messages'];
+
+    // Keeps track of the last day, month and year combo we were viewing.
+    $day = '';
+    $month = '';
+    $year = '';
+
+    // Parse the messages to add missing fields for backward compatibility.
+    $messages = array_map(function($message) use ($user1, $user2, $USER, $day, $month, $year) {
+        // Add useridto.
+        if (empty($message->useridto)) {
+            if ($message->useridfrom == $user1->id) {
+                $message->useridto = $user2->id;
+            } else {
+                $message->useridto = $user1->id;
+            }
+        }
+
+        // Add currentuserid.
+        $message->currentuserid = $USER->id;
+
+        // Add displayblocktime.
+        $date = usergetdate($message->timecreated);
+        if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
+            $day = $date['mday'];
+            $month = $date['month'];
+            $year = $date['year'];
+            $message->displayblocktime = true;
+        } else {
+            $message->displayblocktime = false;
+        }
+        // We don't have this information here so, for now, we leave an empty value.
+        // This is a temporary solution because a new UI is being built in MDL-63303.
+        $message->timeread = 0;
+
+        return $message;
+    }, $messages);
 }
 
 $pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;