From 64a8518e9033404577d97bbcc7dda054a7b020b7 Mon Sep 17 00:00:00 2001 From: mjansen Date: Fri, 20 Dec 2024 10:27:48 +0100 Subject: [PATCH] Test: Fix `\ilTestEvaluationUserData::getAvailablePoints` accesses undefined array keys --- .../class.ilTestEvaluationUserData.php | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/Modules/Test/classes/class.ilTestEvaluationUserData.php b/Modules/Test/classes/class.ilTestEvaluationUserData.php index f2c968482d2d..e09f6f27c921 100755 --- a/Modules/Test/classes/class.ilTestEvaluationUserData.php +++ b/Modules/Test/classes/class.ilTestEvaluationUserData.php @@ -53,6 +53,9 @@ class ilTestEvaluationUserData */ public array $passes; public ?int $lastFinishedPass; + /** + * @var array> + */ public array $questions; /** @@ -163,12 +166,14 @@ public function setMark(string $a_mark): void public function getQuestionsWorkedThrough(): int { $questionpass = $this->getScoredPass(); - if (!is_object($this->passes[$questionpass])) { + if (!isset($this->passes[$questionpass])) { $questionpass = 0; } - if (is_object($this->passes[$questionpass])) { + + if (isset($this->passes[$questionpass])) { return $this->passes[$questionpass]->getNrOfAnsweredQuestions(); } + return 0; } @@ -183,9 +188,11 @@ public function getNumberOfQuestions(): int if (!is_object($this->passes[$questionpass])) { $questionpass = 0; } - if (is_object($this->passes[$questionpass])) { + + if (isset($this->passes[$questionpass])) { return $this->passes[$questionpass]->getQuestionCount(); } + return 0; } @@ -233,6 +240,9 @@ public function setLastVisit(int $time): void $this->lastVisit = $time; } + /** + * @return array + */ public function getPasses(): array { return $this->passes; @@ -245,11 +255,7 @@ public function addPass(int $pass_nr, ilTestEvaluationPassData $pass): void public function getPass(int $pass_nr): ?ilTestEvaluationPassData { - if (array_key_exists($pass_nr, $this->passes)) { - return $this->passes[$pass_nr]; - } else { - return null; - } + return $this->passes[$pass_nr] ?? null; } public function getPassCount(): int @@ -259,11 +265,11 @@ public function getPassCount(): int public function getScoredPass(): int { - if ($this->getPassScoring() == 1) { + if ($this->getPassScoring() === 1) { return $this->getBestPass(); - } else { - return $this->getLastPass(); } + + return $this->getLastPass(); } /** * This is used in the export of test results @@ -329,13 +335,12 @@ public function getQuestionTitles(): array return $this->questionTitles; } + /** + * @return null|list + */ public function getQuestions(int $pass = 0): ?array { - if (array_key_exists($pass, $this->questions)) { - return $this->questions[$pass]; - } else { - return null; - } + return $this->questions[$pass] ?? null; } public function addQuestion(int $original_id, int $question_id, float $max_points, int $sequence = null, int $pass = 0): void @@ -345,20 +350,19 @@ public function addQuestion(int $original_id, int $question_id, float $max_point } $this->questions[$pass][] = [ - "id" => $question_id, // the so called "aid" from any historical time - "o_id" => $original_id, // when the "aid" was valid this was the "id" - "points" => $max_points, - "sequence" => $sequence + 'id' => $question_id, // the so called "aid" from any historical time + 'o_id' => $original_id, // when the "aid" was valid this was the "id" + 'points' => $max_points, + 'sequence' => $sequence ]; } + /** + * @return array{id: int, o_id: int, points: float, sequenence: ?int}|null + */ public function getQuestion(int $index, int $pass = 0): ?array { - if (array_key_exists($index, $this->questions[$pass])) { - return $this->questions[$pass][$index]; - } else { - return null; - } + return $this->questions[$pass][$index] ?? null; } public function getQuestionCount(int $pass = 0): int @@ -383,15 +387,17 @@ public function getReachedPoints(int $pass = 0): float public function getAvailablePoints(int $pass = 0): float { - $available = 0; - if (!is_object($this->passes[$pass])) { + if (!isset($this->passes[$pass])) { $pass = 0; } - if (!is_object($this->passes[$pass])) { + + if (!isset($this->passes[$pass])) { return 0; } + $available = $this->passes[$pass]->getMaxPoints(); $available = round($available, 2); + return $available; } @@ -429,11 +435,11 @@ public function getMarkOfficial(): string */ public function getScoredPassObject(): ilTestEvaluationPassData { - if ($this->getPassScoring() == 1) { + if ($this->getPassScoring() === 1) { return $this->getBestPassObject(); - } else { - return $this->getLastPassObject(); } + + return $this->getLastPassObject(); } /**