Get the contacts for a given user.

@param int $userid
@param int $limitfrom
@param int $limitnum
@return array An array of contacts
517766
public static function get_user_contacts(int $userid, int $limitfrom = 0, int $limitnum = 0) {
        global $DB;

        $sql = "SELECT *
                  FROM {message_contacts} mc
                 WHERE mc.userid = ? OR mc.contactid = ?
              ORDER BY timecreated DESC, id ASC";
        if ($contacts = $DB->get_records_sql($sql, [$userid, $userid], $limitfrom, $limitnum)) {
            $userids = [];
            foreach ($contacts as $contact) {
                if ($contact->userid == $userid) {
                    $userids[] = $contact->contactid;
                } else {
                    $userids[] = $contact->userid;
                }
            }
            return helper::get_member_info($userid, $userids);
        }

        return [];
    }
695769
public static function confirm_contact_request(int $userid, int $requesteduserid) {
        global $DB;

        if ($request = $DB->get_record('message_contact_requests', ['userid' => $userid,
                'requesteduserid' => $requesteduserid])) {
            self::add_contact($userid, $requesteduserid);

            $DB->delete_records('message_contact_requests', ['id' => $request->id]);
        }
    }
256343
public static function can_create_contact(int $userid, int $requesteduserid) : bool {
        global $CFG;

        // If we can't message at all, then we can't create a contact.
        if (empty($CFG->messaging)) {
            return false;
        }

        // If we can message anyone on the site then we can create a contact.
        if ($CFG->messagingallusers) {
            return true;
        }

        // We need to check if they are in the same course.
        return enrol_sharing_course($userid, $requesteduserid);
    }
1062151
public function syncMappingUser(int $userId): array
    {
        return $this->getApi()->request($this->getApi()->sprintf('/admin/ldap/user/:user_id/sync', (string)$userId),
            Request::METHOD_POST);
    }
407540
public function getUserMedia($id = 'self', $limit = 0)
    {
        $params = array();

        if ($limit > 0) {
            $params['count'] = $limit;
        }

        return $this->_makeCall('users/' . $id . '/media/recent', strlen($this->getAccessToken()), $params);
    }
1082199
public function getUser(string $userid)
    {
        $cl = $this->getUserClass();
        $user = new $cl;
        return $user->obtainByUserID($userid);
    }
397277
public static function unblock_user(int $userid, int $usertounblockid) {
        global $DB;

        if ($blockeduser = $DB->get_record('message_users_blocked',
                ['userid' => $userid, 'blockeduserid' => $usertounblockid])) {
            $DB->delete_records('message_users_blocked', ['id' => $blockeduser->id]);

            // Trigger event for unblocking a contact.
            $event = \core\event\message_user_unblocked::create(array(
                'objectid' => $blockeduser->id,
                'userid' => $userid,
                'relateduserid' => $usertounblockid,
                'context' => \context_user::instance($userid)
            ));
            $event->add_record_snapshot('message_users_blocked', $blockeduser);
            $event->trigger();
        }
    }
1225898
public function findByStatusIdsAndUser($locale, $statusIds, UserInterface $user, $limit = null)
    {
        try {
            $qb = $this->getOrderQuery($locale)
                ->andWhere('o.creator = :user')
                ->setParameter('user', $user)
                ->andWhere('status.id IN (:statusId)')
                ->setParameter('statusId', $statusIds)
                ->orderBy('o.created', 'DESC')
                ->setMaxResults($limit);

            return $qb->getQuery()->getResult();
        } catch (NoResultException $exc) {
            return null;
        }
    }
999560
protected static function get_records($userid) {
        global $DB;

        $sql = "SELECT *
                  FROM {user_info_data} uda
                  JOIN {user_info_field} uif ON uda.fieldid = uif.id
                 WHERE uda.userid = :userid
                       AND uif.datatype = :datatype";
        $params = [
            'userid' => $userid,
            'datatype' => 'checkbox'
        ];

        return $DB->get_records_sql($sql, $params);
    }
224958
public static function get_contexts_for_userid(int $userid) : contextlist {
        $params = ['userid' => $userid, 'contextcourse' => CONTEXT_COURSE];
        $sql = "SELECT ctx.id
                FROM {context} ctx
                JOIN {stats_user_daily} sud ON sud.courseid = ctx.instanceid AND sud.userid = :userid
                WHERE ctx.contextlevel = :contextcourse";

        $contextlist = new contextlist();
        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                FROM {context} ctx
                JOIN {stats_user_weekly} suw ON suw.courseid = ctx.instanceid AND suw.userid = :userid
                WHERE ctx.contextlevel = :contextcourse";
        $contextlist->add_from_sql($sql, $params);

        $sql = "SELECT ctx.id
                FROM {context} ctx
                JOIN {stats_user_monthly} sum ON sum.courseid = ctx.instanceid AND sum.userid = :userid
                WHERE ctx.contextlevel = :contextcourse";
        $contextlist->add_from_sql($sql, $params);

        return $contextlist;
    }
