/** * Wrapper for cq_question_from_dom_element() that takes plain-text XML, parses it * into a DOMElement and passes that on to cq_question_from_dom_element(). * * @param String $xmlContent * containing the XML. * @param CqUserAnswerInterface $userAnswer * to use for storing the user's answer * @param Object $node * Drupal node object that this comes from. * * @return CqQuestion * The question. */ function cq_question_from_xml($xml_content, &$user_answer, &$node) { if (strlen($xml_content) == 0) { drupal_set_message(t('Your question XML is empty. Please supply a question.'), 'error'); return NULL; } $dom = new DomDocument(); if (!$dom) { drupal_set_message(t('DOM-XML php extension probably not installed, please see the requirements section in the README.txt'), 'error'); return NULL; } if (!$dom->loadXML($xml_content)) { drupal_set_message(t('Problem loading question in node %nid', array('%nid' => $node->nid)), 'error'); return NULL; } $xpath = new DOMXPath($dom); $questions = $xpath->query('/question'); $first_child = $questions->item(0); if ($first_child) { $question = cq_question_from_dom_element($first_child, $user_answer, $node); } else { drupal_set_message(t('It seems your XML does not contain a <question> tag as root tag.'), 'error'); return NULL; } return $question; }
/** * Overrides CqQuestionAbstract::loadXml() */ public function loadXml(DOMElement $dom) { parent::loadXml($dom); module_load_include('inc.php', 'closedquestion', 'lib/XmlLib'); $i = 0; foreach ($dom->childNodes as $child) { if (drupal_strtolower($child->nodeName) == 'question') { $uac =& new CqUserAnswerClient($this->userAnswer, $i); $question =& cq_question_from_dom_element($child, $uac, $this->node); if ($question) { $question->loadXML($child); $this->subQuestions[] =& $question; $question->addListener($this); unset($question); $i++; } } } $answer = $this->userAnswer->getAnswer(); if (isset($_REQUEST['CqQS_' . $this->node->nid . '_Step'])) { $this->currentIndex = (int) $_REQUEST['CqQS_' . $this->node->nid . '_Step']; $answer['ci'] = $this->currentIndex; $this->userAnswer->setAnswer($answer); $this->userAnswer->store(); } else { $this->currentIndex = (int) $answer['ci']; } if ($this->currentIndex < 0 || $this->currentIndex >= count($this->subQuestions)) { $this->currentIndex = 0; } }