Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| taoQtiTest_models_classes_ManifestParser | |
0.00% |
0 / 59 |
|
0.00% |
0 / 4 |
650 | |
0.00% |
0 / 1 |
| getResources | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| getAllResources | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getResourcesFromManifest | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
132 | |||
| getSimpleXMLElement | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | use oat\taoQtiItem\model\qti\ManifestParser; |
| 24 | |
| 25 | /** |
| 26 | * Enables you to parse and validate a QTI Package. |
| 27 | * The Package is formated as a zip archive containing the manifest and the |
| 28 | * (item files and media files) |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
| 32 | * @author Joel Bout <joel@taotesting.com> |
| 33 | * @package taoQTITest |
| 34 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_intgv2p1.html#section10005 IMS QTI: Packaging Tests |
| 35 | |
| 36 | */ |
| 37 | class taoQtiTest_models_classes_ManifestParser extends ManifestParser |
| 38 | { |
| 39 | private $resources = null; |
| 40 | |
| 41 | /** |
| 42 | * Flag to be used while getting resources |
| 43 | * by type. |
| 44 | * |
| 45 | * @var integer |
| 46 | */ |
| 47 | public const FILTER_RESOURCE_TYPE = 0; |
| 48 | |
| 49 | /** |
| 50 | * Flag to be used while getting resources |
| 51 | * by identifier. |
| 52 | * |
| 53 | * @var integer |
| 54 | */ |
| 55 | public const FILTER_RESOURCE_IDENTIFIER = 1; |
| 56 | |
| 57 | /** |
| 58 | * Get the resources contained within the manifest. |
| 59 | * |
| 60 | * @param string|array $filter The resource types you want to obtain. An empty $filter will make the method return |
| 61 | * all the resources within the manifest. |
| 62 | * @param integer $target The critera to be used for filtering. ManifestParser::FILTER_RESOURCE_TYPE allows |
| 63 | * to filter by resource type, ManifestParser::FILTER_RESOURCE_IDENTIFIER allows to filter |
| 64 | * by resource identifier. |
| 65 | * @return array An array of oat\taoQtiItem\model\qti\Resource objects matching $filter (if given). |
| 66 | */ |
| 67 | public function getResources($filter = null, $target = self::FILTER_RESOURCE_TYPE) |
| 68 | { |
| 69 | $returnValue = []; |
| 70 | |
| 71 | if (is_null($filter)) { |
| 72 | $returnValue = $this->getAllResources(); |
| 73 | } else { |
| 74 | $filter = is_array($filter) ? $filter : [$filter]; |
| 75 | |
| 76 | foreach ($this->getAllResources() as $resource) { |
| 77 | $stringTarget = ($target === self::FILTER_RESOURCE_TYPE) |
| 78 | ? $resource->getType() |
| 79 | : $resource->getIdentifier(); |
| 80 | |
| 81 | if (in_array($stringTarget, $filter)) { |
| 82 | $returnValue[] = $resource; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return $returnValue; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get all the resources contained within the manifest. |
| 91 | * |
| 92 | * @return An array of oat\taoQtiItem\model\qti\Resource objects. |
| 93 | */ |
| 94 | protected function getAllResources() |
| 95 | { |
| 96 | if ($this->resources == null) { |
| 97 | $this->resources = $this->getResourcesFromManifest($this->getSimpleXMLElement()); |
| 98 | } |
| 99 | return $this->resources; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get all the resources contained by the $source SimpleXMLElement. |
| 104 | * |
| 105 | * @param SimpleXMLElement $source The SimpleXMLElement object you want to extract resources from. |
| 106 | * @throws common_exception_Error If $source does not correspond to a <manifest> element. |
| 107 | * @return array An array of oat\taoQtiItem\model\qti\Resource objects. |
| 108 | */ |
| 109 | private function getResourcesFromManifest(SimpleXMLElement $source) |
| 110 | { |
| 111 | $returnValue = []; |
| 112 | |
| 113 | //check of the root tag |
| 114 | if ($source->getName() != 'manifest') { |
| 115 | throw new common_exception_Error("Incorrect manifest root tag '" . $source->getName() . "'."); |
| 116 | } |
| 117 | |
| 118 | $resourceNodes = $source->xpath("//*[name(.)='resource']"); |
| 119 | |
| 120 | foreach ($resourceNodes as $resourceNode) { |
| 121 | $type = (string) $resourceNode['type']; |
| 122 | $id = (string) $resourceNode['identifier']; |
| 123 | $href = (isset($resourceNode['href'])) ? (string) $resourceNode['href'] : ''; |
| 124 | |
| 125 | $idRefs = []; |
| 126 | $auxFiles = []; |
| 127 | $xmlFiles = []; |
| 128 | |
| 129 | // Retrieve Auxilliary files. |
| 130 | foreach ($resourceNode->file as $fileNode) { |
| 131 | $fileHref = (string) $fileNode['href']; |
| 132 | |
| 133 | if (preg_match("/\.xml$|\.css$/", $fileHref)) { |
| 134 | if (empty($href) || $href === $fileHref) { |
| 135 | $xmlFiles[] = $fileHref; |
| 136 | } else { |
| 137 | $auxFiles[] = $fileHref; |
| 138 | } |
| 139 | } else { |
| 140 | $auxFiles[] = $fileHref; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (count($xmlFiles) == 1 && empty($href)) { |
| 145 | $href = $xmlFiles[0]; |
| 146 | } |
| 147 | |
| 148 | // Retrieve Dependencies. |
| 149 | foreach ($resourceNode->dependency as $dependencyNode) { |
| 150 | $idRefs[] = (string) $dependencyNode['identifierref']; |
| 151 | } |
| 152 | |
| 153 | $resource = new taoQtiTest_models_classes_QtiResource($id, $type, $href); |
| 154 | $resource->setAuxiliaryFiles($auxFiles); |
| 155 | $resource->setDependencies($idRefs); |
| 156 | |
| 157 | $returnValue[] = $resource; |
| 158 | } |
| 159 | |
| 160 | return (array) $returnValue; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the root SimpleXMLElement object of the currently parsed manifest. |
| 165 | * |
| 166 | * @throws common_exception_Error |
| 167 | * @return SimpleXMLElement |
| 168 | */ |
| 169 | private function getSimpleXMLElement() |
| 170 | { |
| 171 | switch ($this->sourceType) { |
| 172 | case self::SOURCE_FILE: |
| 173 | $xml = simplexml_load_file($this->source); |
| 174 | break; |
| 175 | |
| 176 | case self::SOURCE_URL: |
| 177 | $xmlContent = tao_helpers_Request::load($this->source, true); |
| 178 | $xml = simplexml_load_string($xmlContent); |
| 179 | break; |
| 180 | |
| 181 | case self::SOURCE_STRING: |
| 182 | $xml = simplexml_load_string($this->source); |
| 183 | break; |
| 184 | |
| 185 | default: |
| 186 | throw new taoItems_models_classes_Import_ImportException('Invalid sourceType'); |
| 187 | } |
| 188 | |
| 189 | if ($xml === false) { |
| 190 | $this->addErrors(libxml_get_errors()); |
| 191 | libxml_clear_errors(); |
| 192 | throw new common_exception_Error('Invalid XML.'); |
| 193 | } |
| 194 | |
| 195 | return $xml; |
| 196 | } |
| 197 | } |