/** * Get the elements on a page * @param int $pageId * @return array */ public function getElements($pageId) { $pageQuestionService = $this->getServiceLocator()->get("Api\\Service\\PageQuestion"); $questionService = $this->getServiceLocator()->get("Api\\Service\\Question"); $answerService = $this->getServiceLocator()->get("Api\\Service\\Answer"); $questions = $pageQuestionService->fetchAll(array('page_id' => $pageId)); $questionCollection = array(); foreach ($questions as $q) { $questionModel = $questionService->fetchById($q->getQuestionId()); //now get any answers $answers = $answerService->fetchAll(array('question_id' => $questionModel->getId())); $questionModel = $questionModel->toArray(); $questionModel['answers'] = \Api\Service\Encoder\Answer::toArray($answers); if ($questionModel['renderId'] != 5) { $questionCollection[$questionModel['id']]['questions'][] = $questionModel; $questionCollection[$questionModel['id']]['questionText'] = $questionModel['questionText']; $questionCollection[$questionModel['id']]['renderId'] = $questionModel['renderId']; $questionCollection[$questionModel['id']]['id'] = $questionModel['id']; } else { if (isset($questionCollection[$questionModel['parentId']])) { $questionCollection[$questionModel['parentId']]['questions'][] = $questionModel; } else { $questionCollection[$questionModel['id']]['questions'][] = $questionModel; $questionCollection[$questionModel['id']]['questionText'] = $questionModel['header']; $questionCollection[$questionModel['id']]['renderId'] = $questionModel['renderId']; $questionCollection[$questionModel['id']]['id'] = $questionModel['id']; $questionCollection[$questionModel['id']]['columns'] = $questionModel['answers']; } } } $questionCollection = array_values($questionCollection); return $questionCollection; }
/** * Edit a multiple choice question * @param array $parameters * @param int $render * @return array */ public function editMultipleChoice($parameters, $render) { $answerService = $this->getServiceLocator()->get("Api\\Service\\Answer"); $pageQuestionService = $this->getServiceLocator()->get("Api\\Service\\PageQuestion"); $surveyId = $parameters['surveyId']; $pageId = $parameters['pageId']; $questionModel = $this->getServiceLocator()->get("Api\\Service\\Question")->fetchById($parameters['questionId']); $questionModel->setQuestionText($parameters['questionText'])->setRenderId($render); $questionModel = $this->update($questionModel); //get the existing answers $answerModels = $this->getServiceLocator()->get("Api\\Service\\Answer")->fetchAll(array('question_id' => $parameters['questionId'])); //now update the answers $answerCollection = array(); for ($i = 0; $i < 50; $i++) { if (isset($answerModels[$i])) { $answerModel = $answerModels[$i]; if (isset($parameters['answerChoice' . $i])) { $answerModel->setAnswerText($parameters['answerChoice' . $i])->setQuestionId($questionModel->getId()); $answerModel = $answerService->update($answerModel); } else { //delete the extra $answerService->delete($answerModel->getId()); } } else { if (isset($parameters['answerChoice' . $i])) { $answerModel = new \Api\Model\Answer(); $answerModel->setAnswerText($parameters['answerChoice' . $i])->setQuestionId($questionModel->getId()); $answerModel = $answerService->insert($answerModel); } } $answerCollection[] = $answerModel; } return array('question' => $questionModel->toArray(), 'answers' => \Api\Service\Encoder\Answer::toArray($answerCollection)); }