Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 81 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ImsPciExporter | |
0.00% |
0 / 81 |
|
0.00% |
0 / 8 |
756 | |
0.00% |
0 / 1 |
| copyAssetFiles | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| getNodeName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTypeIdentifierAttributeName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getXmlnsName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| exportDom | |
0.00% |
0 / 55 |
|
0.00% |
0 / 1 |
342 | |||
| getImsPciExportPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| replaceFile | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getItemRelativePath | |
0.00% |
0 / 5 |
|
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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\qtiItemPci\model\portableElement\export; |
| 23 | |
| 24 | use DOMDocument; |
| 25 | use DOMXPath; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\taoQtiItem\model\portableElement\element\PortableElementObject; |
| 28 | use oat\taoQtiItem\model\portableElement\export\PortableElementExporter; |
| 29 | use oat\taoQtiItem\model\portableElement\PortableElementService; |
| 30 | |
| 31 | class ImsPciExporter extends PortableElementExporter |
| 32 | { |
| 33 | private const EXPORTER_PATH_SEPARATOR = '/'; |
| 34 | |
| 35 | /** |
| 36 | * Cope the asset files of the PCI to the item exporter and return the list of copied assets |
| 37 | * @param $replacementList |
| 38 | * @return array |
| 39 | * @throws \oat\taoQtiItem\model\portableElement\exception\PortableElementInvalidAssetException |
| 40 | */ |
| 41 | public function copyAssetFiles(&$replacementList) |
| 42 | { |
| 43 | $object = $this->object; |
| 44 | $portableAssetsToExport = []; |
| 45 | $service = new PortableElementService(); |
| 46 | $service->setServiceLocator(ServiceManager::getServiceManager()); |
| 47 | $files = $object->getModel()->getValidator()->getAssets($object, 'runtime'); |
| 48 | $baseUrl = '';//add pcis to the root of the package |
| 49 | foreach ($files as $url) { |
| 50 | $stream = $service->getFileStream($object, $url); |
| 51 | $replacement = $this->qtiItemExporter->copyAssetFile($stream, $baseUrl, $url, $replacementList); |
| 52 | $portableAssetsToExport[$url] = $replacement; |
| 53 | \common_Logger::i('File copied: "' . $url . '" for portable element ' . $object->getTypeIdentifier()); |
| 54 | } |
| 55 | return $this->portableAssetsToExport = $portableAssetsToExport; |
| 56 | } |
| 57 | |
| 58 | public function getNodeName() |
| 59 | { |
| 60 | return 'portableCustomInteraction'; |
| 61 | } |
| 62 | |
| 63 | public function getTypeIdentifierAttributeName() |
| 64 | { |
| 65 | return 'customInteractionTypeIdentifier'; |
| 66 | } |
| 67 | |
| 68 | public function getXmlnsName() |
| 69 | { |
| 70 | return 'pci'; |
| 71 | } |
| 72 | |
| 73 | public function exportDom(DOMDocument $dom) |
| 74 | { |
| 75 | // If asset files list is empty for current identifier skip |
| 76 | if (empty($this->portableAssetsToExport)) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $xpath = new DOMXPath($dom); |
| 81 | |
| 82 | // Get all portable element from qti.xml |
| 83 | $portableElementNodes = $dom->getElementsByTagName($this->getNodeName()); |
| 84 | |
| 85 | /** @var PortableElementObject $portableElement */ |
| 86 | $portableElement = $this->object; |
| 87 | |
| 88 | for ($i = 0; $i < $portableElementNodes->length; $i++) { |
| 89 | /** @var \DOMElement $currentPortableNode */ |
| 90 | $currentPortableNode = $portableElementNodes->item($i); |
| 91 | |
| 92 | //get the local namespace prefix to be used in new node creation |
| 93 | $localNs = $currentPortableNode->hasAttribute('xmlns') ? '' : $this->getXmlnsName() . ':'; |
| 94 | |
| 95 | //get the portable element type identifier |
| 96 | $identifier = $currentPortableNode->getAttribute($this->getTypeIdentifierAttributeName()); |
| 97 | |
| 98 | if ($identifier != $portableElement->getTypeIdentifier()) { |
| 99 | continue; |
| 100 | } |
| 101 | |
| 102 | $basePath = $this->qtiItemExporter->buildBasePath(); |
| 103 | $itemRelPath = $this->getItemRelativePath($basePath); |
| 104 | |
| 105 | //set version |
| 106 | $currentPortableNode->setAttribute('data-version', $portableElement->getVersion()); |
| 107 | |
| 108 | /** @var \DOMElement $resourcesNode */ |
| 109 | $modulesNode = $currentPortableNode->getElementsByTagName('modules')->item(0); |
| 110 | |
| 111 | if (!empty($modulesNode)) { |
| 112 | $this->removeOldNode($modulesNode, 'module'); |
| 113 | } else { |
| 114 | $modulesNode = $currentPortableNode->appendChild($dom->createElement('modules')); |
| 115 | } |
| 116 | |
| 117 | $runtime = $portableElement->getRuntime(); |
| 118 | if (isset($runtime['config'])) { |
| 119 | $configs = $runtime['config']; |
| 120 | |
| 121 | if (isset($configs[0])) { |
| 122 | $file = $configs[0]['file']; |
| 123 | $finalRelPath = $this->getImsPciExportPath($itemRelPath, $file); |
| 124 | //make this path relative to the item ! |
| 125 | $modulesNode->setAttribute('primaryConfiguration', $finalRelPath); |
| 126 | |
| 127 | if (isset($configs[0]['data']) && isset($configs[0]['data']['paths'])) { |
| 128 | foreach ($configs[0]['data']['paths'] as $id => $paths) { |
| 129 | if (is_string($paths)) { |
| 130 | $adaptedPath = $this->getRawExportPath($paths); |
| 131 | $paths = $this->getRelPath($file, $adaptedPath); |
| 132 | } elseif (is_array($paths)) { |
| 133 | for ($i = 0; $i < count($paths); $i++) { |
| 134 | $paths[$i] = $this->getRawExportPath($paths[$i]); |
| 135 | } |
| 136 | } |
| 137 | $configs[0]['data']['paths'][$id] = $paths; |
| 138 | } |
| 139 | |
| 140 | $this->replaceFile( |
| 141 | json_encode($configs[0]['data'], JSON_UNESCAPED_SLASHES), |
| 142 | $this->getRawExportPath($file) |
| 143 | ); |
| 144 | } |
| 145 | } |
| 146 | if (isset($configs[1])) { |
| 147 | $file = $configs[1]['file']; |
| 148 | $modulesNode->setAttribute( |
| 149 | 'fallbackConfiguration', |
| 150 | $this->getImsPciExportPath($itemRelPath, $file) |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | foreach ($portableElement->getRuntimeKey('modules') as $id => $modules) { |
| 156 | $moduleNode = $dom->createElement($localNs . 'module'); |
| 157 | $moduleNode->setAttribute('id', $id); |
| 158 | if (isset($modules[0])) { |
| 159 | $file = $modules[0]; |
| 160 | $moduleNode->setAttribute('primaryPath', $this->getImsPciExportPath($itemRelPath, $file)); |
| 161 | } |
| 162 | if (isset($modules[1])) { |
| 163 | $file = $modules[1]; |
| 164 | $moduleNode->setAttribute('fallbackPath', $this->getImsPciExportPath($itemRelPath, $file)); |
| 165 | } |
| 166 | $modulesNode->appendChild($moduleNode); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | unset($xpath); |
| 171 | } |
| 172 | |
| 173 | private function getImsPciExportPath($itemRelPath, $file) |
| 174 | { |
| 175 | return $itemRelPath . $this->portableAssetsToExport[$file]; |
| 176 | } |
| 177 | |
| 178 | private function replaceFile($dataString, $fileToReplace) |
| 179 | { |
| 180 | $stream = fopen('php://memory', 'r+'); |
| 181 | fwrite($stream, $dataString); |
| 182 | rewind($stream); |
| 183 | $this->qtiItemExporter->addFile($stream, $fileToReplace); |
| 184 | fclose($stream); |
| 185 | } |
| 186 | |
| 187 | private function getItemRelativePath(string $itemBasePath): string |
| 188 | { |
| 189 | $returnValue = ''; |
| 190 | $arrDir = explode(self::EXPORTER_PATH_SEPARATOR, rtrim($itemBasePath, self::EXPORTER_PATH_SEPARATOR)); |
| 191 | for ($i = 0, $iMax = count($arrDir); $i < $iMax; $i++) { |
| 192 | $returnValue .= '..' . self::EXPORTER_PATH_SEPARATOR; |
| 193 | } |
| 194 | return $returnValue; |
| 195 | } |
| 196 | } |