From d5dc910d94790f03c3e2db5cf05b38c660f2eb70 Mon Sep 17 00:00:00 2001 From: Matheus Zych Date: Mon, 5 Jan 2026 10:29:21 +0100 Subject: [PATCH] Fixes issue when creating a formula question with units --- .../classes/class.assFormulaQuestion.php | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php b/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php index 08249fe42224..2e3d309b1b9a 100755 --- a/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php +++ b/components/ILIAS/TestQuestionPool/classes/class.assFormulaQuestion.php @@ -848,12 +848,12 @@ public function calculateReachedPointsFromPreviewSession(ilAssQuestionPreviewSes $points = 0; foreach ($this->getResults() as $result) { - $unit_id = $user_solution[$result->getResult() . '_unit'] ?? null; + $unit_id = $user_solution["{$result->getResult()}_unit"] ?? null; $points += $result->getReachedPoints( $this->getVariables(), $this->getResults(), $user_solution[$result->getResult()] ?? '', - $unit_id !== null ? $this->unitrepository->getUnit($unit_id) : null, + $unit_id !== null ? $this->unitrepository->getUnit((int) $unit_id) : null, $this->unitrepository->getUnits() ); } @@ -1096,30 +1096,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']); }