You are here

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

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

Builds training content.

Parameters

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

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

Return value

array Render array.

Throws

\Exception

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

File

src/Form/TrainingForm.php, line 321

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildTrainingContent(GroupInterface $group, $expired_uids = NULL) {
  $gid = $group
    ->id();
  $query = $this->database
    ->select('users', '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
    ->fields('u', [
    'uid',
  ]);
  $users = $query
    ->condition('u.uid', 0, '<>')
    ->execute()
    ->fetchAll();
  $users_number = count($users);

  // Filter users with expired certificate.
  $users = array_filter($users, function ($user) use ($expired_uids) {
    return !in_array($user->uid, $expired_uids);
  });
  $table = [
    '#type' => 'table',
    '#attributes' => [
      'class' => [
        'statistics-table',
        'training-content-list',
        'table-striped',
      ],
    ],
    '#prefix' => '<div class="training-content-wrapper">',
    '#suffix' => '</div>',
    '#header' => [
      [
        'data' => $this
          ->t('Step'),
        'class' => 'step',
      ],
      [
        'data' => $this
          ->t('% Completed'),
        'class' => 'completed',
      ],
      [
        'data' => $this
          ->t('Avg score'),
        'class' => 'score',
      ],
      [
        'data' => $this
          ->t('Avg time spent'),
        'class' => 'time',
      ],
    ],
    '#rows' => [],
  ];
  $rows = [];
  if (LPStatus::isCertificateExpireSet($group)) {

    // Calculate training content in condition of certification.
    $contents = OpignoGroupManagedContent::loadByGroupId($gid);
    if ($users && $contents) {
      foreach ($contents as $content) {
        $content_id = $content
          ->id();
        $rows[$content_id] = new \stdClass();
        $rows[$content_id]->name = '';
        $rows[$content_id]->completed = 0;
        $rows[$content_id]->score = 0;
        $rows[$content_id]->time = 0;
        $statistics = $this
          ->getGroupContentStatisticsCertificated($group, $users, $content);
        if (!empty($statistics['name'])) {

          // Step name.
          $rows[$content_id]->name = $statistics['name'];

          // Step average completed.
          $rows[$content_id]->completed = $statistics['completed'];

          // Step average score.
          $rows[$content_id]->score = $statistics['score'] / $users_number;

          // Step average score.
          $rows[$content_id]->time = round($statistics['time'] / $users_number);
        }
      }
    }
  }
  else {

    // Calculate training content without certification condition.
    $rows = $this
      ->getTrainingContentStatistics($gid);
  }
  if ($rows) {
    foreach ($rows as $row) {
      if (!empty($row->name)) {
        $completed = round(100 * $row->completed / $users_number);
        $score = round($row->score);
        $time = $row->time > 0 ? $this->dateFormatter
          ->formatInterval($row->time) : '-';
        $table['#rows'][] = [
          [
            'data' => $row->name,
            'class' => 'step',
          ],
          [
            'data' => $completed . '%',
            'class' => 'completed',
          ],
          [
            'data' => $score . '%',
            'class' => 'score',
          ],
          [
            'data' => $time,
            'class' => 'time',
          ],
        ];
      }
    }
  }
  return [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'training-content',
        'content-box',
      ],
    ],
    'title' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#attributes' => [
        'class' => [
          'training-content-title',
        ],
      ],
      '#value' => $this
        ->t('Training Content'),
    ],
    'list' => $table,
  ];
}