Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
34.72% |
50 / 144 |
|
25.00% |
5 / 20 |
CRAP | |
0.00% |
0 / 1 |
| Container | |
34.72% |
50 / 144 |
|
25.00% |
5 / 20 |
1096.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUsedAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setElement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setElements | |
22.22% |
6 / 27 |
|
0.00% |
0 / 1 |
67.93 | |||
| afterElementSet | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| afterElementRemove | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| edit | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
4.59 | |||
| checkIntegrity | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
7.91 | |||
| fixNonvoidTags | |
22.58% |
7 / 31 |
|
0.00% |
0 / 1 |
22.71 | |||
| isValidElement | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getValidElementTypes | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getElement | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getElements | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| removeElement | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| replaceElement | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getIdentifiedElements | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
6.99 | |||
| toQTI | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| toArray | |
80.00% |
12 / 15 |
|
0.00% |
0 / 1 |
3.07 | |||
| isDebugMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 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\container; |
| 24 | |
| 25 | use Monolog\Logger; |
| 26 | use oat\taoQtiItem\model\qti\Element; |
| 27 | use oat\taoQtiItem\model\qti\IdentifiedElementContainer; |
| 28 | use oat\taoQtiItem\model\qti\Item; |
| 29 | use oat\taoQtiItem\model\qti\IdentifiedElement; |
| 30 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
| 31 | use oat\taoQtiItem\model\qti\IdentifierCollection; |
| 32 | use InvalidArgumentException; |
| 33 | use common_Logger; |
| 34 | |
| 35 | /** |
| 36 | * The QTI_Container object represents the generic element container |
| 37 | * |
| 38 | * @access public |
| 39 | * @author Sam, <sam@taotesting.com> |
| 40 | * @package taoQTI |
| 41 | |
| 42 | */ |
| 43 | abstract class Container extends Element implements IdentifiedElementContainer |
| 44 | { |
| 45 | /** |
| 46 | * The data containing the position of qti elements within the html body |
| 47 | * |
| 48 | * @access protected |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $body = ''; |
| 52 | |
| 53 | /** |
| 54 | * The list of available elements |
| 55 | * |
| 56 | * @access protected |
| 57 | * @var array |
| 58 | */ |
| 59 | protected $elements = []; |
| 60 | |
| 61 | /** |
| 62 | * Short description of method __construct |
| 63 | * |
| 64 | * @access public |
| 65 | * @author Sam, <sam@taotesting.com> |
| 66 | * @param string body |
| 67 | * @return mixed |
| 68 | */ |
| 69 | public function __construct($body = '', Item $relatedItem = null, $serial = '') |
| 70 | { |
| 71 | parent::__construct([], $relatedItem, $serial); |
| 72 | $this->body = $body; |
| 73 | } |
| 74 | |
| 75 | public function __toString() |
| 76 | { |
| 77 | return $this->body; |
| 78 | } |
| 79 | |
| 80 | protected function getUsedAttributes() |
| 81 | { |
| 82 | return []; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * add one qtiElement into the body |
| 87 | * if the body content is not specified, it appends to the end |
| 88 | * |
| 89 | * @access public |
| 90 | * @author Sam, <sam@taotesting.com> |
| 91 | * @return boolean |
| 92 | */ |
| 93 | public function setElement(Element $qtiElement, $body = '', $integrityCheck = true, $requiredPlaceholder = true) |
| 94 | { |
| 95 | return $this->setElements([$qtiElement], $body, $integrityCheck, $requiredPlaceholder); |
| 96 | } |
| 97 | |
| 98 | public function setElements($qtiElements, $body = '', $integrityCheck = true, $requiredPlaceholder = true) |
| 99 | { |
| 100 | |
| 101 | $missingElements = []; |
| 102 | if ($integrityCheck && !empty($body) && !$this->checkIntegrity($body, $missingElements)) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | if (empty($body)) { |
| 107 | $body = $this->body; |
| 108 | } |
| 109 | |
| 110 | foreach ($qtiElements as $qtiElement) { |
| 111 | if ($this->isValidElement($qtiElement)) { |
| 112 | $placeholder = $qtiElement->getPlaceholder(); |
| 113 | if (strpos($body, $placeholder) === false) { |
| 114 | if ($requiredPlaceholder) { |
| 115 | throw new InvalidArgumentException( |
| 116 | 'no placeholder found for the element in the new container body: ' |
| 117 | . get_class($qtiElement) . ':' . $placeholder |
| 118 | ); |
| 119 | } else { |
| 120 | //assume implicitly add to the end |
| 121 | $body .= $placeholder; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $relatedItem = $this->getRelatedItem(); |
| 126 | if (!is_null($relatedItem)) { |
| 127 | $qtiElement->setRelatedItem($relatedItem); |
| 128 | if ($qtiElement instanceof IdentifiedElement) { |
| 129 | $qtiElement->getIdentifier();//generate one |
| 130 | } |
| 131 | } |
| 132 | $this->elements[$qtiElement->getSerial()] = $qtiElement; |
| 133 | $this->afterElementSet($qtiElement); |
| 134 | } else { |
| 135 | throw new QtiModelException( |
| 136 | 'The container ' . get_class($this) . ' cannot contain element of type ' . get_class($qtiElement) |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $this->edit($body); |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | public function afterElementSet(Element $qtiElement) |
| 147 | { |
| 148 | |
| 149 | if ($qtiElement instanceof IdentifiedElement) { |
| 150 | //check ids |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public function afterElementRemove(Element $qtiElement) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | public function getBody() |
| 159 | { |
| 160 | return $this->body; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * modify the content of the body |
| 165 | * |
| 166 | * @param string $body |
| 167 | * @param bool $integrityCheck |
| 168 | * @return bool |
| 169 | */ |
| 170 | public function edit($body, $integrityCheck = false) |
| 171 | { |
| 172 | if (!is_string($body)) { |
| 173 | throw new InvalidArgumentException('a QTI container must have a body of string type'); |
| 174 | } |
| 175 | if ($integrityCheck && !$this->checkIntegrity($body)) { |
| 176 | return false; |
| 177 | } |
| 178 | $this->body = $body; |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Check if modifying the body won't have an element placeholder deleted |
| 184 | * |
| 185 | * @param string $body |
| 186 | * @return boolean |
| 187 | */ |
| 188 | public function checkIntegrity($body, &$missingElements = null) |
| 189 | { |
| 190 | |
| 191 | $returnValue = true; |
| 192 | |
| 193 | foreach ($this->elements as $element) { |
| 194 | if (strpos($body, $element->getPlaceholder()) === false) { |
| 195 | $returnValue = false; |
| 196 | if (is_array($missingElements)) { |
| 197 | $missingElements[$element->getSerial()] = $element; |
| 198 | } else { |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | return (bool) $returnValue; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Converts <foo/> to <foo></foo> unless foo is a proper void element such as img etc. |
| 210 | * |
| 211 | * @param $html |
| 212 | * @return mixed |
| 213 | */ |
| 214 | public function fixNonvoidTags($html) |
| 215 | { |
| 216 | $content = preg_replace_callback('~(<([\w]+)[^>]*?)(\s*/>)~u', function ($matches) { |
| 217 | // something went wrong |
| 218 | if (empty($matches[2])) { |
| 219 | // do nothing |
| 220 | return $matches[0]; |
| 221 | } |
| 222 | |
| 223 | $voidElements = [ |
| 224 | 'area', |
| 225 | 'base', |
| 226 | 'br', |
| 227 | 'col', |
| 228 | 'embed', |
| 229 | 'hr', |
| 230 | 'img', |
| 231 | 'input', |
| 232 | 'keygen', |
| 233 | 'link', |
| 234 | 'meta', |
| 235 | 'param', |
| 236 | 'source', |
| 237 | 'track', |
| 238 | 'wbr', |
| 239 | ]; |
| 240 | |
| 241 | // regular void elements |
| 242 | if (in_array($matches[2], $voidElements)) { |
| 243 | // do nothing |
| 244 | return $matches[0]; |
| 245 | } |
| 246 | // correctly closed element |
| 247 | return trim(mb_substr($matches[0], 0, -2), 'UTF-8') . '></' . $matches[2] . '>'; |
| 248 | }, $html); |
| 249 | |
| 250 | $pregLastError = preg_last_error(); |
| 251 | if ( |
| 252 | $content === null && |
| 253 | ( |
| 254 | $pregLastError === PREG_BACKTRACK_LIMIT_ERROR || |
| 255 | $pregLastError === PREG_RECURSION_LIMIT_ERROR |
| 256 | ) |
| 257 | ) { |
| 258 | common_Logger::w('Content size is exceeding preg backtrack limits, could not fix non void tags'); |
| 259 | return $html; |
| 260 | } |
| 261 | |
| 262 | return $content; |
| 263 | } |
| 264 | |
| 265 | public function isValidElement(Element $element) |
| 266 | { |
| 267 | $returnValue = false; |
| 268 | |
| 269 | $validClasses = $this->getValidElementTypes(); |
| 270 | foreach ($validClasses as $validClass) { |
| 271 | if ($element instanceof $validClass) { |
| 272 | $returnValue = true; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | return $returnValue; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * return the list of available element classes |
| 281 | * |
| 282 | * @access public |
| 283 | * @author Sam, <sam@taotesting.com> |
| 284 | * @return string[] |
| 285 | */ |
| 286 | abstract public function getValidElementTypes(): array; |
| 287 | |
| 288 | /** |
| 289 | * Get the element by its serial |
| 290 | * |
| 291 | * @param string $serial |
| 292 | * @return oat\taoQtiItem\model\qti\Element |
| 293 | */ |
| 294 | public function getElement($serial) |
| 295 | { |
| 296 | |
| 297 | $returnValue = null; |
| 298 | |
| 299 | if (isset($this->elements[$serial])) { |
| 300 | $returnValue = $this->elements[$serial]; |
| 301 | } |
| 302 | |
| 303 | return $returnValue; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Get all elements of the given type |
| 308 | * Returns all elements if class name is not specified |
| 309 | * |
| 310 | * @param string $className |
| 311 | * @return array |
| 312 | */ |
| 313 | public function getElements($className = '') |
| 314 | { |
| 315 | |
| 316 | $returnValue = []; |
| 317 | |
| 318 | if ($className) { |
| 319 | foreach ($this->elements as $serial => $element) { |
| 320 | if ($element instanceof $className) { |
| 321 | $returnValue[$serial] = $element; |
| 322 | } |
| 323 | } |
| 324 | } else { |
| 325 | $returnValue = $this->elements; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | return $returnValue; |
| 330 | } |
| 331 | |
| 332 | public function removeElement($element) |
| 333 | { |
| 334 | |
| 335 | $returnValue = false; |
| 336 | |
| 337 | $serial = ''; |
| 338 | if ($element instanceof Element) { |
| 339 | $serial = $element->getSerial(); |
| 340 | } elseif (is_string($element)) { |
| 341 | $serial = $element; |
| 342 | } |
| 343 | |
| 344 | if (!empty($serial) && isset($this->elements[$serial])) { |
| 345 | $this->body = str_replace($this->elements[$serial]->getPlaceholder(), '', $this->body); |
| 346 | $this->afterElementRemove($this->elements[$serial]); |
| 347 | unset($this->elements[$serial]); |
| 348 | $returnValue = true; |
| 349 | } |
| 350 | |
| 351 | return $returnValue; |
| 352 | } |
| 353 | |
| 354 | public function replaceElement(Element $oldElement, Element $newElement) |
| 355 | { |
| 356 | $body = str_replace($oldElement->getPlaceholder(), $newElement->getPlaceholder(), $this->body, $count); |
| 357 | if ($count === 0) { |
| 358 | throw new QtiModelException('cannot find the element to be replaced'); |
| 359 | } elseif ($count > 1) { |
| 360 | throw new QtiModelException('multiple placeholder found for the element to be replaced'); |
| 361 | } |
| 362 | $this->removeElement($oldElement); |
| 363 | $this->setElement($newElement, $body); |
| 364 | } |
| 365 | |
| 366 | public function getIdentifiedElements() |
| 367 | { |
| 368 | |
| 369 | $returnValue = new IdentifierCollection(); |
| 370 | |
| 371 | foreach ($this->elements as $element) { |
| 372 | if ($element instanceof IdentifiedElementContainer) { |
| 373 | $returnValue->merge($element->getIdentifiedElements()); |
| 374 | } |
| 375 | if ($element instanceof IdentifiedElement) { |
| 376 | $returnValue->add($element); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return $returnValue; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Export the data to QTI XML format |
| 385 | * |
| 386 | * @access public |
| 387 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 388 | * @return string |
| 389 | */ |
| 390 | public function toQTI() |
| 391 | { |
| 392 | $returnValue = $this->getBody(); |
| 393 | |
| 394 | foreach ($this->elements as $element) { |
| 395 | $returnValue = str_replace($element->getPlaceholder(), $element->toQTI(), $returnValue); |
| 396 | } |
| 397 | |
| 398 | return (string) $returnValue; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get the array representation of the Qti Element. |
| 403 | * Particularly helpful for data transformation, e.g. json |
| 404 | * |
| 405 | * @access public |
| 406 | * @author Sam, <sam@taotesting.com> |
| 407 | * @return array |
| 408 | */ |
| 409 | public function toArray($filterVariableContent = false, &$filtered = []) |
| 410 | { |
| 411 | |
| 412 | $data = [ |
| 413 | 'serial' => $this->getSerial(), |
| 414 | 'body' => $this->getBody(), |
| 415 | 'elements' => $this->getArraySerializedElementCollection( |
| 416 | $this->getElements(), |
| 417 | $filterVariableContent, |
| 418 | $filtered |
| 419 | ), |
| 420 | 'attributes' => $this->getAttributeValues() |
| 421 | ]; |
| 422 | |
| 423 | if ($this->isDebugMode()) { |
| 424 | //in debug mode, add debug data, such as the related item |
| 425 | $data['debug'] = [ |
| 426 | 'relatedItem' => is_null($this->getRelatedItem()) ? '' : $this->getRelatedItem()->getSerial() |
| 427 | ]; |
| 428 | } |
| 429 | |
| 430 | return $data; |
| 431 | } |
| 432 | |
| 433 | private function isDebugMode(): bool |
| 434 | { |
| 435 | return defined('DEBUG_MODE') ? DEBUG_MODE : false; |
| 436 | } |
| 437 | } |