Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.15% |
25 / 26 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ChoiceResponseValidationStrategy | |
96.15% |
25 / 26 |
|
66.67% |
2 / 3 |
13 | |
0.00% |
0 / 1 |
isApplicable | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
5.12 | |||
validate | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
6 | |||
getQtiIdentifiersForChoicesByResponseDeclarationId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /* |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2025 (original work) Open Assessment Technologies SA |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoQtiTest\model\Infrastructure\Validation; |
24 | |
25 | use oat\taoQtiTest\models\classes\runner\QtiRunnerInvalidResponsesException; |
26 | use qtism\runtime\common\State; |
27 | |
28 | class ChoiceResponseValidationStrategy implements InteractionResponseValidationStrategy |
29 | { |
30 | private const QTI_CLASS = 'choiceInteraction'; |
31 | |
32 | public function isApplicable(array $itemData): bool |
33 | { |
34 | if (!isset($itemData['data']['body']['elements']) || !is_array($itemData['data']['body']['elements'])) { |
35 | return false; |
36 | } |
37 | |
38 | foreach ($itemData['data']['body']['elements'] as $element) { |
39 | if ($element['qtiClass'] === self::QTI_CLASS) { |
40 | return true; |
41 | } |
42 | } |
43 | |
44 | return false; |
45 | } |
46 | |
47 | public function validate(array $itemData, State $responses): void |
48 | { |
49 | foreach ($itemData['data']['body']['elements'] as $element) { |
50 | if ($element['qtiClass'] !== self::QTI_CLASS || !isset($element['choices'])) { |
51 | continue; |
52 | } |
53 | |
54 | $userChoiceIds = $this->getQtiIdentifiersForChoicesByResponseDeclarationId( |
55 | $responses, |
56 | $element['attributes']['responseIdentifier'] |
57 | ); |
58 | $validChoiceIds = []; |
59 | foreach ($element['choices'] as $choice) { |
60 | $validChoiceIds[] = $choice['identifier']; |
61 | } |
62 | |
63 | $diff = array_diff($userChoiceIds, $validChoiceIds); |
64 | if ($diff !== []) { |
65 | throw new QtiRunnerInvalidResponsesException(sprintf( |
66 | 'Invalid choice identifiers: [%s]', |
67 | implode(',', $diff) |
68 | )); |
69 | } |
70 | } |
71 | } |
72 | |
73 | private function getQtiIdentifiersForChoicesByResponseDeclarationId(State $responses, string $key): array |
74 | { |
75 | $result = []; |
76 | foreach ($responses->getVariable($key)->getValue() as $response) { |
77 | $result[] = (string) $response->getValue(); |
78 | } |
79 | |
80 | return $result; |
81 | } |
82 | } |