You are here

public function UserController::buildTrainingsList in Opigno statistics 3.x

Same name and namespace in other branches
  1. 8 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.

File

src/Controller/UserController.php, line 1338

Class

UserController
Statistics user controller.

Namespace

Drupal\opigno_statistics\Controller

Code

public function buildTrainingsList(UserInterface $user) : array {
  $uid = (int) $user
    ->id();
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a')
    ->fields('a', [
    'gid',
    'name',
    'progress',
    'status',
  ])
    ->condition('a.uid', $uid)
    ->groupBy('a.gid')
    ->groupBy('a.name')
    ->groupBy('a.progress')
    ->groupBy('a.status')
    ->orderBy('a.name');
  $rows = $query
    ->execute()
    ->fetchAll();
  $table_rows = [];

  // Build table rows.
  foreach ($rows as $row) {
    $gid = $row->gid;
    $status = $row->status ?? 'pending';
    $progress = $row->progress ?? 0;

    // Generate the details link.
    $options = [
      'attributes' => [
        'class' => [
          'btn',
          'btn-rounded',
        ],
        'data-user' => $uid,
        'data-training' => $gid,
      ],
    ];
    $params = [
      'user' => $uid,
      'group' => $gid,
    ];
    $details = Link::createFromRoute($this
      ->t('Details'), 'opigno_statistics.user.training_details', $params, $options)
      ->toRenderable();
    $table_rows[] = [
      'class' => 'training',
      'data-training' => $gid,
      'data-user' => $uid,
      'data' => [
        [
          'data' => $row->name ?? '',
          'class' => 'name',
        ],
        [
          'data' => $progress . '%',
          'class' => 'progress',
        ],
        [
          'data' => $this
            ->buildStatus($status),
          'class' => 'status',
        ],
        [
          'data' => $details,
          'class' => 'details',
        ],
      ],
    ];
  }
  return [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
      ],
    ],
    '#header' => [
      [
        'data' => $this
          ->t('Training'),
        'class' => 'name',
      ],
      [
        'data' => $this
          ->t('Progress'),
        'class' => 'progress',
      ],
      [
        'data' => $this
          ->t('Passed'),
        'class' => 'status',
      ],
      [
        'data' => $this
          ->t('Details'),
        'class' => 'hidden',
      ],
    ],
    '#rows' => $table_rows,
  ];
}