/**
  * Loads a assFormulaQuestion object from a database
  * @param integer $question_id A unique key which defines the question in the database
  */
 public function loadFromDb($question_id)
 {
     global $ilDB;
     $result = $ilDB->queryF("SELECT qpl_questions.* FROM qpl_questions WHERE question_id = %s", array('integer'), array($question_id));
     if ($result->numRows() == 1) {
         $data = $ilDB->fetchAssoc($result);
         $this->setId($question_id);
         $this->setTitle($data["title"]);
         $this->setComment($data["description"]);
         $this->setSuggestedSolution($data["solution_hint"]);
         $this->setOriginalId($data["original_id"]);
         $this->setObjId($data["obj_fi"]);
         $this->setAuthor($data["author"]);
         $this->setOwner($data["owner"]);
         try {
             $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
         } catch (ilTestQuestionPoolException $e) {
         }
         $this->unitrepository = new ilUnitConfigurationRepository($question_id);
         include_once "./Services/RTE/classes/class.ilRTE.php";
         $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
         $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
         // load variables
         $result = $ilDB->queryF("SELECT * FROM il_qpl_qst_fq_var WHERE question_fi = %s", array('integer'), array($question_id));
         if ($result->numRows() > 0) {
             while ($data = $ilDB->fetchAssoc($result)) {
                 $varObj = new assFormulaQuestionVariable($data["variable"], $data["range_min"], $data["range_max"], $this->getUnitrepository()->getUnit($data["unit_fi"]), $data["varprecision"], $data["intprecision"]);
                 $varObj->setRangeMinTxt($data['range_min_txt']);
                 $varObj->setRangeMaxTxt($data['range_max_txt']);
                 $this->addVariable($varObj);
             }
         }
         // load results
         $result = $ilDB->queryF("SELECT * FROM il_qpl_qst_fq_res WHERE question_fi = %s", array('integer'), array($question_id));
         if ($result->numRows() > 0) {
             while ($data = $ilDB->fetchAssoc($result)) {
                 $resObj = new assFormulaQuestionResult($data["result"], $data["range_min"], $data["range_max"], $data["tolerance"], $this->getUnitrepository()->getUnit($data["unit_fi"]), $data["formula"], $data["points"], $data["resprecision"], $data["rating_simple"], $data["rating_sign"], $data["rating_value"], $data["rating_unit"]);
                 $resObj->setResultType($data['result_type']);
                 $resObj->setRangeMinTxt($data['range_min_txt']);
                 $resObj->setRangeMaxTxt($data['range_max_txt']);
                 $this->addResult($resObj);
             }
         }
         // load result units
         $result = $ilDB->queryF("SELECT * FROM il_qpl_qst_fq_res_unit WHERE question_fi = %s", array('integer'), array($question_id));
         if ($result->numRows() > 0) {
             while ($data = $ilDB->fetchAssoc($result)) {
                 $unit = $this->getUnitrepository()->getUnit($data["unit_fi"]);
                 $resObj = $this->getResult($data["result"]);
                 $this->addResultUnit($resObj, $unit);
             }
         }
     }
     parent::loadFromDb($question_id);
 }
 /**
  * Evaluates a posted edit form and writes the form data in the question object
  * @return integer A positive value, if one of the required fields wasn't set, else 0
  */
 public function writePostData($always = false)
 {
     $hasErrors = !$always ? $this->editQuestion(true) : false;
     $checked = true;
     if (!$hasErrors) {
         $this->object->setTitle($_POST["title"]);
         $this->object->setAuthor($_POST["author"]);
         $this->object->setComment($_POST["comment"]);
         include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
         $questiontext = ilUtil::stripOnlySlashes($_POST["question"]);
         $this->object->setQuestion($questiontext);
         $this->object->setEstimatedWorkingTime($_POST["Estimated"]["hh"], $_POST["Estimated"]["mm"], $_POST["Estimated"]["ss"]);
         $this->object->parseQuestionText();
         $found_vars = array();
         $found_results = array();
         foreach ($_POST as $key => $value) {
             if (preg_match("/^unit_(\\\$v\\d+)\$/", $key, $matches)) {
                 array_push($found_vars, $matches[1]);
             }
             if (preg_match("/^unit_(\\\$r\\d+)\$/", $key, $matches)) {
                 array_push($found_results, $matches[1]);
             }
         }
         //			if(!$this->object->checkForDuplicateVariables())
         //			{
         //
         //				$this->addErrorMessage($this->lng->txt("err_duplicate_variables"));
         //				$checked = FALSE;
         //			}
         if (!$this->object->checkForDuplicateResults()) {
             $this->addErrorMessage($this->lng->txt("err_duplicate_results"));
             $checked = FALSE;
         }
         foreach ($found_vars as $variable) {
             if ($this->object->getVariable($variable) != null) {
                 $varObj = new assFormulaQuestionVariable($variable, $_POST["range_min_{$variable}"], $_POST["range_max_{$variable}"], $this->object->getUnitrepository()->getUnit($_POST["unit_{$variable}"]), $_POST["precision_{$variable}"], $_POST["intprecision_{$variable}"]);
                 $varObj->setRangeMinTxt($_POST["range_min_{$variable}"]);
                 $varObj->setRangeMaxTxt($_POST["range_max_{$variable}"]);
                 $this->object->addVariable($varObj);
             }
         }
         $tmp_form_vars = array();
         $tmp_quest_vars = array();
         foreach ($found_results as $result) {
             $tmp_res_match = preg_match_all("/([\$][v][0-9]*)/", $_POST["formula_{$result}"], $form_vars);
             $tmp_form_vars = array_merge($tmp_form_vars, $form_vars[0]);
             $tmp_que_match = preg_match_all("/([\$][v][0-9]*)/", $_POST['question'], $quest_vars);
             $tmp_quest_vars = array_merge($tmp_quest_vars, $quest_vars[0]);
         }
         $result_has_undefined_vars = array_diff($tmp_form_vars, $found_vars);
         $question_has_unused_vars = array_diff($tmp_quest_vars, $tmp_form_vars);
         if (count($result_has_undefined_vars) > 0 || count($question_has_unused_vars) > 0) {
             $error_message = '';
             if (count($result_has_undefined_vars) > 0) {
                 $error_message .= $this->lng->txt("res_contains_undef_var") . '<br>';
             }
             if (count($question_has_unused_vars) > 0) {
                 $error_message .= $this->lng->txt("que_contains_unused_var");
             }
             $checked = false;
             if ($this->isSaveCommand()) {
                 ilUtil::sendFailure($error_message);
             }
         }
         foreach ($found_results as $result) {
             if (is_object($this->object->getUnitrepository()->getUnit($_POST["unit_{$result}"]))) {
                 $tmp_result_unit = $this->object->getUnitrepository()->getUnit($_POST["unit_{$result}"]);
             } else {
                 $tmp_result_unit = NULL;
             }
             if ($this->object->getResult($result) != null) {
                 $use_simple_rating = $_POST["rating_advanced_{$result}"] == 1 ? FALSE : TRUE;
                 $resObj = new assFormulaQuestionResult($result, $_POST["range_min_{$result}"], $_POST["range_max_{$result}"], $_POST["tolerance_{$result}"], $tmp_result_unit, $_POST["formula_{$result}"], $_POST["points_{$result}"], $_POST["precision_{$result}"], $use_simple_rating, $_POST["rating_advanced_{$result}"] == 1 ? $_POST["rating_sign_{$result}"] : "", $_POST["rating_advanced_{$result}"] == 1 ? $_POST["rating_value_{$result}"] : "", $_POST["rating_advanced_{$result}"] == 1 ? $_POST["rating_unit_{$result}"] : "", $_POST["result_type_{$result}"] != 0 ? $_POST["result_type_{$result}"] : 0);
                 $resObj->setRangeMinTxt($_POST["range_min_{$result}"]);
                 $resObj->setRangeMaxTxt($_POST["range_max_{$result}"]);
                 $this->object->addResult($resObj);
                 $this->object->addResultUnits($resObj, $_POST["units_{$result}"]);
             }
         }
         if ($checked == false) {
             return 1;
         } else {
             $this->resetSavedPreviewSession();
             return 0;
         }
     } else {
         return 1;
     }
 }