Exemplo n.º 1
0
 /**
  * @see \qtism\runtime\rendering\markup\xhtml\AbstractXhtmlRenderer::appendChildren()
  */
 protected function appendChildren(DOMDocumentFragment $fragment, QtiComponent $component, $base = '')
 {
     parent::appendChildren($fragment, $component, $base);
     // Retrieve the two rendered simpleMatchSets and shuffle if needed.
     $currentSet = 0;
     $choiceElts = array();
     $simpleMatchSetElts = array();
     for ($i = 0; $i < $fragment->firstChild->childNodes->length; $i++) {
         $n = $fragment->firstChild->childNodes->item($i);
         if (Utils::hasClass($n, 'qti-simpleMatchSet') === true) {
             $simpleMatchSetElts[] = $n;
             $sets = $component->getSimpleMatchSets();
             if ($this->getRenderingEngine()->getShufflingPolicy() === AbstractMarkupRenderingEngine::CONTEXT_AWARE && $component->mustShuffle()) {
                 Utils::shuffle($n, new ShufflableCollection($sets[$currentSet]->getSimpleAssociableChoices()->getArrayCopy()));
             }
             // Retrieve the two content of the two simpleMatchSets, separately.
             $choiceElts[] = Marshaller::getChildElementsByTagName($n, 'li');
             $currentSet++;
         }
     }
     // simpleMatchSet class cannot be rendered into a table :/
     foreach ($simpleMatchSetElts as $sms) {
         $fragment->firstChild->removeChild($sms);
     }
     $table = $fragment->ownerDocument->createElement('table');
     $fragment->firstChild->appendChild($table);
     // Build the table header.
     $tr = $fragment->ownerDocument->createElement('tr');
     $table->appendChild($tr);
     // Empty upper left cell.
     $tr->appendChild($fragment->ownerDocument->createElement('th'));
     $ifVerticalStatementsStorage = array();
     for ($i = 0; $i < count($choiceElts[1]); $i++) {
         $ifStatements = Utils::extractStatements($choiceElts[1][$i], Utils::EXTRACT_IF);
         $incStatements = Utils::extractStatements($choiceElts[1][$i], Utils::EXTRACT_INCLUDE);
         if (empty($incStatements) === false) {
             $tr->appendChild($incStatements[0]);
         }
         if (empty($ifStatements) === false) {
             $ifVerticalStatementsStorage[$i] = $ifStatements;
             $tr->appendChild($ifStatements[0]);
         }
         $th = XmlUtils::changeElementName($choiceElts[1][$i], 'th');
         $tr->appendChild($th);
         if (empty($incStatements) === false) {
             $th->parentNode->insertBefore($incStatements[1], $th->nextSibling);
         }
         if (empty($ifStatements) === false) {
             $th->parentNode->insertBefore($ifStatements[1], $th->nextSibling);
         }
     }
     // Build all remaining rows.
     for ($i = 0; $i < count($choiceElts[0]); $i++) {
         $tr = $fragment->ownerDocument->createElement('tr');
         $ifStatements = Utils::extractStatements($choiceElts[0][$i], Utils::EXTRACT_IF);
         $incStatements = Utils::extractStatements($choiceElts[0][$i], Utils::EXTRACT_INCLUDE);
         $th = XmlUtils::changeElementName($choiceElts[0][$i], 'th');
         $tr->appendChild($th);
         $table->appendChild($tr);
         if (empty($incStatements) === false) {
             $tr->parentNode->insertBefore($incStatements[0], $tr);
         }
         if (empty($ifStatements) === false) {
             $tr->parentNode->insertBefore($ifStatements[0], $tr);
         }
         for ($j = 0; $j < count($choiceElts[1]); $j++) {
             $input = $fragment->ownerDocument->createElement('input');
             $input->setAttribute('type', 'checkbox');
             $td = $fragment->ownerDocument->createElement('td');
             $td->appendChild($input);
             $tr->appendChild($td);
             if (isset($ifVerticalStatementsStorage[$j])) {
                 $td->parentNode->insertBefore($ifVerticalStatementsStorage[$j][0]->cloneNode(), $td);
                 $td->parentNode->insertBefore($ifVerticalStatementsStorage[$j][1]->cloneNode(), $td->nextSibling);
             }
         }
         if (empty($incStatements) === false && isset($td)) {
             $tr->parentNode->insertBefore($incStatements[1], $tr->nextSibling);
         }
         if (empty($ifStatements) === false && isset($td)) {
             $tr->parentNode->insertBefore($ifStatements[1], $tr->nextSibling);
         }
     }
 }
