Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 114 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
MonitorCacheService | |
0.00% |
0 / 114 |
|
0.00% |
0 / 10 |
702 | |
0.00% |
0 / 1 |
executionCreated | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
executionStateChanged | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
fillMonitoringOnExecutionStateChanged | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
30 | |||
testStateChanged | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 | |||
qtiTestStatusChanged | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
deliveryLabelChanged | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
deliveryAuthorized | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
catchTestReactivatedEvent | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
updateTestTakerInformation | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
updateDeliveryInformation | |
0.00% |
0 / 9 |
|
0.00% |
0 / 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) 2015 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * |
21 | */ |
22 | |
23 | namespace oat\taoProctoring\model\monitorCache\implementation; |
24 | |
25 | use oat\generis\model\GenerisRdf; |
26 | use oat\generis\model\OntologyRdfs; |
27 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
28 | use oat\taoDelivery\model\execution\DeliveryExecutionInterface; |
29 | use oat\taoDelivery\model\execution\DeliveryExecutionService; |
30 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionCreated; |
31 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionReactivated; |
32 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionState; |
33 | use oat\tao\model\event\MetadataModified; |
34 | use oat\taoDeliveryRdf\model\DeliveryAssemblyService; |
35 | use oat\taoDeliveryRdf\model\guest\GuestTestUser; |
36 | use oat\taoProctoring\model\monitorCache\DeliveryMonitoringService; |
37 | use oat\taoProctoring\model\Tasks\DeliveryUpdaterTask; |
38 | use oat\taoQtiTest\models\event\QtiTestChangeEvent; |
39 | use oat\taoProctoring\model\execution\DeliveryExecution; |
40 | use oat\taoTests\models\event\TestChangedEvent; |
41 | use oat\taoQtiTest\models\event\QtiTestStateChangeEvent; |
42 | use oat\taoProctoring\model\authorization\AuthorizationGranted; |
43 | |
44 | /** |
45 | * Class MonitorCacheService |
46 | * |
47 | * RDS Implementation of DeliveryMonitoringService without using autoincrement fields and PDO::lastInsertId function. |
48 | * |
49 | * @package oat\taoProctoring\model |
50 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
51 | * |
52 | * @deprecated |
53 | */ |
54 | class MonitorCacheService extends MonitoringStorage |
55 | { |
56 | /** |
57 | * @param DeliveryExecutionCreated $event |
58 | * @throws \common_exception_NotFound |
59 | */ |
60 | public function executionCreated(DeliveryExecutionCreated $event) |
61 | { |
62 | $deliveryExecution = $event->getDeliveryExecution(); |
63 | |
64 | $data = $this->createMonitoringData($deliveryExecution, []); |
65 | |
66 | $data = $this->updateDeliveryInformation($data, $deliveryExecution); |
67 | $data = $this->updateTestTakerInformation($data, $event->getUser()); |
68 | |
69 | $data->updateData([DeliveryMonitoringService::CONNECTIVITY]); |
70 | $success = $this->save($data); |
71 | if (!$success) { |
72 | \common_Logger::w( |
73 | 'monitor cache for delivery ' . $deliveryExecution->getIdentifier() . ' could not be created' |
74 | ); |
75 | } |
76 | } |
77 | |
78 | /** |
79 | * @param DeliveryExecutionState $event |
80 | * @throws \common_exception_Error |
81 | * @throws \common_exception_NotFound |
82 | */ |
83 | public function executionStateChanged(DeliveryExecutionState $event) |
84 | { |
85 | $data = $this->createMonitoringData($event->getDeliveryExecution()); |
86 | |
87 | $this->fillMonitoringOnExecutionStateChanged($event, $data); |
88 | |
89 | $success = $this->partialSave($data); |
90 | if (!$success) { |
91 | \common_Logger::w( |
92 | 'monitor cache for delivery ' . $event->getDeliveryExecution()->getIdentifier() |
93 | . ' could not be created' |
94 | ); |
95 | } |
96 | } |
97 | |
98 | /** |
99 | * @param DeliveryExecutionState $event |
100 | * @param DeliveryMonitoringData $data |
101 | * @throws \common_exception_Error |
102 | * @throws \common_exception_NotFound |
103 | */ |
104 | protected function fillMonitoringOnExecutionStateChanged( |
105 | DeliveryExecutionState $event, |
106 | DeliveryMonitoringData $data |
107 | ) { |
108 | $data->update(DeliveryMonitoringService::STATUS, $event->getState()); |
109 | $data->updateData([DeliveryMonitoringService::CONNECTIVITY]); |
110 | $user = \common_session_SessionManager::getSession()->getUser(); |
111 | |
112 | if ( |
113 | in_array($event->getState(), [DeliveryExecution::STATE_AWAITING, DeliveryExecution::STATE_PAUSED]) |
114 | && $user instanceof GuestTestUser |
115 | ) { |
116 | $data->getDeliveryExecution()->setState(DeliveryExecution::STATE_AUTHORIZED); |
117 | } |
118 | |
119 | if ($event->getState() == DeliveryExecution::STATE_TERMINATED) { |
120 | $data->update( |
121 | DeliveryMonitoringService::END_TIME, |
122 | \tao_helpers_Date::getTimeStamp(time(), true) |
123 | ); |
124 | } |
125 | if ($event->getState() == DeliveryExecution::STATE_FINISHED) { |
126 | $data->update( |
127 | DeliveryMonitoringService::END_TIME, |
128 | \tao_helpers_Date::getTimeStamp($event->getDeliveryExecution()->getFinishTime(), true) |
129 | ); |
130 | } |
131 | } |
132 | |
133 | /** |
134 | * Something changed in the state of the test execution |
135 | * (for example: the current item in the test) |
136 | * |
137 | * @param TestChangedEvent $event |
138 | * @throws \common_exception_NotFound |
139 | * @throws \common_exception_Error |
140 | */ |
141 | public function testStateChanged(TestChangedEvent $event) |
142 | { |
143 | $deliveryExecution = $this |
144 | ->getServiceLocator() |
145 | ->get(DeliveryExecutionService::SERVICE_ID) |
146 | ->getDeliveryExecution($event->getServiceCallId()); |
147 | |
148 | $data = $this->createMonitoringData($deliveryExecution); |
149 | |
150 | $data->update(DeliveryMonitoringService::CURRENT_ASSESSMENT_ITEM, $event->getNewStateDescription()); |
151 | if ($event instanceof QtiTestChangeEvent) { |
152 | $data->setTestSession($event->getSession()); |
153 | $data->updateData([ |
154 | DeliveryMonitoringService::REMAINING_TIME, |
155 | DeliveryMonitoringService::EXTRA_TIME, |
156 | DeliveryMonitoringService::CONNECTIVITY |
157 | ]); |
158 | } |
159 | |
160 | $dataKeys = [ |
161 | DeliveryMonitoringService::STATUS, |
162 | ]; |
163 | $session = $event->getSession(); |
164 | $userId = \common_session_SessionManager::getSession()->getUser()->getIdentifier(); |
165 | if ($deliveryExecution->getUserIdentifier() === $userId) { |
166 | $dataKeys[] = DeliveryMonitoringService::DIFF_TIMESTAMP; |
167 | $dataKeys[] = DeliveryMonitoringService::LAST_TEST_TAKER_ACTIVITY; |
168 | } |
169 | $data->setTestSession($session); |
170 | $data->updateData($dataKeys); |
171 | |
172 | $success = $this->partialSave($data); |
173 | if (!$success) { |
174 | \common_Logger::w('monitor cache for teststate could not be updated'); |
175 | } |
176 | } |
177 | |
178 | /** |
179 | * The status of the test execution has changed |
180 | * (for example: from running to paused) |
181 | * |
182 | * @param QtiTestStateChangeEvent $event |
183 | * @throws \common_exception_NotFound |
184 | */ |
185 | public function qtiTestStatusChanged(QtiTestStateChangeEvent $event) |
186 | { |
187 | /** @var DeliveryExecutionService $deliveryExecutionService */ |
188 | $deliveryExecutionService = $this->getServiceLocator()->get(DeliveryExecutionService::SERVICE_ID); |
189 | $deliveryExecution = $deliveryExecutionService->getDeliveryExecution($event->getServiceCallId()); |
190 | |
191 | $data = $this->createMonitoringData($deliveryExecution); |
192 | |
193 | $data->setTestSession($event->getSession()); |
194 | $data->update(DeliveryMonitoringService::CURRENT_ASSESSMENT_ITEM, $event->getNewStateDescription()); |
195 | $data->updateData([ |
196 | DeliveryMonitoringService::CONNECTIVITY, |
197 | DeliveryMonitoringService::REMAINING_TIME |
198 | ]); |
199 | $success = $this->partialSave($data); |
200 | if (!$success) { |
201 | \common_Logger::w('monitor cache for teststate could not be updated'); |
202 | } |
203 | } |
204 | |
205 | /** |
206 | * Update the label of the delivery across the entrie cache |
207 | * |
208 | * @param MetadataModified $event |
209 | */ |
210 | public function deliveryLabelChanged(MetadataModified $event) |
211 | { |
212 | $resource = $event->getResource(); |
213 | if ($event->getMetadataUri() === OntologyRdfs::RDFS_LABEL) { |
214 | $assemblyClass = DeliveryAssemblyService::singleton()->getRootClass(); |
215 | if ($resource->isInstanceOf($assemblyClass)) { |
216 | /** @var $queueService QueueDispatcherInterface */ |
217 | $queueService = $this->getServiceLocator()->get(QueueDispatcherInterface::SERVICE_ID); |
218 | $queueService->createTask( |
219 | new DeliveryUpdaterTask(), |
220 | [ |
221 | $resource->getUri(), |
222 | $event->getMetadataValue(), |
223 | ], |
224 | 'Update delivery label' |
225 | ); |
226 | } |
227 | } |
228 | } |
229 | |
230 | /** |
231 | * Sets the protor who authorized this delivery execution |
232 | * |
233 | * @param AuthorizationGranted $event |
234 | * @throws \common_exception_NotFound |
235 | */ |
236 | public function deliveryAuthorized(AuthorizationGranted $event) |
237 | { |
238 | $deliveryExecution = $event->getDeliveryExecution(); |
239 | $data = $this->createMonitoringData($deliveryExecution); |
240 | |
241 | $data->update(DeliveryMonitoringService::AUTHORIZED_BY, $event->getAuthorizer()->getIdentifier()); |
242 | if (!$this->partialSave($data)) { |
243 | \common_Logger::w('monitor cache for authorization could not be updated'); |
244 | } |
245 | } |
246 | |
247 | |
248 | /** |
249 | * @param DeliveryExecutionReactivated $event |
250 | * @throws \common_exception_NotFound |
251 | */ |
252 | public function catchTestReactivatedEvent(DeliveryExecutionReactivated $event) |
253 | { |
254 | $deliveryExecution = $event->getDeliveryExecution(); |
255 | |
256 | $data = $this->createMonitoringData($deliveryExecution); |
257 | |
258 | $data->update(DeliveryMonitoringService::REACTIVATE_AUTHORIZED_BY, $event->getUser()->getIdentifier()); |
259 | |
260 | $success = $this->partialSave($data); |
261 | if (!$success) { |
262 | \common_Logger::w( |
263 | 'monitor cache for delivery ' . $deliveryExecution->getIdentifier() . ' could not be created' |
264 | ); |
265 | } |
266 | } |
267 | |
268 | /** |
269 | * @param \oat\taoProctoring\model\monitorCache\implementation\DeliveryMonitoringData $data |
270 | * @param \oat\oatbox\user\User $user |
271 | * @return \oat\taoProctoring\model\monitorCache\implementation\DeliveryMonitoringData |
272 | */ |
273 | protected function updateTestTakerInformation($data, $user) |
274 | { |
275 | // need to add user to event |
276 | $firstNames = $user->getPropertyValues(GenerisRdf::PROPERTY_USER_FIRSTNAME); |
277 | if (!empty($firstNames)) { |
278 | $data->update(DeliveryMonitoringService::TEST_TAKER_FIRST_NAME, reset($firstNames)); |
279 | } |
280 | $lastNames = $user->getPropertyValues(GenerisRdf::PROPERTY_USER_LASTNAME); |
281 | if (!empty($lastNames)) { |
282 | $data->update(DeliveryMonitoringService::TEST_TAKER_LAST_NAME, reset($lastNames)); |
283 | } |
284 | |
285 | return $data; |
286 | } |
287 | |
288 | /** |
289 | * @param \oat\taoProctoring\model\monitorCache\implementation\DeliveryMonitoringData $data |
290 | * @param DeliveryExecutionInterface $deliveryExecution |
291 | * @return \oat\taoProctoring\model\monitorCache\implementation\DeliveryMonitoringData |
292 | * @throws \common_exception_NotFound |
293 | */ |
294 | protected function updateDeliveryInformation($data, $deliveryExecution) |
295 | { |
296 | $data->update(DeliveryMonitoringService::STATUS, $deliveryExecution->getState()->getUri()); |
297 | $data->update(DeliveryMonitoringService::TEST_TAKER, $deliveryExecution->getUserIdentifier()); |
298 | $data->update(DeliveryMonitoringService::DELIVERY_ID, $deliveryExecution->getDelivery()->getUri()); |
299 | $data->update(DeliveryMonitoringService::DELIVERY_NAME, $deliveryExecution->getDelivery()->getLabel()); |
300 | $data->update( |
301 | DeliveryMonitoringService::START_TIME, |
302 | \tao_helpers_Date::getTimeStamp($deliveryExecution->getStartTime(), true) |
303 | ); |
304 | |
305 | return $data; |
306 | } |
307 | } |