Skip to content

Commit

Permalink
Test: Fix \ilTestEvaluationUserData::getAvailablePoints accesses un…
Browse files Browse the repository at this point in the history
…defined array keys
  • Loading branch information
mjansenDatabay committed Dec 20, 2024
1 parent ae37ca0 commit 64a8518
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions Modules/Test/classes/class.ilTestEvaluationUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class ilTestEvaluationUserData
*/
public array $passes;
public ?int $lastFinishedPass;
/**
* @var array<int, list<array{id: int, o_id: int, points: float, sequenence: ?int}>>
*/
public array $questions;

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -233,6 +240,9 @@ public function setLastVisit(int $time): void
$this->lastVisit = $time;
}

/**
* @return array<int, ilTestEvaluationPassData>
*/
public function getPasses(): array
{
return $this->passes;
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -329,13 +335,12 @@ public function getQuestionTitles(): array
return $this->questionTitles;
}

/**
* @return null|list<array{id: int, o_id: int, points: float, sequenence: ?int}>
*/
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
Expand All @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit 64a8518

Please sign in to comment.