Beispiel #1
0
 function __construct($args = array())
 {
     $args = array_merge(array('depth' => 'response'), $args);
     $this->depth = $args['depth'];
     if (!isset(self::$questionTable)) {
         self::$questionTable = QFrame_Db_Table::getTable('question');
     }
     if (!isset(self::$questionReferenceTable)) {
         self::$questionReferenceTable = QFrame_Db_Table::getTable('question_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::$questionTypeTable)) {
         self::$questionTypeTable = QFrame_Db_Table::getTable('question_type');
     }
     if (!isset(self::$questionPromptTable)) {
         self::$questionPromptTable = QFrame_Db_Table::getTable('question_prompt');
     }
     if (isset($args['questionID'])) {
         $rows = self::$questionTable->fetchRows('questionID', $args['questionID']);
         // question row assertion
         if (!isset($rows[0])) {
             throw new Exception('Question not found');
         }
         $this->questionRow = $rows[0];
     } else {
         throw new InvalidArgumentException('Missing arguments to QuestionModel constructor');
     }
     if (isset($args['parent'])) {
         $this->parent = $args['parent'];
     }
     $questionTypes = self::$questionTypeTable->fetchRows('questionTypeID', $this->questionRow->questionTypeID);
     $this->questionTypeRow = $questionTypes[0];
     // question type row assertion
     if ($this->questionTypeRow === NULL) {
         throw new Exception('Question type not found');
     }
     // virtual question
     if ($this->questionTypeRow->format === 'V') {
         $this->virtualQuestion = 1;
         $questions = self::$questionTable->fetchRows('questionGUID', $this->questionRow->questionGUID);
         foreach ($questions as $question) {
             if ($question->instanceID == $this->questionRow->instanceID && $question->questionTypeID != $this->questionTypeRow->questionTypeID) {
                 $seqNumber = $this->questionRow->seqNumber;
                 $questionNumber = $this->questionRow->questionNumber;
                 $this->questionRow = $question;
                 $this->questionRow->seqNumber = $seqNumber;
                 $this->questionRow->questionNumber = $questionNumber;
                 $questionTypes = self::$questionTypeTable->fetchRows('questionTypeID', $this->questionRow->questionTypeID);
                 $this->questionTypeRow = $questionTypes[0];
                 break;
             }
         }
     }
     $questionPromptRows = self::$questionPromptTable->fetchRows('questionTypeID', $this->questionRow->questionTypeID, null, $this->questionRow->instanceID);
     if (!isset(self::$ruleTable)) {
         self::$ruleTable = QFrame_Db_Table::getTable('rule');
     }
     foreach ($questionPromptRows as $row) {
         $array = $row->toArray();
         $array['rules'] = array();
         $ruleRows = self::$ruleTable->fetchRows('sourceID', $row->promptID, null, $this->questionRow->instanceID);
         foreach ($ruleRows as $ruleRow) {
             array_push($array['rules'], $ruleRow);
         }
         array_push($this->questionPromptRows, $array);
     }
     if ($this->questionRow->parentID != 0) {
         $parent = new QuestionModel(array('questionID' => $this->questionRow->parentID, 'depth' => 'question', 'noChildren' => true));
     } else {
         $parent = new SectionModel(array('sectionID' => $this->questionRow->sectionID, 'depth' => 'section'));
     }
     $ruleRows = self::$ruleTable->fetchRows('targetID', $this->questionRow->questionID, null, $this->questionRow->instanceID);
     $disableCount = 0;
     foreach ($ruleRows as $row) {
         if ($row->enabled == 'Y' && $row->type == 'disableQuestion') {
             $disableCount++;
         } elseif ($row->enabled == 'Y' && $row->type == 'enableQuestion') {
             $disableCount--;
         }
     }
     if ($this->questionRow->defaultQuestionHidden) {
         $disableCount++;
     }
     $disableCount += $parent->disableCount;
     if ($disableCount != $this->questionRow->disableCount) {
         $this->questionRow->disableCount = $disableCount;
         $this->questionRow->save();
     }
     $questionReferenceRows = self::$questionReferenceTable->fetchRows('questionID', $this->questionRow->questionID, null, $this->questionRow->pageID);
     foreach ($questionReferenceRows as $row) {
         $referenceDetailRows = self::$referenceDetailTable->fetchRows('referenceDetailID', $row->referenceDetailID, null, $this->questionRow->instanceID);
         foreach ($referenceDetailRows as $rd) {
             $array = $rd->toArray();
             $referenceRows = self::$referenceTable->fetchRows('shortName', $rd->shortName, null, $this->questionRow->instanceID);
             $array['referenceName'] = $referenceRows[0]->referenceName;
             array_push($this->referenceDetailRows, $array);
         }
     }
     if ($args['depth'] !== 'question') {
         $this->_loadResponses();
     }
     if (!isset($args['noChildren']) && $this->questionTypeRow->format == '_questionGroup') {
         $this->_loadChildren();
     }
 }