Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
53.33% |
32 / 60 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
TransformationService | |
53.33% |
32 / 60 |
|
50.00% |
3 / 6 |
128.67 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
transformChildren | |
78.95% |
15 / 19 |
|
0.00% |
0 / 1 |
12.13 | |||
transformAttributes | |
40.74% |
11 / 27 |
|
0.00% |
0 / 1 |
54.79 | |||
createQtiElementName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addClassFromAttribute | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
camelToHyphen | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 |
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) 2025 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoQtiItem\model\Export\Qti3Package; |
24 | |
25 | use DOMAttr; |
26 | use DOMDocument; |
27 | use DOMElement; |
28 | use DOMText; |
29 | |
30 | class TransformationService |
31 | { |
32 | private Qti3XsdValidator $validator; |
33 | |
34 | public function __construct(Qti3XsdValidator $validator) |
35 | { |
36 | $this->validator = $validator; |
37 | } |
38 | |
39 | public function transformChildren(DOMElement $oldElement, DOMElement $newParent, DOMDocument $newDom): void |
40 | { |
41 | foreach ($oldElement->childNodes as $child) { |
42 | if ($child instanceof DOMElement) { |
43 | $newName = $this->createQtiElementName($child->nodeName); |
44 | |
45 | if (!$this->validator->isQtiElementName($newName)) { |
46 | $newName = $child->nodeName; |
47 | } |
48 | |
49 | if ($this->validator->isMathElementName($child->nodeName)) { |
50 | $newName = substr($newName, 2); |
51 | } |
52 | |
53 | $newElement = $newDom->createElement($newName); |
54 | |
55 | $this->transformAttributes($child, $newElement); |
56 | |
57 | if ($newName === 'math') { |
58 | $newElement->setAttribute('xmlns', 'http://www.w3.org/1998/Math/MathML'); |
59 | } |
60 | |
61 | if ($child->childNodes->length === 1 && $child->firstChild->nodeType === XML_TEXT_NODE) { |
62 | $newElement->textContent = $child->textContent; |
63 | } else { |
64 | $this->transformChildren($child, $newElement, $newDom); |
65 | } |
66 | |
67 | $newParent->appendChild($newElement); |
68 | } elseif ($child->nodeType === XML_TEXT_NODE && trim($child->nodeValue) !== '') { |
69 | $newParent->appendChild($newDom->createTextNode($child->nodeValue)); |
70 | } elseif ($child->nodeType === XML_CDATA_SECTION_NODE) { |
71 | $newParent->appendChild($newDom->createCDATASection($child->nodeValue)); |
72 | } |
73 | } |
74 | } |
75 | |
76 | public function transformAttributes(DOMElement $sourceElement, DOMElement $targetElement): void |
77 | { |
78 | if (!$sourceElement->hasAttributes()) { |
79 | return; |
80 | } |
81 | |
82 | foreach ($sourceElement->attributes as $attribute) { |
83 | if ( |
84 | !str_starts_with($attribute->nodeName, 'xmlns') |
85 | && $attribute->nodeName !== 'xsi:schemaLocation' |
86 | ) { |
87 | $attrName = $this->camelToHyphen($attribute->nodeName); |
88 | if (!empty($attrName)) { |
89 | if ($sourceElement->nodeName === 'extendedTextInteraction') { |
90 | $this->addClassFromAttribute($attribute, 'expectedLines', 'qti-height-lines-'); |
91 | if ($attribute->nodeName === 'expectedLines') { |
92 | continue; |
93 | } |
94 | } elseif ($sourceElement->nodeName === 'textEntryInteraction') { |
95 | $this->addClassFromAttribute($attribute, 'expectedLength', 'qti-input-width-'); |
96 | if ($attribute->nodeName === 'expectedLength') { |
97 | continue; |
98 | } |
99 | } elseif ($sourceElement->nodeName === 'choiceInteraction' && $attribute->nodeName === 'class') { |
100 | if (strpos($attribute->value, 'list-style-') === 0) { |
101 | $listStyleValue = str_replace('list-style-', '', $attribute->value); |
102 | $finalClass = ''; |
103 | |
104 | if (preg_match('/^(.*)-(parenthesis|period)$/', $listStyleValue, $matches)) { |
105 | $baseStyle = $matches[1]; |
106 | $suffixStyle = $matches[2]; |
107 | |
108 | $finalClass = 'qti-labels-' . $baseStyle . ' ' . 'qti-labels-suffix-' . $suffixStyle; |
109 | } else { |
110 | $finalClass = 'qti-labels-' . $listStyleValue; |
111 | } |
112 | |
113 | $targetElement->setAttribute($attrName, $finalClass); |
114 | continue; |
115 | } |
116 | } |
117 | |
118 | $targetElement->setAttribute($attrName, $attribute->value); |
119 | } |
120 | } |
121 | } |
122 | } |
123 | |
124 | public function createQtiElementName(string $nodeName): string |
125 | { |
126 | return sprintf('qti-%s', $this->camelToHyphen($nodeName)); |
127 | } |
128 | |
129 | private function addClassFromAttribute(DOMAttr $node, string $expectedName, string $classPrefix): void |
130 | { |
131 | if ($node->nodeName !== $expectedName) { |
132 | return; |
133 | } |
134 | |
135 | $parent = $node->parentNode; |
136 | $classAttr = $parent->attributes->getNamedItem('class'); |
137 | $classValue = $classPrefix . $node->nodeValue; |
138 | |
139 | if ($classAttr === null) { |
140 | $parent->setAttribute('class', $classValue); |
141 | } else { |
142 | $classAttr->nodeValue .= ' ' . $classValue; |
143 | } |
144 | } |
145 | |
146 | private function camelToHyphen(string $string): string |
147 | { |
148 | $string = preg_replace('/([a-z])([A-Z])/', '$1-$2', $string); |
149 | $string = str_replace('_', '-', $string); |
150 | $string = strtolower($string); |
151 | return ltrim($string, '-'); |
152 | } |
153 | } |