Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
1.23% |
1 / 81 |
|
4.00% |
1 / 25 |
CRAP | |
0.00% |
0 / 1 |
| ExtendedState | |
1.23% |
1 / 81 |
|
4.00% |
1 / 25 |
1909.18 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTestSessionId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTestSessionId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getUserId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setUserId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getStorage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setStorage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getStorageKeyFromTestSessionId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getStorageKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| load | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| save | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| setItemFlag | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getItemFlag | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| setStoreId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getStoreId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| addEvent | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| getEvents | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| removeEvents | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| clearEvents | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setItemHrefIndex | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getItemHrefIndex | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| setCatValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getCatValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| removeCatValue | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| deleteDeliveryExecutionData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 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-2017 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiTest\models\runner; |
| 22 | |
| 23 | use oat\oatbox\service\ServiceManagerAwareInterface; |
| 24 | use oat\oatbox\service\ServiceManagerAwareTrait; |
| 25 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDelete; |
| 26 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDeleteRequest; |
| 27 | |
| 28 | /** |
| 29 | * Manage the flagged items |
| 30 | */ |
| 31 | class ExtendedState implements ServiceManagerAwareInterface, DeliveryExecutionDelete |
| 32 | { |
| 33 | use ServiceManagerAwareTrait; |
| 34 | |
| 35 | public const STORAGE_PREFIX = 'extra_'; |
| 36 | public const VAR_REVIEW = 'review'; |
| 37 | public const VAR_STORE_ID = 'client_store_id'; |
| 38 | public const VAR_EVENTS_QUEUE = 'events_queue'; |
| 39 | public const VAR_CAT = 'cat'; |
| 40 | public const VAR_HREF_INDEX = 'item_href_index'; |
| 41 | |
| 42 | /** |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $testSessionId; |
| 46 | |
| 47 | /** |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $userId; |
| 51 | |
| 52 | /** |
| 53 | * @var array |
| 54 | */ |
| 55 | protected $state = []; |
| 56 | |
| 57 | /** |
| 58 | * @var StorageManager |
| 59 | */ |
| 60 | protected $storage; |
| 61 | |
| 62 | /** |
| 63 | * ExtendedState constructor. |
| 64 | * @param string $testSessionId |
| 65 | * @param string $userId |
| 66 | */ |
| 67 | public function __construct($testSessionId = '', $userId = '') |
| 68 | { |
| 69 | $this->setTestSessionId($testSessionId); |
| 70 | $this->setUserId($userId); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | */ |
| 76 | public function getTestSessionId() |
| 77 | { |
| 78 | return $this->testSessionId; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param string $testSessionId |
| 83 | * @return ExtendedState |
| 84 | */ |
| 85 | public function setTestSessionId($testSessionId) |
| 86 | { |
| 87 | $this->testSessionId = $testSessionId; |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return string |
| 93 | */ |
| 94 | public function getUserId() |
| 95 | { |
| 96 | return $this->userId; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param string $userId |
| 101 | * @return ExtendedState |
| 102 | */ |
| 103 | public function setUserId($userId) |
| 104 | { |
| 105 | $this->userId = $userId; |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Gets the StorageManager service |
| 111 | * @return StorageManager |
| 112 | */ |
| 113 | public function getStorage() |
| 114 | { |
| 115 | if (!$this->storage) { |
| 116 | $this->storage = $this->getServiceLocator()->get(StorageManager::SERVICE_ID); |
| 117 | } |
| 118 | return $this->storage; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the StorageManager service |
| 123 | * @param StorageManager $storage |
| 124 | * @return ExtendedState |
| 125 | */ |
| 126 | public function setStorage($storage) |
| 127 | { |
| 128 | $this->storage = $storage; |
| 129 | return $this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets the storage key based on the provided Test Session Id. |
| 134 | * @param string $testSessionId |
| 135 | * @return string |
| 136 | */ |
| 137 | public static function getStorageKeyFromTestSessionId($testSessionId) |
| 138 | { |
| 139 | return self::STORAGE_PREFIX . $testSessionId; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Gets the storage key based on the nested Test Session Id. |
| 144 | * @return string |
| 145 | */ |
| 146 | public function getStorageKey() |
| 147 | { |
| 148 | return self::getStorageKeyFromTestSessionId($this->testSessionId); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Loads the extended state from the storage |
| 153 | * @return bool |
| 154 | */ |
| 155 | public function load() |
| 156 | { |
| 157 | $storage = $this->getStorage(); |
| 158 | if ($storage) { |
| 159 | $data = $storage->get($this->userId, $this->getStorageKey()); |
| 160 | if ($data) { |
| 161 | $this->state = json_decode($data, true); |
| 162 | } else { |
| 163 | $this->state = []; |
| 164 | } |
| 165 | $success = is_array($this->state); |
| 166 | } else { |
| 167 | $success = false; |
| 168 | } |
| 169 | return $success; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Saves the extended state into the storage |
| 174 | * @return bool |
| 175 | */ |
| 176 | public function save() |
| 177 | { |
| 178 | $storage = $this->getStorage(); |
| 179 | if ($storage) { |
| 180 | $success = $storage->set($this->userId, $this->getStorageKey(), json_encode($this->state)); |
| 181 | } else { |
| 182 | $success = false; |
| 183 | } |
| 184 | return $success; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Set the marked for review state of an item |
| 189 | * @param string $itemRef |
| 190 | * @param bool $flag |
| 191 | * @return ExtendedState |
| 192 | */ |
| 193 | public function setItemFlag($itemRef, $flag) |
| 194 | { |
| 195 | $this->state[self::VAR_REVIEW][$itemRef] = $flag; |
| 196 | return $this; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Gets the marked for review state of an item |
| 201 | * @param string $itemRef |
| 202 | * @return bool |
| 203 | */ |
| 204 | public function getItemFlag($itemRef) |
| 205 | { |
| 206 | return isset($this->state[self::VAR_REVIEW]) && isset($this->state[self::VAR_REVIEW][$itemRef]) |
| 207 | ? $this->state[self::VAR_REVIEW][$itemRef] |
| 208 | : false; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Sets the name of the client store used for the timer |
| 213 | * @param string $storeId |
| 214 | * @return ExtendedState |
| 215 | */ |
| 216 | public function setStoreId($storeId) |
| 217 | { |
| 218 | $this->state[self::VAR_STORE_ID] = $storeId; |
| 219 | return $this; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Gets the name of the client store used for the timer |
| 224 | * @return bool |
| 225 | */ |
| 226 | public function getStoreId() |
| 227 | { |
| 228 | return isset($this->state[self::VAR_STORE_ID]) ? $this->state[self::VAR_STORE_ID] : false; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Add an event on top of the queue |
| 233 | * @param string $eventName |
| 234 | * @param mixed $data |
| 235 | * @return string |
| 236 | * @throws \common_Exception |
| 237 | */ |
| 238 | public function addEvent($eventName, $data = null) |
| 239 | { |
| 240 | $eventId = uniqid('event', true); |
| 241 | $this->state[self::VAR_EVENTS_QUEUE][$eventId] = [ |
| 242 | 'id' => $eventId, |
| 243 | 'timestamp' => microtime(true), |
| 244 | 'user' => \common_session_SessionManager::getSession()->getUserUri(), |
| 245 | 'type' => $eventName, |
| 246 | 'data' => $data, |
| 247 | ]; |
| 248 | return $eventId; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Gets all events from the queue |
| 253 | * @return array |
| 254 | */ |
| 255 | public function getEvents() |
| 256 | { |
| 257 | if (isset($this->state[self::VAR_EVENTS_QUEUE])) { |
| 258 | $events = $this->state[self::VAR_EVENTS_QUEUE]; |
| 259 | } else { |
| 260 | $events = []; |
| 261 | } |
| 262 | return $events; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Removes particular events from the queue |
| 267 | * @param array $ids |
| 268 | * @return bool |
| 269 | */ |
| 270 | public function removeEvents($ids = []) |
| 271 | { |
| 272 | $removed = false; |
| 273 | if (isset($this->state[self::VAR_EVENTS_QUEUE])) { |
| 274 | foreach ($ids as $id) { |
| 275 | if (isset($this->state[self::VAR_EVENTS_QUEUE][$id])) { |
| 276 | unset($this->state[self::VAR_EVENTS_QUEUE][$id]); |
| 277 | $removed = true; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | return $removed; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Removes all events from the queue |
| 286 | * @return ExtendedState |
| 287 | */ |
| 288 | public function clearEvents() |
| 289 | { |
| 290 | $this->state[self::VAR_EVENTS_QUEUE] = []; |
| 291 | return $this; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Stores the table that maps the items identifiers to item reference |
| 296 | * Fallback index in case of the delivery was compiled without the index of item href |
| 297 | * @param array $table |
| 298 | * @return ExtendedState |
| 299 | */ |
| 300 | public function setItemHrefIndex($table) |
| 301 | { |
| 302 | $this->state[self::VAR_HREF_INDEX] = $table; |
| 303 | return $this; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Loads the table that maps the items identifiers to item reference |
| 308 | * Fallback index in case of the delivery was compiled without the index of item href |
| 309 | * @return array |
| 310 | */ |
| 311 | public function getItemHrefIndex() |
| 312 | { |
| 313 | if (isset($this->state[self::VAR_HREF_INDEX])) { |
| 314 | $table = $this->state[self::VAR_HREF_INDEX]; |
| 315 | } else { |
| 316 | $table = []; |
| 317 | } |
| 318 | return $table; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Sets a CAT value in the Extended State. |
| 323 | * @param string $assessmentSectionId |
| 324 | * @param string $key |
| 325 | * @param string $value |
| 326 | * @return ExtendedState |
| 327 | */ |
| 328 | public function setCatValue($assessmentSectionId, $key, $value) |
| 329 | { |
| 330 | $this->state[self::VAR_CAT][$assessmentSectionId][$key] = $value; |
| 331 | return $this; |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Gets a CAT value from the Extended State. |
| 336 | * @param string $assessmentSectionId |
| 337 | * @param string $key |
| 338 | * @return string |
| 339 | */ |
| 340 | public function getCatValue($assessmentSectionId, $key) |
| 341 | { |
| 342 | return (isset($this->state[self::VAR_CAT]) |
| 343 | && isset($this->state[self::VAR_CAT][$assessmentSectionId]) |
| 344 | && isset($this->state[self::VAR_CAT][$assessmentSectionId][$key])) |
| 345 | ? $this->state[self::VAR_CAT][$assessmentSectionId][$key] |
| 346 | : null; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Removes a CAT value from the ExtendedState. |
| 351 | * @param string $assessmentSectionId |
| 352 | * @param string $key |
| 353 | * @return ExtendedState |
| 354 | */ |
| 355 | public function removeCatValue($assessmentSectionId, $key) |
| 356 | { |
| 357 | if ( |
| 358 | isset($this->state[self::VAR_CAT]) |
| 359 | && isset($this->state[self::VAR_CAT][$assessmentSectionId]) |
| 360 | && isset($this->state[self::VAR_CAT][$assessmentSectionId][$key]) |
| 361 | ) { |
| 362 | unset($this->state[self::VAR_CAT][$assessmentSectionId][$key]); |
| 363 | } |
| 364 | return $this; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * @inheritdoc |
| 369 | */ |
| 370 | public function deleteDeliveryExecutionData(DeliveryExecutionDeleteRequest $request) |
| 371 | { |
| 372 | $storage = $this->getStorage(); |
| 373 | if ($storage) { |
| 374 | return $storage->del($this->userId, $this->getStorageKey()); |
| 375 | } |
| 376 | |
| 377 | return false; |
| 378 | } |
| 379 | } |