From 9b1cb229abb87917cb1199c875b520e772f8c2ff Mon Sep 17 00:00:00 2001 From: = Date: Wed, 18 Nov 2015 18:00:57 +0100 Subject: [PATCH] Ability to print template variables in MathML. --- bin/qtisdk | 4 +- .../markup/AbstractMarkupRenderingEngine.php | 57 +++++++++++++++++-- test/samples/rendering/math_3.xml | 32 ++++++++++- test/scripts/MathRendering3.php | 22 ++++++- 4 files changed, 103 insertions(+), 12 deletions(-) diff --git a/bin/qtisdk b/bin/qtisdk index cf717e75f..1f34887cf 100644 --- a/bin/qtisdk +++ b/bin/qtisdk @@ -31,7 +31,7 @@ CliTools\Colors::enable(false); // Execute main entry point. $availableModules = array('render'); -$requestedModuleName = $argv[1]; +$requestedModuleName = (isset($argv[1])) ? $argv[1] : ''; $module = strtolower($requestedModuleName); switch ($module) { case 'render': @@ -40,4 +40,4 @@ switch ($module) { default: CliTools\err("%RUnknown module '${requestedModuleName}'. Available modules are: " . implode(',', $availableModules) . ".%n"); exit(Cli::EXIT_FAILURE); -} \ No newline at end of file +} diff --git a/src/qtism/runtime/rendering/markup/AbstractMarkupRenderingEngine.php b/src/qtism/runtime/rendering/markup/AbstractMarkupRenderingEngine.php index 0c77be79d..0f145e4b9 100644 --- a/src/qtism/runtime/rendering/markup/AbstractMarkupRenderingEngine.php +++ b/src/qtism/runtime/rendering/markup/AbstractMarkupRenderingEngine.php @@ -14,23 +14,24 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * Copyright (c) 2013-2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); + * Copyright (c) 2013-2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); * * @author Jérôme Bogaerts * @license GPLv2 * - * - * */ namespace qtism\runtime\rendering\markup; +use qtism\data\AssessmentItem; +use qtism\data\content\PrintedVariable; use qtism\data\content\interactions\Gap; use qtism\common\collections\Container; use qtism\common\datatypes\QtiIdentifier; use qtism\common\datatypes\QtiScalar; use qtism\runtime\rendering\RenderingException; use qtism\runtime\rendering\Renderable; +use qtism\runtime\rendering\markup\xhtml\PrintedVariableRenderer; use qtism\data\content\ModalFeedback; use qtism\data\content\interactions\Interaction; use qtism\common\utils\Url; @@ -48,6 +49,7 @@ use \SplStack; use \DOMDocument; use \DOMDocumentFragment; +use \DOMXPath; /** * The base class to be used by any rendering engines. @@ -506,7 +508,7 @@ public function render($component, $base = '') } } - $finalRendering = $this->createFinalRendering(); + $finalRendering = $this->createFinalRendering($component); return $finalRendering; } @@ -555,18 +557,61 @@ protected function markAsExplored(QtiComponent $component) } /** - * Create the final rendering as it must be rendered by the final + * Create the final rendering of the rendered $component as it must be rendered by the final * implementation. * * @return mixed */ - protected function createFinalRendering() + protected function createFinalRendering(QtiComponent $component) { $dom = $this->getDocument(); if (($last = $this->getLastRendering()) !== null) { $dom->appendChild($last); } + // If we are rendering an item, let's try to find some Math to + // for template variable mapping. + if ($component instanceof AssessmentItem && count($templateDeclarations = $component->getTemplateDeclarations()) > 0) { + $xpath = new DOMXPath($dom); + $xpath->registerNamespace('m', 'http://www.w3.org/1998/Math/MathML'); + $maths = $xpath->query('//m:math|//math'); + + if ($maths->length > 0) { + $printedVariableRenderer = new PrintedVariableRenderer($this); + + foreach ($templateDeclarations as $templateDeclaration) { + if ($templateDeclaration->isMathVariable() === true) { + $templateIdentifier = $templateDeclaration->getIdentifier(); + + foreach ($maths as $math) { + // Find and elements. + foreach ($xpath->query(".//m:mi[text() = '${templateIdentifier}']|.//m:ci[text() = '${templateIdentifier}']|.//mi[text() = '${templateIdentifier}']|.//ci[text() = '${templateIdentifier}']", $math) as $mathElement) { + $localElementName = ($mathElement->localName === 'mi') ? 'mn' : 'cn'; + $newMathElement = $mathElement->ownerDocument->createElement($localElementName); + $printedVariable = new PrintedVariable($templateIdentifier); + $printedVariableFragment = $printedVariableRenderer->render($printedVariable); + + if ($this->getPrintedVariablePolicy() === self::CONTEXT_STATIC) { + foreach ($printedVariableFragment->childNodes as $node) { + $newMathElement->appendChild($node); + $mathElement->parentNode->replaceChild($newMathElement, $mathElement); + } + } else if ($this->getPrintedVariablePolicy() === self::CONTEXT_AWARE) { + $newMathElement->appendChild($newMathElement->ownerDocument->createTextNode($printedVariableFragment->firstChild->nodeValue)); + $mathElement->parentNode->replaceChild($newMathElement, $mathElement); + } else { + foreach ($printedVariableFragment->firstChild->childNodes as $node) { + $newMathElement->appendChild($node); + $mathElement->parentNode->replaceChild($newMathElement, $mathElement); + } + } + } + } + } + } + } + } + return $dom; } diff --git a/test/samples/rendering/math_3.xml b/test/samples/rendering/math_3.xml index 770afe813..bdd1512ae 100644 --- a/test/samples/rendering/math_3.xml +++ b/test/samples/rendering/math_3.xml @@ -11,16 +11,42 @@ E + + + E + + + + + m + + + + + c + + + + + + + + + + + + + Which famous scientist is popularly associated with the equation - E + TPL_E = - m + TPL_m - c + TPL_c 2 diff --git a/test/scripts/MathRendering3.php b/test/scripts/MathRendering3.php index 082edf3de..ab7e8c8d8 100644 --- a/test/scripts/MathRendering3.php +++ b/test/scripts/MathRendering3.php @@ -2,14 +2,34 @@ use qtism\data\storage\xml\XmlDocument; use qtism\runtime\rendering\markup\xhtml\XhtmlRenderingEngine; +use qtism\runtime\common\State; +use qtism\runtime\common\TemplateVariable; +use qtism\common\enums\BaseType; +use qtism\common\enums\Cardinality; +use qtism\common\datatypes\QtiString; require_once(dirname(__FILE__) . '/../../vendor/autoload.php'); $doc = new XmlDocument(); $doc->load(dirname(__FILE__) . '/../samples/rendering/math_3.xml'); +$tpl_E = new TemplateVariable('TPL_E', Cardinality::SINGLE, BaseType::STRING, new QtiString('E')); +$tpl_m = new TemplateVariable('TPL_m', Cardinality::SINGLE, BaseType::STRING, new QtiString('m')); +$tpl_c = new TemplateVariable('TPL_c', Cardinality::SINGLE, BaseType::STRING, new QtiString('c')); + $renderer = new XhtmlRenderingEngine(); +$renderer->setState( + new State( + array( + $tpl_E, + $tpl_m, + $tpl_c + ) + ) +); + +$renderer->setPrintedVariablePolicy(XhtmlRenderingEngine::TEMPLATE_ORIENTED); $rendering = $renderer->render($doc->getDocumentComponent()); $rendering->formatOutput = true; -echo $rendering->saveXML(); \ No newline at end of file +echo $rendering->saveXML();