public function getAnswers() { return isset($this->answers) ? $this->answers : $this->parent->getAnswers(); }
public function getQuestions($surveyId, $groupId, $language) { $result = []; $subQuestions = []; // First create parent questions. $questions = array_filter($this->listQuestions($surveyId, null, $language), function ($question) use($groupId) { return $question['gid'] == $groupId; }); foreach ($questions as $data) { if ($data['parent_qid'] == 0) { $answers = $this->getAnswersForData($data, $language, 0); // Special handling for dual scale array question. if ($data['type'] == '1') { $answers1 = $answers; $answers2 = $this->getAnswersForData($data, $language, 1); $answers = null; } elseif ($data['type'] == 'R') { $answers1 = $answers; $answers = null; } $result[$data['qid']] = $question = new Question($this, ['id' => $data['qid'], 'text' => $data['question'], 'title' => $data['title'], 'index' => (int) $data['question_order']], ['language' => $language, 'surveyId' => $surveyId, 'answers' => $answers]); // Special handling for array dual scale. if ($data['type'] == '1') { new SubQuestion($this, ['id' => $data['qid'] . '-1', 'index' => 0, 'text' => "Scale 1", 'title' => "Scale 1"], ['dimension' => 1, 'answers' => $answers1, 'parent' => $question]); new SubQuestion($this, ['id' => $data['qid'] . '-2', 'index' => 1, 'text' => "Scale 2", 'title' => "Scale 2"], ['dimension' => 1, 'answers' => $answers2, 'parent' => $question]); } elseif ($data['type'] == 'R') { // Special handling for ranking. for ($i = 1; $i <= count($answers1); $i++) { new SubQuestion($this, ['id' => $data['qid'] . '-R' . $i, 'text' => "Rank {$i}", 'index' => $data['question_order'], 'title' => "{$question->getTitle()}_{$i}"], ['dimension' => 0, 'answers' => $answers1, 'parent' => $question]); } } } } foreach ($questions as $data) { if ($data['parent_qid'] != 0) { /** @var QuestionInterface $parent */ $parent = $result[$data['parent_qid']]; new SubQuestion($this, ['id' => $data['qid'], 'text' => $data['question'], 'index' => $data['question_order'], 'title' => $data['title']], ['language' => $language, 'surveyId' => $surveyId, 'parent' => $parent, 'dimension' => (int) $data['scale_id']]); // Multiple choice with commment, add comment question. if ($data['type'] == 'P') { new SubQuestion($this, ['id' => $data['qid'] . 'comment', 'text' => "Comment for: " . $data['question'], 'index' => $data['question_order'], 'title' => $data['title'] . 'comment'], ['language' => $language, 'surveyId' => $surveyId, 'parent' => $parent, 'dimension' => 11]); } } } return array_values($result); }