diff --git a/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php b/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php index c3c8a7c44231..f47d721ccda8 100755 --- a/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php +++ b/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php @@ -451,7 +451,7 @@ public function substituteVariables(array $userdata, bool $graphicalOutput = fal $user_value = ''; if (is_array($userdata) && is_array($userdata[$result])) { if (isset($userdata[$result]["unit"]) && $userdata[$result]["unit"] > 0) { - $resunit = $this->getUnitrepository()->getUnit($userdata[$result]["unit"]); + $resunit = $this->getUnitrepository()->getUnit((int) $userdata[$result]["unit"]); } if (isset($userdata[$result]["value"])) { @@ -848,7 +848,7 @@ public function calculateReachedPointsFromPreviewSession(ilAssQuestionPreviewSes $points = 0; foreach ($this->getResults() as $result) { - $result_unit = $result->getResult() . '_unit'; + $result_unit = "{$result->getResult()}_unit"; $unit_id = isset($user_solution[$result_unit]) && is_numeric($user_solution[$result_unit]) ? (int) $user_solution[$result_unit] : null; @@ -893,7 +893,7 @@ public function saveWorkingData( function () use ($answer, $active_id, $pass, $authorized) { foreach ($answer as $key => $value) { $matches = null; - if (preg_match('/^result_(\$r\d+)$/', $key, $matches) !== false) { + if (preg_match('/^result_(\$r\d+)$/', $key, $matches) !== false && $matches !== []) { $queryResult = "SELECT solution_id FROM tst_solutions WHERE active_fi = %s AND pass = %s AND question_fi = %s AND authorized = %s AND " . $this->db->like('value1', 'clob', $matches[1]); if ($this->getStep() !== null) { @@ -1097,30 +1097,27 @@ public function getBestSolution(array $solutions): array if (!array_key_exists($matches[1], $user_solution)) { $user_solution[$matches[1]] = []; } - $user_solution[$matches[1]]['unit'] = $solution_value['value2']; + $user_solution[$matches[1]]['unit'] = (int) $solution_value['value2']; } } foreach ($this->getResults() as $result) { $resVal = $result->calculateFormula($this->getVariables(), $this->getResults(), $this->getId(), false); - if (is_object($result->getUnit())) { - $user_solution[$result->getResult()]['unit'] = $result->getUnit()->getId(); + $unit = $result->getUnit(); + if ($unit instanceof assFormulaQuestionUnit) { + $user_solution[$result->getResult()]['unit'] = $unit->getId(); $user_solution[$result->getResult()]['value'] = $resVal; - } elseif ($result->getUnit() === null) { + } elseif ($unit === null) { $unit_factor = 1; // there is no fix result_unit, any "available unit" is accepted - $available_units = $result->getAvailableResultUnits(parent::getId()); + $available_units = $result->getAvailableResultUnits($this->getId()); $result_name = $result->getResult(); - $check_unit = false; - if (array_key_exists($result_name, $available_units) && - $available_units[$result_name] !== null) { - $check_unit = in_array($user_solution[$result_name]['unit'] ?? null, $available_units[$result_name]); - } - - if ($check_unit == true) { - //get unit-factor + if ( + isset($available_units[$result_name]) + && in_array($user_solution[$result_name]['unit'] ?? null, $available_units[$result_name]) + ) { $unit_factor = assFormulaQuestionUnit::lookupUnitFactor($user_solution[$result_name]['unit']); } diff --git a/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestionGUI.php b/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestionGUI.php index 525cbe6546a8..d56216e7f4ad 100755 --- a/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestionGUI.php +++ b/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestionGUI.php @@ -979,7 +979,7 @@ public function getTestOutput( if (!array_key_exists($matches[1], $user_solution)) { $user_solution[$matches[1]] = []; } - $user_solution[$matches[1]][['unit']] = $solution_value['value2']; + $user_solution[$matches[1]]['unit'] = $solution_value['value2']; } if (preg_match('/^(\$r\d+)/', $solution_value['value1'], $matches) && !isset($user_solution[$matches[1]]['result_type'])) { $user_solution[$matches[1]]['result_type'] = assFormulaQuestionResult::getResultTypeByQstId($this->object->getId(), $solution_value['value1']);