You are here

public function UserModuleStatus::calculateBestScore in Opigno module 3.x

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

Calculates module best score.

Return value

int Score in percent.

Overrides UserModuleStatusInterface::calculateBestScore

1 call to UserModuleStatus::calculateBestScore()
UserModuleStatus::finishAttempt in src/Entity/UserModuleStatus.php
Finish user attempt.

File

src/Entity/UserModuleStatus.php, line 355

Class

UserModuleStatus
Defines the User module status entity.

Namespace

Drupal\opigno_module\Entity

Code

public function calculateBestScore($latest_cert_date = NULL) {

  /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
  $module = $this
    ->getModule();
  $user = $this
    ->getOwner();

  // For each attempt, check the score and get the best one.
  $user_attempts = $module
    ->getModuleAttempts($user, 'best', $latest_cert_date);
  if (!$user_attempts) {
    return 0;
  }
  $best_score = 0;

  /** @var \Drupal\opigno_module\Entity\UserModuleStatus $user_attempt */
  foreach ($user_attempts as $user_attempt) {

    // Get the scores.
    $actual_score = (int) $user_attempt
      ->getScore();

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

    // Save the best score.
    if ($actual_score > $best_score) {
      $best_score = $actual_score;
    }
  }
  return $best_score;
}