You are here

public static function OpignoTinCanApiStatements::setScore in Opigno TinCan API 8

Same name and namespace in other branches
  1. 3.x src/OpignoTinCanApiStatements.php \Drupal\opigno_tincan_api\OpignoTinCanApiStatements::setScore()

Sets score.

1 call to OpignoTinCanApiStatements::setScore()
OpignoTinCanApiStatements::setResult in src/OpignoTinCanApiStatements.php
Sets result.

File

src/OpignoTinCanApiStatements.php, line 359

Class

OpignoTinCanApiStatements
Class OpignoTinCanApiStatements.

Namespace

Drupal\opigno_tincan_api

Code

public static function setScore(Result &$result, $raw_score, $max_score = NULL, $min_score = NULL) {

  // _opigno_tincan_api_set_score.
  $score = [];
  if ($min_score === NULL) {
    $min_score = 0;
  }
  if ($max_score === NULL || $max_score === 0 && $min_score === 0) {
    $score['raw'] = $raw_score;
    $result
      ->setScore($score);
    return FALSE;
  }
  if ($max_score <= $min_score) {

    // Max must be greater than min.
    $max_score = $min_score + 1;
  }
  $scaled_max = $max_score - $min_score;
  $scaled_raw = $raw_score - $min_score;
  $scaled = $scaled_max > 0 ? round($scaled_raw / $scaled_max, 2) : 0;
  $result
    ->setScore([
    'min' => $min_score,
    'max' => $max_score,
    'raw' => $raw_score,
    'scaled' => $scaled < -1 ? -1 : ($scaled > 1 ? 1 : $scaled),
  ]);
  return TRUE;
}