You are here

public function UserStatisticsManager::getGroupedUserTrainingsNumber in Opigno statistics 3.x

Get the number of user trainings with status grouped by the period of time.

Parameters

int $uid: The ID of user to get statistics for. By default will be calculated for the current user.

string $status: The status to get trainings with.

Return value

array The array of data for the chart rendering.

1 call to UserStatisticsManager::getGroupedUserTrainingsNumber()
UserStatisticsManager::renderUserTrainingsCharts in src/Services/UserStatisticsManager.php
Prepare the render array to display the user training charts.

File

src/Services/UserStatisticsManager.php, line 189

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function getGroupedUserTrainingsNumber(int $uid = 0, string $status = 'completed') : array {
  $uid = $uid ?: $this->currentUid;
  if (!$uid) {
    return [];
  }

  // Get the amount of completed trainings grouped by week.
  $field = $status === 'completed' ? 'completed' : 'registered';

  // Need to display the current day in the chart.
  $timestamp = strtotime("tomorrow -30 days");
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a');
  $query
    ->join('groups', 'g', 'g.id = a.gid');
  $query
    ->addExpression("WEEK(a.{$field})", 'period');
  $query
    ->addExpression('COUNT(DISTINCT a.gid)', 'count');
  $query
    ->condition('a.uid', $uid)
    ->condition('a.status', $status);
  $query
    ->where("UNIX_TIMESTAMP(a.{$field}) >= :timestamp", [
    ':timestamp' => $timestamp,
  ]);
  $data = $query
    ->groupBy('period')
    ->execute()
    ->fetchAllAssoc('period');
  return $this
    ->prepareChartData($data);
}