You are here

protected function StatisticsPageTrait::getTrainingContentStatistics in Opigno statistics 8

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

Returns training content data by each step.

Parameters

int $gid: Group ID.

Return value

array Training content data by each step.

1 call to StatisticsPageTrait::getTrainingContentStatistics()
TrainingForm::buildTrainingContent in src/Form/TrainingForm.php
Builds training content.

File

src/StatisticsPageTrait.php, line 242

Class

StatisticsPageTrait
Common helper methods for a statistics pages.

Namespace

Drupal\opigno_statistics

Code

protected function getTrainingContentStatistics($gid) {
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a');
  $query
    ->leftJoin('opigno_learning_path_step_achievements', 's', 's.gid = a.gid AND s.uid = a.uid');
  $query
    ->leftJoin('opigno_learning_path_step_achievements', 'sc', 'sc.id = s.id AND sc.completed IS NOT NULL');
  $query
    ->addExpression('COUNT(sc.uid)', 'completed');
  $query
    ->addExpression('AVG(s.score)', 'score');
  $query
    ->addExpression('AVG(s.time)', 'time');
  $query
    ->addExpression('MAX(s.entity_id)', 'entity_id');
  $query
    ->addExpression('MAX(s.parent_id)', 'parent_id');
  $query
    ->addExpression('MAX(s.position)', 'position');
  $query
    ->addExpression('MAX(s.typology)', 'typology');
  $query
    ->addExpression('MAX(s.id)', 'id');
  $query
    ->condition('a.uid', 0, '<>');

  // Add only group members.
  $group = Group::load($gid);
  $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;
  }
  $query
    ->condition('a.uid', $members_ids, 'IN');
  $data = $query
    ->fields('s', [
    'name',
  ])
    ->condition('a.gid', $gid)
    ->groupBy('s.name')
    ->orderBy('position')
    ->orderBy('parent_id')
    ->execute()
    ->fetchAll();

  // Sort courses and modules.
  $rows = [];
  foreach ($data as $row) {
    if ($row->typology == 'Course') {
      $rows[] = $row;
    }
    elseif ($row->typology == 'Module' && $row->parent_id == 0 || $row->typology == 'ILT' || $row->typology == 'Meeting') {
      $rows[] = $row;
    }
  }
  return $rows;
}