Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.08% |
37 / 48 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
QtiItemResponseRepository | |
77.08% |
37 / 48 |
|
20.00% |
1 / 5 |
23.34 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
save | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
endItemTimer | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
saveItemState | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
3.02 | |||
saveItemResponses | |
68.00% |
17 / 25 |
|
0.00% |
0 / 1 |
14.96 |
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) 2021 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * @author Ricardo Quintanilha <ricardo.quintanilha@taotesting.com> |
21 | */ |
22 | |
23 | declare(strict_types=1); |
24 | |
25 | namespace oat\taoQtiTest\model\Infrastructure; |
26 | |
27 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
28 | use oat\taoQtiTest\model\Domain\Model\ItemResponse; |
29 | use oat\taoQtiTest\model\Domain\Model\ItemResponseRepositoryInterface; |
30 | use oat\taoQtiTest\models\classes\runner\QtiRunnerInvalidResponsesException; |
31 | use oat\taoQtiTest\models\runner\QtiRunnerEmptyResponsesException; |
32 | use oat\taoQtiTest\models\runner\QtiRunnerItemResponseException; |
33 | use oat\taoQtiTest\models\runner\QtiRunnerService; |
34 | use oat\taoQtiTest\models\runner\RunnerServiceContext; |
35 | use qtism\runtime\tests\AssessmentItemSessionException; |
36 | use taoQtiTest_helpers_TestRunnerUtils as TestRunnerUtils; |
37 | |
38 | class QtiItemResponseRepository implements ItemResponseRepositoryInterface |
39 | { |
40 | /** @var QtiRunnerService */ |
41 | private $runnerService; |
42 | private FeatureFlagChecker $featureFlagChecker; |
43 | private QtiItemResponseValidator $itemResponseValidator; |
44 | |
45 | public function __construct( |
46 | QtiRunnerService $runnerService, |
47 | FeatureFlagChecker $featureFlagChecker, |
48 | QtiItemResponseValidator $itemResponseValidator |
49 | ) { |
50 | $this->runnerService = $runnerService; |
51 | $this->featureFlagChecker = $featureFlagChecker; |
52 | $this->itemResponseValidator = $itemResponseValidator; |
53 | } |
54 | |
55 | public function save(ItemResponse $itemResponse, RunnerServiceContext $serviceContext): void |
56 | { |
57 | if ($this->runnerService->isTerminated($serviceContext)) { |
58 | return; |
59 | } |
60 | |
61 | $this->endItemTimer($itemResponse, $serviceContext); |
62 | $this->saveItemState($itemResponse, $serviceContext); |
63 | $this->saveItemResponses($itemResponse, $serviceContext); |
64 | } |
65 | |
66 | private function endItemTimer(ItemResponse $itemResponse, RunnerServiceContext $serviceContext): void |
67 | { |
68 | if ($itemResponse->getDuration() === null) { |
69 | return; |
70 | } |
71 | |
72 | $this->runnerService->endTimer( |
73 | $serviceContext, |
74 | $itemResponse->getDuration(), |
75 | $itemResponse->getTimestamp() |
76 | ); |
77 | } |
78 | |
79 | private function saveItemState(ItemResponse $itemResponse, RunnerServiceContext $serviceContext): void |
80 | { |
81 | if ( |
82 | empty($itemResponse->getItemIdentifier()) |
83 | || $itemResponse->getState() === null |
84 | ) { |
85 | return; |
86 | } |
87 | |
88 | $this->runnerService->setItemState( |
89 | $serviceContext, |
90 | $itemResponse->getItemIdentifier(), |
91 | $itemResponse->getState() |
92 | ); |
93 | } |
94 | |
95 | private function saveItemResponses(ItemResponse $itemResponse, RunnerServiceContext $serviceContext): void |
96 | { |
97 | if ( |
98 | empty($itemResponse->getItemIdentifier()) |
99 | || $itemResponse->getResponse() === null |
100 | ) { |
101 | return; |
102 | } |
103 | |
104 | $itemDefinition = $this->runnerService->getItemHref($serviceContext, $itemResponse->getItemIdentifier()); |
105 | |
106 | if (empty($itemDefinition)) { |
107 | return; |
108 | } |
109 | |
110 | if ( |
111 | $serviceContext->getCurrentAssessmentItemRef() === false |
112 | || $serviceContext->getCurrentAssessmentItemRef()->getIdentifier() !== $itemResponse->getItemIdentifier() |
113 | ) { |
114 | throw new QtiRunnerItemResponseException(__('Item response identifier does not match current item')); |
115 | } |
116 | |
117 | $responses = $this->runnerService->parsesItemResponse( |
118 | $serviceContext, |
119 | $itemDefinition, |
120 | $itemResponse->getResponse() |
121 | ); |
122 | |
123 | if ($this->featureFlagChecker->isEnabled('FEATURE_FLAG_RESPONSE_VALIDATOR')) { |
124 | try { |
125 | $this->itemResponseValidator->validate($serviceContext->getTestSession(), $responses); |
126 | } catch (AssessmentItemSessionException $e) { |
127 | throw new QtiRunnerInvalidResponsesException($e->getMessage()); |
128 | } |
129 | |
130 | $this->runnerService->storeItemResponse($serviceContext, $itemDefinition, $responses); |
131 | return; |
132 | } |
133 | |
134 | if ( |
135 | $this->runnerService->getTestConfig()->getConfigValue('enableAllowSkipping') |
136 | && !TestRunnerUtils::doesAllowSkipping($serviceContext->getTestSession()) |
137 | && $this->runnerService->emptyResponse($serviceContext, $responses) |
138 | ) { |
139 | throw new QtiRunnerEmptyResponsesException(); |
140 | } |
141 | |
142 | $this->runnerService->storeItemResponse($serviceContext, $itemDefinition, $responses); |
143 | } |
144 | } |