Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.76% |
133 / 262 |
|
22.58% |
7 / 31 |
CRAP | |
0.00% |
0 / 1 |
| Element | |
50.76% |
133 / 262 |
|
22.58% |
7 / 31 |
2083.62 | |
0.00% |
0 / 1 |
| __construct | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
4.43 | |||
| getUsedAttributes | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| resetAttributes | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
4.05 | |||
| getQtiTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeAttributeValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| setAttributes | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| setAttribute | |
40.74% |
11 / 27 |
|
0.00% |
0 / 1 |
21.32 | |||
| validateAttribute | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
90 | |||
| getIdentifiedElement | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| hasAttribute | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| attr | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| addClass | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| removeClass | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getAttributeValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getAttributeValues | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| getPlaceholder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateQti | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getTemplateQtiVariables | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
| toQTI | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| toArray | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| getTemplatePath | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| setRelatedItem | |
60.87% |
14 / 23 |
|
0.00% |
0 / 1 |
34.32 | |||
| getComposingElements | |
71.43% |
15 / 21 |
|
0.00% |
0 / 1 |
16.94 | |||
| getRelatedItem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| xmlizeOptions | |
78.57% |
22 / 28 |
|
0.00% |
0 / 1 |
12.19 | |||
| getSerial | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| buildSerial | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
2.75 | |||
| getArraySerializedElementCollection | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
4.12 | |||
| getArraySerializedPrimitiveCollection | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| isDebug | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getBodyAttributes | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
| 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 | namespace oat\taoQtiItem\model\qti; |
| 22 | |
| 23 | use InvalidArgumentException; |
| 24 | use oat\taoQtiItem\model\qti\exception\QtiModelException; |
| 25 | use oat\taoQtiItem\model\qti\attribute\Generic; |
| 26 | use oat\taoQtiItem\model\qti\container\FlowContainer; |
| 27 | use oat\taoQtiItem\model\qti\attribute\ResponseIdentifier; |
| 28 | use common_Logger; |
| 29 | use taoItems_models_classes_TemplateRenderer; |
| 30 | use ReflectionClass; |
| 31 | use stdClass; |
| 32 | |
| 33 | /** |
| 34 | * The QTI_Element class represent the abstract model for all the QTI objects. |
| 35 | * It contains all the attributes of the different kind of QTI objects. |
| 36 | * It manages the identifiers and serial creation. |
| 37 | * It provides the serialisation and persistence methods. |
| 38 | * And give the interface for the rendering. |
| 39 | * |
| 40 | * @abstract |
| 41 | * |
| 42 | * @access public |
| 43 | * @author Sam, <sam@taotesting.com> |
| 44 | * @package taoQTI |
| 45 | */ |
| 46 | abstract class Element implements Exportable |
| 47 | { |
| 48 | protected $serial = ''; |
| 49 | protected $relatedItem = null; |
| 50 | private static $instances = []; |
| 51 | |
| 52 | /** |
| 53 | * Short description of attribute templatesPath |
| 54 | * |
| 55 | * @access protected |
| 56 | * @var string |
| 57 | */ |
| 58 | protected static $templatesPath = ''; |
| 59 | |
| 60 | /** |
| 61 | * the QTI tag name as defined in QTI standard |
| 62 | * |
| 63 | * @access protected |
| 64 | * @var string |
| 65 | */ |
| 66 | protected static $qtiTagName = ''; |
| 67 | protected static $qtiNamespaceAlias = ''; |
| 68 | |
| 69 | /** |
| 70 | * the options of the element |
| 71 | * |
| 72 | * @access protected |
| 73 | * @var array |
| 74 | */ |
| 75 | protected $attributes = []; |
| 76 | |
| 77 | public function __construct($attributes = [], Item $relatedItem = null, $serial = '') |
| 78 | { |
| 79 | if (!is_null($relatedItem)) { |
| 80 | $this->setRelatedItem($relatedItem); |
| 81 | } |
| 82 | if (!empty($serial)) { |
| 83 | //try setting object serial manually: |
| 84 | if (isset(self::$instances[$this->getSerial()])) { |
| 85 | throw new QtiModelException('the serial must be unique'); |
| 86 | } else { |
| 87 | $this->serial = $serial; |
| 88 | } |
| 89 | } else { |
| 90 | $this->getSerial(); //generate one |
| 91 | } |
| 92 | |
| 93 | $this->resetAttributes(); |
| 94 | |
| 95 | $this->setAttributes($attributes); |
| 96 | |
| 97 | self::$instances[$this->getSerial()] = $this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Provide the list of attributes of the Qti Element Class |
| 102 | */ |
| 103 | abstract protected function getUsedAttributes(); |
| 104 | |
| 105 | /** |
| 106 | * Reset the attributes values to the default values defined by the standard |
| 107 | */ |
| 108 | public function resetAttributes() |
| 109 | { |
| 110 | $this->attributes = []; |
| 111 | foreach ($this->getUsedAttributes() as $attributeClass) { |
| 112 | if ( |
| 113 | class_exists($attributeClass) |
| 114 | && is_subclass_of($attributeClass, 'oat\\taoQtiItem\\model\\qti\\attribute\\Attribute') |
| 115 | ) { |
| 116 | $attribute = new $attributeClass(); |
| 117 | $this->attributes[$attribute->getName()] = $attribute; |
| 118 | } else { |
| 119 | common_Logger::w('attr does not exists ' . $attributeClass); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public function getQtiTag() |
| 125 | { |
| 126 | return static::$qtiTagName; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Remove the actual value of an attribute, distinguish from empty value |
| 131 | * |
| 132 | * @param string $name |
| 133 | */ |
| 134 | public function removeAttributeValue($name) |
| 135 | { |
| 136 | if (isset($this->attributes[$name])) { |
| 137 | $this->attributes[$name]->setNull(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Set the attributes for the the Qti Element |
| 143 | * Argument format: array(attributeName => value) |
| 144 | * |
| 145 | * @param array $values |
| 146 | * @throws InvalidArgumentException |
| 147 | */ |
| 148 | public function setAttributes($values) |
| 149 | { |
| 150 | |
| 151 | if (is_array($values)) { |
| 152 | foreach ($values as $name => $value) { |
| 153 | $this->setAttribute($name, $value); |
| 154 | } |
| 155 | } else { |
| 156 | throw new InvalidArgumentException('"values" must be an array'); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set the value of an attribute |
| 162 | * |
| 163 | * @param string $name |
| 164 | * @param mixed $value |
| 165 | * @return boolean |
| 166 | * @throws InvalidArgumentException |
| 167 | * @throws \oat\taoQtiItem\model\qti\exception\QtiModelException |
| 168 | */ |
| 169 | public function setAttribute($name, $value) |
| 170 | { |
| 171 | |
| 172 | $returnValue = false; |
| 173 | |
| 174 | if (is_null($value)) { |
| 175 | return $returnValue; |
| 176 | } |
| 177 | |
| 178 | if (isset($this->attributes[$name])) { |
| 179 | $datatypeClass = $this->attributes[$name]->getType(); |
| 180 | // check if the attribute needs an element level validation |
| 181 | if (is_subclass_of($datatypeClass, 'oat\\taoQtiItem\\model\\qti\\datatype\\Identifier')) { |
| 182 | if ($value instanceof IdentifiedElement) { |
| 183 | if ($this->validateAttribute($name, $value)) { |
| 184 | $this->attributes[$name]->setValue($value); |
| 185 | $returnValue = true; |
| 186 | } else { |
| 187 | $vr = print_r($value, true); |
| 188 | common_Logger::w($vr); |
| 189 | throw new InvalidArgumentException('Invalid identifier attribute value'); |
| 190 | } |
| 191 | } elseif (is_string($value)) { |
| 192 | // try converting to string identifier and search the identified object: |
| 193 | $identifier = (string) $value; |
| 194 | $elt = $this->getIdentifiedElement($identifier, $datatypeClass::getAllowedClasses()); |
| 195 | if (!is_null($elt)) { |
| 196 | // ok, found among allowed classes |
| 197 | $this->attributes[$name]->setValue($elt); |
| 198 | $returnValue = true; |
| 199 | } else { |
| 200 | throw new QtiModelException( |
| 201 | 'No QTI element with the identifier has been found: ' . $identifier |
| 202 | ); |
| 203 | } |
| 204 | } |
| 205 | } else { |
| 206 | $this->attributes[$name]->setValue($value); |
| 207 | $returnValue = true; |
| 208 | } |
| 209 | } else { |
| 210 | $this->attributes[$name] = new Generic($value); |
| 211 | $returnValue = true; |
| 212 | } |
| 213 | |
| 214 | return $returnValue; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Validate an attribute of the element, at the element level |
| 219 | * (the validator of the attributes are on the attribute level) |
| 220 | * |
| 221 | * @param string $name |
| 222 | * @param mixed $value |
| 223 | * @return boolean |
| 224 | * @throws \oat\taoQtiItem\model\qti\exception\QtiModelException |
| 225 | */ |
| 226 | public function validateAttribute($name, $value = null) |
| 227 | { |
| 228 | $returnValue = false; |
| 229 | |
| 230 | if (isset($this->attributes[$name])) { |
| 231 | if (is_null($value)) { |
| 232 | $value = $this->attributes[$name]->getValue(); |
| 233 | } |
| 234 | |
| 235 | $datatypeClass = $this->attributes[$name]->getType(); |
| 236 | if (is_subclass_of($datatypeClass, 'oat\\taoQtiItem\\model\\qti\\datatype\\Identifier')) { |
| 237 | } else { |
| 238 | $returnValue = $datatypeClass::validate($value); |
| 239 | } |
| 240 | |
| 241 | if (is_subclass_of($datatypeClass, 'oat\\taoQtiItem\\model\\qti\\datatype\\Identifier')) { |
| 242 | if ($datatypeClass::validate($value)) { |
| 243 | // validate itentifier |
| 244 | $relatedItem = $this->getRelatedItem(); |
| 245 | if (!is_null($relatedItem)) { |
| 246 | $idCollection = $relatedItem->getIdentifiedElements(); |
| 247 | if ($value instanceof IdentifiedElement && $idCollection->exists($value->getIdentifier())) { |
| 248 | $returnValue = true; |
| 249 | } |
| 250 | } else { |
| 251 | common_Logger::w('iden'); |
| 252 | |
| 253 | throw new QtiModelException( |
| 254 | 'Cannot verify identifier reference because the element is not in a QTI Item ' |
| 255 | . get_class($this) . '::' . $name, |
| 256 | 0 |
| 257 | ); |
| 258 | } |
| 259 | } |
| 260 | } else { |
| 261 | $returnValue = $datatypeClass::validate($value); |
| 262 | } |
| 263 | } else { |
| 264 | throw new InvalidArgumentException('no attribute found with the name "' . $name . '"'); |
| 265 | } |
| 266 | |
| 267 | return $returnValue; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Find the identified object corresponding to the identifier string |
| 272 | * The optional argument $elementClasses search a specific QTI element class |
| 273 | * |
| 274 | * @param string $identifier |
| 275 | * @param array $elementClasses |
| 276 | * @return \oat\taoQtiItem\model\qti\IdentifiedElement |
| 277 | */ |
| 278 | public function getIdentifiedElement($identifier, $elementClasses = []) |
| 279 | { |
| 280 | $returnValue = null; |
| 281 | |
| 282 | if (!is_array($elementClasses)) { |
| 283 | throw new InvalidArgumentException('elementClasses must be an array'); |
| 284 | } |
| 285 | |
| 286 | $relatedItem = $this->getRelatedItem(); |
| 287 | |
| 288 | if (!is_null($relatedItem)) { |
| 289 | $identifiedElementsCollection = $relatedItem->getIdentifiedElements(); |
| 290 | |
| 291 | if (empty($elementClasses)) { |
| 292 | $returnValue = $identifiedElementsCollection->getUnique($identifier); |
| 293 | } else { |
| 294 | foreach ($elementClasses as $elementClass) { |
| 295 | $returnValue = $identifiedElementsCollection->getUnique($identifier, $elementClass); |
| 296 | if (!is_null($returnValue)) { |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return $returnValue; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Check if an attribute exists within the Qti Element |
| 308 | * |
| 309 | * @param string $name |
| 310 | * @return boolean |
| 311 | */ |
| 312 | public function hasAttribute($name) |
| 313 | { |
| 314 | return isset($this->attributes[$name]); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Short handy method to get/set an attribute value |
| 319 | * |
| 320 | * @param string $name |
| 321 | * @param mixed $value |
| 322 | * @return mixed |
| 323 | */ |
| 324 | public function attr($name, $value = null) |
| 325 | { |
| 326 | if (is_null($value)) { |
| 327 | return $this->getAttributeValue($name); |
| 328 | } else { |
| 329 | return $this->setAttribute($name, $value); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Add a CSS class to the item body |
| 335 | * |
| 336 | * @author Dieter Raber <dieter@taotesting.com> |
| 337 | * @param $className one or more class names, separated by space |
| 338 | */ |
| 339 | public function addClass($className) |
| 340 | { |
| 341 | $oldClassName = $this->getAttributeValue('class'); |
| 342 | $oldClassNameArr = $oldClassName ? explode(' ', $oldClassName) : []; |
| 343 | $classNameArr = array_merge($oldClassNameArr, explode(' ', $className)); |
| 344 | // housekeeping |
| 345 | $classNameArr = array_unique(array_filter(array_map('trim', $classNameArr))); |
| 346 | $this->setAttribute('class', implode(' ', $classNameArr)); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Add a CSS class from the item body |
| 351 | * |
| 352 | * @author Dieter Raber <dieter@taotesting.com> |
| 353 | * @param $className |
| 354 | */ |
| 355 | public function removeClass($className) |
| 356 | { |
| 357 | $oldClassName = $this->getAttributeValue('class'); |
| 358 | $oldClassNameArr = $oldClassName ? explode(' ', $oldClassName) : []; |
| 359 | unset($oldClassNameArr[array_search($className, $oldClassNameArr)]); |
| 360 | $this->setAttribute('class', implode(' ', $oldClassNameArr)); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get the attribute as an Attribute object |
| 365 | * |
| 366 | * @param type $name |
| 367 | * @return \oat\taoQtiItem\model\qti\attribute\Attribute |
| 368 | */ |
| 369 | protected function getAttribute($name) |
| 370 | { |
| 371 | return $this->hasAttribute($name) ? $this->attributes[$name] : null; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get the attribute's actual value (not as an Attribute object) |
| 376 | * |
| 377 | * @param string $name |
| 378 | * @return mixed |
| 379 | */ |
| 380 | public function getAttributeValue($name) |
| 381 | { |
| 382 | $returnValue = null; |
| 383 | if ($this->hasAttribute($name)) { |
| 384 | $returnValue = $this->attributes[$name]->getValue(); |
| 385 | } |
| 386 | return $returnValue; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Get all attributes' values |
| 391 | * |
| 392 | * @return array |
| 393 | */ |
| 394 | public function getAttributeValues($filterNull = true) |
| 395 | { |
| 396 | $returnValue = []; |
| 397 | foreach ($this->attributes as $name => $attribute) { |
| 398 | if (!$filterNull || !$attribute->isNull()) { |
| 399 | $returnValue[$name] = $attribute->getValue(); |
| 400 | } |
| 401 | } |
| 402 | return $returnValue; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Get the placeholder of the Qti Element to used in a Container |
| 407 | * |
| 408 | * @see oat\taoQtiItem\model\qti\container\Container |
| 409 | * @return string |
| 410 | */ |
| 411 | public function getPlaceholder() |
| 412 | { |
| 413 | return '{{' . $this->getSerial() . '}}'; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Get the absolute path of the template of the qti.xml |
| 418 | * |
| 419 | * @return string |
| 420 | * @throws \oat\taoQtiItem\model\qti\exception\QtiModelException |
| 421 | */ |
| 422 | public static function getTemplateQti() |
| 423 | { |
| 424 | if (empty(static::$qtiTagName)) { |
| 425 | throw new QtiModelException('The element has no tag name defined : ' . get_called_class()); |
| 426 | } |
| 427 | $template = static::getTemplatePath() . '/qti.' . static::$qtiTagName . '.tpl.php'; |
| 428 | if (!file_exists($template)) { |
| 429 | $template = static::getTemplatePath() . '/qti.element.tpl.php'; |
| 430 | } |
| 431 | |
| 432 | return $template; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Get the variables to be used in the qti.xml template |
| 437 | * |
| 438 | * @return array |
| 439 | */ |
| 440 | protected function getTemplateQtiVariables() |
| 441 | { |
| 442 | $variables = []; |
| 443 | $variables['tag'] = static::$qtiTagName; |
| 444 | $variables['attributes'] = $this->getAttributeValues(); |
| 445 | if ($this instanceof FlowContainer) { |
| 446 | $variables['body'] = $this->getBody()->toQTI(); |
| 447 | $variables['bodyAttributes'] = $this->getBodyAttributes(); |
| 448 | } |
| 449 | |
| 450 | if (static::$qtiNamespaceAlias !== '') { |
| 451 | $variables['tag'] = static::$qtiNamespaceAlias . ':' . static::$qtiTagName; |
| 452 | } |
| 453 | |
| 454 | return $variables; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Export the data to the QTI XML format |
| 459 | * |
| 460 | * @return string |
| 461 | */ |
| 462 | public function toQTI() |
| 463 | { |
| 464 | |
| 465 | $template = static::getTemplateQti(); |
| 466 | $variables = $this->getTemplateQtiVariables(); |
| 467 | if (isset($variables['attributes'])) { |
| 468 | $variables['attributes'] = $this->xmlizeOptions($variables['attributes'], true); |
| 469 | } |
| 470 | $tplRenderer = new taoItems_models_classes_TemplateRenderer($template, $variables); |
| 471 | $returnValue = $tplRenderer->render(); |
| 472 | |
| 473 | return (string) $returnValue; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Get the array representation of the Qti Element. |
| 478 | * Particularly helpful for data transformation, e.g. json |
| 479 | * |
| 480 | * @param $filterVariableContent |
| 481 | * @param array $filtered |
| 482 | * @return array |
| 483 | */ |
| 484 | public function toArray($filterVariableContent = false, &$filtered = []) |
| 485 | { |
| 486 | |
| 487 | $data = []; |
| 488 | $data['serial'] = $this->getSerial(); |
| 489 | $tag = $this->getQtiTag(); |
| 490 | if (!empty($tag)) { |
| 491 | $data['qtiClass'] = $tag; |
| 492 | } |
| 493 | $attributes = $this->getAttributeValues(); |
| 494 | $data['attributes'] = empty($attributes) ? new StdClass() : $attributes; |
| 495 | |
| 496 | if ($this instanceof FlowContainer) { |
| 497 | $data['body'] = $this->getBody()->toArray($filterVariableContent, $filtered); |
| 498 | } |
| 499 | |
| 500 | if ($this->isDebug()) { |
| 501 | //in debug mode, add debug data, such as the related item |
| 502 | $data['debug'] = [ |
| 503 | 'relatedItem' => is_null($this->getRelatedItem()) ? '' : $this->getRelatedItem()->getSerial() |
| 504 | ]; |
| 505 | } |
| 506 | |
| 507 | return $data; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Get the main template directory |
| 512 | * |
| 513 | * @access public |
| 514 | * @author Sam, <sam@taotesting.com> |
| 515 | * @return string |
| 516 | */ |
| 517 | public static function getTemplatePath() |
| 518 | { |
| 519 | if (empty(self::$templatesPath)) { |
| 520 | $dir = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiItem')->getDir(); |
| 521 | self::$templatesPath = $dir . 'model/qti/templates/'; |
| 522 | } |
| 523 | $returnValue = self::$templatesPath; |
| 524 | |
| 525 | return (string) $returnValue; |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Set the item the current Qti Element belongs to. |
| 530 | * The related item assignment is propagated to all containing Qti Element of the current one. |
| 531 | * The "force" option allows changing the associated item (even if it has already been defined) |
| 532 | * |
| 533 | * @param \oat\taoQtiItem\model\qti\Item $item |
| 534 | * @param boolean $force |
| 535 | * @return boolean |
| 536 | * @throws \oat\taoQtiItem\model\qti\exception\QtiModelException |
| 537 | */ |
| 538 | public function setRelatedItem(Item $item, $force = false) |
| 539 | { |
| 540 | $returnValue = false; |
| 541 | |
| 542 | if (!is_null($this->relatedItem) && $this->relatedItem->getSerial() == $item->getSerial()) { |
| 543 | $returnValue = true; // identical |
| 544 | } elseif (!$force && !is_null($this->relatedItem)) { |
| 545 | throw new QtiModelException('attempt to change item reference for a QTI element'); |
| 546 | } else { |
| 547 | // propagate the assignation of item to all included objects |
| 548 | $reflection = new ReflectionClass($this); |
| 549 | foreach ($reflection->getProperties() as $property) { |
| 550 | if (!$property->isStatic() && !$property->isPrivate()) { |
| 551 | $propertyName = $property->getName(); |
| 552 | $value = $this->$propertyName; |
| 553 | if (is_array($value)) { |
| 554 | foreach ($value as $subvalue) { |
| 555 | if (is_object($subvalue) && $subvalue instanceof Element) { |
| 556 | $subvalue->setRelatedItem($item); |
| 557 | } elseif (is_object($subvalue) && $subvalue instanceof ResponseIdentifier) { |
| 558 | // manage the reference of identifier |
| 559 | $idenfierBaseType = $subvalue->getValue(true); |
| 560 | if (!is_null($idenfierBaseType)) { |
| 561 | $idenfierBaseType->getReferencedObject()->setRelatedItem($item); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | } elseif (is_object($value) && $value instanceof Element) { |
| 566 | $value->setRelatedItem($item); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | // set item reference to current object |
| 572 | $this->relatedItem = $item; |
| 573 | |
| 574 | $returnValue = true; |
| 575 | } |
| 576 | |
| 577 | return $returnValue; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Recursively get all Qti Elements contained within the current Qti Element |
| 582 | * |
| 583 | * @param string $className |
| 584 | * @return array |
| 585 | */ |
| 586 | public function getComposingElements($className = '') |
| 587 | { |
| 588 | |
| 589 | $returnValue = []; |
| 590 | if ($className === '') { |
| 591 | $className = 'oat\taoQtiItem\model\qti\Element'; |
| 592 | } |
| 593 | |
| 594 | $reflection = new ReflectionClass($this); |
| 595 | foreach ($reflection->getProperties() as $property) { |
| 596 | if (!$property->isStatic() && !$property->isPrivate()) { |
| 597 | $propertyName = $property->getName(); |
| 598 | if ($propertyName != 'relatedItem') { |
| 599 | $value = $this->$propertyName; |
| 600 | if (is_array($value)) { |
| 601 | foreach ($value as $subvalue) { |
| 602 | if ($subvalue instanceof Element) { |
| 603 | if ($subvalue instanceof $className) { |
| 604 | $returnValue[$subvalue->getSerial()] = $subvalue; |
| 605 | } |
| 606 | $returnValue = array_merge($returnValue, $subvalue->getComposingElements($className)); |
| 607 | } |
| 608 | } |
| 609 | } else { |
| 610 | if ($value instanceof Element) { |
| 611 | if ($value instanceof $className) { |
| 612 | if ($value->getSerial() != $this->getSerial()) { |
| 613 | $returnValue[$value->getSerial()] = $value; |
| 614 | } |
| 615 | } |
| 616 | $returnValue = array_merge($returnValue, $value->getComposingElements($className)); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | return $returnValue; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Get the Qti Item the current Qti Element belongs to |
| 628 | * |
| 629 | * @return \oat\taoQtiItem\model\qti\Item |
| 630 | */ |
| 631 | public function getRelatedItem() |
| 632 | { |
| 633 | return $this->relatedItem; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * This method enables you to build a string of attributes for an xml node |
| 638 | * from the Qti Element attributes according to their types. |
| 639 | * |
| 640 | * @access protected |
| 641 | * @author Sam, <sam@taotesting.com> |
| 642 | * @param array formalOpts |
| 643 | * @param boolean recursive |
| 644 | * @return string |
| 645 | */ |
| 646 | protected function xmlizeOptions($formalOpts = [], $recursive = false) |
| 647 | { |
| 648 | $returnValue = (string) ''; |
| 649 | if (!is_array($formalOpts)) { |
| 650 | throw new InvalidArgumentException('formalOpts must be an array, ' . gettype($formalOpts) . ' given'); |
| 651 | } |
| 652 | |
| 653 | $options = (!$recursive) ? $this->getAttributeValues() : $formalOpts; |
| 654 | foreach ($options as $key => $value) { |
| 655 | if (is_string($value) || is_numeric($value)) { |
| 656 | // str_replace is unicode safe... |
| 657 | $returnValue .= ' ' . $key . '="' . str_replace([ |
| 658 | '&', |
| 659 | '<', |
| 660 | '>', |
| 661 | '\'', |
| 662 | '"' |
| 663 | ], [ |
| 664 | '&', |
| 665 | '<', |
| 666 | '>', |
| 667 | ''', |
| 668 | '"' |
| 669 | ], $value) . '"'; |
| 670 | } |
| 671 | if (is_bool($value)) { |
| 672 | $returnValue .= ' ' . $key . '="' . (($value) ? 'true' : 'false') . '"'; |
| 673 | } |
| 674 | if (is_array($value)) { |
| 675 | if (count($value) > 0) { |
| 676 | $keys = array_keys($value); |
| 677 | if (is_int($keys[0])) { // repeat the attribute key |
| 678 | $returnValue .= ' ' . $key . '="' . implode(' ', array_values($value)) . '"'; |
| 679 | } else { |
| 680 | $returnValue .= $this->xmlizeOptions($value, true); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return (string) $returnValue; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Obtain a serial for the instance of the class that implements the |
| 691 | * |
| 692 | * @access public |
| 693 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 694 | * @return string |
| 695 | */ |
| 696 | public function getSerial() |
| 697 | { |
| 698 | if (empty($this->serial)) { |
| 699 | $this->serial = $this->buildSerial(); |
| 700 | } |
| 701 | $returnValue = $this->serial; |
| 702 | |
| 703 | return (string) $returnValue; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * create a unique serial number |
| 708 | * |
| 709 | * @access protected |
| 710 | * @author Sam, <sam@taotesting.com> |
| 711 | * @return string |
| 712 | */ |
| 713 | protected function buildSerial() |
| 714 | { |
| 715 | |
| 716 | if ($this->isDebug()) { |
| 717 | //in debug mode, use more meaningful serials |
| 718 | $clazz = strtolower(get_class($this)); |
| 719 | $prefix = substr($clazz, strpos($clazz, 'taoqtiitem\\model\\qti\\') + 21) . '_'; |
| 720 | $serial = str_replace('.', '', uniqid($prefix, true)); |
| 721 | $serial = str_replace('\\', '_', $serial); |
| 722 | } else { |
| 723 | //build a short unique id for memory saving |
| 724 | $serial = uniqid('i'); |
| 725 | } |
| 726 | |
| 727 | return (string) $serial; |
| 728 | } |
| 729 | |
| 730 | protected function getArraySerializedElementCollection($elements, $filterVariableContent = false, &$filtered = []) |
| 731 | { |
| 732 | if (empty($elements)) { |
| 733 | $data = new stdClass(); |
| 734 | } else { |
| 735 | $data = []; |
| 736 | foreach ($elements as $element) { |
| 737 | $data[$element->getSerial()] = $element->toArray($filterVariableContent, $filtered); |
| 738 | } |
| 739 | } |
| 740 | return $data; |
| 741 | } |
| 742 | |
| 743 | protected function getArraySerializedPrimitiveCollection($elements) |
| 744 | { |
| 745 | |
| 746 | if (empty($elements)) { |
| 747 | $data = new stdClass(); |
| 748 | } else { |
| 749 | $data = []; |
| 750 | foreach ($elements as $key => $value) { |
| 751 | if (is_array($value)) { |
| 752 | $data[$key] = $this->getArraySerializedPrimitiveCollection($value); |
| 753 | } else { |
| 754 | $data[$key] = $value; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | return $data; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * @deprecated we should not use global constant to debug classes |
| 763 | */ |
| 764 | private function isDebug(): bool |
| 765 | { |
| 766 | if (defined('DEBUG_MODE')) { |
| 767 | return DEBUG_MODE; |
| 768 | } |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | private function getBodyAttributes(): string |
| 773 | { |
| 774 | $body = $this->getBody(); |
| 775 | |
| 776 | if (!$body instanceof Element) { |
| 777 | return ''; |
| 778 | } |
| 779 | |
| 780 | $attributeValues = $body->getAttributeValues(); |
| 781 | $bodyAttributes = []; |
| 782 | foreach ($attributeValues as $attributeName => $attributeValue) { |
| 783 | if (is_string($attributeValue)) { |
| 784 | $bodyAttributes[] = sprintf('%s="%s"', $attributeName, $attributeValue); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return implode(" ", $bodyAttributes); |
| 789 | } |
| 790 | } |