You are here

public static function LPStatus::setCertificateExpireTimestamp in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/Entity/LPStatus.php \Drupal\opigno_learning_path\Entity\LPStatus::setCertificateExpireTimestamp()

Saves training certificate expire timestamp.

Parameters

int $gid: Group ID.

int $uid: User ID.

int $latest_cert_date: Training latest certificate timestamp.

int $expire: Training certificate expire timestamp.

File

src/Entity/LPStatus.php, line 363

Class

LPStatus
Defines the User Learning Path attempt status entity.

Namespace

Drupal\opigno_learning_path\Entity

Code

public static function setCertificateExpireTimestamp($gid, $uid, $latest_cert_date = 0, $expire = 0) {
  $db_connection = \Drupal::service('database');
  try {
    $result = $db_connection
      ->select('user_lp_status_expire', 'lps')
      ->fields('lps', [
      'id',
    ])
      ->condition('gid', $gid)
      ->condition('uid', $uid)
      ->execute()
      ->fetchField();
    if (!$result) {

      // Certification not exists.
      // Add training certification for the user.
      $db_connection
        ->insert('user_lp_status_expire')
        ->fields([
        'gid' => $gid,
        'uid' => $uid,
        'latest_cert_date' => $latest_cert_date,
        'expire' => $expire,
      ])
        ->execute();
    }
    if ($result) {

      // Certification expired.
      // Update certification.
      $db_connection
        ->merge('user_lp_status_expire')
        ->key([
        'gid' => $gid,
        'uid' => $uid,
      ])
        ->fields([
        'gid' => $gid,
        'uid' => $uid,
        'latest_cert_date' => $latest_cert_date,
        'expire' => $expire,
      ])
        ->execute();
    }
  } catch (\Exception $e) {
    \Drupal::logger('opigno_learning_path')
      ->error($e
      ->getMessage());
    \Drupal::messenger()
      ->addMessage($e
      ->getMessage(), 'error');
  }
}