* @package taoQTI
*/
class Math extends Element
{
/**
* the QTI tag name as defined in QTI standard
*
* @access protected
* @var string
*/
protected static $qtiTagName = 'math';
protected $mathML = '';
protected $annotations = [];
public function setMathML($mathML)
{
$ns = $this->getMathNamespace();
//strip the outer math tags, to only store the body
$mathML = preg_replace('/<(\/)?' . ($ns ? $ns . ':' : '') . 'math/is', '', $mathML);
if ($ns) {
//strip ns usage, to store raw mathML
$mathML = preg_replace('/<(\/)?/is', '<$1', $mathML);
}
$this->mathML = $mathML;
}
public function getMathML()
{
return $this->mathML;
}
protected function getUsedAttributes()
{
return [];
}
protected function getTemplateQtiVariables()
{
$variables = parent::getTemplateQtiVariables();
$tag = static::$qtiTagName;
$body = $this->mathML;
//render annotation:
$annotations = '';
foreach ($this->annotations as $encoding => $value) {
$annotations .= '' . $value . '';
}
if (!empty($annotations)) {
if (strpos($body, '')) {
$body = str_replace('', $annotations . '', $body);
} else {
$body = '' . $body . $annotations . '';
}
}
//search existing mathML ns declaration:
$ns = $this->getMathNamespace();
if (empty($ns)) {
//add one!
$relatedItem = $this->getRelatedItem();
if (!is_null($relatedItem)) {
$ns = 'm';
$relatedItem->addNamespace($ns, 'http://www.w3.org/1998/Math/MathML');
}
}
if (!empty($ns)) {
//proceed to ns addition:
$body = preg_replace('/<(\/)?([^!])/', '<$1' . $ns . ':$2', $body);
$tag = $ns . ':' . $tag;
}
$variables['tag'] = $tag;
$variables['body'] = $body;
return $variables;
}
public function getMathNamespace()
{
$ns = '';
$relatedItem = $this->getRelatedItem();
if (!is_null($relatedItem)) {
foreach ($relatedItem->getNamespaces() as $name => $uri) {
if (strpos($uri, 'MathML') > 0) {
$ns = $name;
break;
}
}
}
return $ns;
}
public function toArray($filterVariableContent = false, &$filtered = [])
{
$data = parent::toArray($filterVariableContent, $filtered);
$data['mathML'] = $this->mathML;
$data['annotations'] = $this->annotations;
return $data;
}
public function getAnnotations()
{
return $this->annotations;
}
public function setAnnotations($annotations)
{
$this->annotations = $annotations;
}
public function setAnnotation($encoding, $value)
{
$this->annotations[$encoding] = $value;
}
public function removeAnnotation($encoding)
{
unset($this->annotations[$encoding]);
}
public function getAnnotation($encoding)
{
return isset($this->annotations[$encoding]) ? $this->annotations[$encoding] : '';
}
public function toForm()
{
$formContainer = new Math($this);
return $formContainer->getForm();
}
}