Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
QtiTimeConstraint | |
0.00% |
0 / 76 |
|
0.00% |
0 / 12 |
992 | |
0.00% |
0 / 1 |
getTimer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setTimer | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getApplyExtraTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setApplyExtraTime | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setTimerTarget | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getRemainingTimeFrom | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getMaximumRemainingTime | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
getMinimumRemainingTime | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
getAdjustedMaxTime | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
durationToMs | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
jsonSerialize | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
56 |
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) 2016-2018 (original work) Open Assessment Technologies SA ; |
19 | */ |
20 | |
21 | /** |
22 | * @author Jean-Sébastien Conan <jean-sebastien.conan@vesperiagroup.com> |
23 | */ |
24 | |
25 | namespace oat\taoQtiTest\models\runner\time; |
26 | |
27 | use oat\oatbox\service\ServiceManager; |
28 | use oat\taoTests\models\runner\time\TimePoint; |
29 | use qtism\common\datatypes\QtiDuration; |
30 | use qtism\data\NavigationMode; |
31 | use qtism\data\QtiComponent; |
32 | use qtism\runtime\tests\TimeConstraint; |
33 | use taoQtiTest_helpers_TestRunnerUtils as TestRunnerUtils; |
34 | |
35 | /** |
36 | * Class QtiTimeConstraint |
37 | * |
38 | * Represents a time constraint during an AssessmentTestSession. |
39 | * |
40 | * @package oat\taoQtiTest\models\runner\time |
41 | */ |
42 | class QtiTimeConstraint extends TimeConstraint implements \JsonSerializable |
43 | { |
44 | /** |
45 | * @var QtiTimer |
46 | */ |
47 | protected $timer; |
48 | |
49 | /** |
50 | * Allow to take care of extra time |
51 | * @var boolean |
52 | */ |
53 | protected $applyExtraTime; |
54 | |
55 | /** |
56 | * @var integer |
57 | */ |
58 | protected $timerTarget; |
59 | |
60 | /** |
61 | * @return QtiTimer |
62 | */ |
63 | public function getTimer() |
64 | { |
65 | return $this->timer; |
66 | } |
67 | |
68 | /** |
69 | * @param QtiTimer $timer |
70 | * @return QtiTimeConstraint |
71 | */ |
72 | public function setTimer($timer) |
73 | { |
74 | $this->timer = $timer; |
75 | return $this; |
76 | } |
77 | |
78 | /** |
79 | * @return boolean |
80 | */ |
81 | public function getApplyExtraTime() |
82 | { |
83 | return $this->applyExtraTime; |
84 | } |
85 | |
86 | /** |
87 | * @param boolean $applyExtraTime |
88 | * @return QtiTimeConstraint |
89 | */ |
90 | public function setApplyExtraTime($applyExtraTime) |
91 | { |
92 | $this->applyExtraTime = $applyExtraTime; |
93 | return $this; |
94 | } |
95 | |
96 | /** |
97 | * @param integer $timerTarget |
98 | * @return QtiTimeConstraint |
99 | */ |
100 | public function setTimerTarget($timerTarget) |
101 | { |
102 | $this->timerTarget = $timerTarget; |
103 | return $this; |
104 | } |
105 | |
106 | /** |
107 | * Create a new TimeConstraint object. |
108 | * |
109 | * @param QtiComponent $source The TestPart or SectionPart the constraint applies on. |
110 | * @param QtiDuration $duration The already spent duration by the candidate on $source. |
111 | * @param int|NavigationMode $navigationMode The current navigation mode. |
112 | * @param boolean $considerMinTime Whether or not to consider minimum time limits. |
113 | * @param boolean $applyExtraTime Allow to take care of extra time |
114 | * @param integer $timerTarget client/server |
115 | */ |
116 | public function __construct( |
117 | QtiComponent $source, |
118 | QtiDuration $duration, |
119 | $navigationMode = NavigationMode::LINEAR, |
120 | $considerMinTime = true, |
121 | $applyExtraTime = false, |
122 | $timerTarget = TimePoint::TARGET_SERVER |
123 | ) { |
124 | $this->setSource($source); |
125 | $this->setDuration($duration); |
126 | $this->setNavigationMode($navigationMode); |
127 | $this->setApplyExtraTime($applyExtraTime); |
128 | $this->setTimerTarget($timerTarget); |
129 | } |
130 | |
131 | /** |
132 | * Get the remaining duration from a source (min or max time, usually) |
133 | * @param QtiDuration $duration the source duration |
134 | * @return Duration|false A Duration object (or false of not available) that represents the remaining time |
135 | */ |
136 | protected function getRemainingTimeFrom(QtiDuration $duration) |
137 | { |
138 | if (!is_null($duration)) { |
139 | $remaining = clone $duration; |
140 | $remaining->sub($this->getDuration()); |
141 | return ($remaining->isNegative() === true) ? new QtiDuration('PT0S') : $remaining; |
142 | } |
143 | return false; |
144 | } |
145 | |
146 | /** |
147 | * Get the time remaining to be spent by the candidate on the source of the time |
148 | * constraint. Please note that this method will never return negative durations. |
149 | * |
150 | * @return Duration A Duration object or null if there is no maxTime constraint running for the source of the time |
151 | * constraint. |
152 | */ |
153 | public function getMaximumRemainingTime() |
154 | { |
155 | if (($maxTime = $this->getAdjustedMaxTime()) !== null) { |
156 | $maximumTime = clone $maxTime; |
157 | |
158 | if ($this->getApplyExtraTime() && $this->timer) { |
159 | // take care of the already consumed extra time under the current constraint |
160 | // and append the full remaining extra time |
161 | // the total must correspond to the already elapsed time plus the remaining time |
162 | $maximumTime->add(new QtiDuration('PT' . $this->timer->getExtraTime() . 'S')); |
163 | } |
164 | |
165 | return $this->getRemainingTimeFrom($maximumTime); |
166 | } |
167 | return false; |
168 | } |
169 | |
170 | /** |
171 | * Get the time remaining the candidate has to spend by the candidate on the source of the time |
172 | * constraint. Please note that this method will never return negative durations. |
173 | * |
174 | * @return Duration A Duration object or null if there is no minTime constraint running for the source of the time |
175 | * constraint. |
176 | */ |
177 | public function getMinimumRemainingTime() |
178 | { |
179 | if ( |
180 | ($timeLimits = $this->getSource()->getTimeLimits()) !== null |
181 | && ($minTime = $timeLimits->getMinTime()) !== null |
182 | ) { |
183 | return $this->getRemainingTimeFrom($minTime); |
184 | } |
185 | return false; |
186 | } |
187 | |
188 | /** |
189 | * Calculates maximum time with applied adjustments |
190 | * @return QtiDuration|null |
191 | */ |
192 | public function getAdjustedMaxTime() |
193 | { |
194 | $timeLimits = $this->getSource()->getTimeLimits(); |
195 | if ($timeLimits === null) { |
196 | return null; |
197 | } |
198 | |
199 | $maxTime = $timeLimits->getMaxTime(); |
200 | if ($maxTime === null) { |
201 | return null; |
202 | } |
203 | |
204 | $maximumTime = clone $maxTime; |
205 | if ($this->timer) { |
206 | $adjustmentSeconds = $this->timer->getAdjustmentMap()->get($this->getSource()->getIdentifier()); |
207 | if ($adjustmentSeconds > 0) { |
208 | $maximumTime->add(new QtiDuration('PT' . $adjustmentSeconds . 'S')); |
209 | } else { |
210 | $maximumTime->sub(new QtiDuration('PT' . abs($adjustmentSeconds) . 'S')); |
211 | } |
212 | } |
213 | |
214 | return $maximumTime; |
215 | } |
216 | |
217 | /** |
218 | * Converts a duration to milliseconds |
219 | * @param QtiDuration|null $duration the duration to convert |
220 | * @return int|false the duration in ms or false if none |
221 | */ |
222 | private function durationToMs($duration) |
223 | { |
224 | if (!is_null($duration) && $duration instanceof QtiDuration) { |
225 | return TestRunnerUtils::getDurationWithMicroseconds($duration); |
226 | } |
227 | return false; |
228 | } |
229 | |
230 | /** |
231 | * Serialize the constraint the expected way by the TestContext and the TestMap |
232 | * @return array |
233 | */ |
234 | public function jsonSerialize(): array |
235 | { |
236 | $source = $this->getSource(); |
237 | $timeLimits = $source->getTimeLimits(); |
238 | if (!is_null($timeLimits)) { |
239 | $identifier = $source->getIdentifier(); |
240 | |
241 | $maxTime = $this->getAdjustedMaxTime(); |
242 | $minTime = $timeLimits->getMinTime(); |
243 | $maxTimeRemaining = $this->getMaximumRemainingTime(); |
244 | $minTimeRemaining = $this->getMinimumRemainingTime(); |
245 | if ($maxTimeRemaining !== false || $minTimeRemaining !== false) { |
246 | $label = method_exists($source, 'getTitle') ? $source->getTitle() : $identifier; |
247 | |
248 | $extraTime = []; |
249 | if ($this->getTimer() !== null && $maxTime !== null) { |
250 | $timer = $this->getTimer(); |
251 | $maxTimeSeconds = $maxTime->getSeconds(true); |
252 | $extraTime = [ |
253 | 'total' => $timer->getExtraTime(), |
254 | 'consumed' => $timer->getConsumedExtraTime($identifier, $maxTimeSeconds, $this->timerTarget), |
255 | 'remaining' => $timer->getRemainingExtraTime($identifier, $maxTimeSeconds, $this->timerTarget), |
256 | ]; |
257 | } |
258 | |
259 | /** @var TimerLabelFormatterService $labelFormatter */ |
260 | $labelFormatter = ServiceManager::getServiceManager()->get(TimerLabelFormatterService::SERVICE_ID); |
261 | return [ |
262 | 'label' => $labelFormatter->format($label), |
263 | 'source' => $identifier, |
264 | 'qtiClassName' => $source->getQtiClassName(), |
265 | 'extraTime' => $extraTime, |
266 | 'allowLateSubmission' => $this->allowLateSubmission(), |
267 | 'minTime' => $this->durationToMs($minTime), |
268 | 'minTimeRemaining' => $this->durationToMs($minTimeRemaining), |
269 | 'maxTime' => $this->durationToMs($maxTime), |
270 | 'maxTimeRemaining' => $this->durationToMs($maxTimeRemaining), |
271 | ]; |
272 | } |
273 | } |
274 | return []; |
275 | } |
276 | } |