Add a message to a conversation.

@since 2.0.0
@access public

@param int $ConversationID Unique ID of the conversation.
15823
public function AddMessage($ConversationID = '') {
      $this->Form->SetModel($this->ConversationMessageModel);
      if (is_numeric($ConversationID) && $ConversationID > 0)
         $this->Form->AddHidden('ConversationID', $ConversationID);
      
      if ($this->Form->AuthenticatedPostBack()) {
         $ConversationID = $this->Form->GetFormValue('ConversationID', '');
         $Conversation = $this->ConversationModel->GetID($ConversationID, Gdn::Session()->UserID);   
         
         $this->EventArguments['Conversation'] = $Conversation;
         $this->EventArguments['ConversationID'] = $ConversationID;
         $this->FireEvent('BeforeAddMessage');
         
         $NewMessageID = $this->Form->Save();
         
         if ($NewMessageID) {
            if ($this->DeliveryType() == DELIVERY_TYPE_ALL)
               Redirect('messages/'.$ConversationID.'/#'.$NewMessageID, 302);
               
            $this->SetJson('MessageID', $NewMessageID);
            // If this was not a full-page delivery type, return the partial response
            // Load all new messages that the user hasn't seen yet (including theirs)
            $LastMessageID = $this->Form->GetFormValue('LastMessageID');
            if (!is_numeric($LastMessageID))
               $LastMessageID = $NewMessageID - 1;
            
            $Session = Gdn::Session();
            $MessageData = $this->ConversationMessageModel->GetNew($ConversationID, $LastMessageID);
            $this->Conversation = $Conversation;
            $this->MessageData = $MessageData;

            $this->View = 'messages';
         } else {
            // Handle ajax based errors...
            if ($this->DeliveryType() != DELIVERY_TYPE_ALL)
               $this->ErrorMessage($this->Form->Errors());
         }
      }
      $this->Render();      
   }
330536
public static function mark_all_conversation_messages_as_read(int $userid, int $conversationid) {
        global $CFG;

        // Check if messaging is enabled.
        if (empty($CFG->messaging)) {
            throw new moodle_exception('disabled', 'message');
        }

        $params = array(
            'userid' => $userid,
            'conversationid' => $conversationid,
        );
        $params = self::validate_parameters(self::mark_all_conversation_messages_as_read_parameters(), $params);

        $context = context_system::instance();
        self::validate_context($context);

        $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
        core_user::require_active_user($user);

        if (\core_message\api::can_mark_all_messages_as_read($params['userid'], $params['conversationid'])) {
            \core_message\api::mark_all_messages_as_read($params['userid'], $params['conversationid']);
        } else {
            throw new moodle_exception('accessdenied', 'admin');
        }
    }
384689
public function addMessage()
    {
        $messages  = \MUtil_Ra::flatten(func_get_args());
        $messenger = $this->getMessenger();

        foreach ($messages as $message) {
            $messenger->addMessage($message);
        }

        return $this;
    }
504580
protected function makeMessage($conversationId, $message)
    {
        $message = $this->message->create([
            'message' => $message,
            'conversation_id' => $conversationId,
            'user_id' => $this->authUserId,
            'is_seen' => 0,
        ]);

        $message->conversation->touch();

        $this->broadcast->transmission($message);

        return $message;
    }
943473
public function Edit($MessageID = '') {
      $this->AddJsFile('jquery.autogrow.js');
      $this->AddJsFile('messages.js');
         
      $this->Permission('Garden.Messages.Manage');
      $this->AddSideMenu('dashboard/message');
      
      // Generate some Controller & Asset data arrays
      $this->SetData('Locations', $this->_GetLocationData());
      $this->AssetData = $this->_GetAssetData();
      
      // Set the model on the form.
      $this->Form->SetModel($this->MessageModel);
      $this->Message = $this->MessageModel->GetID($MessageID);
      $this->Message = $this->MessageModel->DefineLocation($this->Message);
      
      // Make sure the form knows which item we are editing.
      if (is_numeric($MessageID) && $MessageID > 0)
         $this->Form->AddHidden('MessageID', $MessageID);
      
      $CategoriesData = CategoryModel::Categories();
      $Categories = array();
      foreach ($CategoriesData as $Row) {
         if ($Row['CategoryID'] < 0)
            continue;
         
         $Categories[$Row['CategoryID']] = str_repeat('&nbsp;&nbsp;&nbsp;', max(0, $Row['Depth'] - 1)).$Row['Name'];
      }
      $this->SetData('Categories', $Categories);

      // If seeing the form for the first time...
      if (!$this->Form->AuthenticatedPostBack()) {
         $this->Form->SetData($this->Message);
      } else {
         if ($MessageID = $this->Form->Save()) {
            // Reset the message cache
            $this->MessageModel->SetMessageCache();
            
            // Redirect
            $this->InformMessage(T('Your changes have been saved.'));
            //$this->RedirectUrl = Url('dashboard/message');
         }
      }
      $this->Render();
   }
