Find a discussion by ID, optionally making sure it is visible to a
certain user, or throw an exception.

@param int $id
@param User $user
@return \Flarum\Discussion\Discussion
347977
public function findOrFail($id, User $user = null)
    {
        $query = Discussion::where('id', $id);

        return $this->scopeVisibleTo($query, $user)->firstOrFail();
    }
747945
public static function read_user_evidence($id) {
        static::require_enabled();
        $userevidence = new user_evidence($id);

        if (!$userevidence->can_read()) {
            $context = $userevidence->get_context();
            throw new required_capability_exception($context, 'moodle/competency:userevidenceview', 'nopermissions', '');
        }

        return $userevidence;
    }
1310034
public function refresh(User $user)
    {
        if ($user['id'] == $this->session['user']['id']) {
            $this->loginByRecord($user);
        }

        return $this;
    }
436164
public function prepareUser($id)
    {
        $userClass = config('auth.providers.users.model');

        if (! class_exists($userClass)) {
            throw new Exception("User class {$userClass} not found.");
        }

        if(! $user = $userClass::whereId($id)->first()) {
            throw new Exception("User by ID {$id} not found.");
        }

        return $user;
    }
1089252
public function commentAction(Request $request, $resourceId, User $user)
    {
        $text = $request->get('social_media_comment_text');

        if ($text !== null) {
            $comment = new CommentAction();
            $comment->setText($text);
            $comment->setUser($user);
            $commentManager = $this->getCommentActionManager();
            $userIds = $commentManager->getHasCommentedUserIds($resourceId);
            $commentManager->createComment($resourceId, $comment);
            $this->dispatchCommentEvent($comment, $userIds);
        }
        if ($request->isXmlHttpRequest()) {
            $response = new JsonResponse(true);
        } else {
            $response = $this->redirectToRoute('icap_socialmedia_comments_view', array('resourceId' => $resourceId));
        }

        return $response;
    }
121739
public function stateFor(User $user)
    {
        $state = $this->state($user)->first();

        if (! $state) {
            $state = new UserState;
            $state->discussion_id = $this->id;
            $state->user_id = $user->id;
        }

        return $state;
    }
849399
public function get(int $id, ConversationRequest $conversationRequest = null): Conversation
    {
        $conversationResource = $this->restClient->getResource(Conversation::class, sprintf('/v2/conversations/%d', $id));

        return $this->hydrateConversationWithSubEntities($conversationResource, $conversationRequest ?: new ConversationRequest());
    }
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]);
    }
1049369
public static function collectByUser($user_id)
    {
        $user = AclUser::find($user_id);
        if ($user === null) {
            return collect([]);
        }

        return (new static)->where('group_id', $user->acl_group_id)->get();
    }
386953
public function show($id)
    {
        if(Request::isAjax()){
            $user = null;
            $message = '';

            try{
                $user = Sentry::findUserById($id);
            }catch(Exception $e){
                $message = $e->getMessage();
            }


            Response::headers()->set('Content-Type', 'application/json');
            Response::setBody(json_encode(
                array(
                    'success'   => !is_null($user),
                    'data'      => !is_null($user) ? $user->toArray() : $user,
                    'message'   => $message,
                    'code'      => is_null($user) ? 404 : 200
                )
            ));
        }else{

        }
    }
1121538
public function find($id)
    {
        return new self($this->_user, self::get($this->_user, $id));
    }
376468
public function show($id)
    {
        try {
            $thread = Thread::findOrFail($id);
        } catch (ModelNotFoundException $e) {
            Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');

            return redirect('message');
        }

        // show current user in list if not a current participant
        // $users = User::whereNotIn('id', $thread->participantsUserIds())->get();

        // don't show the current user in list
        $userId = Auth::user()->id;
        $users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get();

        $thread->markAsRead($userId);

        return view('chat.show', compact('thread', 'users'));
    }
1219355
public static function set_user_id($user_id) {
	if (self::is_active() == false) {
		$exception = bootstrap::get_library('exception');
		throw new $exception('inactive session');
	}
	
	$_SESSION['_session_user_id'] = $user_id;
}
649559
static function _get_user_by_id( $user_id ) {
            self::$_static_logger->entrance( "user_id = {$user_id}" );

            $users = self::get_all_users();

            if ( is_array( $users ) ) {
                if ( isset( $users[ $user_id ] ) &&
                     $users[ $user_id ] instanceof FS_User &&
                     $user_id == $users[ $user_id ]->id
                ) {
                    return $users[ $user_id ];
                }

                // If user wasn't found by the key, iterate over all the users collection.
                foreach ( $users as $user ) {
                    /**
                     * @var FS_User $user
                     */
                    if ( $user_id == $user->id ) {
                        return $user;
                    }
                }
            }

            return null;
        }
869315
public static function deleteUser($id)
    {
        $activity = static::find()->where(['user_id' => $id])->limit(1)->one();
        if (empty($activity) || !$activity->delete()) {
            Log::error('Cannot delete user activity', $id, __METHOD__);
            return;
        }
        Podium::getInstance()->podiumCache->delete('forum.lastactive');
    }
1208266
public static function get_self_conversation(int $userid) {
        global $DB;

        $conditions = [
            'type' => self::MESSAGE_CONVERSATION_TYPE_SELF,
            'convhash' => helper::get_conversation_hash([$userid])
        ];
        return $DB->get_record('message_conversations', $conditions);
    }
565340
public function resolveUserFunction($id)
    {
        if ($this->cache->contains($id)) {
            return $this->cache->fetch($id);
        }

        $user = $this->userRepository->findUserById($id);
        if (null === $user) {
            return;
        }

        $this->cache->save($id, $user);

        return $user;
    }