attributes() as $name => $value) { $attributes[$name] = (string) $value; } $returnValue = [ 'qtiClass' => $xml->getName() ]; if (count($attributes)) { $returnValue['attributes'] = $attributes; } return $returnValue; } /** * Parse a QTI expression node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10569 * @param SimpleXMLElement $xml * @return array */ public static function parseExpressionXml(SimpleXMLElement $xml) { $returnValue = self::parseElementXml($xml); $value = trim($xml); $expressions = []; foreach ($xml->children() as $child) { $expressions[] = self::parseExpressionXml($child); } if (count($expressions)) { $returnValue['expressions'] = $expressions; } if (strlen($value)) { $returnValue['value'] = $value; } return $returnValue; } /** * Parse a QTI responseRule node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10408 * @param SimpleXMLElement $xml * @return array */ public static function parseResponseRuleXml(SimpleXMLElement $xml) { $returnValue = self::parseElementXml($xml); foreach ($xml->children() as $child) { $returnValue['expression'] = self::parseExpressionXml($child); break; } return $returnValue; } /** * Parse a generic QTI element node that contains responseRules as children into an array * * @param SimpleXMLElement $xml * @return array */ private static function parseResponseRulesContainerXml(SimpleXMLElement $xml) { $returnValue = self::parseElementXml($xml); $responseRules = []; foreach ($xml->children() as $child) { $name = $child->getName(); $methodName = 'parse' . ucfirst($name) . 'Xml'; if (method_exists(__CLASS__, $methodName)) { $responseRules[] = self::$methodName($child); } else { $responseRules[] = self::parseResponseRuleXml($child); } } $returnValue['responseRules'] = $responseRules; return $returnValue; } /** * Parse a QTI responseProcessing node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10712 * @param SimpleXMLElement $xml * @return array */ public static function parseResponseProcessingFragmentXml(SimpleXMLElement $xml) { return self::parseResponseRulesContainerXml($xml); } /** * Parse a QTI responseProcessing node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10413 * @param SimpleXMLElement $xml * @return array */ public static function parseResponseIfXml(SimpleXMLElement $xml) { $returnValue = self::parseElementXml($xml); $i = 0; $expression = null; $responseRules = []; foreach ($xml->children() as $child) { if ($i) { $name = $child->getName(); $methodName = 'parse' . ucfirst($name) . 'Xml'; if (method_exists(__CLASS__, $methodName)) { $responseRules[] = self::$methodName($child); } else { $responseRules[] = self::parseResponseRuleXml($child); } } else { //the first child is the expression $expression = self::parseExpressionXml($child); } $i++; } $returnValue['expression'] = $expression; $returnValue['responseRules'] = $responseRules; return $returnValue; } /** * Parse a QTI responseProcessing node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10419 * @param SimpleXMLElement $xml * @return array */ public static function parseResponseElseXml(SimpleXMLElement $xml) { return self::parseResponseRulesContainerXml($xml); } /** * Parse a QTI responseProcessing node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10409 * @param SimpleXMLElement $xml * @return array */ public static function parseResponseConditionXml(SimpleXMLElement $xml) { $returnValue = self::parseElementXml($xml); foreach ($xml->responseIf as $responseIfXml) { $returnValue['responseIf'] = self::parseResponseIfXml($responseIfXml); break; } foreach ($xml->responseElseIf as $responseIfXml) { if (!isset($returnValue['responseElseIfs'])) { $returnValue['responseElseIfs'] = []; } $returnValue['responseElseIfs'][] = self::parseResponseIfXml($responseIfXml); } foreach ($xml->responseElse as $responseIfXml) { $returnValue['responseElse'] = self::parseResponseElseXml($responseIfXml); break; } return $returnValue; } /** * Parse a QTI responseProcessing node into an array * * @see http://www.imsglobal.org/question/qtiv2p1/imsqti_infov2p1.html#element10404 * @param SimpleXMLElement $xml * @return array */ public static function parseResponseProcessingXml(SimpleXMLElement $xml) { return self::parseResponseRulesContainerXml($xml); } }