Mark all conversation messages as read function.

@param int $userid The user id of who we want to delete the conversation for
@param int $conversationid The id of the conversations
@since 3.6
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');
        }
    }
1079560
private function migrate_data($userid, $otheruserid) {
        global $DB;

        if ($userid == $otheruserid) {
            // Since 3.7, pending self-conversations should be migrated during the upgrading process so shouldn't be any
            // self-conversations on the legacy tables. However, this extra-check has been added just in case.
            $conversation = \core_message\api::get_self_conversation($userid);
            if (empty($conversation)) {
                $conversation = \core_message\api::create_conversation(
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
                    [$userid]
                );
            }
            $conversationid = $conversation->id;
        } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userid, $otheruserid])) {
            $conversation = \core_message\api::create_conversation(
                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
                [
                    $userid,
                    $otheruserid
                ]
            );
            $conversationid = $conversation->id;
        }

        // First, get the rows from the 'message' table.
        $select = "(useridfrom = ? AND useridto = ?) OR (useridfrom = ? AND useridto = ?)";
        $params = [$userid, $otheruserid, $otheruserid, $userid];
        $messages = $DB->get_recordset_select('message', $select, $params, 'id ASC');
        foreach ($messages as $message) {
            if ($message->notification) {
                $this->migrate_notification($message, false);
            } else {
                $this->migrate_message($conversationid, $message);
            }
        }
        $messages->close();

        // Ok, all done, delete the records from the 'message' table.
        $DB->delete_records_select('message', $select, $params);

        // Now, get the rows from the 'message_read' table.
        $messages = $DB->get_recordset_select('message_read', $select, $params, 'id ASC');
        foreach ($messages as $message) {
            if ($message->notification) {
                $this->migrate_notification($message, true);
            } else {
                $this->migrate_message($conversationid, $message);
            }
        }
        $messages->close();

        // Ok, all done, delete the records from the 'message_read' table.
        $DB->delete_records_select('message_read', $select, $params);
    }
317368
public function deleteUser($userId)
    {
        // Forget remembered users.
        $this->userEntities = [];

        $query = $this->deleteUserQuery($userId);

        return $query->execute();
    }
677846
public function GetRecipients($ConversationID, $IgnoreUserID = '0') {
      $Data = $this->SQL
         ->Select('uc.*')
         ->From('UserConversation uc')
         ->Where('uc.ConversationID', $ConversationID)
         ->Get();
      
      Gdn::UserModel()->JoinUsers($Data->Result(), array('UserID'));
      return $Data;
   }
808205
public static function disable_conversation(int $conversationid) {
        global $DB;

        $conversation = new \stdClass();
        $conversation->id = $conversationid;
        $conversation->enabled = self::MESSAGE_CONVERSATION_DISABLED;
        $conversation->timemodified = time();
        $DB->update_record('message_conversations', $conversation);
    }
821612
public function checkUserExist($userId, $groupConversationId)
    {
        $group = $this->find($groupConversationId);

        if ($group) {
            foreach ($group->users()->get() as $member) {
                if ($member->id == $userId) {
                    return true;
                }
            }
        }

        return false;
    }
197965
public function GetNew($ConversationID, $LastMessageID) {
      $Session = Gdn::Session();
      $this->SQL->Where('MessageID > ', $LastMessageID);
      return $this->Get($ConversationID, $Session->UserID);
   }
329372
public function updateSubject(int $conversationId, string $subject): void
    {
        $patch = Patch::replace('subject', $subject);
        $this->patchConversation($conversationId, $patch);
    }
924008
public function markAsRead(int $messageId)
    {
        $this->sql = 'update message set is_read = \'Y\' where id = ?';
        $this->bindValues[] = $messageId;

        return $this->execute();
    }
1180850
function(conversationId, userId, text, callback) {
      send(null, conversationId, utils.messageText('user_id', userId, text), callback);
    }
867041
protected function empty_queue(int $userid, int $timemodified) : void {
        global $DB;

        $DB->delete_records_select('forum_queue', "userid = :userid AND timemodified < :timemodified", [
                'userid' => $userid,
                'timemodified' => $timemodified,
            ]);
    }
1173345
public static function is_contact(int $userid, int $contactid) : bool {
        global $DB;

        $sql = "SELECT id
                  FROM {message_contacts} mc
                 WHERE (mc.userid = ? AND mc.contactid = ?)
                    OR (mc.userid = ? AND mc.contactid = ?)";
        return $DB->record_exists_sql($sql, [$userid, $contactid, $contactid, $userid]);
    }
965428
private function get_conversations_for_user(int $userid) : moodle_recordset {
        global $DB;

        // We shouldn't be joining directly on the group table as group
        // conversations may (in the future) be something created that
        // isn't related to an actual group in a course. However, for
        // now this will have to do before 3.7 code freeze.
        // See related MDL-63814.
        $sql = "SELECT DISTINCT mc.id, mc.name, c.id as courseid, c.fullname as coursename, g.id as groupid,
                                g.picture, g.hidepicture
                  FROM {message_conversations} mc
                  JOIN {groups} g
                    ON mc.itemid = g.id
                  JOIN {course} c
                    ON g.courseid = c.id
                  JOIN {message_email_messages} mem
                    ON mem.conversationid = mc.id
                 WHERE mem.useridto = ?
                   AND mem.id <= ?";

        return $DB->get_recordset_sql($sql, [$userid, $this->maxid]);
    }
245344
public function markAsUnread(int $messageId)
    {
        $this->sql = 'update message set is_read = \'N\' where id = ?';
        $this->bindValues[] = $messageId;

        return $this->execute();
    }
808437
public function getReceiverInfo($conversationId)
    {
        $conversation = $this->conversation->find($conversationId);
        $receiver = '';
        if ($conversation->user_one == $this->authUserId) {
            $receiver = $conversation->user_two;
        } else {
            $receiver = $conversation->user_one;
        }

        $userModel = $this->config('talk.user.model');
        $user = new $userModel();

        return $user->find($receiver);
    }
734604
public function userUnreadMessages($userId)
    {
        $messages = $this->messages()->get();

        try {
            $participant = $this->getParticipantFromUser($userId);
        } catch (ModelNotFoundException $e) {
            return collect();
        }

        if (!$participant->last_read) {
            return $messages;
        }

        return $messages->filter(function ($message) use ($participant) {
            return $message->updated_at->gt($participant->last_read);
        });
    }
882285
protected static function delete_user_data($userid) {
        global $DB;
        $DB->delete_records_select('messageinbound_messagelist', 'userid = :userid', ['userid' => $userid]);
        \core_userkey\privacy\provider::delete_userkeys('messageinbound_handler', $userid);
    }