You are here

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

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

Builds render array for a user statistics index page.

Parameters

\Drupal\user\UserInterface $user: User.

Return value

array Render array.

1 string reference to 'UserController::index'
opigno_statistics.routing.yml in ./opigno_statistics.routing.yml
opigno_statistics.routing.yml

File

src/Controller/UserController.php, line 1488

Class

UserController
Statistics user controller.

Namespace

Drupal\opigno_statistics\Controller

Code

public function index(UserInterface $user) : array {
  $uid = (int) $user
    ->id();
  $days = 7;

  // Check if the profile is private and if the current user has an access to
  // view it.
  $is_private = $user
    ->hasField('field_private_profile') && (bool) $user
    ->get('field_private_profile')
    ->getString();
  $can_access_private = $this->userAccessManager
    ->canViewPrivateProfile($user);

  // Check if the user is in network.
  $network = $this->connectionsManager
    ->getUserNetwork();
  $is_in_network = in_array($uid, $network);

  // Charts and trends should be accessible only for friends of not-private
  // profiles or to users with the special permissions.
  $stats = [];
  $attached = [];
  if (!$is_private && $is_in_network || $can_access_private) {
    $stats = [
      'trends' => $this->statsManager
        ->renderUserStatistics($days, $uid),
      'charts' => $this->statsManager
        ->renderUserTrainingsCharts($uid),
    ];
    $attached = [
      'library' => [
        'opigno_statistics/opigno_charts',
      ],
    ];
  }

  // Trainings list should be accessible only to the user and to those who
  // have the specific permissions.
  $trainings_visible = $this->userAccessManager
    ->canAccessUserStatistics($user);
  return [
    '#theme' => 'opigno_user_statistics_page',
    '#user_info' => $this
      ->buildUserInfo($user, $is_private, $can_access_private, $is_in_network),
    '#trainings' => $trainings_visible ? $this->statsManager
      ->buildTrainingsList($uid) : [],
    '#stats' => $stats,
    '#attached' => $attached,
  ];
}