Skip to content

Commit

Permalink
Ability to print template variables in MathML.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Nov 18, 2015
1 parent 6198a99 commit 9b1cb22
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 12 deletions.
4 changes: 2 additions & 2 deletions bin/qtisdk
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -40,4 +40,4 @@ switch ($module) {
default:
CliTools\err("%RUnknown module '${requestedModuleName}'. Available modules are: " . implode(',', $availableModules) . ".%n");
exit(Cli::EXIT_FAILURE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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;
Expand All @@ -48,6 +49,7 @@
use \SplStack;
use \DOMDocument;
use \DOMDocumentFragment;
use \DOMXPath;

/**
* The base class to be used by any rendering engines.
Expand Down Expand Up @@ -506,7 +508,7 @@ public function render($component, $base = '')
}
}

$finalRendering = $this->createFinalRendering();
$finalRendering = $this->createFinalRendering($component);

return $finalRendering;
}
Expand Down Expand Up @@ -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 <mi> and <ci> 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;
}

Expand Down
32 changes: 29 additions & 3 deletions test/samples/rendering/math_3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,42 @@
<value>E</value>
</correctResponse>
</responseDeclaration>
<templateDeclaration identifier="TPL_E" cardinality="single" baseType="string" mathVariable="true">
<defaultValue>
<value>E</value>
</defaultValue>
</templateDeclaration>
<templateDeclaration identifier="TPL_m" cardinality="single" baseType="string" mathVariable="true">
<defaultValue>
<value>m</value>
</defaultValue>
</templateDeclaration>
<templateDeclaration identifier="TPL_c" cardinality="single" baseType="string" mathVariable="true">
<defaultValue>
<value>c</value>
</defaultValue>
</templateDeclaration>
<templateProcessing>
<setTemplateValue identifier="TPL_E">
<default identifier="TPL_E"/>
</setTemplateValue>
<setTemplateValue identifier="TPL_m">
<default identifier="TPL_m"/>
</setTemplateValue>
<setTemplateValue identifier="TPL_c">
<default identifier="TPL_c"/>
</setTemplateValue>
</templateProcessing>
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float"/>
<itemBody>
<choiceInteraction responseIdentifier="RESPONSE" shuffle="true" maxChoices="1">
<prompt>Which famous scientist is popularly associated with the equation <m:math>
<m:mrow>
<m:mi>E</m:mi>
<m:mi>TPL_E</m:mi>
<m:mo>=</m:mo>
<m:mi>m</m:mi>
<m:mi>TPL_m</m:mi>
<m:msup>
<m:mi>c</m:mi>
<m:mi>TPL_c</m:mi>
<m:mn>2</m:mn>
</m:msup>
</m:mrow>
Expand Down
22 changes: 21 additions & 1 deletion test/scripts/MathRendering3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
echo $rendering->saveXML();

0 comments on commit 9b1cb22

Please sign in to comment.