Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManifestParser
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 validate
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
56
 load
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
56
 getServiceManager
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
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-2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 *
21 */
22
23namespace oat\taoQtiItem\model\qti;
24
25use oat\oatbox\service\ServiceManager;
26use oat\taoQtiItem\model\ValidationService;
27use tao_models_classes_Parser;
28use tao_helpers_Request;
29use oat\oatbox\log\LoggerAwareTrait;
30
31/**
32 * Enables you to parse and validate an imsmanifest.xml file.
33 * You can load a list QTI_Resources from the parsed file.
34 *
35 * @access public
36 * @author Jerome Bogaerts <jerome@taotesting.com>
37 * @author Joel Bout <joel@taotesting.com>
38 * @author Somsack Sipasseuth <sam@taotesting.com>
39 * @package taoQTI
40 * @see http://www.imsglobal.org/question/qti_v2p0/imsqti_intgv2p0.html#section10003
41
42 */
43class ManifestParser extends tao_models_classes_Parser
44{
45    use LoggerAwareTrait;
46
47    /**
48     * Validate the manifest against an XML Schema Definition.
49     *
50     * @access public
51     * @param  string schema
52     * @return boolean
53     */
54    public function validate($schema = '')
55    {
56        if (empty($schema)) {
57            // Let's detect NS in use...
58            $dom = new \DOMDocument('1.0', 'UTF-8');
59
60            switch ($this->sourceType) {
61                case self::SOURCE_FILE:
62                    $dom->load($this->source);
63                    break;
64                case self::SOURCE_URL:
65                    $xmlContent = tao_helpers_Request::load($this->source, true);
66                    $dom->loadXML($xmlContent);
67                    break;
68                case self::SOURCE_STRING:
69                    $dom->loadXML($this->source);
70                    break;
71            }
72
73            // Retrieve Root's namespace.
74            if ($dom->documentElement == null) {
75                $this->addError('dom is null and could not be validate');
76                $returnValue = false;
77            } else {
78                $ns = $dom->documentElement->lookupNamespaceUri(null);
79                $servicemanager = $this->getServiceManager();
80                $validationService = $servicemanager->get(ValidationService::SERVICE_ID);
81                $schemas = $validationService->getManifestValidationSchema($ns);
82
83                $validSchema = $this->validateMultiple($schemas);
84                $returnValue = $validSchema !== '';
85            }
86        } elseif (!file_exists($schema)) {
87            throw new \common_Exception('no schema found in the location ' . $schema);
88        } else {
89            $this->logDebug("The following schema will be used to validate imsmanifest.xml: '" . $schema . "'.");
90            $returnValue = parent::validate($schema);
91        }
92
93        return (bool) $returnValue;
94    }
95
96    /**
97     * Extract the resources informations about the items
98     * and build a list a QTI_Resource
99     *
100     * @access public
101     * @return array
102     */
103    public function load()
104    {
105        $returnValue = [];
106
107        //load it using the SimpleXml library
108        $xml = false;
109        switch ($this->sourceType) {
110            case self::SOURCE_FILE:
111                $xml = simplexml_load_file($this->source);
112                break;
113
114            case self::SOURCE_URL:
115                $xmlContent = tao_helpers_Request::load($this->source, true);
116                $xml = simplexml_load_string($xmlContent);
117                break;
118
119            case self::SOURCE_STRING:
120                $xml = simplexml_load_string($this->source);
121                break;
122        }
123
124        if ($xml !== false) {
125            //get the QTI Item's resources from the imsmanifest.xml
126            $returnValue = ManifestParserFactory::getResourcesFromManifest($xml);
127
128            if (!$this->valid) {
129                $this->valid = true;
130                libxml_clear_errors();
131            }
132        } elseif (!$this->valid) {
133            $this->addErrors(libxml_get_errors());
134            libxml_clear_errors();
135        }
136
137        return (array) $returnValue;
138    }
139
140    protected function getServiceManager()
141    {
142        return ServiceManager::getServiceManager();
143    }
144}