You are here

function OpignoModule::getUserScore in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Entity/OpignoModule.php \Drupal\opigno_module\Entity\OpignoModule::getUserScore()

Implements opigno_module_get_user_module_score().

Parameters

\Drupal\Core\Session\AccountInterface $account: User account object.

int $latest_cert_date: The last date when was attempt.

Return value

int Score in percents depends of OpignoModule keep_results option.

File

src/Entity/OpignoModule.php, line 567

Class

OpignoModule
Defines the Module entity.

Namespace

Drupal\opigno_module\Entity

Code

function getUserScore(AccountInterface $account, $latest_cert_date = NULL) {
  $which_score_keep = $this
    ->getKeepResultsOption();
  $attempts = $this
    ->getModuleAttempts($account, 'last', $latest_cert_date);
  if (!$attempts) {
    return 0;
  }

  /* @var \Drupal\opigno_module\Entity\UserModuleStatus $last_attempt */
  $last_attempt = end($attempts);
  $score = 0;
  switch ($which_score_keep) {

    // The newest score always saved in last attempt.
    case 'newest':
      $score = (int) $last_attempt
        ->getScore();
      break;

    // For these options get best score.
    case 'best':
    case 'all':
      $score = (int) $last_attempt
        ->calculateBestScore($latest_cert_date);
      break;
  }

  // Clamp score.
  $score = max(0, $score);
  $score = min(100, $score);
  return $score;
}