You are here

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

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

Builds trainings listing.

Parameters

array $lp_ids: Learning path IDs.

Return value

array Render array.

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

File

src/Form/DashboardForm.php, line 362

Class

DashboardForm
Implements the statistics dashboard.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildTrainingsList(array $lp_ids) : array {
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a');
  $query
    ->addExpression('COUNT(a.completed)', 'users_completed');
  $query
    ->addExpression('AVG(a.time)', 'time');
  $query
    ->fields('a', [
    'gid',
    'name',
  ]);
  if ($lp_ids) {
    $query
      ->condition('a.gid', $lp_ids, 'IN');
  }
  $data = $query
    ->groupBy('a.gid')
    ->groupBy('a.name')
    ->orderBy('a.name')
    ->distinct()
    ->execute()
    ->fetchAll();
  $query = $this->database
    ->select('opigno_learning_path_group_user_status', 's');
  $query
    ->addField('s', 'gid');
  $query
    ->condition('s.uid', 0, '<>');
  $query
    ->addExpression('COUNT(*)', 'count');
  $query
    ->groupBy('s.gid');
  $groups = $query
    ->execute()
    ->fetchAllAssoc('gid');
  $table = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'table-striped',
      ],
    ],
    '#prefix' => '<div class="trainings-list-wrapper">',
    '#suffix' => '</div>',
    '#header' => [
      [
        'data' => $this
          ->t('Training'),
        'class' => 'training',
      ],
      [
        'data' => $this
          ->t('Nb of users'),
        'class' => 'users',
      ],
      [
        'data' => $this
          ->t('Nb completed'),
        'class' => 'completed',
      ],
      [
        'data' => $this
          ->t('Avg time spent'),
        'class' => 'time',
      ],
      [
        'data' => $this
          ->t('Details'),
        'class' => 'hidden',
      ],
    ],
    '#rows' => [],
  ];

  // Groups ids of existing groups.
  $gids = $this->database
    ->select('groups', 'g')
    ->fields('g', [
    'id',
  ])
    ->execute()
    ->fetchCol();
  $options = [
    'attributes' => [
      'class' => [
        'btn',
        'btn-rounded',
      ],
    ],
  ];
  foreach ($data as $row) {
    $row_time = max(0, round($row->time));
    $time_str = $row_time > 0 ? $this->dateFormatter
      ->formatInterval($row_time) : '-';
    $gid = $row->gid;

    // Set links only for existing trainings, empty link otherwise.
    if (in_array($gid, $gids)) {
      $details_link = Link::createFromRoute($this
        ->t('Details'), 'opigno_statistics.training', [
        'group' => $gid,
      ], $options)
        ->toRenderable();
    }
    else {
      $details_link = [];
    }
    $nb_users = isset($groups[$gid]) ? $groups[$gid]->count : '';
    $table['#rows'][] = [
      [
        'data' => $row->name,
        'class' => 'training',
      ],
      [
        'data' => $nb_users,
        'class' => 'users',
      ],
      [
        'data' => $row->users_completed,
        'class' => 'completed',
      ],
      [
        'data' => $time_str,
        'class' => 'time',
      ],
      [
        'data' => $details_link,
        'class' => 'details',
      ],
    ];
  }
  return $table;
}