You are here

public function UserController::buildTrainingsList in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Controller/UserController.php \Drupal\opigno_statistics\Controller\UserController::buildTrainingsList()

Builds render array for a user trainings list.

Parameters

\Drupal\user\UserInterface $user: User.

Return value

array Render array.

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

File

src/Controller/UserController.php, line 1286

Class

UserController
Class UserController.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildTrainingsList(UserInterface $user) {
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a')
    ->fields('a', [
    'gid',
    'name',
    'score',
    'status',
    'time',
  ])
    ->condition('a.uid', $user
    ->id())
    ->groupBy('a.gid')
    ->groupBy('a.name')
    ->groupBy('a.score')
    ->groupBy('a.status')
    ->groupBy('a.time')
    ->orderBy('a.name');
  $rows = $query
    ->execute()
    ->fetchAll();
  $rows = array_map(function ($row) use ($user) {
    $uid = $user
      ->id();
    $gid = $row->gid;
    $name = $row->name;
    $score = isset($row->score) ? $row->score : 0;
    $score = [
      'data' => $this
        ->buildScore($score),
    ];
    $status = isset($row->status) ? $row->status : 'pending';
    $status = [
      'data' => $this
        ->buildStatus($status),
    ];
    $time_spent = $row->time > 0 ? $this->date_formatter
      ->formatInterval($row->time) : '-';
    $details = Link::createFromRoute('', 'opigno_statistics.user.training_details', [
      'user' => $uid,
      'group' => $gid,
    ])
      ->toRenderable();
    $details['#attributes']['class'][] = 'details';
    $details['#attributes']['data-user'] = $uid;
    $details['#attributes']['data-training'] = $gid;
    $details = [
      'data' => $details,
    ];
    return [
      'class' => 'training',
      'data-training' => $gid,
      'data-user' => $uid,
      'data' => [
        $name,
        $score,
        $status,
        $time_spent,
        $details,
      ],
    ];
  }, $rows);
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'trainings-list',
      ],
    ],
    [
      '#type' => 'table',
      '#attributes' => [
        'class' => [
          'statistics-table',
          'trainings-list',
          'table-striped',
        ],
      ],
      '#header' => [
        $this
          ->t('Training'),
        $this
          ->t('Score'),
        $this
          ->t('Passed'),
        $this
          ->t('Time spent'),
        $this
          ->t('Details'),
      ],
      '#rows' => $rows,
    ],
  ];
}