You are here

protected function DashboardForm::buildSkillsTable in Opigno statistics 3.x

Same name and namespace in other branches
  1. 8 src/Form/DashboardForm.php \Drupal\opigno_statistics\Form\DashboardForm::buildSkillsTable()

Builds skills listing.

Return value

array Render array.

1 call to DashboardForm::buildSkillsTable()
DashboardForm::buildForm in src/Form/DashboardForm.php
Form constructor.

File

src/Form/DashboardForm.php, line 550

Class

DashboardForm
Implements the statistics dashboard.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildSkillsTable() : array {
  $query = $this->database
    ->select('opigno_skills_statistic', 'a')
    ->fields('a', [
    'tid',
  ]);
  $query
    ->addExpression('AVG(a.score)', 'score');
  $query
    ->addExpression('AVG(a.progress)', 'progress');
  $query
    ->groupBy('tid');
  $rows = $query
    ->execute()
    ->fetchAllAssoc('tid');
  $table_rows = [];
  foreach ($rows as $row) {
    if (!$this->termStorage instanceof TermStorageInterface) {
      continue;
    }
    $tid = $row->tid ?? 0;
    $term = $this->termStorage
      ->load($tid);
    if ($term instanceof TermInterface) {
      $table_rows[] = [
        'data-training' => $row->tid,
        'data' => [
          [
            'data' => $term
              ->getName(),
            'class' => 'skill',
          ],
          [
            'data' => $this
              ->buildScore(round($row->score)),
            'class' => 'score',
          ],
          [
            'data' => $this
              ->buildScore(round($row->progress)),
            'class' => 'progress',
          ],
        ],
      ];
    }
  }
  $rows = array_filter($table_rows);
  if (empty($rows)) {
    return [];
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'content-box',
        'skills-list',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'skills-content-title',
        ],
      ],
      '#value' => $this
        ->t('Skills'),
    ],
    'table' => [
      '#type' => 'table',
      '#prefix' => '<div class="skills-list-wrapper">',
      '#suffix' => '</div>',
      '#attributes' => [
        'class' => [
          'statistics-table',
          'table-striped',
        ],
      ],
      '#header' => [
        [
          'data' => $this
            ->t('Skill'),
          'class' => 'skill',
        ],
        [
          'data' => $this
            ->t('Score'),
          'class' => 'score',
        ],
        [
          'data' => $this
            ->t('Progress'),
          'class' => 'progress',
        ],
      ],
      '#rows' => $rows,
    ],
  ];
}