Exemplo n.º 1
0
 public function testMarshall()
 {
     $stylesheet = new Stylesheet('./stylesheet.css');
     $h3 = new H3();
     $h3->setContent(new InlineCollection(array(new TextRun('Be carefull kiddo!'))));
     $p = new P();
     $p->setContent(new InlineCollection(array(new TextRun('Read the instructions twice.'))));
     $rubricBlock = new RubricBlock(new ViewCollection(array(View::CANDIDATE, View::TUTOR)));
     $rubricBlock->setClass('warning');
     $rubricBlock->setContent(new FlowStaticCollection(array($h3, $p)));
     $rubricBlock->setStylesheets(new StylesheetCollection(array($stylesheet)));
     $element = $this->getMarshallerFactory('2.1.0')->createMarshaller($rubricBlock)->marshall($rubricBlock);
     $dom = new DOMDocument('1.0', 'UTF-8');
     $element = $dom->importNode($element, true);
     $this->assertEquals('<rubricBlock view="candidate tutor" class="warning"><h3>Be carefull kiddo!</h3><p>Read the instructions twice.</p><stylesheet href="./stylesheet.css" media="screen" type="text/css"/></rubricBlock>', $dom->saveXML($element));
 }
 /**
  * Unmarshall a DOMElement object corresponding to a QTI rubrickBlock element.
  * 
  * @param DOMElement $element A DOMElement object.
  * @return RubricBlock A RubricBlock object.
  * @throws UnmarshallingException If the mandatory attribute 'href' is missing from $element.
  */
 protected function unmarshall(DOMElement $element)
 {
     // First we retrieve the mandatory views.
     if (($value = static::getDOMElementAttributeAs($element, 'view', 'string')) !== null) {
         $viewsArray = explode(" ", $value);
         $viewsCollection = new ViewCollection();
         $ref = View::asArray();
         foreach ($viewsArray as $viewString) {
             $key = strtoupper(str_replace("ò", " ", $viewString));
             if (array_key_exists($key, $ref)) {
                 $viewsCollection[] = $ref[$key];
             }
         }
         $object = new RubricBlock($viewsCollection);
         if (($value = static::getDOMElementAttributeAs($element, 'use', 'string')) !== null) {
             $object->setUse($value);
         }
         if (($xmlBase = static::getXmlBase($element)) !== false) {
             $component->setXmlBase($xmlBase);
         }
         $stylesheets = new StylesheetCollection();
         $content = new FlowStaticCollection();
         foreach (self::getChildElements($element, true) as $elt) {
             if ($elt instanceof DOMText) {
                 $elt = self::getDOMCradle()->createElement('textRun', $elt->wholeText);
             }
             $marshaller = $this->getMarshallerFactory()->createMarshaller($elt);
             $cpt = $marshaller->unmarshall($elt);
             if ($cpt instanceof Stylesheet) {
                 $stylesheets[] = $cpt;
             } else {
                 if ($cpt instanceof FlowStatic && !in_array($cpt->getQtiClassName(), array('hottext', 'feedbackBlock', 'feedbackInline', 'rubricBlock', 'infoControl'))) {
                     $content[] = $cpt;
                 } else {
                     $msg = "The 'rubricBlock' cannot contain '" . $cpt->getQtiClassName() . "' elements.";
                     throw new UnmarshallingException($msg, $element);
                 }
             }
         }
         $object->setStylesheets($stylesheets);
         $object->setContent($content);
         self::fillBodyElement($object, $element);
         return $object;
     } else {
         $msg = "The mandatory attribute 'views' is missing.";
         throw new UnmarshallingException($msg, $element);
     }
 }
 /**
  * Copy all remote resource (absolute URLs to another host) contained in a rubricBlock into a dedicated directory. Remote resources
  * can be refereced by the following QTI classes/attributes:
  * 
  * * a:href
  * * object:data
  * * img:src
  * 
  * @param AssessmentTest $assessmentTest An AssessmentTest object.
  * @throws taoQtiTest_models_classes_QtiTestCompilationFailedException If a remote resource cannot be retrieved.
  */
 protected function copyRemoteResources(RubricBlock $rubricBlock)
 {
     $ds = DIRECTORY_SEPARATOR;
     $tmpDir = tao_helpers_File::createTempDir();
     $destPath = trim($this->getExtraPath(), $ds) . $ds . TAOQTITEST_REMOTE_FOLDER . $ds;
     // Search for all class-attributes in QTI-XML that might reference a remote file.
     $search = $rubricBlock->getComponentsByClassName(array('a', 'object', 'img'));
     foreach ($search as $component) {
         switch ($component->getQtiClassName()) {
             case 'object':
                 $url = $component->getData();
                 break;
             case 'img':
                 $url = $component->getSrc();
                 break;
         }
         if (isset($url) && !preg_match('@^' . ROOT_URL . '@', $url) && !Url::isRelative($url)) {
             $tmpFile = taoItems_helpers_Deployment::retrieveFile($url, $tmpDir);
             if ($tmpFile !== false) {
                 $pathinfo = pathinfo($tmpFile);
                 $handle = fopen($tmpFile, 'r');
                 $this->getPublicDirectory()->writeStream($destPath . $pathinfo['basename'], $handle);
                 fclose($handle);
                 unlink($tmpFile);
                 $newUrl = TAOQTITEST_REMOTE_FOLDER . '/' . $pathinfo['basename'];
                 switch ($component->getQtiClassName()) {
                     case 'object':
                         $component->setData($newUrl);
                         break;
                     case 'img':
                         $component->setSrc($newUrl);
                         break;
                 }
             } else {
                 $msg = "The remote resource referenced by '{$url}' could not be retrieved.";
                 throw new taoQtiTest_models_classes_QtiTestCompilationFailedException($msg, $this->getResource(), taoQtiTest_models_classes_QtiTestCompilationFailedException::REMOTE_RESOURCE);
             }
         }
     }
 }
 /**
  * Copy all remote resource (absolute URLs to another host) contained in a rubricBlock into a dedicated directory. Remote resources
  * can be refereced by the following QTI classes/attributes:
  * 
  * * a:href
  * * object:data
  * * img:src
  * 
  * @param AssessmentTest $assessmentTest An AssessmentTest object.
  * @throws taoQtiTest_models_classes_QtiTestCompilationFailedException If a remote resource cannot be retrieved.
  */
 protected function copyRemoteResources(RubricBlock $rubricBlock)
 {
     $publicCompiledDocDir = $this->getPublicDirectory()->getPath();
     $ds = DIRECTORY_SEPARATOR;
     $destination = $publicCompiledDocDir . trim($this->getExtraPath(), $ds) . $ds . TAOQTITEST_REMOTE_FOLDER . $ds;
     // If remote directory does not exist yet, create it.
     if (file_exists($destination) === false) {
         mkdir($destination, 0770, true);
     }
     // Search for all class-attributes in QTI-XML that might reference a remote file.
     $search = $rubricBlock->getComponentsByClassName(array('a', 'object', 'img'));
     foreach ($search as $component) {
         switch ($component->getQtiClassName()) {
             case 'object':
                 $url = $component->getData();
                 break;
             case 'img':
                 $url = $component->getSrc();
                 break;
         }
         if (isset($url) && !preg_match('@^' . ROOT_URL . '@', $url) && !Url::isRelative($url)) {
             $finalDestination = taoItems_helpers_Deployment::retrieveFile($url, $destination);
             $pathinfo = pathinfo($finalDestination);
             if ($finalDestination !== false) {
                 $newUrl = TAOQTITEST_REMOTE_FOLDER . '/' . $pathinfo['basename'];
                 switch ($component->getQtiClassName()) {
                     case 'object':
                         $component->setData($newUrl);
                         break;
                     case 'img':
                         $component->setSrc($newUrl);
                         break;
                 }
             } else {
                 $msg = "The remote resource referenced by '{$url}' could not be retrieved.";
                 throw new taoQtiTest_models_classes_QtiTestCompilationFailedException($msg, $this->getResource(), taoQtiTest_models_classes_QtiTestCompilationFailedException::REMOTE_RESOURCE);
             }
         }
     }
 }