1139426
public function getPendingFriendships($limit = null, $offset = 0): array
    {
        return $this->findFriendshipsByStatus(Status::PENDING, $limit, $offset);
    }
803600
public function Get2($UserID, $Offset = 0, $Limit = FALSE) {
      if (!$Limit) 
         $Limit = C('Conversations.Conversations.PerPage', 30);
      
      // The self join is intentional in order to force the query to us an index-scan instead of a table-scan.
      $Data = $this->SQL
         ->Select('c.*')
         ->Select('uc2.DateLastViewed')
         ->Select('uc2.CountReadMessages')
         ->Select('uc2.LastMessageID', '', 'UserLastMessageID')
         ->From('UserConversation uc')
         ->Join('UserConversation uc2', 'uc.ConversationID = uc2.ConversationID and uc.UserID = uc2.UserID')
         ->Join('Conversation c', 'c.ConversationID = uc2.ConversationID')
         ->Where('uc.UserID', $UserID)
         ->Where('uc.Deleted', 0)
         ->OrderBy('uc.DateConversationUpdated', 'desc')
         ->Limit($Limit, $Offset)
         ->Get();
      
      $Data->DatasetType(DATASET_TYPE_ARRAY);
      $Result =& $Data->Result();
      
      // Add some calculated fields.
      foreach ($Result as &$Row) {
         if ($Row['UserLastMessageID'])
            $Row['LastMessageID'] = $Row['UserLastMessageID'];
         $Row['CountNewMessages'] = $Row['CountMessages'] - $Row['CountReadMessages'];
         unset($Row['UserLastMessageID']);
      }
      
      // Join the participants.
      $this->JoinParticipants($Result);
      
      // Join in the last message.
      Gdn_DataSet::Join($Result, 
         array(
             'table' => 'ConversationMessage',
             'prefix' => 'Last',
             'parent' => 'LastMessageID', 
             'child' => 'MessageID', 
             'InsertUserID', 'DateInserted', 'Body', 'Format'));
      
      return $Data;
   }
1015786
public function getContactsAsync($limit = null, $starting_after = null, $ending_before = null)
    {
        return $this->getContactsAsyncWithHttpInfo($limit, $starting_after, $ending_before)
            ->then(
                function ($response) {
                    return $response[0];
                }
            );
    }
701577
public static function block_user(int $userid, int $usertoblockid) {
        global $DB;

        $blocked = new \stdClass();
        $blocked->userid = $userid;
        $blocked->blockeduserid = $usertoblockid;
        $blocked->timecreated = time();
        $blocked->id = $DB->insert_record('message_users_blocked', $blocked);

        // Trigger event for blocking a contact.
        $event = \core\event\message_user_blocked::create(array(
            'objectid' => $blocked->id,
            'userid' => $userid,
            'relateduserid' => $usertoblockid,
            'context' => \context_user::instance($userid)
        ));
        $event->add_record_snapshot('message_users_blocked', $blocked);
        $event->trigger();
    }
672536
public static function data_for_messagearea_search_users($userid, $search, $limitnum = 0) {
        global $CFG, $PAGE, $USER;

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

        $systemcontext = context_system::instance();

        $params = array(
            'userid' => $userid,
            'search' => $search,
            'limitnum' => $limitnum
        );
        $params = self::validate_parameters(self::data_for_messagearea_search_users_parameters(), $params);
        self::validate_context($systemcontext);

        if (($USER->id != $params['userid']) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
            throw new moodle_exception('You do not have permission to perform this action.');
        }

        list($contacts, $courses, $noncontacts) = \core_message\api::search_users(
            $params['userid'],
            $params['search'],
            $params['limitnum']
        );

        $search = new \core_message\output\messagearea\user_search_results($contacts, $courses, $noncontacts);

        $renderer = $PAGE->get_renderer('core_message');
        return $search->export_for_template($renderer);
    }
1060388
public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
        $contextlist = new \core_privacy\local\request\contextlist();

        // Always add the user context so we're sure we're not dodging user keys, besides it's not costly to do so.
        $contextlist->add_user_context($userid);

        return $contextlist;
    }
115718
public function Get($ViewingUserID, $Offset = '0', $Limit = '') {
      if ($Limit == '') 
         $Limit = Gdn::Config('Conversations.Conversations.PerPage', 30);

      $Offset = !is_numeric($Offset) || $Offset < 0 ? 0 : $Offset;
      
      // Grab the base list of conversations.
      $Data = $this->SQL
         ->Select('c.*')
         ->Select('uc.CountReadMessages')
         ->Select('uc.LastMessageID', '', 'UserLastMessageID')
         ->From('UserConversation uc')
         ->Join('Conversation c', 'uc.ConversationID = c.ConversationID')
         ->Where('uc.UserID', $ViewingUserID)
         ->Where('uc.Deleted', 0)
         ->OrderBy('c.DateUpdated', 'desc')
         ->Limit($Limit, $Offset)
         ->Get()->ResultArray();
      
      $this->JoinLastMessages($Data);
      return $Data;
   }