You are here

public function ContentTypeModule::getUserScore in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Plugin/OpignoGroupManagerContentType/ContentTypeModule.php \Drupal\opigno_module\Plugin\OpignoGroupManagerContentType\ContentTypeModule::getUserScore()

Get the score of the user for a specific entity.

Parameters

int $user_id: The user ID.

int $entity_id: The entity ID.

Return value

float|false The score between 0 and 1. FALSE if no score found.

Overrides ContentTypeInterface::getUserScore

File

src/Plugin/OpignoGroupManagerContentType/ContentTypeModule.php, line 68

Class

ContentTypeModule
Class ContentTypeModule.

Namespace

Drupal\opigno_module\Plugin\OpignoGroupManagerContentType

Code

public function getUserScore($user_id, $entity_id) {

  // Get the module and the concerned user.
  $opigno_module = OpignoModule::load($entity_id);
  $user = User::load($user_id);

  // For each attempt, check the score and save the best one.
  $user_attempts = $opigno_module
    ->getModuleAttempts($user);
  $best_score = 0;
  foreach ($user_attempts as $user_attempt) {

    /* @var $user_attempt UserModuleStatus */

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

    // Divide the score to receive value between 0 and 1
    // instead of percents count.
    $actual_score = $score / 100;

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

  // Finally, return the BEST !
  return $best_score;
}