Skip to content

Commit

Permalink
Fix wrong order of units in formula question
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuszych authored and thojou committed Jul 8, 2024
1 parent 16b169b commit 9805e75
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Modules/TestQuestionPool/classes/class.assFormulaQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,19 @@ public function addResultUnit($result, $unit): void
}
}

public function getResultUnits($result)
public function getResultUnits(assFormulaQuestionResult $result): array
{
if (array_key_exists($result->getResult(), $this->resultunits)) {
return $this->resultunits[$result->getResult()];
} else {
return array();
if (!isset($this->resultunits[$result->getResult()])) {
return [];
}

$result_units = $this->resultunits[$result->getResult()];

usort($result_units, static function (assFormulaQuestionUnit $a, assFormulaQuestionUnit $b) {
return $a->getSequence() <=> $b->getSequence();
});

return $result_units;
}

public function getAllResultUnits(): array
Expand Down Expand Up @@ -318,10 +324,11 @@ public function substituteVariables(array $userdata, bool $graphicalOutput = fal
}

$units = "";
if (count($this->getResultUnits($resObj)) > 0) {
$result_units = $this->getResultUnits($resObj);
if (count($result_units) > 0) {
if ($forsolution) {
if (is_array($userdata)) {
foreach ($this->getResultUnits($resObj) as $unit) {
foreach ($result_units as $unit) {
if (isset($userdata[$result]["unit"]) && $userdata[$result]["unit"] == $unit->getId()) {
$units = $unit->getUnit();
}
Expand All @@ -334,7 +341,7 @@ public function substituteVariables(array $userdata, bool $graphicalOutput = fal
} else {
$units = '<select name="result_' . $result . '_unit">';
$units .= '<option value="-1">' . $this->lng->txt("select_unit") . '</option>';
foreach ($this->getResultUnits($resObj) as $unit) {
foreach ($result_units as $unit) {
$units .= '<option value="' . $unit->getId() . '"';
if (array_key_exists($result, $userdata) &&
is_array($userdata[$result]) &&
Expand Down

0 comments on commit 9805e75

Please sign in to comment.