/**
  * 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;
 }
 function getTestOutput($active_id, $pass = NULL, $is_postponed = FALSE, $use_post_solutions = FALSE, $show_feedback = FALSE)
 {
     ilUtil::sendInfo($this->lng->txt('enter_valid_values'));
     // get the solution of the user for the active pass or from the last pass if allowed
     $user_solution = null;
     if ($active_id) {
         $solutions = NULL;
         include_once "./Modules/Test/classes/class.ilObjTest.php";
         if (is_null($pass)) {
             $pass = ilObjTest::_getPass($active_id);
         }
         $user_solution["active_id"] = $active_id;
         $user_solution["pass"] = $pass;
         $solutions =& $this->object->getSolutionValues($active_id, $pass);
         foreach ($solutions as $idx => $solution_value) {
             if (preg_match("/^(\\\$v\\d+)\$/", $solution_value["value1"], $matches)) {
                 $user_solution[$matches[1]] = $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"];
                     }
                 }
             }
             if (preg_match("/^(\\\$r\\d+)/", $solution_value["value1"], $matches) && $user_solution[$matches[1]]["result_type"] == 0) {
                 $user_solution[$matches[1]]["result_type"] = assFormulaQuestionResult::getResultTypeByQstId($this->object->getId(), $solution_value["value1"]);
             }
         }
     }
     // generate the question output
     $template = new ilTemplate("tpl.il_as_qpl_formulaquestion_output.html", true, true, 'Modules/TestQuestionPool');
     $questiontext = $this->object->substituteVariables($user_solution);
     $template->setVariable("QUESTIONTEXT", $this->object->prepareTextareaOutput($questiontext, TRUE));
     $questionoutput = $template->get();
     $pageoutput = $this->outQuestionPage("", $is_postponed, $active_id, $questionoutput);
     return $pageoutput;
 }