You are here

protected function TrainingForm::buildUsersResultsLp in Opigno statistics 3.x

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

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildUsersResultsLp(GroupInterface $group, $expired_uids = NULL) {
  $gid = $group
    ->id();
  $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' => $gid,
  ]);
  $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',
      ],
    ],
    '#prefix' => '<div class="user-results-wrapper">',
    '#suffix' => '</div>',
    '#header' => [
      [
        'data' => $this
          ->t('User'),
        'class' => 'user',
      ],
      [
        'data' => $this
          ->t('Score'),
        'class' => 'score',
      ],
      [
        'data' => $this
          ->t('Passed'),
        'class' => 'passed',
      ],
      [
        'data' => $this
          ->t('Time spent'),
        'class' => 'time',
      ],
      [
        'data' => $this
          ->t('Details'),
        'class' => 'hidden',
      ],
    ],
    '#rows' => [],
  ];
  foreach ($data as $row) {

    // Expired training certification flag.
    $expired = FALSE;
    $uid = $row->uid;
    if (!empty($expired_uids) && in_array($uid, $expired_uids)) {
      $expired = TRUE;
    }
    if ($expired) {
      $row->score = 0;
    }
    $score = isset($row->score) ? $row->score : 0;
    if ($expired) {
      $row->status = 'expired';
    }
    $status = isset($row->status) ? $row->status : 'pending';
    if ($expired) {
      $row->time = 0;
    }
    $time = $row->time > 0 ? $this->dateFormatter
      ->formatInterval($row->time) : '-';
    $options = [
      'attributes' => [
        'class' => [
          'btn',
          'btn-rounded',
        ],
        'data-user' => $uid,
        'data-training' => $gid,
      ],
    ];
    $details_link = Link::createFromRoute($this
      ->t('Details'), 'opigno_learning_path.training_by_user', [
      'account' => $uid,
      'group' => $gid,
    ], $options)
      ->toRenderable();
    $user_link = Link::createFromRoute($row->name, 'entity.user.canonical', [
      'user' => $uid,
    ])
      ->toRenderable();
    $user_link['#attributes']['data-user'][] = $uid;
    $user_link['#attributes']['data-training'][] = $gid;
    $table['#rows'][] = [
      'data-training' => $gid,
      'data-user' => $uid,
      'data' => [
        [
          'data' => $user_link,
          'class' => 'user',
        ],
        [
          'data' => $this
            ->buildScore($score),
          'class' => 'score',
        ],
        [
          'data' => $this
            ->buildStatus($status),
          'class' => 'passed',
        ],
        [
          'data' => $time,
          'class' => 'time',
        ],
        [
          'data' => $details_link,
          'class' => 'details',
        ],
      ],
    ];
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'users-results',
        'content-box',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'users-results-title',
        ],
      ],
      '#value' => $this
        ->t('Users results'),
    ],
    'list' => $table,
  ];
}