You are here

public static function LPStatus::getCertificateExpireTimestamp 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::getCertificateExpireTimestamp()

Returns training certificate expire timestamp.

Parameters

int $gid: Group ID.

int $uid: User ID.

Return value

int|null Timestamp if found, null otherwise.

6 calls to LPStatus::getCertificateExpireTimestamp()
LPStatus::getCertificateExpirationMessage in src/Entity/LPStatus.php
Returns training certificate expiration message.
LPStatus::getTrainingStartDate in src/Entity/LPStatus.php
Returns training start date for displaying statistics.
LPStatus::isCertificateExpired in src/Entity/LPStatus.php
Returns flag if training certificate expired for the user.
Progress::buildSummary in src/Progress.php
Progress::getProgressBuildAchievementsPage in src/Progress.php
Get get progress for achievements page.

... See full list

File

src/Entity/LPStatus.php, line 307

Class

LPStatus
Defines the User Learning Path attempt status entity.

Namespace

Drupal\opigno_learning_path\Entity

Code

public static function getCertificateExpireTimestamp($gid, $uid) : ?int {
  $group = Group::load($gid);
  try {
    $completed_on = opigno_learning_path_completed_on($gid, $uid, TRUE);
  } catch (InvalidPluginDefinitionException|PluginNotFoundException|PluginException $e) {
    $completed_on = NULL;
    watchdog_exception('opigno_learning_path_exception', $e);
  }

  // Get the data from the database if that's impossible to get the completion
  // date (old approach). This returns the incorrect result if the user
  // completed the same LP for several times.
  if (!$completed_on || !$group instanceof GroupInterface) {
    return \Drupal::database()
      ->select('user_lp_status_expire', 'lps')
      ->fields('lps', [
      'expire',
    ])
      ->condition('gid', $gid)
      ->condition('uid', $uid)
      ->execute()
      ->fetchField();
  }

  // Get the certificate expiration time depending on the completion timestamp
  // and LP certificate validity settings.
  if (!self::isCertificateExpireSet($group)) {
    return NULL;
  }

  // Get the available options for the expiration time.
  $field = 'field_certificate_expire_results';
  $definitions = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('group', 'learning_path');
  $definition = $definitions[$field] ?? NULL;
  if (!$definition instanceof FieldDefinitionInterface) {
    return NULL;
  }
  $options = $definition
    ->getSetting('allowed_values');
  $valid_for = $group
    ->get($field)
    ->getString();
  $valid_for = $options[$valid_for] ?? $valid_for . ' months';
  return strtotime($valid_for, $completed_on) ?? NULL;
}