Exemplo n.º 2
0
 /**
  * @see \qtism\runtime\rendering\markup\xhtml\AbstractXhtmlRenderer::appendChildren()
  */
 protected function appendChildren(DOMDocumentFragment $fragment, QtiComponent $component, $base = '')
 {
     parent::appendChildren($fragment, $component, $base);
     if ($this->getRenderingEngine()->getShufflingPolicy() === AbstractMarkupRenderingEngine::CONTEXT_AWARE && $component->mustShuffle() === true) {
         Utils::shuffle($fragment->firstChild, new ShufflableCollection($component->getSimpleChoices()->getArrayCopy()));
     }
     // Put the choice elements into an unordered list.
     // Dev note: it seems we need a trick ... http://php.net/manual/en/domnode.removechild.php#90292
     $choiceElts = $fragment->firstChild->getElementsByTagName('li');
     $choiceQueue = array();
     $ulElt = $fragment->ownerDocument->createElement('ul');
     foreach ($choiceElts as $choiceElt) {
         $choiceQueue[] = $choiceElt;
     }
     foreach ($choiceQueue as $choiceElt) {
         $ifStatements = Utils::extractStatements($choiceElt, Utils::EXTRACT_IF);
         $incStatements = Utils::extractStatements($choiceElt, Utils::EXTRACT_INCLUDE);
         $fragment->firstChild->removeChild($choiceElt);
         $ulElt->appendChild($choiceElt);
         // Re-append qtism-include/qtism-endinclude.
         $statements = Utils::extractStatements($choiceElt, Utils::EXTRACT_INCLUDE);
         if (empty($incStatements) === false) {
             $choiceElt->parentNode->insertBefore($incStatements[0], $choiceElt);
             $choiceElt->parentNode->insertBefore($incStatements[1], $choiceElt->nextSibling);
         }
         // Re-append qtism-if/qtism-endif.
         if (empty($ifStatements) === false) {
             $choiceElt->parentNode->insertBefore($ifStatements[0], $choiceElt);
             $choiceElt->parentNode->insertBefore($ifStatements[1], $choiceElt->nextSibling);
         }
     }
     $fragment->firstChild->appendChild($ulElt);
 }
Exemplo n.º 3
0
 public function testExtractStatementsIfOnly()
 {
     $dom = new DOMDocument('1.0', 'UTF-8');
     $node = $dom->createElement('fakenode');
     $dom->appendChild($node);
     $div = $dom->createElement('div');
     $node->appendChild($div);
     $if = $dom->createComment('qtism-if (true)');
     $node->insertBefore($if, $div);
     $statements = Utils::extractStatements($div);
     $this->assertEquals(array(), $statements);
 }
 /**
  * @see \qtism\runtime\rendering\markup\xhtml\AbstractXhtmlRenderer::appendChildren()
  */
 protected function appendChildren(DOMDocumentFragment $fragment, QtiComponent $component, $base = '')
 {
     parent::appendChildren($fragment, $component, $base);
     if ($this->getRenderingEngine()->getShufflingPolicy() === AbstractMarkupRenderingEngine::CONTEXT_AWARE && $component->mustShuffle() === true) {
         Utils::shuffle($fragment->firstChild, new ShufflableCollection($component->getSimpleAssociableChoices()->getArrayCopy()));
     }
     // Put the choice elements into an unordered list.
     // Dev note: it seems we need a trick ... http://php.net/manual/en/domnode.removechild.php#90292
     // @dev Bwaaaah copy/paste!
     $choiceElts = $fragment->firstChild->getElementsByTagName('li');
     $choiceQueue = array();
     $ulElt = $fragment->ownerDocument->createElement('ul');
     foreach ($choiceElts as $choiceElt) {
         $choiceQueue[] = $choiceElt;
     }
     foreach ($choiceQueue as $choiceElt) {
         $ifStatements = Utils::extractStatements($choiceElt, Utils::EXTRACT_IF);
         $incStatements = Utils::extractStatements($choiceElt, Utils::EXTRACT_INCLUDE);
         $fragment->firstChild->removeChild($choiceElt);
         $ulElt->appendChild($choiceElt);
         if (empty($incStatements) === false) {
             $choiceElt->parentNode->insertBefore($incStatements[0], $choiceElt);
             $choiceElt->parentNode->insertBefore($incStatements[1], $choiceElt->nextSibling);
         }
         if (empty($ifStatements) === false) {
             $choiceElt->parentNode->insertBefore($ifStatements[0], $choiceElt);
             $choiceElt->parentNode->insertBefore($ifStatements[1], $choiceElt->nextSibling);
         }
     }
     $fragment->firstChild->appendChild($ulElt);
     // The number of possible associations to display is maxAssociations if the attribute is present and different from 0, otherwise:
     //
     // * minAssociations, if different from 0 is used to determine the possible associations to display. Otherwise,
     // * a single possible association is displayed. Actions to undertake when this first association is done by the candidate depends on the implementation.
     // QUESTION: Should we delegate that to implementers decisions i.e. JS libraries to generate as they whish?
     // Below is commented code of such a generation directly in the markup...
     // At the present time, my feeling is to delegate ...
     $nbAssoc = ($assoc = $component->getMaxAssociations()) > 0 ? $assoc : (($assoc = $component->getMinAssociations()) > 0 ? $assoc : 1);
     for ($i = 0; $i < $nbAssoc; $i++) {
         $associationElt = $fragment->ownerDocument->createElement('div');
         $associationElt->setAttribute('class', 'qti-association');
         // A container for the first selected option...
         $firstElt = $fragment->ownerDocument->createElement('span');
         $firstElt->setAttribute('class', 'qti-association-first');
         $associationElt->appendChild($firstElt);
         // And a second container for the second selected option.
         $secondElt = $fragment->ownerDocument->createElement('span');
         $secondElt->setAttribute('class', 'qti-association-second');
         $associationElt->appendChild($secondElt);
         $fragment->firstChild->appendChild($associationElt);
     }
 }