Check whether a user has read a post.

@param stdClass $user The user to check
@param post_entity $post The post to check
@return bool
430969
public function has_user_read_post(stdClass $user, post_entity $post) : bool {
        global $CFG;
        $isoldpost = ($post->get_time_modified() < (time() - ($CFG->forum_oldpostdays * 24 * 3600)));

        if ($isoldpost) {
            return true;
        }

        $receipts = isset($this->receiptsbypostid[$post->get_id()]) ? $this->receiptsbypostid[$post->get_id()] : [];

        foreach ($receipts as $receipt) {
            if ($receipt->userid == $user->id) {
                return true;
            }
        }

        return false;
    }
1263031
protected function check_is_post_type_allowed( $post_type ) {
		if ( ! is_object( $post_type ) ) {
			$post_type = get_post_type_object( $post_type );
		}

		if ( ! empty( $post_type ) && ! empty( $post_type->show_in_rest ) ) {
			return true;
		}

		return false;
	}
580094
protected function check_delete_permission( $post ) {
		$post_type = get_post_type_object( $post->post_type );

		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
			return false;
		}

		return current_user_can( $post_type->cap->delete_post, $post->ID );
	}
1281909
protected static function get_post_area_for_parent(\stdClass $post) {
        global $DB;

        $subcontext = [];
        if ($parent = $DB->get_record('forum_posts', ['id' => $post->parent], 'id, created, subject')) {
            $subcontext = array_merge($subcontext, static::get_post_area($parent));
        }
        $subcontext = array_merge($subcontext, static::get_post_area($post));

        return $subcontext;
    }
1148393
public function updateDataProtection($user = null) {
    if (!isset($user)) {
      $user = self::current();
    }

    $this->clientEnabledMethods = self::DEFAULT_CLIENT_ENABLED_METHODS;
    $this->privateData = self::DEFAULT_PRIVATE_DATA;
    $this->whitelistData = self::DEFAULT_WHITELIST_DATA;

    if (Tilmeld::$config['email_usernames']) {
      $this->privateData[] = 'username';
    }

    $isCurrentUser = $user !== null && $this->is($user);
    $isNewUser = !isset($this->guid);

    if ($isCurrentUser) {
      // Users can check to see what abilities they have.
      $this->clientEnabledMethods[] = 'gatekeeper';
      $this->clientEnabledMethods[] = 'changePassword';
      $this->clientEnabledMethods[] = 'logout';
      $this->clientEnabledMethods[] = 'sendEmailVerification';
    }

    if ($user !== null && $user->gatekeeper('tilmeld/admin')) {
      // Users who can edit other users can see most of their data.
      $this->privateData = [
        'password',
        'salt'
      ];
      $this->whitelistData = false;
    } elseif ($isCurrentUser || $isNewUser) {
      // Users can see their own data, and edit some of it.
      $this->whitelistData[] = 'username';
      $this->whitelistData[] = 'avatar';
      if (in_array('name', Tilmeld::$config['user_fields'])) {
        $this->whitelistData[] = 'nameFirst';
        $this->whitelistData[] = 'nameMiddle';
        $this->whitelistData[] = 'nameLast';
        $this->whitelistData[] = 'name';
      }
      if (in_array('email', Tilmeld::$config['user_fields'])) {
        $this->whitelistData[] = 'email';
      }
      if (in_array('phone', Tilmeld::$config['user_fields'])) {
        $this->whitelistData[] = 'phone';
      }
      if (in_array('timezone', Tilmeld::$config['user_fields'])) {
        $this->whitelistData[] = 'timezone';
      }
      if (in_array('address', Tilmeld::$config['user_fields'])) {
        $this->whitelistData[] = 'addressType';
        $this->whitelistData[] = 'addressStreet';
        $this->whitelistData[] = 'addressStreet2';
        $this->whitelistData[] = 'addressCity';
        $this->whitelistData[] = 'addressState';
        $this->whitelistData[] = 'addressZip';
        $this->whitelistData[] = 'addressInternational';
      }
      $this->privateData = [
        'originalEmail',
        'secret',
        'cancelEmailAddress',
        'cancelEmailSecret',
        'emailChangeDate',
        'recoverSecret',
        'recoverSecretTime',
        'password',
        'salt'
      ];
    }
  }
1135462
public function update(User $user, User $entity)
    {
        if ($entity->id === $user->id) {
            return true;
        }
        return $user->hasPermission('user-update');
    }
