Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| PortableElementTrait | |
0.00% |
0 / 58 |
|
0.00% |
0 / 9 |
812 | |
0.00% |
0 / 1 |
| getConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setConfig | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| addModule | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| setModules | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getModules | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| serializePortableProperties | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
110 | |||
| extractProperties | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
56 | |||
| setNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNamespace | |
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) 2016 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\qti; |
| 23 | |
| 24 | use DOMElement; |
| 25 | |
| 26 | /** |
| 27 | * Trait EventManagerAwareTrait |
| 28 | * @package taoQtiItem |
| 29 | */ |
| 30 | trait PortableElementTrait |
| 31 | { |
| 32 | protected $config = []; |
| 33 | protected $modules = []; |
| 34 | |
| 35 | /** |
| 36 | * @var QtiNamespace |
| 37 | */ |
| 38 | protected $ns = null; |
| 39 | |
| 40 | public function getConfig() |
| 41 | { |
| 42 | return $this->config; |
| 43 | } |
| 44 | |
| 45 | public function setConfig($configFiles) |
| 46 | { |
| 47 | if (is_array($configFiles)) { |
| 48 | $this->config = $configFiles; |
| 49 | } else { |
| 50 | throw new InvalidArgumentException('config files should be an array'); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function addModule($id, $paths) |
| 55 | { |
| 56 | if (is_string($paths)) { |
| 57 | $paths = [$paths]; |
| 58 | } |
| 59 | if (is_array($paths)) { |
| 60 | $this->modules[$id] = $paths; |
| 61 | } else { |
| 62 | throw new InvalidArgumentException('modue paths should be an array'); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function setModules($paths) |
| 67 | { |
| 68 | if (is_array($paths)) { |
| 69 | $this->modules = $paths; |
| 70 | } else { |
| 71 | throw new InvalidArgumentException('modue paths should be an array'); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public function getModules() |
| 76 | { |
| 77 | return $this->modules; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Serialize an associative array of pci properties into a pci xml |
| 82 | * |
| 83 | * @param array $properties |
| 84 | * @param string $ns |
| 85 | * @return string |
| 86 | */ |
| 87 | private function serializePortableProperties($properties, $ns = '', $nsUri = '', $name = null, $element = null) |
| 88 | { |
| 89 | $document = null; |
| 90 | $result = ''; |
| 91 | |
| 92 | if ($element === null) { |
| 93 | $document = new \DomDocument(); |
| 94 | $element = $ns ? |
| 95 | $document->createElementNS($nsUri, $ns . ':properties') : |
| 96 | $document->createElement('properties'); |
| 97 | |
| 98 | $document->appendChild($element); |
| 99 | } else { |
| 100 | $newElement = $ns ? |
| 101 | $element->ownerDocument->createElementNS($nsUri, $ns . ':properties') : |
| 102 | $element->ownerDocument->createElement('properties'); |
| 103 | |
| 104 | $element->appendChild($newElement); |
| 105 | $element = $newElement; |
| 106 | } |
| 107 | |
| 108 | if ($name !== null) { |
| 109 | $element->setAttribute('key', $name); |
| 110 | } |
| 111 | |
| 112 | foreach ($properties as $name => $value) { |
| 113 | if (is_array($value)) { |
| 114 | $this->serializePortableProperties($value, $ns, $nsUri, $name, $element); |
| 115 | } else { |
| 116 | $entryElement = $ns ? |
| 117 | $element->ownerDocument->createElementNS($nsUri, $ns . ':property') : |
| 118 | $element->ownerDocument->createElement('property'); |
| 119 | |
| 120 | $entryElement->setAttribute('key', $name); |
| 121 | $entryElement->appendChild(new \DOMText($value)); |
| 122 | $element->appendChild($entryElement); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if ($document !== null) { |
| 127 | foreach ($document->childNodes as $node) { |
| 128 | $result .= $document->saveXML($node); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return $result; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Parse a pci properties dom node into an associative array |
| 137 | * |
| 138 | * @param DOMElement $propertiesNode |
| 139 | * @param string $ns |
| 140 | * @return array |
| 141 | */ |
| 142 | private function extractProperties(DOMElement $propertiesNode, $ns = '') |
| 143 | { |
| 144 | |
| 145 | $properties = []; |
| 146 | $ns = $ns ? trim($ns, ':') . ':' : ''; |
| 147 | |
| 148 | foreach ($propertiesNode->childNodes as $prop) { |
| 149 | if ($prop instanceof DOMElement) { |
| 150 | switch ($prop->tagName) { |
| 151 | case $ns . 'entry'://OAT PCI uses entry as property node |
| 152 | case $ns . 'property'://IMS PCI uses entry as property node |
| 153 | $key = $prop->getAttribute('key'); |
| 154 | $properties[$key] = $prop->nodeValue; |
| 155 | break; |
| 156 | case $ns . 'properties': |
| 157 | $key = $prop->getAttribute('key'); |
| 158 | $properties[$key] = $this->extractProperties($prop, $ns); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return $properties; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Set the namespace used by this custom interaction |
| 169 | * @param QtiNamespace $xmlns |
| 170 | */ |
| 171 | public function setNamespace(QtiNamespace $xmlns) |
| 172 | { |
| 173 | $this->ns = $xmlns; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get the namespace used by this custom interaction |
| 178 | * @return QtiNamespace |
| 179 | */ |
| 180 | public function getNamespace() |
| 181 | { |
| 182 | return $this->ns; |
| 183 | } |
| 184 | } |