/** * Check if $value is a valid View enumeration value. * * @throws InvalidArgumentException If $value is not a valid View enumeration value. */ protected function checkType($value) { if (!in_array($value, View::asArray())) { $msg = "The ViewsCollection class only accept View enumeration values, '{$value}' given."; throw new InvalidArgumentException($msg); } }
<?php use qtism\runtime\rendering\markup\AbstractMarkupRenderingEngine; use qtism\data\View; use qtism\data\ViewCollection; use qtism\data\storage\xml\XmlDocument; use qtism\runtime\rendering\markup\xhtml\XhtmlRenderingEngine; require_once dirname(__FILE__) . '/../../qtism/qtism.php'; $doc = new XmlDocument(); $doc->load('../samples/rendering/rubricblock_1.xml'); $renderer = new XhtmlRenderingEngine(); if (isset($argv[1])) { if (strpos($argv[1], ',') !== false) { $strviews = explode(',', $argv[1]); $view = new ViewCollection(); foreach ($strviews as $v) { $view[] = View::getConstantByName(trim($v)); } } else { $view = new ViewCollection(array(View::getConstantByName(trim($argv[1])))); } $renderer->setViewPolicy(AbstractMarkupRenderingEngine::CONTEXT_AWARE); $renderer->setViews($view); } $rendering = $renderer->render($doc->getDocumentComponent()); $rendering->formatOutput = true; echo $rendering->saveXML();
/** * Instantiate and fill a QtiComponentCollection * * @param ReflectionClass $class * @param array $values * @return \qtism\data\QtiComponentCollection|null */ private function createComponentCollection(ReflectionClass $class, $values) { $collection = $class->newInstance(); if ($collection instanceof ViewCollection) { foreach ($values as $value) { $collection[] = View::getConstantByName($value); } return $collection; } if ($collection instanceof QtiComponentCollection) { foreach ($values as $value) { $collection->attach($this->arrayToComponent($value, null, false)); } return $collection; } if ($collection instanceof IntegerCollection || $collection instanceof StringCollection) { foreach ($values as $value) { $collection[] = $value; } return $collection; } return null; }
/** * Unmarshall a DOMElement object corresponding to a QTI outcomeDeclaration element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent An OutcomeDeclaration object. * @throws UnmarshallingException */ protected function unmarshall(DOMElement $element) { try { $baseComponent = parent::unmarshall($element); $object = new OutcomeDeclaration($baseComponent->getIdentifier()); $object->setBaseType($baseComponent->getBaseType()); $object->setCardinality($baseComponent->getCardinality()); $object->setDefaultValue($baseComponent->getDefaultValue()); // deal with views. if (($views = static::getDOMElementAttributeAs($element, 'view')) != null) { $viewCollection = new ViewCollection(); foreach (explode(" ", $views) as $viewName) { $viewCollection[] = View::getConstantByName($viewName); } $object->setViews($viewCollection); } // deal with interpretation. if (($interpretation = static::getDOMElementAttributeAs($element, 'interpretation')) != null) { $object->setInterpretation($interpretation); } // deal with longInterpretation. if (($longInterpretation = static::getDOMElementAttributeAs($element, 'longInterpretation')) != null) { $object->setLongInterpretation($longInterpretation); } // deal with normalMaximum. if (($normalMaximum = static::getDOMElementAttributeAs($element, 'normalMaximum', 'float')) !== null) { $object->setNormalMaximum($normalMaximum); } // deal with normalMinimum. if (($normalMinimum = static::getDOMElementAttributeAs($element, 'normalMinimum', 'float')) !== null) { $object->setNormalMinimum($normalMinimum); } // deal with matseryValue. if (($masteryValue = static::getDOMElementAttributeAs($element, 'masteryValue', 'float')) !== null) { $object->setMasteryValue($masteryValue); } // deal with lookupTable. $interpolationTables = $element->getElementsByTagName('interpolationTable'); $matchTable = $element->getElementsByTagName('matchTable'); if ($interpolationTables->length == 1 || $matchTable->length == 1) { // we have a lookupTable defined. $lookupTable = null; if ($interpolationTables->length == 1) { $lookupTable = $interpolationTables->item(0); } else { $lookupTable = $matchTable->item(0); } $lookupTableMarshaller = $this->getMarshallerFactory()->createMarshaller($lookupTable, array($object->getBaseType())); $object->setLookupTable($lookupTableMarshaller->unmarshall($lookupTable)); } return $object; } catch (InvalidArgumentException $e) { $msg = "An unexpected error occured while unmarshalling the outcomeDeclaration."; throw new UnmarshallingException($msg, $element, $e); } }
/** * 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); } }