Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Math | |
0.00% |
0 / 49 |
|
0.00% |
0 / 12 |
600 | |
0.00% |
0 / 1 |
| setMathML | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getMathML | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUsedAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateQtiVariables | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
56 | |||
| getMathNamespace | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| toArray | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getAnnotations | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setAnnotations | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setAnnotation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeAnnotation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAnnotation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| toForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiItem\model\qti; |
| 22 | |
| 23 | /** |
| 24 | * |
| 25 | * @access public |
| 26 | * @author Sam, <sam@taotesting.com> |
| 27 | * @package taoQTI |
| 28 | |
| 29 | */ |
| 30 | class Math extends Element |
| 31 | { |
| 32 | /** |
| 33 | * the QTI tag name as defined in QTI standard |
| 34 | * |
| 35 | * @access protected |
| 36 | * @var string |
| 37 | */ |
| 38 | protected static $qtiTagName = 'math'; |
| 39 | protected $mathML = ''; |
| 40 | protected $annotations = []; |
| 41 | |
| 42 | public function setMathML($mathML) |
| 43 | { |
| 44 | $ns = $this->getMathNamespace(); |
| 45 | //strip the outer math tags, to only store the body |
| 46 | $mathML = preg_replace('/<(\/)?' . ($ns ? $ns . ':' : '') . 'math/is', '', $mathML); |
| 47 | if ($ns) { |
| 48 | //strip ns usage, to store raw mathML |
| 49 | $mathML = preg_replace('/<(\/)?/is', '<$1', $mathML); |
| 50 | } |
| 51 | $this->mathML = $mathML; |
| 52 | } |
| 53 | |
| 54 | public function getMathML() |
| 55 | { |
| 56 | return $this->mathML; |
| 57 | } |
| 58 | |
| 59 | protected function getUsedAttributes() |
| 60 | { |
| 61 | return []; |
| 62 | } |
| 63 | |
| 64 | protected function getTemplateQtiVariables() |
| 65 | { |
| 66 | |
| 67 | $variables = parent::getTemplateQtiVariables(); |
| 68 | |
| 69 | $tag = static::$qtiTagName; |
| 70 | $body = $this->mathML; |
| 71 | |
| 72 | //render annotation: |
| 73 | $annotations = ''; |
| 74 | foreach ($this->annotations as $encoding => $value) { |
| 75 | $annotations .= '<annotation encoding="' . $encoding . '">' . $value . '</annotation>'; |
| 76 | } |
| 77 | |
| 78 | if (!empty($annotations)) { |
| 79 | if (strpos($body, '</semantics>')) { |
| 80 | $body = str_replace('</semantics>', $annotations . '</semantics>', $body); |
| 81 | } else { |
| 82 | $body = '<semantics>' . $body . $annotations . '</semantics>'; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | //search existing mathML ns declaration: |
| 87 | $ns = $this->getMathNamespace(); |
| 88 | if (empty($ns)) { |
| 89 | //add one! |
| 90 | $relatedItem = $this->getRelatedItem(); |
| 91 | if (!is_null($relatedItem)) { |
| 92 | $ns = 'm'; |
| 93 | $relatedItem->addNamespace($ns, 'http://www.w3.org/1998/Math/MathML'); |
| 94 | } |
| 95 | } |
| 96 | if (!empty($ns)) { |
| 97 | //proceed to ns addition: |
| 98 | $body = preg_replace('/<(\/)?([^!])/', '<$1' . $ns . ':$2', $body); |
| 99 | $tag = $ns . ':' . $tag; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | $variables['tag'] = $tag; |
| 104 | $variables['body'] = $body; |
| 105 | |
| 106 | return $variables; |
| 107 | } |
| 108 | |
| 109 | public function getMathNamespace() |
| 110 | { |
| 111 | $ns = ''; |
| 112 | $relatedItem = $this->getRelatedItem(); |
| 113 | if (!is_null($relatedItem)) { |
| 114 | foreach ($relatedItem->getNamespaces() as $name => $uri) { |
| 115 | if (strpos($uri, 'MathML') > 0) { |
| 116 | $ns = $name; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return $ns; |
| 122 | } |
| 123 | |
| 124 | public function toArray($filterVariableContent = false, &$filtered = []) |
| 125 | { |
| 126 | $data = parent::toArray($filterVariableContent, $filtered); |
| 127 | $data['mathML'] = $this->mathML; |
| 128 | $data['annotations'] = $this->annotations; |
| 129 | return $data; |
| 130 | } |
| 131 | |
| 132 | public function getAnnotations() |
| 133 | { |
| 134 | return $this->annotations; |
| 135 | } |
| 136 | |
| 137 | public function setAnnotations($annotations) |
| 138 | { |
| 139 | $this->annotations = $annotations; |
| 140 | } |
| 141 | |
| 142 | public function setAnnotation($encoding, $value) |
| 143 | { |
| 144 | $this->annotations[$encoding] = $value; |
| 145 | } |
| 146 | |
| 147 | public function removeAnnotation($encoding) |
| 148 | { |
| 149 | unset($this->annotations[$encoding]); |
| 150 | } |
| 151 | |
| 152 | public function getAnnotation($encoding) |
| 153 | { |
| 154 | return isset($this->annotations[$encoding]) ? $this->annotations[$encoding] : ''; |
| 155 | } |
| 156 | |
| 157 | public function toForm() |
| 158 | { |
| 159 | $formContainer = new Math($this); |
| 160 | return $formContainer->getForm(); |
| 161 | } |
| 162 | } |