You are here

protected function TrainingForm::buildUsersResultsClass in Opigno statistics 8

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

Builds users results for Classes.

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

File

src/Form/TrainingForm.php, line 596

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildUsersResultsClass(GroupInterface $group, $lp_id = NULL) {
  if (!$lp_id) {
    return;
  }
  $members = $group
    ->getMembers();
  $title = Group::load($lp_id)
    ->label();
  foreach ($members as $member) {
    $user = $member
      ->getUser();
    if ($user) {
      $members_ids[$user
        ->id()] = $member
        ->getUser()
        ->id();
    }
  }
  if (empty($members_ids)) {
    $members_ids[] = 0;
  }
  $query = $this->database
    ->select('users_field_data', 'u')
    ->fields('u', [
    'uid',
    'name',
  ]);
  $query
    ->condition('u.uid', $members_ids, 'IN');
  $query
    ->condition('u.uid', 0, '<>');
  $query
    ->innerJoin('group_content_field_data', 'g_c', 'g_c.entity_id = u.uid');
  $query
    ->condition('g_c.type', [
    'learning_path-group_membership',
    'opigno_course-group_membership',
  ], 'IN');
  $query
    ->condition('g_c.gid', $lp_id);
  $query
    ->leftJoin('opigno_learning_path_achievements', 'a', 'a.gid = g_c.gid AND a.uid = u.uid');
  $query
    ->fields('a', [
    'status',
    'score',
    'time',
    'gid',
  ]);
  $query
    ->orderBy('u.name', 'ASC');
  $statistic = $query
    ->execute()
    ->fetchAll();
  $table = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'users-results-list',
        'table-striped',
      ],
    ],
    '#header' => [
      $this
        ->t('User'),
      $this
        ->t('Score'),
      $this
        ->t('Passed'),
      $this
        ->t('Time spent'),
      $this
        ->t('Details'),
    ],
    '#rows' => [],
  ];
  foreach ($statistic as $row) {
    $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 = $row->time > 0 ? $this->date_formatter
      ->formatInterval($row->time) : '-';
    $details_link = Link::createFromRoute('', 'opigno_statistics.user', [
      'user' => $row->uid,
    ])
      ->toRenderable();
    $details_link['#attributes']['class'][] = 'details';
    $details_link = [
      'data' => $details_link,
    ];
    $table['#rows'][] = [
      $row->name,
      $score,
      $status,
      $time,
      $details_link,
    ];
  }

  // Hide links on detail user pages if user don't have permissions.
  $account = \Drupal::currentUser();
  if (!$account
    ->hasPermission('view module results')) {
    unset($table['#header'][4]);
    foreach ($table['#rows'] as $key => $table_row) {
      unset($table['#rows'][$key][4]);
    }
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'users-results',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'users-results-title',
        ],
      ],
      '#value' => $this
        ->t($title),
    ],
    'list' => $table,
  ];
}