Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Parser | |
0.00% |
0 / 72 |
|
0.00% |
0 / 4 |
650 | |
0.00% |
0 / 1 |
| validate | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
90 | |||
| load | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
182 | |||
| addError | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getServiceManager | |
0.00% |
0 / 1 |
|
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoQtiItem\model\qti; |
| 24 | |
| 25 | use oat\oatbox\service\ServiceManager; |
| 26 | use oat\taoQtiItem\model\qti\ParserFactory; |
| 27 | use oat\taoQtiItem\model\qti\exception\UnsupportedQtiElement; |
| 28 | use oat\taoQtiItem\model\ValidationService; |
| 29 | use tao_models_classes_Parser; |
| 30 | use DOMDocument; |
| 31 | use tao_helpers_Request; |
| 32 | use oat\oatbox\log\LoggerAwareTrait; |
| 33 | |
| 34 | /** |
| 35 | * The QTI Parser enables you to parse QTI item xml files and build the |
| 36 | * objects |
| 37 | * |
| 38 | * @access public |
| 39 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 40 | * @package taoQTI |
| 41 | * @see http://www.imsglobal.org/question/qti_v2p0/imsqti_infov2p0.html#element10010 |
| 42 | |
| 43 | */ |
| 44 | class Parser extends tao_models_classes_Parser |
| 45 | { |
| 46 | use LoggerAwareTrait; |
| 47 | |
| 48 | /** |
| 49 | * Run the validation process |
| 50 | * |
| 51 | * @access public |
| 52 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 53 | * |
| 54 | * @param string $schema |
| 55 | * |
| 56 | * @return bool |
| 57 | * @throws \Exception |
| 58 | * @throws \common_Exception |
| 59 | */ |
| 60 | public function validate($schema = '') |
| 61 | { |
| 62 | |
| 63 | if (empty($schema)) { |
| 64 | // Let's detect NS in use... |
| 65 | $dom = new DOMDocument('1.0', 'UTF-8'); |
| 66 | |
| 67 | switch ($this->sourceType) { |
| 68 | case self::SOURCE_FLYFILE: |
| 69 | if ($this->source->exists()) { |
| 70 | $dom->load($this->source->read()); |
| 71 | } |
| 72 | break; |
| 73 | case self::SOURCE_FILE: |
| 74 | $dom->load($this->source); |
| 75 | break; |
| 76 | case self::SOURCE_URL: |
| 77 | $xmlContent = tao_helpers_Request::load($this->source, true); |
| 78 | $dom->loadXML($xmlContent); |
| 79 | break; |
| 80 | case self::SOURCE_STRING: |
| 81 | $dom->loadXML($this->source); |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | // Retrieve Root's namespace. |
| 86 | if ($dom->documentElement == null) { |
| 87 | $this->addError('dom is null and could not be validate'); |
| 88 | $returnValue = false; |
| 89 | } else { |
| 90 | $ns = $dom->documentElement->lookupNamespaceUri(null); |
| 91 | $servicemanager = $this->getServiceManager(); |
| 92 | $validationService = $servicemanager->get(ValidationService::SERVICE_ID); |
| 93 | $schemas = $validationService->getContentValidationSchema($ns); |
| 94 | $this->logDebug("The following schema will be used to validate: '" . $schemas[0] . "'."); |
| 95 | |
| 96 | $validSchema = $this->validateMultiple($schemas); |
| 97 | $returnValue = $validSchema !== ''; |
| 98 | } |
| 99 | } elseif (!file_exists($schema)) { |
| 100 | throw new \common_Exception('no schema found in the location ' . $schema); |
| 101 | } else { |
| 102 | $returnValue = parent::validate($schema); |
| 103 | } |
| 104 | |
| 105 | return (bool) $returnValue; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * load the file content, parse it and build the a QTI_Item instance |
| 110 | * |
| 111 | * @access public |
| 112 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 113 | * @param boolean resolveXInclude |
| 114 | * @return \oat\taoQtiItem\model\qti\Item |
| 115 | */ |
| 116 | public function load($resolveXInclude = false) |
| 117 | { |
| 118 | |
| 119 | $returnValue = null; |
| 120 | |
| 121 | if (!$this->valid) { |
| 122 | libxml_use_internal_errors(true); //retrieve errors if no validation has been done previously |
| 123 | } |
| 124 | |
| 125 | //load it using the DOMDocument library |
| 126 | $xml = new DOMDocument(); |
| 127 | |
| 128 | switch ($this->sourceType) { |
| 129 | case self::SOURCE_FLYFILE: |
| 130 | if ($this->source->exists()) { |
| 131 | $xml->load($this->source->read()); |
| 132 | } |
| 133 | break; |
| 134 | case self::SOURCE_FILE: |
| 135 | $xml->load($this->source); |
| 136 | break; |
| 137 | case self::SOURCE_URL: |
| 138 | $xmlContent = tao_helpers_Request::load($this->source, true); |
| 139 | $xml->loadXML($xmlContent); |
| 140 | break; |
| 141 | case self::SOURCE_STRING: |
| 142 | $xml->loadXML($this->source); |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | if ($xml !== false) { |
| 147 | $basePath = ''; |
| 148 | if ($this->sourceType == self::SOURCE_FILE || $this->sourceType == self::SOURCE_URL) { |
| 149 | $basePath = dirname($this->source) . '/'; |
| 150 | } |
| 151 | //build the item from the xml |
| 152 | $parserFactory = new ParserFactory($xml, $basePath); |
| 153 | try { |
| 154 | $returnValue = $parserFactory->load(); |
| 155 | } catch (UnsupportedQtiElement $e) { |
| 156 | $this->addError($e); |
| 157 | } |
| 158 | |
| 159 | if (!$this->valid) { |
| 160 | $this->valid = true; |
| 161 | libxml_clear_errors(); |
| 162 | } |
| 163 | } elseif (!$this->valid) { |
| 164 | $this->addErrors(libxml_get_errors()); |
| 165 | libxml_clear_errors(); |
| 166 | } |
| 167 | |
| 168 | return $returnValue; |
| 169 | } |
| 170 | |
| 171 | protected function addError($error) |
| 172 | { |
| 173 | |
| 174 | $this->valid = false; |
| 175 | |
| 176 | if ($error instanceof UnsupportedQtiElement) { |
| 177 | $this->errors[] = [ |
| 178 | 'message' => '[Unsupported Qti Type] ' . $error->getUserMessage() |
| 179 | ]; |
| 180 | } else { |
| 181 | parent::addError($error); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | protected function getServiceManager() |
| 186 | { |
| 187 | return ServiceManager::getServiceManager(); |
| 188 | } |
| 189 | } |