/** * @param assFormulaQuestionUnitCategory $category * @param assFormulaQuestionUnit $unit * @return ilPropertyFormGUI */ protected function initUnitForm(assFormulaQuestionUnitCategory $category = null, assFormulaQuestionUnit $unit = null) { if ($this->unit_form instanceof ilPropertyFormGUI) { return $this->unit_form; } $unit_in_use = false; if ($unit instanceof assFormulaQuestionUnit && $this->repository->isUnitInUse($unit->getId())) { $unit_in_use = true; } $this->unit_form = new ilPropertyFormGUI(); $title = new ilTextInputGUI($this->lng->txt('unit'), 'unit_title'); $title->setDisabled($unit_in_use); $title->setRequired(true); $this->unit_form->addItem($title); $baseunit = new ilSelectInputGUI($this->lng->txt('baseunit'), 'base_unit'); $items = $this->repository->getCategorizedUnits(); $options = array(); $category_name = ''; $new_category = false; foreach ((array) $items as $item) { if ($unit instanceof assFormulaQuestionUnit && $unit->getId() == $item->getId()) { continue; } /** * @var $item assFormulaQuestionUnitCategory|assFormulaQuestionUnitCategory */ if ($item instanceof assFormulaQuestionUnitCategory) { if ($category_name != $item->getDisplayString()) { $new_category = true; $category_name = $item->getDisplayString(); } continue; } $options[$item->getId()] = $item->getDisplayString() . ($new_category ? ' (' . $category_name . ')' : ''); $new_category = false; } $baseunit->setDisabled($unit_in_use); $baseunit->setOptions(array(0 => $this->lng->txt('no_selection')) + $options); $this->unit_form->addItem($baseunit); $factor = new ilNumberInputGUI($this->lng->txt('factor'), 'factor'); $factor->setRequired(true); $factor->setSize(3); $factor->setMinValue(0); $factor->allowDecimals(true); $factor->setDisabled($unit_in_use); $this->unit_form->addItem($factor); if (null === $unit) { $this->unit_form->setTitle($this->lng->txt('new_unit')); $this->unit_form->setFormAction($this->ctrl->getFormAction($this, 'addUnit')); $this->unit_form->addCommandButton('addUnit', $this->lng->txt('save')); } else { $this->ctrl->setParameter($this, 'unit_id', $unit->getId()); if ($unit_in_use) { $this->unit_form->setFormAction($this->ctrl->getFormAction($this, 'showUnitsOfCategory')); } else { $this->unit_form->addCommandButton('saveUnit', $this->lng->txt('save')); $this->unit_form->setFormAction($this->ctrl->getFormAction($this, 'saveUnit')); } $this->unit_form->setTitle(sprintf($this->lng->txt('un_sel_cat_sel_unit'), $category->getDisplayString(), $unit->getDisplayString())); } $this->unit_form->addCommandButton('showUnitsOfCategory', $this->lng->txt('cancel')); return $this->unit_form; }
/** * 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; }
/** * @param assFormulaQuestionUnit $unit */ public function saveUnit(assFormulaQuestionUnit $unit) { /** * @var $ilDB ilDB */ global $ilDB; $res = $ilDB->queryF('SELECT unit_id FROM il_qpl_qst_fq_unit WHERE unit_id = %s', array('integer'), array($unit->getId())); if ($ilDB->fetchAssoc($res)) { $row = $ilDB->fetchAssoc($res); $sequence = $row['sequence']; if (is_null($unit->getBaseUnit()) || !strlen($unit->getBaseUnit())) { $unit->setFactor(1); } $ar = $ilDB->manipulateF('UPDATE il_qpl_qst_fq_unit SET unit = %s, factor = %s, baseunit_fi = %s, category_fi = %s, sequence = %s WHERE unit_id = %s AND question_fi = %s', array('text', 'float', 'integer', 'integer', 'integer', 'integer', 'integer'), array($unit->getUnit(), $unit->getFactor(), (int) $unit->getBaseUnit(), (int) $unit->getCategory(), (int) $unit->getSequence(), (int) $unit->getId(), (int) $this->getConsumerId())); if ($ar > 0) { $this->clearUnits(); } } }