name = $name; $this->question = $question; $this->shuffle = $shuffle; $this->minChoices = $minChoices; $this->maxChoices = $maxChoices; $this->language = $language; $this->choices = $choices; $this->metadata = $metadata; } public function getName(): string { return $this->name; } public function getQuestion(): string { return $this->question; } public function isShuffle(): bool { return $this->shuffle; } public function getMinChoices(): int { return $this->minChoices; } public function getMaxChoices(): int { return $this->maxChoices; } public function getLanguage(): string { return $this->language; } /** * @return ParsedChoice[] */ public function getChoices(): array { return $this->choices; } public function getCorrectChoices(): array { $choices = []; foreach ($this->choices as $choice) { if ($choice->isCorrect()) { $choices[] = $choice; } } return $choices; } /** * @return ParsedMetadatum[] */ public function getMetadata(): array { return $this->metadata; } public function getMaxScore(): float { if ($this->isMatchCorrectResponse()) { return 1; } if ($this->isNoneResponse()) { return 0; } if ($this->maxChoices === 0) { return $this->getMaxTotalScore(); } if ($this->maxChoices === 1) { return $this->getHigherScore(); } if ($this->maxChoices > 1) { return $this->getHigherPossibleTotalScore(); } return 0; } public function isMatchCorrectResponse(): bool { return $this->getScoreCount() === 0 && $this->hasAtLeastOneCorrectAnswer(); } public function isMapResponse(): bool { return $this->getScoreCount() > 0; } public function isNoneResponse(): bool { return $this->getScoreCount() === 0 && !$this->hasAtLeastOneCorrectAnswer(); } private function hasAtLeastOneCorrectAnswer(): bool { return count($this->getCorrectChoices()) > 0; } private function getMaxTotalScore(): float { $totalScore = 0; foreach ($this->choices as $choice) { if ($choice->getChoiceScore() > 0) { $totalScore += $choice->getChoiceScore(); } } return $totalScore; } private function getHigherScore(): float { $scores = []; foreach ($this->choices as $choice) { if ($choice->getChoiceScore() > 0) { $scores[$choice->getChoiceScore()] = $choice->getChoiceScore(); } } return count($scores) ? max($scores) : 0; } private function getHigherPossibleTotalScore(): float { $scores = []; foreach ($this->choices as $choice) { if ($choice->getChoiceScore() > 0) { $scores[] = $choice->getChoiceScore(); } } rsort($scores); $totalScore = 0; for ($i = 0; $i < $this->maxChoices; $i++) { $totalScore += current($scores); next($scores); } return $totalScore; } private function getScoreCount(): int { $totalScores = 0; foreach ($this->choices as $choice) { if ($choice->getChoiceScore() > 0) { $totalScores++; } } return $totalScores; } }