You are here

public function UserController::buildUserInfo in Opigno statistics 3.x

Same name and namespace in other branches
  1. 8 src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::buildUserInfo()

Builds render array for a user info block.

Parameters

\Drupal\user\UserInterface $user: User.

bool $is_private: Whether the user's profile private or not.

bool $can_access_private: If the current user can access the private profile info or not.

bool $is_in_network: If the viewed user in the connections list of thr current one.

Return value

array Data to display the user info.

1 call to UserController::buildUserInfo()
UserController::index in src/Controller/UserController.php
Builds render array for a user statistics index page.

File

src/Controller/UserController.php, line 129

Class

UserController
Statistics user controller.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildUserInfo(UserInterface $user, bool $is_private, bool $can_access_private, bool $is_in_network) {
  $uid = (int) $user
    ->id();
  $build = [
    'picture' => UserStatisticsManager::getUserPicture($user, 'user_profile'),
    'name' => $user
      ->getDisplayName(),
  ];

  // Only first and last name should be accessible for the private profile.
  // Users with specific permissions can see the full info.
  if ($is_private && !$can_access_private) {
    return $build;
  }

  // Email should be accessible for not-private profiles.
  $build['email'] = $user
    ->getEmail();
  if (!$is_private && !$is_in_network && !$can_access_private) {
    return $build;
  }
  $created_timestamp = $user
    ->getCreatedTime();
  $accessed_timestamp = $user
    ->getLastAccessedTime();

  // Achievements.
  $trainings = $this->statsManager
    ->getUserTrainingsNumberByStatus(0, $uid);
  $certificates = $this->statsManager
    ->getUserCertificates(0, $uid);
  $badges = $this->statsManager
    ->getUserBadges($uid);
  $skills = count($this->statsManager
    ->getSkillsAcquired($uid));
  $achievements_url = Url::fromRoute('opigno_statistics.user_achievements_page', [
    'user' => $uid,
  ]);
  return $build + [
    'data' => [
      'edit_link' => $user
        ->access('update') ? Url::fromRoute('entity.user.edit_form', [
        'user' => $uid,
      ])
        ->toString() : '',
      'role' => $this->statsManager
        ->getUserRole($user),
      'join' => $this->dateFormatter
        ->format($created_timestamp, 'year_month_day'),
      'access' => $this->dateFormatter
        ->format($accessed_timestamp, 'year_month_day'),
      'member_for' => $this->dateFormatter
        ->formatTimeDiffSince($created_timestamp),
      'connections' => $this->connectionsManager
        ->renderUserConnectionsBlock($uid),
      'achievements' => [
        'trainings' => [
          '#theme' => 'opigno_statistics_user_achievement',
          '#count' => $trainings,
          '#text' => $this
            ->formatPlural($trainings, 'Training completed', 'Trainings completed'),
          '#img' => $this
            ->getAchievementImagePath('trainings.svg'),
          '#url' => $this
            ->buildAchievementsPageUrl($achievements_url),
        ],
        'certificates' => [
          '#theme' => 'opigno_statistics_user_achievement',
          '#count' => $certificates,
          '#text' => $this
            ->formatPlural($certificates, 'Certificate received', 'Certificates received'),
          '#img' => $this
            ->getAchievementImagePath('certificate.svg'),
          '#url' => $this
            ->buildAchievementsPageUrl($achievements_url, UserAchievements::CERTIFICATES_TAB),
        ],
        'badges' => [
          '#theme' => 'opigno_statistics_user_achievement',
          '#count' => $badges,
          '#text' => $this
            ->formatPlural($badges, 'Badge earned', 'Badges earned'),
          '#img' => $this
            ->getAchievementImagePath('badges.svg'),
          '#url' => $this
            ->buildAchievementsPageUrl($achievements_url, UserAchievements::BADGES_TAB),
        ],
        'skills' => [
          '#theme' => 'opigno_statistics_user_achievement',
          '#count' => $skills,
          '#text' => $this
            ->formatPlural($skills, 'Skill acquired', 'Skills acquired'),
          '#img' => $this
            ->getAchievementImagePath('skills.svg'),
          '#url' => $this
            ->buildAchievementsPageUrl($achievements_url, UserAchievements::SKILLS_TAB),
        ],
      ],
    ],
  ];
}