You are here

public function UserStatisticsManager::buildSkillsList in Opigno statistics 3.x

Build the user skills list table.

Parameters

int $uid: The user ID to build the skills list for.

Return value

array The render array to build the user skills list table.

File

src/Services/UserStatisticsManager.php, line 881

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function buildSkillsList(int $uid) : array {
  $rows = $this
    ->getSkillsAcquired($uid);
  $table_rows = [];
  $build = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'skills-list',
      ],
    ],
    '#header' => [
      [
        'data' => $this
          ->t('Name'),
        'class' => 'name',
      ],
      [
        'data' => $this
          ->t('Level'),
        'class' => 'level',
      ],
    ],
  ];
  if (!$rows || !$this->termStorage instanceof TermStorageInterface) {
    return $build + [
      '#rows' => $table_rows,
    ];
  }

  // Build the list of user skills.
  foreach ($rows as $row) {
    $tid = $row->tid ?? NULL;
    if (!$tid) {
      continue;
    }
    $term = $this->termStorage
      ->load($tid);
    if (!$term instanceof TermInterface || !$term
      ->hasField('field_level_names')) {
      continue;
    }

    // Get the total amount of the skill levels.
    $levels = $term
      ->get('field_level_names')
      ->getValue();
    $levels_amount = count($levels);
    $not_completed_amount = $row->stage ?? 0;
    $result = [];

    // Generate the skill level markup.
    for ($i = $levels_amount; $i > 0; $i--) {
      $result[] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'class' => [
            'skill-stage',
            $i > $not_completed_amount ? 'skill-stage__completed' : 'skill-stage__not-completed',
          ],
        ],
      ];
    }
    $table_rows[] = [
      [
        'data' => $term
          ->label(),
        'class' => 'name',
      ],
      [
        'data' => $result,
        'class' => 'level',
      ],
    ];
  }
  return $build + [
    '#rows' => $table_rows,
  ];
}