You are here

function _opigno_tincan_api_set_score in Opigno TinCan API 7

1 call to _opigno_tincan_api_set_score()
_opigno_tincan_api_set_result in includes/opigno_tincan_api.statements_func.inc

File

includes/opigno_tincan_api.statements_func.inc, line 265

Code

function _opigno_tincan_api_set_score(TinCan\Result &$result, $raw_score, $max_score = null, $min_score = null) {
  if (!_opigno_tincan_api_tincanphp_is_installed()) {
    return false;
  }
  $score = array();
  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_score = $min_score + 1;

    // Max must be greater than min
  }
  $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(array(
    'min' => $min_score,
    'max' => $max_score,
    'raw' => $raw_score,
    'scaled' => $scaled < -1 ? -1 : ($scaled > 1 ? 1 : $scaled),
  ));
  return true;
}