You are here

public static function LPStatus::getTrainingStatus in Opigno Learning path 8

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

Returns user training status.

Parameters

int $gid: Group ID.

int $uid: User ID.

string $type: Kind of the query result - best|last.

Return value

string User training status.

File

src/Entity/LPStatus.php, line 215

Class

LPStatus
Defines the User Learning Path attempt status entity.

Namespace

Drupal\opigno_learning_path\Entity

Code

public static function getTrainingStatus($gid, $uid, $type = 'best') {
  $db_connection = \Drupal::service('database');
  try {
    $result = $db_connection
      ->select('user_lp_status', 'lp')
      ->fields('lps', [
      'status',
    ])
      ->condition('gid', $gid)
      ->condition('uid', $uid)
      ->orderBy('finished', 'DESC')
      ->execute()
      ->fetchCol();
  } catch (\Exception $e) {
    \Drupal::logger('opigno_learning_path')
      ->error($e
      ->getMessage());
    \Drupal::messenger()
      ->addMessage($e
      ->getMessage(), 'error');
  }
  if (!empty($result)) {
    if ($type == 'best') {
      if (in_array('passed', $result)) {
        return 'passed';
      }
      elseif (in_array('failed', $result)) {
        return 'failed';
      }
    }
    else {
      return array_shift($stack);
    }
  }
  return '';
}