public function UserStatisticsManager::buildTrainingsList in Opigno statistics 3.x
Prepare the render array for the user's trainings list.
Parameters
int $uid: The user ID to build the trainings list for.
bool $completed: If TRUE, the list of completed trainings will be returned. Otherwise all trainings will be returned.
Return value
array Render array to display user's trainings list.
File
- src/
Services/ UserStatisticsManager.php, line 629
Class
- UserStatisticsManager
- User statistics manager service definition.
Namespace
Drupal\opigno_statistics\ServicesCode
public function buildTrainingsList(int $uid, bool $completed = FALSE) : array {
$fields = [
'gid',
'name',
'progress',
'status',
];
// Add an extra column with the completion date.
if ($completed) {
array_splice($fields, 3, 0, 'completed');
}
// Join the groups table to be sure that the training wasn't deleted.
$query = $this->database
->select('opigno_learning_path_achievements', 'a');
$query
->join('groups', 'g', 'g.id = a.gid');
$query
->fields('a', $fields)
->condition('a.uid', $uid);
if ($completed) {
$query
->condition('a.status', 'completed');
}
$rows = $query
->orderBy('a.name')
->execute()
->fetchAll();
// Prepare the table header.
$header = [
[
'data' => $this
->t('Name'),
'class' => 'name',
],
[
'data' => $this
->t('Progress'),
'class' => 'progress',
],
[
'data' => $this
->t('Status'),
'class' => 'status',
],
[
'data' => $this
->t('Details'),
'class' => 'hidden',
],
];
if ($completed) {
$date = [
[
'data' => $this
->t('Date'),
'class' => 'date',
],
];
array_splice($header, 2, 0, $date);
}
$table_rows = [];
$build = [
'#type' => 'table',
'#attributes' => [
'class' => [
'statistics-table',
'trainings-list',
],
],
'#header' => $header,
];
if (!$rows) {
return $build + [
'#rows' => $table_rows,
];
}
// Details link options.
$options = [
'attributes' => [
'class' => [
'btn',
'btn-rounded',
],
],
];
// Build table rows.
foreach ($rows as $row) {
$gid = $row->gid;
$status = $row->status ?? 'pending';
$progress = $row->progress ?? 0;
// Generate the details link.
$params = [
'account' => $uid,
'group' => $gid,
];
$details = Link::createFromRoute($this
->t('Details'), 'opigno_learning_path.training_by_user', $params, $options)
->toRenderable();
// Prepare the table row.
$table_row = [
[
'data' => $row->name ?? '',
'class' => 'name',
],
[
'data' => $progress . '%',
'class' => 'progress',
],
[
'data' => $this
->buildStatus($status),
'class' => 'status',
],
[
'data' => $details,
'class' => 'details',
],
];
if ($completed) {
$timestamp = strtotime($row->completed ?: 'now');
$date = $this->dateFormatter
->format($timestamp, 'day_month_year');
array_splice($table_row, 2, 0, $date);
}
$table_rows[] = $table_row;
}
return $build + [
'#rows' => $table_rows,
];
}