922944
public function setMessageId($messageId)
    {
        if (is_integer($messageId) || is_float($messageId)) {
            $this->messageId = $messageId;
        } else {
            throw new InvalidArgumentException();
        }
    }
885452
public void setMessageId(String messageId) {
        if (messageId == null) {
            _properties.remove(AMQP_PROP_MESSAGE_ID);
            return;
        }
        
        _properties.put(AMQP_PROP_MESSAGE_ID, messageId);
    }
1214818
public Conversation updateConversation(final String id, final ConversationStatus status)
            throws UnauthorizedException, GeneralException {
        if (id == null) {
            throw new IllegalArgumentException("Id must be specified.");
        }
        String url = String.format("%s%s/%s", CONVERSATIONS_BASE_URL, CONVERSATION_PATH, id);
        return messageBirdService.sendPayLoad("PATCH", url, status, Conversation.class);
    }
1019035
public function UpdateUserCommentCounts($DiscussionID) {
      $Sql = "update ".$this->Database->DatabasePrefix."UserDiscussion ud
         set CountComments = (
            select count(c.CommentID)+1 
            from ".$this->Database->DatabasePrefix."Comment c 
            where c.DateInserted < ud.DateLastViewed
         )
         where DiscussionID = $DiscussionID";
      $this->SQL->Query($Sql);
   }
984625
public function addListenerId($listenerId)
    {
        if (null !== $this->getListenerId($listenerId)) {
            return;
        }
        
        $this->listeners[$listenerId] = $listenerId;
    }
1180267
public void sendMessage(@NonNull final String conversationId, @NonNull final MessageToSend message, @Nullable Callback<ComapiResult<MessageSentResponse>> callback) {
        adapter.adapt(sendMessage(conversationId, message), callback);
    }
528276
public static function delete_conversations_by_id($userid, array $conversationids) {
        global $CFG;

        // Check if private messaging between users is allowed.
        if (empty($CFG->messaging)) {
            throw new moodle_exception('disabled', 'message');
        }

        // Validate params.
        $params = [
            'userid' => $userid,
            'conversationids' => $conversationids,
        ];
        $params = self::validate_parameters(self::delete_conversations_by_id_parameters(), $params);

        // Validate context.
        $context = context_system::instance();
        self::validate_context($context);

        $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
        core_user::require_active_user($user);

        foreach ($params['conversationids'] as $conversationid) {
            if (\core_message\api::can_delete_conversation($user->id, $conversationid)) {
                \core_message\api::delete_conversation_by_id($user->id, $conversationid);
            } else {
                throw new moodle_exception("You do not have permission to delete the conversation '$conversationid'");
            }
        }

        return [];
    }
599587
public function getMessagesById($conversationId, $userId, $offset, $take)
    {
        return Conversation::with(
            [
                'messages' => function ($query) use ($userId, $offset, $take) {
                    $query->where(
                        function ($qr) use ($userId) {
                            $qr->where('user_id', '=', $userId)
                                ->where('deleted_from_sender', 0);
                        }
                    )
                        ->orWhere(
                            function ($q) use ($userId) {
                                $q->where('user_id', '!=', $userId)
                                    ->where('deleted_from_receiver', 0);
                            }
                        );

                    $query->offset($offset)->take($take);

                }
            ]
        )->with(['userone', 'usertwo'])->find($conversationId);

    }
236804
public static function is_user_in_conversation(int $userid, int $conversationid) : bool {
        global $DB;

        return $DB->record_exists('message_conversation_members', ['conversationid' => $conversationid,
            'userid' => $userid]);
    }
1268923
public function readMessage($messageId = null)
    {
        if (!is_null($messageId)) {
            $message = $this->message->with(['sender', 'conversation'])->find($messageId);

            if ($message->coversation->user_one == $this->authUserId || $message->coversation->user_two == $this->authUserId) {
                return $message;
            }
        }

        return false;
    }
1090931
public void getConversation(@NonNull final String conversationId, @Nullable Callback<ComapiResult<ConversationDetails>> callback) {
        adapter.adapt(getConversation(conversationId), callback);
    }
1327344
public function getConversationMessageById($conversationId, $userID, $channel)
    {
        $conversation = $this->query()->with(['messages', 'messages.sender', 'messages.files', 'firstUser', 'secondUser', 'files'])->find($conversationId);

        $collection = (object) null;
        $collection->conversationId = $conversationId;
        $collection->channel_name = $channel;
        $collection->user = ($conversation->firstUser->id == $userID) ? $conversation->secondUser : $conversation->firstUser;
        $collection->messages = $conversation->messages;
        $collection->files = $conversation->files;

        return collect($collection);
    }