You are here

public static function OpignoModuleBadges::opignoModuleSaveBadge in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/OpignoModuleBadges.php \Drupal\opigno_module\OpignoModuleBadges::opignoModuleSaveBadge()

Saves/updates badges count.

Parameters

int $uid: User ID.

int $gid: Training ID.

string $typology: Course or Module string.

int $entity_id: Module/Course ID.

Throws

\Exception

1 call to OpignoModuleBadges::opignoModuleSaveBadge()
OpignoModuleController::userResult in src/Controller/OpignoModuleController.php
Returns user result.

File

src/OpignoModuleBadges.php, line 28

Class

OpignoModuleBadges
Class OpignoModuleBadges.

Namespace

Drupal\opigno_module

Code

public static function opignoModuleSaveBadge($uid, $gid, $typology, $entity_id) {
  $table_name = 'opigno_module_badges';

  // Get existing badge count.
  $badges = self::opignoModuleGetBadges($uid, $gid, $typology, $entity_id);

  // Update/create badge count.
  $keys = [
    'uid' => $uid,
    'gid' => $gid,
    'entity_id' => $entity_id,
    'typology' => $typology,
  ];
  $fields = [
    'uid' => $uid,
    'gid' => $gid,
    'entity_id' => $entity_id,
    'typology' => $typology,
    'badges' => $badges ? $badges + 1 : 1,
  ];
  $query = \Drupal::database()
    ->merge($table_name)
    ->keys($keys)
    ->fields($fields);
  $query
    ->execute();
}