Exemplo n.º 1
0
 /**
  * Load assets from the custom elements (CustomInteraction, PCI, PIC)
  * @param Element $element the custom element
  */
 private function loadCustomElementAssets(Element $element)
 {
     if ($this->getGetCustomElementDefinition()) {
         if ($element instanceof PortableCustomInteraction) {
             $this->assets['PCI'][$element->getTypeIdentifier()] = $element;
         }
         if ($element instanceof PortableInfoControl) {
             $this->assets['PIC'][$element->getTypeIdentifier()] = $element;
         }
     }
     $xmls = array();
     if ($element instanceof PortableCustomInteraction || $element instanceof PortableInfoControl) {
         //some portable elements contains htmlentitied markup in their properties...
         $xmls = $this->getXmlProperties($element->getProperties());
     }
     //parse and extract assets from markup using XPATH
     if ($element instanceof CustomInteraction || $element instanceof InfoControl) {
         // http://php.net/manual/fr/simplexmlelement.xpath.php#116622
         $sanitizedMarkup = str_replace('xmlns=', 'ns=', $element->getMarkup());
         $xmls[] = new SimpleXMLElement($sanitizedMarkup);
         $this->loadCustomElementPropertiesAssets($element->getProperties());
         /** @var SimpleXMLElement $xml */
         foreach ($xmls as $xml) {
             foreach ($xml->xpath('//img') as $img) {
                 $this->addAsset('img', (string) $img['src']);
             }
             foreach ($xml->xpath('//video') as $video) {
                 $this->addAsset('video', (string) $video['src']);
             }
             foreach ($xml->xpath('//audio') as $audio) {
                 $this->addAsset('audio', (string) $audio['src']);
             }
         }
     }
 }
 /**
  * Parse individual portable element into the given portable model
  * @param PortableElementModel $model
  * @param Element $portableElement
  * @throws \common_Exception
  * @throws PortableElementInconsistencyModelException
  */
 protected function parsePortableElement(PortableElementModel $model, Element $portableElement)
 {
     $typeId = $portableElement->getTypeIdentifier();
     $libs = [];
     $requiredLibFiles = [];
     //Adjust file resource entries where {QTI_NS}/xxx/yyy is equivalent to {QTI_NS}/xxx/yyy.js
     foreach ($portableElement->getLibraries() as $lib) {
         if (preg_match('/^' . $typeId . '.*\\.js$/', $lib) && substr($lib, -3) != '.js') {
             //filter shared stimulus
             $requiredLibFiles[] = $lib . '.js';
             //amd modules
             $libs[] = $lib . '.js';
         } else {
             $libs[] = $lib;
         }
     }
     $data = ['typeIdentifier' => $typeId, 'version' => $portableElement->getVersion(), 'label' => $typeId, 'short' => $typeId, 'runtime' => ['hook' => $portableElement->getEntryPoint(), 'libraries' => $libs, 'stylesheets' => $portableElement->getStylesheets(), 'mediaFiles' => $portableElement->getMediaFiles()]];
     /** @var PortableElementObject $portableObject */
     $portableObject = $model->createDataObject($data);
     $lastVersionModel = $this->getService()->getPortableElementByIdentifier($portableObject->getModel()->getId(), $portableObject->getTypeIdentifier());
     if (!is_null($lastVersionModel) && intval($lastVersionModel->getVersion()) != intVal($portableObject->getVersion())) {
         //@todo return a user exception to inform user of incompatible pci version found and that an item update is required
         throw new \common_Exception('Unable to import pci asset because pci is not compatible. ' . 'Current version is ' . $lastVersionModel->getVersion() . ' and imported is ' . $portableObject->getVersion());
     }
     $this->portableObjects[$typeId] = $portableObject;
     $files = array_merge([$portableObject->getRuntimeKey('hook')], $requiredLibFiles, $portableObject->getRuntimeKey('stylesheets'), $portableObject->getRuntimeKey('mediaFiles'));
     $this->requiredFiles = array_merge($this->requiredFiles, array_fill_keys($files, $typeId));
 }