You are here

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

Get the certificates earned by user 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.

bool $count: If results should be counted or not. If FALSE, the array of results will be returned.

Return value

array|int The number of certificates earned by user in the given amount of days OR the list of certificates.

2 calls to UserStatisticsManager::getUserCertificates()
UserStatisticsManager::buildCertificatesList in src/Services/UserStatisticsManager.php
Build the user certificates list table.
UserStatisticsManager::renderUserStatistics in src/Services/UserStatisticsManager.php
Prepare the render array to display the user statistics.

File

src/Services/UserStatisticsManager.php, line 260

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function getUserCertificates(int $days = 0, int $uid = 0, bool $count = TRUE) {
  $uid = $uid ?: $this->currentUid;
  if (!$uid) {
    return [];
  }
  $query = $this->database
    ->select('group__field_certificate', 'gc');
  $query
    ->join('opigno_learning_path_achievements', 'a', 'gc.entity_id = a.gid');
  $query
    ->fields('a', [
    'gid',
    'completed',
    'name',
  ])
    ->condition('a.uid', $uid)
    ->condition('a.status', 'completed');
  if ($days) {
    $timestamp = strtotime("-{$days} days");
    $query
      ->where('UNIX_TIMESTAMP(a.completed) >= :timestamp', [
      ':timestamp' => $timestamp,
    ]);
  }
  if ($count) {
    $result = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    return (int) $result;
  }
  return $query
    ->orderBy('a.name')
    ->execute()
    ->fetchAll();
}