You are here

protected function TrainingForm::buildUsersResultsLp in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Form/TrainingForm.php \Drupal\opigno_statistics\Form\TrainingForm::buildUsersResultsLp()

Builds users results for Learning paths.

Parameters

\Drupal\group\Entity\GroupInterface $group: Group.

mixed $expired_uids: Users uids with the training expired certification.

Return value

array Render array.

1 call to TrainingForm::buildUsersResultsLp()
TrainingForm::buildForm in src/Form/TrainingForm.php
Form constructor.

File

src/Form/TrainingForm.php, line 464

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildUsersResultsLp(GroupInterface $group, $expired_uids = NULL) {
  $query = $this->database
    ->select('users_field_data', 'u');
  $query
    ->innerJoin('group_content_field_data', 'gc', "gc.entity_id = u.uid\nAND gc.type IN ('learning_path-group_membership', 'opigno_course-group_membership')\nAND gc.gid = :gid", [
    ':gid' => $group
      ->id(),
  ]);
  $query
    ->leftJoin('opigno_learning_path_achievements', 'a', 'a.gid = gc.gid AND a.uid = u.uid');
  $query
    ->condition('u.uid', 0, '<>');
  $data = $query
    ->fields('u', [
    'uid',
    'name',
  ])
    ->fields('a', [
    'status',
    'score',
    'time',
  ])
    ->execute()
    ->fetchAll();
  $table = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'users-results-list',
        'table-striped',
        'trainings-list',
      ],
    ],
    '#header' => [
      $this
        ->t('User'),
      $this
        ->t('Score'),
      $this
        ->t('Passed'),
      $this
        ->t('Time spent'),
      $this
        ->t('Details'),
    ],
    '#rows' => [],
  ];
  foreach ($data as $row) {

    // Expired training certification flag.
    $expired = FALSE;
    if (!empty($expired_uids) && in_array($row->uid, $expired_uids)) {
      $expired = TRUE;
    }
    if ($expired) {
      $row->score = 0;
    }
    $score = isset($row->score) ? $row->score : 0;
    $score = [
      'data' => $this
        ->buildScore($score),
    ];
    if ($expired) {
      $row->status = 'expired';
    }
    $status = isset($row->status) ? $row->status : 'pending';
    $status = [
      'data' => $this
        ->buildStatus($status),
    ];
    if ($expired) {
      $row->time = 0;
    }
    $time = $row->time > 0 ? $this->date_formatter
      ->formatInterval($row->time) : '-';
    $details_link = Link::createFromRoute(Markup::create('<span class="sr-only">' . t('@user_name details', [
      '@user_name' => $row->name,
    ]) . '</span>'), 'opigno_statistics.user.training_details', [
      'user' => $row->uid,
      'group' => $group
        ->id(),
    ])
      ->toRenderable();
    $details_link['#attributes']['class'][] = 'details';
    $details_link['#attributes']['data-user'][] = $row->uid;
    $details_link['#attributes']['data-training'][] = $group
      ->id();
    $details_link = [
      'data' => $details_link,
    ];
    $user_link = Link::createFromRoute($row->name, 'entity.user.canonical', [
      'user' => $row->uid,
    ])
      ->toRenderable();
    $user_link['#attributes']['data-user'][] = $row->uid;
    $user_link['#attributes']['data-training'][] = $group
      ->id();
    $user_link = [
      'data' => $user_link,
    ];
    $table['#rows'][] = [
      'class' => 'training',
      'data-training' => $group
        ->id(),
      'data-user' => $row->uid,
      'data' => [
        $user_link,
        $score,
        $status,
        $time,
        $details_link,
      ],
    ];
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'users-results',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'users-results-title',
        ],
      ],
      '#value' => $this
        ->t('Users results'),
    ],
    'list' => $table,
  ];
}