You are here

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

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

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();
  foreach ($members as $member) {
    $user = $member
      ->getUser();
    if ($user) {
      $members_ids[$user
        ->id()] = $member
        ->getUser()
        ->id();
    }
  }
  if (empty($members_ids)) {
    $members_ids[] = 0;
  }
  $membership_types = [
    'learning_path-group_membership',
    'opigno_course-group_membership',
  ];
  $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', $membership_types, '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',
      ],
    ],
    '#prefix' => '<div class="user-results-class-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' => [],
  ];
  $options = [
    'attributes' => [
      'class' => [
        'btn',
        'btn-rounded',
        'details',
      ],
    ],
  ];
  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->dateFormatter
      ->formatInterval($row->time) : '-';
    $details_link = Link::createFromRoute($this
      ->t('Details'), 'opigno_statistics.user', [
      'user' => $row->uid,
    ], $options)
      ->toRenderable();
    $table['#rows'][] = [
      [
        'data' => $row->name,
        'class' => 'user',
      ],
      [
        'data' => $score,
        'class' => 'score',
      ],
      [
        'data' => $status,
        'class' => 'passed',
      ],
      [
        'data' => $time,
        'class' => 'time',
      ],
      [
        'data' => $details_link,
        'class' => 'details',
      ],
    ];
  }

  // Hide links on detail user pages if user don't have permissions.
  $account = $this
    ->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',
        'content-box',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'users-results-title',
        ],
      ],
      '#value' => Group::load($lp_id)
        ->label(),
    ],
    'list' => $table,
  ];
}