Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 79 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| OatPicExporter | |
0.00% |
0 / 79 |
|
0.00% |
0 / 6 |
462 | |
0.00% |
0 / 1 |
| copyAssetFiles | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| 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 / 57 |
|
0.00% |
0 / 1 |
210 | |||
| getOatPciExportPath | |
0.00% |
0 / 2 |
|
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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\qtiItemPic\model\export; |
| 22 | |
| 23 | use common_Logger; |
| 24 | use DOMDocument; |
| 25 | use DOMElement; |
| 26 | use DOMXPath; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\taoQtiItem\model\portableElement\element\PortableElementObject; |
| 29 | use oat\taoQtiItem\model\portableElement\export\PortableElementExporter; |
| 30 | use oat\taoQtiItem\model\portableElement\PortableElementService; |
| 31 | use tao_helpers_File; |
| 32 | |
| 33 | class OatPicExporter extends PortableElementExporter |
| 34 | { |
| 35 | /** |
| 36 | * Copy the asset files of the PIC to the item exporter and return the list of copied assets |
| 37 | * |
| 38 | * @param $replacementList |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function copyAssetFiles(&$replacementList) |
| 43 | { |
| 44 | $object = $this->object; |
| 45 | $portableAssetsToExport = []; |
| 46 | $service = new PortableElementService(); |
| 47 | $service->setServiceLocator(ServiceManager::getServiceManager()); |
| 48 | $files = $object->getModel()->getValidator()->getAssets($object, 'runtime'); |
| 49 | $baseUrl = $this->qtiItemExporter->buildBasePath() . DIRECTORY_SEPARATOR . $object->getTypeIdentifier(); |
| 50 | |
| 51 | foreach ($files as $url) { |
| 52 | // Skip shared libraries into portable element |
| 53 | if (strpos($url, './') !== 0) { |
| 54 | common_Logger::i('Shared libraries skipped : ' . $url); |
| 55 | $portableAssetsToExport[$url] = $url; |
| 56 | |
| 57 | continue; |
| 58 | } |
| 59 | $stream = $service->getFileStream($object, $url); |
| 60 | $replacement = $this->qtiItemExporter->copyAssetFile($stream, $baseUrl, $url, $replacementList); |
| 61 | $portableAssetToExport = preg_replace('/^(.\/)(.*)/', $object->getTypeIdentifier() . '/$2', $replacement); |
| 62 | $portableAssetsToExport[$url] = $portableAssetToExport; |
| 63 | common_Logger::i('File copied: "' . $url . '" for portable element ' . $object->getTypeIdentifier()); |
| 64 | } |
| 65 | |
| 66 | return $this->portableAssetsToExport = $portableAssetsToExport; |
| 67 | } |
| 68 | |
| 69 | public function getNodeName() |
| 70 | { |
| 71 | return 'portableInfoControl'; |
| 72 | } |
| 73 | |
| 74 | public function getTypeIdentifierAttributeName() |
| 75 | { |
| 76 | return 'infoControlTypeIdentifier'; |
| 77 | } |
| 78 | |
| 79 | public function getXmlnsName() |
| 80 | { |
| 81 | return 'pic'; |
| 82 | } |
| 83 | |
| 84 | public function exportDom(DOMDocument $dom) |
| 85 | { |
| 86 | // If asset files list is empty for current identifier skip |
| 87 | if (empty($this->portableAssetsToExport)) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $xpath = new DOMXPath($dom); |
| 92 | |
| 93 | // Get all portable element from qti.xml |
| 94 | $portableElementNodes = $dom->getElementsByTagName($this->getNodeName()); |
| 95 | |
| 96 | /** @var PortableElementObject $portableElement */ |
| 97 | $portableElement = $this->object; |
| 98 | |
| 99 | for ($i = 0; $i < $portableElementNodes->length; $i++) { |
| 100 | /** @var DOMElement $currentPortableNode */ |
| 101 | $currentPortableNode = $portableElementNodes->item($i); |
| 102 | |
| 103 | //get the local namespace prefix to be used in new node creation |
| 104 | $localNs = $currentPortableNode->hasAttribute('xmlns') ? '' : $this->getXmlnsName() . ':'; |
| 105 | |
| 106 | //get the portable element type identifier |
| 107 | $identifier = $currentPortableNode->getAttribute($this->getTypeIdentifierAttributeName()); |
| 108 | |
| 109 | if ($identifier != $portableElement->getTypeIdentifier()) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | // Add hook and version as attributes |
| 114 | if ($portableElement->hasRuntimeKey('hook')) { |
| 115 | $currentPortableNode->setAttribute( |
| 116 | 'hook', |
| 117 | preg_replace( |
| 118 | '/^(.\/)(.*)/', |
| 119 | $portableElement->getTypeIdentifier() . '/$2', |
| 120 | $portableElement->getRuntimeKey('hook') |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | //set version |
| 126 | $currentPortableNode->setAttribute('version', $portableElement->getVersion()); |
| 127 | |
| 128 | // If asset files list is empty for current identifier skip |
| 129 | if ( |
| 130 | !isset($portableAssetsToExport) |
| 131 | || !isset($portableAssetsToExport[$portableElement->getTypeIdentifier()]) |
| 132 | ) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | /** @var DOMElement $resourcesNode */ |
| 137 | $resourcesNode = $currentPortableNode->getElementsByTagName('resources')->item(0); |
| 138 | |
| 139 | $this->removeOldNode($resourcesNode, 'libraries'); |
| 140 | $this->removeOldNode($resourcesNode, 'stylesheets'); |
| 141 | $this->removeOldNode($resourcesNode, 'mediaFiles'); |
| 142 | |
| 143 | // Portable libraries |
| 144 | $librariesNode = $dom->createElement($localNs . 'libraries'); |
| 145 | |
| 146 | foreach ($portableElement->getRuntimeKey('libraries') as $library) { |
| 147 | $libraryNode = $dom->createElement($localNs . 'lib'); |
| 148 | //the exported lib id must be adapted from a href mode to an amd name mode |
| 149 | $libraryNode->setAttribute('id', preg_replace('/\.js$/', '', $library)); |
| 150 | $librariesNode->appendChild($libraryNode); |
| 151 | } |
| 152 | |
| 153 | if ($librariesNode->hasChildNodes()) { |
| 154 | $resourcesNode->appendChild($librariesNode); |
| 155 | } |
| 156 | |
| 157 | // Portable stylesheets |
| 158 | $stylesheetsNode = $dom->createElement($localNs . 'stylesheets'); |
| 159 | |
| 160 | foreach ($portableElement->getRuntimeKey('stylesheets') as $stylesheet) { |
| 161 | $stylesheetNode = $dom->createElement($localNs . 'link'); |
| 162 | $stylesheetNode->setAttribute('href', $this->getOatPciExportPath($stylesheet)); |
| 163 | $stylesheetNode->setAttribute('type', 'text/css'); |
| 164 | |
| 165 | $info = pathinfo($stylesheet); |
| 166 | $stylesheetNode->setAttribute('title', basename($stylesheet, '.' . $info['extension'])); |
| 167 | $stylesheetsNode->appendChild($stylesheetNode); |
| 168 | } |
| 169 | |
| 170 | if ($stylesheetsNode->hasChildNodes()) { |
| 171 | $resourcesNode->appendChild($stylesheetsNode); |
| 172 | } |
| 173 | |
| 174 | // Portable mediaFiles |
| 175 | $mediaFilesNode = $dom->createElement($localNs . 'mediaFiles'); |
| 176 | |
| 177 | foreach ($portableElement->getRuntimeKey('mediaFiles') as $mediaFile) { |
| 178 | $mediaFileNode = $dom->createElement($localNs . 'file'); |
| 179 | $mediaFileNode->setAttribute('src', $this->getOatPciExportPath($mediaFile)); |
| 180 | $mediaFileNode->setAttribute( |
| 181 | 'type', |
| 182 | tao_helpers_File::getMimeType($this->getOatPciExportPath($mediaFile)) |
| 183 | ); |
| 184 | $mediaFilesNode->appendChild($mediaFileNode); |
| 185 | } |
| 186 | |
| 187 | if ($mediaFilesNode->hasChildNodes()) { |
| 188 | $resourcesNode->appendChild($mediaFilesNode); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | unset($xpath); |
| 193 | } |
| 194 | |
| 195 | private function getOatPciExportPath($file) |
| 196 | { |
| 197 | $key = preg_replace('/^' . $this->object->getTypeIdentifier() . '\//', './', $file); |
| 198 | |
| 199 | return $this->portableAssetsToExport[$key]; |
| 200 | } |
| 201 | } |