Skip to content

Commit 78466a0

Browse files
matheuszychthojou
authored andcommitted
Fixes empty input being wrongly interpreted as 0 in numeric question
1 parent 5432fa5 commit 78466a0

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

components/ILIAS/TestQuestionPool/classes/class.assNumeric.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ public function contains($value): bool
199199

200200
public function validateSolutionSubmit(): bool
201201
{
202-
if ($this->getSolutionSubmit() === null) {
203-
$this->tpl->setOnScreenMessage('failure', $this->lng->txt("err_no_numeric_value"), true);
202+
if ($this->getRawSolutionSubmit() !== '' && $this->getSolutionSubmit() === null) {
203+
$this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_no_numeric_value'), true);
204204
return false;
205205
}
206206

@@ -209,7 +209,13 @@ public function validateSolutionSubmit(): bool
209209

210210
protected function getSolutionSubmit(): ?float
211211
{
212-
return $this->questionpool_request->float('numeric_result') ?? null;
212+
$numeric_result = $this->getRawSolutionSubmit();
213+
return is_numeric($numeric_result) ? (float) $numeric_result : null;
214+
}
215+
216+
private function getRawSolutionSubmit(): string
217+
{
218+
return $this->questionpool_request->string('numeric_result');
213219
}
214220

215221
public function isValidSolutionSubmit($numeric_solution): bool
@@ -228,33 +234,27 @@ public function saveWorkingData(
228234
?int $pass = null,
229235
bool $authorized = true
230236
): bool {
231-
if (is_null($pass)) {
232-
$pass = ilObjTest::_getPass($active_id);
233-
}
237+
$pass ??= ilObjTest::_getPass($active_id);
234238

235239
$answer = $this->getSolutionSubmit();
236240
$this->getProcessLocker()->executeUserSolutionUpdateLockOperation(
237-
function () use ($answer, $active_id, $pass, $authorized) {
241+
function () use ($answer, $active_id, $pass, $authorized): void {
238242
$result = $this->getCurrentSolutionResultSet($active_id, $pass, $authorized);
239243
$update = -1;
240244
if ($this->db->numRows($result) !== 0) {
241-
$row = $this->db->fetchAssoc($result);
242-
$update = $row['solution_id'];
245+
$update = $this->db->fetchAssoc($result)['solution_id'];
243246
}
244247

245-
if ($update !== -1
246-
&& $answer === '') {
247-
$this->removeSolutionRecordById($update);
248-
return;
249-
}
250-
if ($update !== -1) {
251-
$this->updateCurrentSolution($update, $answer, null, $authorized);
248+
if ($update === -1) {
249+
if ($answer !== null) {
250+
$this->saveCurrentSolution($active_id, $pass, $answer, null, $authorized);
251+
}
252252
return;
253253
}
254254

255-
if ($answer !== '') {
256-
$this->saveCurrentSolution($active_id, $pass, $answer, null, $authorized);
257-
}
255+
$answer === null
256+
? $this->removeSolutionRecordById($update)
257+
: $this->updateCurrentSolution($update, $answer, null, $authorized);
258258
}
259259
);
260260

0 commit comments

Comments
 (0)