1374804
protected function check_create_permission( $post ) {
		$post_type = get_post_type_object( $post->post_type );

		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
			return false;
		}

		return current_user_can( $post_type->cap->create_posts );
	}
666834
public function deletePost(Blog $blog, Post $post, User $user)
    {
        //remove tags beforehand
        $event = new GenericDataEvent([
            'tags' => [],
            'data' => [
                [
                    'class' => 'Icap\BlogBundle\Entity\Post',
                    'id' => $post->getUuid(),
                    'name' => $post->getTitle(),
                ],
            ],
            'replace' => true,
        ]);
        $this->eventDispatcher->dispatch('claroline_tag_multiple_data', $event);

        $this->om->remove($post);
        $this->om->flush();

        $this->trackingManager->dispatchPostDeleteEvent($post);
    }
1006050
public function can_edit_post(stdClass $user, discussion_entity $discussion, post_entity $post) : bool {
        global $CFG;

        $context = $this->get_context();
        $ownpost = $post->is_owned_by_user($user);
        $ineditingtime = $post->get_age() < $CFG->maxeditingtime;

        switch ($this->forum->get_type()) {
            case 'news':
                // Allow editing of news posts once the discussion has started.
                $ineditingtime = !$post->has_parent() && $discussion->has_started();
                break;
            case 'single':
                return $discussion->is_first_post($post) && has_capability('moodle/course:manageactivities', $context, $user);
        }

        return ($ownpost && $ineditingtime) || has_capability('mod/forum:editanypost', $context, $user);
    }
172107
public function get_post_from_stdclass(stdClass $record) : post_entity {
        return new post_entity(
            $record->id,
            $record->discussion,
            $record->parent,
            $record->userid,
            $record->created,
            $record->modified,
            $record->mailed,
            $record->subject,
            $record->message,
            $record->messageformat,
            $record->messagetrust,
            $record->attachment,
            $record->totalscore,
            $record->mailnow,
            $record->deleted,
            $record->privatereplyto
        );
    }
100183
public function getReadIds(User $user)
    {
        return Discussion::leftJoin('discussion_user', 'discussion_user.discussion_id', '=', 'discussions.id')
            ->where('discussion_user.user_id', $user->id)
            ->whereColumn('last_read_post_number', '>=', 'last_post_number')
            ->pluck('id')
            ->all();
    }
1282067
private static function activated(array $data, $user)
    {
        $input = array_keys($data);

        $field = $input[0];
        $value = $data[$field];

        if ($user->$field != $value) {
            return false;
        }

        return true;
    }
361489
public function can_reply_privately_to_post(stdClass $user, post_entity $post) : bool {
        if ($post->is_private_reply()) {
            // You cannot reply privately to a post which is, itself, a private reply.
            return false;
        }

        return has_capability('mod/forum:postprivatereply', $this->get_context(), $user);
    }
851040
public function markAllAsRead(User $user)
    {
        Notification::where('user_id', $user->id)->update(['read_at' => Carbon::now()]);
    }
284695
public static function is_context_expired_or_unprotected_for_user(\context $context, \stdClass $user) : bool {
        // User/course contexts can't expire if no purpose is set in the system context.
        if (!data_registry::defaults_set()) {
            return false;
        }

        $parents = $context->get_parent_contexts(true);
        foreach ($parents as $parent) {
            if ($parent instanceof \context_course) {
                // This is a context within a course. Check whether _this context_ is expired as a function of a course.
                return self::is_course_context_expired_or_unprotected_for_user($context, $user);
            }

            if ($parent instanceof \context_user) {
                // This is a context within a user. Check whether the _user_ has expired.
                return self::are_user_context_dependencies_expired($parent);
            }
        }

        return false;
    }
693620
public function isApiEnabledForUser(User $user)
    {
        $roles = $user->roles->pluck('id')->toArray();
        return !count($roles) ? false : $this->newQuery()->whereIn('role_id', array_values($roles))->where('enabled', 1)->get()->count() > 0;
    }
970543
protected function readValue(User $user, string $attribute) : void
    {
        $user->$attribute = $this->prompt(mb_convert_case($attribute, MB_CASE_TITLE, 'utf-8') . ':', [
            'validator' => function ($input, &$error) use ($user, $attribute) {
                $user->$attribute = $input;
                if ($user->validate([$attribute])) {
                    return true;
                } else {
                    $error = implode(',', $user->getErrors($attribute));
                    return false;
                }
            },
        ]);
    }