/** * Returns the best solution for a given pass of a participant * @return array An associated array containing the best solution * @access public */ public function getBestSolution($solutions) { $user_solution = array(); foreach ($solutions as $idx => $solution_value) { if (preg_match("/^(\\\$v\\d+)\$/", $solution_value["value1"], $matches)) { $user_solution[$matches[1]] = $solution_value["value2"]; $varObj = $this->getVariable($matches[1]); $varObj->setValue($solution_value["value2"]); } else { if (preg_match("/^(\\\$r\\d+)\$/", $solution_value["value1"], $matches)) { if (!array_key_exists($matches[1], $user_solution)) { $user_solution[$matches[1]] = array(); } $user_solution[$matches[1]]["value"] = $solution_value["value2"]; } else { if (preg_match("/^(\\\$r\\d+)_unit\$/", $solution_value["value1"], $matches)) { if (!array_key_exists($matches[1], $user_solution)) { $user_solution[$matches[1]] = array(); } $user_solution[$matches[1]]["unit"] = $solution_value["value2"]; } } } } foreach ($this->getResults() as $result) { $resVal = $result->calculateFormula($this->getVariables(), $this->getResults(), parent::getId(), false); if (is_object($result->getUnit())) { $user_solution[$result->getResult()]["unit"] = $result->getUnit()->getId(); $user_solution[$result->getResult()]["value"] = $resVal; } else { if ($result->getUnit() == NULL) { $unit_factor = 1; // there is no fix result_unit, any "available unit" is accepted $available_units = $result->getAvailableResultUnits(parent::getId()); $result_name = $result->getResult(); if ($available_units[$result_name] != NULL) { $check_unit = in_array($user_solution[$result_name]['unit'], $available_units[$result_name]); } if ($check_unit == true) { //get unit-factor $unit_factor = assFormulaQuestionUnit::lookupUnitFactor($user_solution[$result_name]['unit']); $user_solution[$result->getResult()]["value"] = round(ilMath::_div($resVal, $unit_factor), 55); } } } if ($result->getResultType() == assFormulaQuestionResult::RESULT_CO_FRAC || $result->getResultType() == assFormulaQuestionResult::RESULT_FRAC) { $value = assFormulaQuestionResult::convertDecimalToCoprimeFraction($resVal); if (is_array($value)) { $frac_helper = $value[1]; $value = $value[0]; } $user_solution[$result->getResult()]["value"] = $value; $user_solution[$result->getResult()]["frac_helper"] = $frac_helper; } elseif ($result->getPrecision() > 0) { $user_solution[$result->getResult()]["value"] = round($resVal, $result->getPrecision()); } else { $user_solution[$result->getResult()]["value"] = round($resVal); } } return $user_solution; }