You are here

protected function TincanContentAnswerAssistant::getScoreFromStatement in Opigno module 8

Same name and namespace in other branches
  1. 3.x ActivityTypes/opigno_tincan_activity/src/TincanContentAnswerAssistant.php \Drupal\opigno_tincan_activity\TincanContentAnswerAssistant::getScoreFromStatement()

This method calculate the score using a statement.

Parameters

\TinCan\Statement $statement: The statement that contains the score.

int $max_score: Max score.

Return value

float|int The final score ready to be returned for the Quiz module. Can returns 0 if the score is not found in the statement.

1 call to TincanContentAnswerAssistant::getScoreFromStatement()
TincanContentAnswerAssistant::getScoreFromLrs in ActivityTypes/opigno_tincan_activity/src/TincanContentAnswerAssistant.php
This method will return the score from the LRS system for this response.

File

ActivityTypes/opigno_tincan_activity/src/TincanContentAnswerAssistant.php, line 276

Class

TincanContentAnswerAssistant
Class TincanContentAnswerAssistant.

Namespace

Drupal\opigno_tincan_activity

Code

protected function getScoreFromStatement(Statement $statement, $max_score) {
  $result = $statement
    ->getResult();
  if (!isset($result)) {
    return 0;
  }
  $score = $result
    ->getScore();
  if (isset($score)) {
    $scaled = $score
      ->getScaled();
    if (isset($scaled) && $scaled >= 0) {
      return $scaled * $max_score;
    }
    $raw = $score
      ->getRaw();
    $max = $score
      ->getMax();
    $min = $score
      ->getMin();
    if (!isset($min)) {
      $min = 0;
    }
    if (isset($raw) && isset($max)) {
      return (double) ($raw - $min) / ($max - $min) * $max_score;
    }
  }
  $success = $result
    ->getSuccess();
  if (isset($success)) {
    return $max_score;
  }
  return 0;
}