You are here

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

Get the number of user trainings by status in the given amount of days.

Parameters

int $days: The amount of days to get statistics for. If 0 given, statistics will be calculated without time limitation.

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

int The number of user trainings by status in the given amount of days.

1 call to UserStatisticsManager::getUserTrainingsNumberByStatus()
UserStatisticsManager::renderUserStatistics in src/Services/UserStatisticsManager.php
Prepare the render array to display the user statistics.

File

src/Services/UserStatisticsManager.php, line 152

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function getUserTrainingsNumberByStatus(int $days = 0, int $uid = 0, string $status = 'completed') : int {
  $uid = $uid ?: $this->currentUid;
  if (!$uid) {
    return 0;
  }
  $query = $this->database
    ->select('opigno_learning_path_achievements', 'a');
  $query
    ->join('groups', 'g', 'g.id = a.gid');
  $query
    ->fields('a', [
    'id',
  ])
    ->condition('a.uid', $uid)
    ->condition('a.status', $status);
  if ($days) {
    $field = $status === 'completed' ? 'completed' : 'registered';
    $timestamp = strtotime("tomorrow -{$days} days");
    $query
      ->where("UNIX_TIMESTAMP(a.{$field}) >= :timestamp", [
      ':timestamp' => $timestamp,
    ]);
  }
  $result = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  return (int) $result;
}