Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
55.00% |
11 / 20 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| UpdateItemContentReferencesService | |
55.00% |
11 / 20 |
|
50.00% |
2 / 4 |
22.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
| isQtiItemContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| resolveCustomInteractionPostProcessing | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 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 | declare(strict_types=1); |
| 21 | |
| 22 | namespace oat\taoQtiTest\models\render; |
| 23 | |
| 24 | use oat\taoItems\model\render\ItemAssetsReplacement; |
| 25 | use oat\taoQtiTest\models\classes\render\CustomInteraction\CustomInteractionPostProcessorAllocator; |
| 26 | |
| 27 | class UpdateItemContentReferencesService |
| 28 | { |
| 29 | /** @var ItemAssetsReplacement */ |
| 30 | private $itemAssetsReplacement; |
| 31 | /** @var CustomInteractionPostProcessorAllocator */ |
| 32 | private $customInteractionPostProcessorAllocator; |
| 33 | |
| 34 | public function __construct( |
| 35 | ItemAssetsReplacement $itemAssetsReplacement, |
| 36 | CustomInteractionPostProcessorAllocator $customInteractionPostProcessorAllocator |
| 37 | ) { |
| 38 | $this->itemAssetsReplacement = $itemAssetsReplacement; |
| 39 | $this->customInteractionPostProcessorAllocator = $customInteractionPostProcessorAllocator; |
| 40 | } |
| 41 | |
| 42 | public function __invoke(array $itemContent): array |
| 43 | { |
| 44 | if ($this->isQtiItemContent($itemContent)) { |
| 45 | $itemContent = $this->resolveCustomInteractionPostProcessing($itemContent); |
| 46 | } |
| 47 | |
| 48 | if (empty($itemContent['assets'])) { |
| 49 | return $itemContent; |
| 50 | } |
| 51 | |
| 52 | $jsonAssets = []; |
| 53 | foreach ($itemContent['assets'] as $type => $assets) { |
| 54 | foreach ($assets as $key => $asset) { |
| 55 | $jsonAssets[$type][$key] = $this->itemAssetsReplacement->postProcessAssets($asset); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | $itemContent['assets'] = $jsonAssets; |
| 60 | |
| 61 | return $itemContent; |
| 62 | } |
| 63 | |
| 64 | private function isQtiItemContent($itemContent): bool |
| 65 | { |
| 66 | return isset($itemContent['type']) && $itemContent['type'] === 'qti'; |
| 67 | } |
| 68 | |
| 69 | private function resolveCustomInteractionPostProcessing(array $itemContent): array |
| 70 | { |
| 71 | foreach ($itemContent['data']['body']['elements'] as &$element) { |
| 72 | if ($element['qtiClass'] === CustomInteractionPostProcessorAllocator::CUSTOM_INTERACTION_QTI_CLASS) { |
| 73 | $postProcessorAllocator = $this->customInteractionPostProcessorAllocator->allocatePostProcessor( |
| 74 | $element['typeIdentifier'] |
| 75 | ); |
| 76 | $element = $postProcessorAllocator->postProcess($element); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return $itemContent; |
| 81 | } |
| 82 | } |