You are here

public function UserController::buildSkillsList in Opigno statistics 8

Builds render array for a user skills list.

Parameters

\Drupal\user\UserInterface $user: User.

Return value

array Render array.

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

File

src/Controller/UserController.php, line 1370

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildSkillsList(UserInterface $user) {
  $content = [];
  $parent_terms = [];
  $parent_tids = [];
  $term_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $query = $this->database
    ->select('opigno_skills_statistic', 'a')
    ->fields('a', [
    'tid',
    'score',
    'progress',
    'stage',
  ])
    ->condition('a.uid', $user
    ->id());
  $user_skills = $query
    ->execute()
    ->fetchAllAssoc('tid');
  foreach ($user_skills as $skill) {
    if ($term_storage
      ->load($skill->tid)) {
      $parents = $term_storage
        ->loadAllParents($skill->tid);
      $parent = array_pop($parents);
      $parent_tids[$parent
        ->id()] = FALSE;
      if (empty($parent_terms[$parent
        ->id()])) {
        $tree = $term_storage
          ->loadTree('skills', $parent
          ->id());
        $depth = [];
        foreach ($tree as $skill_entity) {
          if (!isset($depth[$skill_entity->depth])) {
            $depth[$skill_entity->depth] = 0;
          }
          $depth[$skill_entity->depth]++;
        }
        $content[$parent
          ->id()]['width'] = max($depth);
        $tree_parent = new \stdClass();
        $tree_parent->tid = $parent
          ->id();
        $tree_parent->name = $parent
          ->getName();
        $tree_parent->parents[0] = 'empty';
        $tree[] = $tree_parent;
        $parent_terms[$parent
          ->id()] = $tree;
      }
    }
  }
  foreach ($parent_terms as $id => $skills) {
    foreach ($skills as $skill) {
      $content[$id]['skills'][$skill->tid] = [
        'id' => $skill->tid,
        'label' => $skill->name,
        'group' => $this
          ->t('pending'),
      ];
      $content[$id]['arrows'][$skill->tid] = [
        'from' => $skill->tid,
        'to' => $skill->parents[0],
      ];
      if (empty($user_skills[$skill->tid])) {
        unset($parent_tids[$skill->parents[0]]);
        continue;
      }
      if ($user_skills[$skill->tid]->stage == 0) {
        $content[$id]['skills'][$skill->tid]['group'] = $this
          ->t('done');
        if (isset($parent_tids[$skill->parents[0]])) {
          $parent_tids[$skill->parents[0]] = $id;
        }
      }
      else {
        unset($parent_tids[$skill->parents[0]]);
      }
      $content[$id]['skills'][$skill->tid]['title'] = $this
        ->t('Score:') . ' ' . $user_skills[$skill->tid]->score;
      $content[$id]['arrows'][$skill->tid]['label'] = $user_skills[$skill->tid]->progress . '%';
    }
  }
  $parent_tids = array_filter($parent_tids);
  foreach ($parent_tids as $id => $item) {
    $content[$item]['skills'][$id]['group'] = $this
      ->t('done');
  }
  return $content;
}