You are here

public function UserController::index in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x 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 1624

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function index(UserInterface $user) {
  $moduleHandler = \Drupal::service('module_handler');
  $content = [];
  $content[] = $this
    ->buildUserInfo($user);
  $content[] = $this
    ->buildBadges($user);
  if ($moduleHandler
    ->moduleExists('opigno_skills_system') && !empty($skills = $this
    ->buildSkillsList($user))) {
    $library_exists = file_exists('libraries/vis/dist/vis.js') && file_exists('libraries/vis/dist/vis.css');
    if ($library_exists && \Drupal::service('library.discovery')
      ->getLibraryByName('opigno_skills_system', 'opigno_skills_using_vis')) {
      $content['#attached']['library'][] = 'opigno_skills_system/opigno_skills_using_vis';
      $content['#attached']['library'][] = 'opigno_skills_system/opigno_skills_entity';
      foreach ($skills as $target_skill => $tree) {
        $html = [
          '#markup' => '<div id="opigno-skills-tree-' . $target_skill . '"></div>',
        ];
        $tree_name = '(empty)';
        $term_storage = \Drupal::entityTypeManager()
          ->getStorage('taxonomy_term');
        $target_skill_entity = $term_storage
          ->load($target_skill);
        if ($target_skill_entity) {
          $tree_name = $target_skill_entity
            ->label();
        }
        $content[] = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'opigno_skills_tree_wrapper',
            ],
          ],
          [
            '#type' => 'table',
            '#attributes' => [
              'class' => [
                'statistics-table',
                'statistics-graph',
                'skills-list',
                'table-striped',
              ],
            ],
            '#header' => [
              $this
                ->t('Skills tree "') . $tree_name . '"',
            ],
            '#rows' => [
              [
                \Drupal::service('renderer')
                  ->render($html),
              ],
            ],
          ],
        ];
        $content[] = $this
          ->buildSkillsTable($user, array_keys($tree['skills']));
      }
      $content['#attached']['drupalSettings']['opigno_skills_tree'] = $skills;
    }
    else {
      $message = $this
        ->t('"almende/vis" library is not installed. Please install it from <a href="@library">here</a> and place in <em>libraries/</em> folder', [
        '@library' => 'https://github.com/almende/vis.git',
      ]);
      $this
        ->messenger()
        ->addWarning($message);
    }
  }
  $content[] = $this
    ->buildTrainingsList($user);
  $content['#attached']['library'][] = 'opigno_statistics/user';
  return $content;
}