Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 65 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| QtiSerializer | |
0.00% |
0 / 65 |
|
0.00% |
0 / 9 |
600 | |
0.00% |
0 / 1 |
| parseElementXml | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| parseExpressionXml | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| parseResponseRuleXml | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| parseResponseRulesContainerXml | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| parseResponseProcessingFragmentXml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parseResponseIfXml | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| parseResponseElseXml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parseResponseConditionXml | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| parseResponseProcessingXml | |
0.00% |
0 / 1 |
|
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) 2015 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\helpers; |
| 23 | |
| 24 | use SimpleXMLElement; |
| 25 | |
| 26 | /** |
| 27 | * @access public |
| 28 | * @package taoQtiItem |
| 29 | */ |
| 30 | class QtiSerializer |
| 31 | { |
| 32 | /** |
| 33 | * Parse a generic QTI element node into an array |
| 34 | * |
| 35 | * @param SimpleXMLElement $xml |
| 36 | * @return array |
| 37 | */ |
| 38 | public static function parseElementXml(SimpleXMLElement $xml) |
| 39 | { |
| 40 | |
| 41 | $attributes = []; |
| 42 | foreach ($xml->attributes() as $name => $value) { |
| 43 | $attributes[$name] = (string) $value; |
| 44 | } |
| 45 | |
| 46 | $returnValue = [ |
| 47 | 'qtiClass' => $xml->getName() |
| 48 | ]; |
| 49 | |
| 50 | if (count($attributes)) { |
| 51 | $returnValue['attributes'] = $attributes; |
| 52 | } |
| 53 | |
| 54 | return $returnValue; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Parse a QTI expression node into an array |
| 59 | * |
| 60 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10569 |
| 61 | * @param SimpleXMLElement $xml |
| 62 | * @return array |
| 63 | */ |
| 64 | public static function parseExpressionXml(SimpleXMLElement $xml) |
| 65 | { |
| 66 | $returnValue = self::parseElementXml($xml); |
| 67 | $value = trim($xml); |
| 68 | $expressions = []; |
| 69 | foreach ($xml->children() as $child) { |
| 70 | $expressions[] = self::parseExpressionXml($child); |
| 71 | } |
| 72 | if (count($expressions)) { |
| 73 | $returnValue['expressions'] = $expressions; |
| 74 | } |
| 75 | if (strlen($value)) { |
| 76 | $returnValue['value'] = $value; |
| 77 | } |
| 78 | return $returnValue; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Parse a QTI responseRule node into an array |
| 83 | * |
| 84 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10408 |
| 85 | * @param SimpleXMLElement $xml |
| 86 | * @return array |
| 87 | */ |
| 88 | public static function parseResponseRuleXml(SimpleXMLElement $xml) |
| 89 | { |
| 90 | $returnValue = self::parseElementXml($xml); |
| 91 | foreach ($xml->children() as $child) { |
| 92 | $returnValue['expression'] = self::parseExpressionXml($child); |
| 93 | break; |
| 94 | } |
| 95 | return $returnValue; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Parse a generic QTI element node that contains responseRules as children into an array |
| 100 | * |
| 101 | * @param SimpleXMLElement $xml |
| 102 | * @return array |
| 103 | */ |
| 104 | private static function parseResponseRulesContainerXml(SimpleXMLElement $xml) |
| 105 | { |
| 106 | $returnValue = self::parseElementXml($xml); |
| 107 | $responseRules = []; |
| 108 | foreach ($xml->children() as $child) { |
| 109 | $name = $child->getName(); |
| 110 | $methodName = 'parse' . ucfirst($name) . 'Xml'; |
| 111 | |
| 112 | if (method_exists(__CLASS__, $methodName)) { |
| 113 | $responseRules[] = self::$methodName($child); |
| 114 | } else { |
| 115 | $responseRules[] = self::parseResponseRuleXml($child); |
| 116 | } |
| 117 | } |
| 118 | $returnValue['responseRules'] = $responseRules; |
| 119 | return $returnValue; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Parse a QTI responseProcessing node into an array |
| 124 | * |
| 125 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10712 |
| 126 | * @param SimpleXMLElement $xml |
| 127 | * @return array |
| 128 | */ |
| 129 | public static function parseResponseProcessingFragmentXml(SimpleXMLElement $xml) |
| 130 | { |
| 131 | return self::parseResponseRulesContainerXml($xml); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Parse a QTI responseProcessing node into an array |
| 136 | * |
| 137 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10413 |
| 138 | * @param SimpleXMLElement $xml |
| 139 | * @return array |
| 140 | */ |
| 141 | public static function parseResponseIfXml(SimpleXMLElement $xml) |
| 142 | { |
| 143 | $returnValue = self::parseElementXml($xml); |
| 144 | $i = 0; |
| 145 | $expression = null; |
| 146 | $responseRules = []; |
| 147 | foreach ($xml->children() as $child) { |
| 148 | if ($i) { |
| 149 | $name = $child->getName(); |
| 150 | $methodName = 'parse' . ucfirst($name) . 'Xml'; |
| 151 | if (method_exists(__CLASS__, $methodName)) { |
| 152 | $responseRules[] = self::$methodName($child); |
| 153 | } else { |
| 154 | $responseRules[] = self::parseResponseRuleXml($child); |
| 155 | } |
| 156 | } else { |
| 157 | //the first child is the expression |
| 158 | $expression = self::parseExpressionXml($child); |
| 159 | } |
| 160 | $i++; |
| 161 | } |
| 162 | $returnValue['expression'] = $expression; |
| 163 | $returnValue['responseRules'] = $responseRules; |
| 164 | return $returnValue; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Parse a QTI responseProcessing node into an array |
| 169 | * |
| 170 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10419 |
| 171 | * @param SimpleXMLElement $xml |
| 172 | * @return array |
| 173 | */ |
| 174 | public static function parseResponseElseXml(SimpleXMLElement $xml) |
| 175 | { |
| 176 | return self::parseResponseRulesContainerXml($xml); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Parse a QTI responseProcessing node into an array |
| 181 | * |
| 182 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10409 |
| 183 | * @param SimpleXMLElement $xml |
| 184 | * @return array |
| 185 | */ |
| 186 | public static function parseResponseConditionXml(SimpleXMLElement $xml) |
| 187 | { |
| 188 | $returnValue = self::parseElementXml($xml); |
| 189 | foreach ($xml->responseIf as $responseIfXml) { |
| 190 | $returnValue['responseIf'] = self::parseResponseIfXml($responseIfXml); |
| 191 | break; |
| 192 | } |
| 193 | foreach ($xml->responseElseIf as $responseIfXml) { |
| 194 | if (!isset($returnValue['responseElseIfs'])) { |
| 195 | $returnValue['responseElseIfs'] = []; |
| 196 | } |
| 197 | $returnValue['responseElseIfs'][] = self::parseResponseIfXml($responseIfXml); |
| 198 | } |
| 199 | foreach ($xml->responseElse as $responseIfXml) { |
| 200 | $returnValue['responseElse'] = self::parseResponseElseXml($responseIfXml); |
| 201 | break; |
| 202 | } |
| 203 | return $returnValue; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Parse a QTI responseProcessing node into an array |
| 208 | * |
| 209 | * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10404 |
| 210 | * @param SimpleXMLElement $xml |
| 211 | * @return array |
| 212 | */ |
| 213 | public static function parseResponseProcessingXml(SimpleXMLElement $xml) |
| 214 | { |
| 215 | return self::parseResponseRulesContainerXml($xml); |
| 216 | } |
| 217 | } |