You are here

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

Get the amount of badges earned by the user.

Parameters

int $uid: The user ID. The current user will be taken by default.

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

Return value

int|array The amount of badges earned by the user OR the list of badges.

1 call to UserStatisticsManager::getUserBadges()
UserStatisticsManager::buildBadgesList in src/Services/UserStatisticsManager.php
Build the user badges list table.

File

src/Services/UserStatisticsManager.php, line 372

Class

UserStatisticsManager
User statistics manager service definition.

Namespace

Drupal\opigno_statistics\Services

Code

public function getUserBadges(int $uid = 0, bool $count = TRUE) {
  if (!$uid) {
    $uid = $this->currentUid;
  }

  // Badges for courses and modules are stored in different tables and have
  // the different structure.
  $query = $this->database
    ->select('opigno_module_badges', 'omb');
  $query
    ->leftJoin('group__badge_name', 'gbn', 'gbn.entity_id = omb.entity_id AND omb.typology = :course', [
    ':course' => 'Course',
  ]);
  $query
    ->leftJoin('opigno_module_field_data', 'omf', 'omf.id = omb.entity_id AND omb.typology = :module', [
    ':module' => 'Module',
  ]);
  $query
    ->fields('gbn', [
    'badge_name_value',
  ])
    ->fields('omf', [
    'badge_name',
  ])
    ->fields('omb', [
    'typology',
    'entity_id',
  ])
    ->condition('omb.uid', $uid);
  if ($count) {
    $result = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    return (int) $result;
  }
  return $query
    ->execute()
    ->fetchAll();
}