예제 #1
0
 /**
  * Imports an XML Document and creates an instance
  *
  * @param mixed $import maybe a string xml document, DOMDocument object, or ZipArchiveModel object
  * @param string $instanceName is the name of the new instance
  * @param array $options contains a mix of options for import sources and validation 
  * @return integer instanceID
  */
 public static function importXML(&$import, $instanceName, $options = array())
 {
     $options = array_merge(array('pageClones' => 0, 'sectionClones' => 0, 'questionClones' => 0, 'pageResponses' => array('all' => 0), 'hidden' => 0), $options);
     if (!isset($instanceName) || strlen($instanceName) == 0) {
         throw new InvalidArgumentException('Missing instanceName argument');
     }
     libxml_use_internal_errors(true);
     if (is_a($import, 'ZipArchiveModel')) {
         $zip =& $import;
         $xml = $import->getInstanceFullResponsesXMLDocument();
         if ($xml === NULL) {
             $xml = $import->getQuestionnaireDefinitionXMLDocument();
         }
         if ($xml === NULL) {
             throw new Exception('Questionnaire definition not found in zip archive');
         }
         $dom = new DOMDocument();
         $dom->loadXML($xml);
     } elseif (is_a($import, 'DOMDocument')) {
         $dom =& $import;
         $xml = $dom->saveXML();
     } else {
         $xml =& $import;
         $dom = new DOMDocument();
         $dom->loadXML($xml);
     }
     $errors = libxml_get_errors();
     try {
         $logger = Zend_Registry::get('logger');
     } catch (Zend_Exception $e) {
     }
     foreach ($errors as $error) {
         $message = rtrim("XML error on line {$error->line} of {$error->file}: {$error->message}");
         if (isset($logger) && $logger) {
             $logger->log($message, Zend_Log::ERR);
         }
         error_log($message);
     }
     if (count($errors) > 0) {
         throw new Exception('XML Exception');
     }
     if (!isset(self::$questionReferenceTable)) {
         self::$questionReferenceTable = QFrame_Db_Table::getTable('question_reference');
     }
     if (!isset(self::$sectionReferenceTable)) {
         self::$sectionReferenceTable = QFrame_Db_Table::getTable('section_reference');
     }
     if (!isset(self::$pageReferenceTable)) {
         self::$pageReferenceTable = QFrame_Db_Table::getTable('page_reference');
     }
     if (!isset(self::$referenceTable)) {
         self::$referenceTable = QFrame_Db_Table::getTable('reference');
     }
     if (!isset(self::$referenceDetailTable)) {
         self::$referenceDetailTable = QFrame_Db_Table::getTable('reference_detail');
     }
     if (!isset(self::$ruleTable)) {
         self::$ruleTable = QFrame_Db_Table::getTable('rule');
     }
     if (!isset(self::$questionTypeTable)) {
         self::$questionTypeTable = QFrame_Db_Table::getTable('question_type');
     }
     if (!isset(self::$questionPromptTable)) {
         self::$questionPromptTable = QFrame_Db_Table::getTable('question_prompt');
     }
     if (!isset(self::$pageTable)) {
         self::$pageTable = QFrame_Db_Table::getTable('page');
     }
     if (!isset(self::$sectionTable)) {
         self::$sectionTable = QFrame_Db_Table::getTable('section');
     }
     if (!isset(self::$questionTable)) {
         self::$questionTable = QFrame_Db_Table::getTable('question');
     }
     if (!isset(self::$questionnaireTable)) {
         self::$questionnaireTable = QFrame_Db_Table::getTable('questionnaire');
     }
     if (!isset(self::$instanceTable)) {
         self::$instanceTable = QFrame_Db_Table::getTable('instance');
     }
     $transactionNumber = self::startSerializableTransaction();
     $questionTypeIDCache = array();
     // Stores questionTypes that have already been inserted
     $rulesMap = array();
     // Stores sourceID to questionGUID
     $pageGuidMap = array();
     // Stores pageGUID to pageID
     $sectionGuidMap = array();
     // Stores sectionGUID to sectionID
     $questionGuidMap = array();
     // Stores questionGUID to questionID
     $responseObjs = array();
     // Stores response objects so that they may be saved at the end
     // to ensure all rules and rule targets have been inserted
     $fileAttachments = array();
     // Stores information for attachments so that attachments may be inserted
     // at the end, after the question bulk load is complete
     $processedReferences = array();
     // Stores references (shortNames) that have already been inserted
     // into the reference table
     $questionPromptsMap = array();
     // Stores question prompts.  First key is questionTypeID, second key is
     // prompt value, and the value is the promptID.
     $questionnaire = $dom->getElementsByTagName('questionnaire')->item(0);
     $questionnaireName = $questionnaire->getAttribute('questionnaireName');
     $questionnaireVersion = $questionnaire->getAttribute('questionnaireVersion');
     $revision = $questionnaire->getAttribute('revision');
     // If questionnaireID is already known and passed an argument, use it instead of looking it up
     if (isset($options['questionnaireID'])) {
         $questionnaireID = $options['questionnaireID'];
     } else {
         $questionnaireID = self::$questionnaireTable->getQuestionnaireID($questionnaireName, $questionnaireVersion, $revision);
     }
     if (isset($questionnaireID)) {
         $instanceID = self::$instanceTable->getInstanceID($questionnaireID, $instanceName);
         if (isset($instanceID)) {
             throw new Exception('Instance name already exists for this questionnaire');
         }
     } else {
         throw new Exception('Questionnaire was not found');
     }
     if (isset($options['instanceID'])) {
         $importResponsesInstance = new InstanceModel(array('instanceID' => $options['instanceID']));
         $importDom = new DOMDocument();
         $importDom->loadXML($importResponsesInstance->toXML());
         $importInstanceQuestionsDom = $importDom->getElementsByTagName('question');
         for ($q = 0; $q < $importInstanceQuestionsDom->length; $q++) {
             $question = $importInstanceQuestionsDom->item($q);
             if ($question->getElementsByTagName('questionType') === 'V') {
                 continue;
             }
             $responses = $question->getElementsByTagName('responses');
             if ($responses->length) {
                 $resps = $responses->item(0)->getElementsByTagName('response');
                 for ($r = 0; $r < $resps->length; $r++) {
                     $response = $resps->item($r);
                     $additionalInfo = isset($responses->item(0)->getElementsByTagName('additionalInfo')->item(0)->nodeValue) ? $responses->item(0)->getElementsByTagName('additionalInfo')->item(0)->nodeValue : '';
                     $approverComments = isset($responses->item(0)->getElementsByTagName('approverComments')->item(0)->nodeValue) ? $responses->item(0)->getElementsByTagName('approverComments')->item(0)->nodeValue : '';
                     $importInstanceResponses[$question->getElementsByTagName('questionGUID')->item(0)->nodeValue][] = array('responseText' => $response->getElementsByTagName('responseText')->item(0)->nodeValue, 'additionalInfo' => $additionalInfo, 'approverComments' => $approverComments);
                 }
             }
         }
     }
     $instanceID = self::$instanceTable->insert(array('questionnaireID' => $questionnaireID, 'instanceName' => $instanceName, 'hidden' => $options['hidden']));
     $pages = $questionnaire->getElementsByTagName('page');
     for ($t = 0; $t < $pages->length; $t++) {
         $page = $pages->item($t);
         $pageIDs = self::importXMLPage($page, $questionnaireID, $instanceID, $pageGuidMap);
         $pageID = $pageIDs[0];
         $pageGUID = $pageIDs[1];
         $pageReferences = $page->getElementsByTagName('pageReferences');
         self::importXMLReferences('page', $pageReferences, $instanceID, $pageID, null, null, $processedReferences);
         $sections = $page->getElementsByTagName('section');
         for ($s = 0; $s < $sections->length; $s++) {
             $section = $sections->item($s);
             $sectionIDs = self::importXMLSection($section, $instanceID, $pageID, $sectionGuidMap);
             $sectionID = $sectionIDs[0];
             $sectionGUID = $sectionIDs[1];
             $sectionReferences = $section->getElementsByTagName('sectionReferences');
             self::importXMLReferences('section', $sectionReferences, $instanceID, $pageID, $sectionID, null, $processedReferences);
             foreach ($section->getElementsByTagName('questions')->item(0)->childNodes as $question) {
                 if ($question->nodeName === 'csi:question') {
                     $questionIDs = self::importXMLQuestion(false, null, $question, $questionnaireID, $instanceID, $pageID, $pageGUID, $sectionID, $sectionGUID, $questionGuidMap, $questionTypeIDCache, $rulesMap, $questionPromptsMap);
                     $questionID = $questionIDs[0];
                     $questionGUID = $questionIDs[1];
                     $questionTypeID = $questionIDs[2];
                     $questionPrompts = $question->getElementsByTagName('questionPrompt');
                     $questionReferences = $question->getElementsByTagName('questionReferences');
                     self::importXMLReferences('question', $questionReferences, $instanceID, $pageID, $sectionID, $questionID, $processedReferences);
                     $responses = $question->getElementsByTagName('responses');
                     self::importXMLResponses($responses, $question, $instanceID, $pageID, $sectionID, $questionID, $questionGUID, $questionTypeID, $importInstanceResponses, $questionPrompts, $questionPromptsMap, $responseObjs, $fileAttachments, $options);
                 } elseif ($question->nodeName === 'csi:questionGroup') {
                     $questionIDs = self::importXMLQuestion(true, null, $question, $questionnaireID, $instanceID, $pageID, $pageGUID, $sectionID, $sectionGUID, $questionGuidMap, $questionTypeIDCache, $rulesMap, $questionPromptsMap);
                     $parentQuestionID = $questionIDs[0];
                     $questionReferences = $question->getElementsByTagName('groupQuestionReferences');
                     self::importXMLReferences('question', $questionReferences, $instanceID, $pageID, $sectionID, $parentQuestionID, $processedReferences);
                     if ($options['pageResponses']['all'] || isset($options['pageResponses'][$pageID]) && $options['pageResponses'][$pageID]) {
                         $attachments = $question->getElementsByTagName('attachment');
                         $fileAttachments[$parentQuestionID] = $attachments;
                     }
                     $childQuestions = $question->getElementsByTagName('question');
                     for ($cq = 0; $cq < $childQuestions->length; $cq++) {
                         $question = $childQuestions->item($cq);
                         $questionIDs = self::importXMLQuestion(false, $parentQuestionID, $question, $questionnaireID, $instanceID, $pageID, $pageGUID, $sectionID, $sectionGUID, $questionGuidMap, $questionTypeIDCache, $rulesMap, $questionPromptsMap);
                         $questionID = $questionIDs[0];
                         $questionGUID = $questionIDs[1];
                         $questionTypeID = $questionIDs[2];
                         $questionPrompts = $question->getElementsByTagName('questionPrompt');
                         $questionReferences = $question->getElementsByTagName('questionReferences');
                         self::importXMLReferences('question', $questionReferences, $instanceID, $pageID, $sectionID, $questionID, $processedReferences);
                         $responses = $question->getElementsByTagName('responses');
                         self::importXMLResponses($responses, $question, $instanceID, $pageID, $sectionID, $questionID, $questionGUID, $questionTypeID, $importInstanceResponses, $questionPrompts, $questionPromptsMap, $responseObjs, $fileAttachments, $options);
                     }
                 }
             }
         }
     }
     self::importXMLRules($questionnaireID, $instanceID, $rulesMap, $pageGuidMap, $sectionGuidMap, $questionGuidMap);
     self::$questionTable->processBulk();
     self::$sectionTable->processBulk();
     self::$pageTable->processBulk();
     self::$questionPromptTable->processBulk();
     self::$questionTypeTable->processBulk();
     self::$ruleTable->processBulk();
     self::$referenceTable->processBulk();
     self::$referenceDetailTable->processBulk();
     self::$questionReferenceTable->processBulk();
     self::$sectionReferenceTable->processBulk();
     self::$pageReferenceTable->processBulk();
     foreach ($responseObjs as $response) {
         $response->save();
     }
     foreach ($fileAttachments as $questionID => $attachments) {
         $questionModel = new QuestionModel(array('questionID' => $questionID, 'depth' => 'question'));
         self::importXMLAttachments($attachments, $questionModel, $import);
     }
     $instance = new InstanceModel(array('instanceID' => $instanceID, 'depth' => 'page'));
     $instance->save();
     self::dbCommit($transactionNumber);
     return $instance->instanceID;
 }