public function __construct(SurveyAnswer $surveyAnswer) { $survey = $surveyAnswer->getSurvey(); $resourceNode = $survey->getResourceNode(); $user = $surveyAnswer->getUser(); $details = ['answer_date' => $surveyAnswer->getAnswerDate(), 'nb_answers' => $surveyAnswer->getNbAnswers()]; parent::__construct(self::ACTION, $details, $user, null, $resourceNode, null, $resourceNode->getWorkspace()); }
/** * @EXT\Route( * "/survey/{survey}/answer", * name="claro_survey_answer" * ) * @EXT\Template("ClarolineSurveyBundle:Survey:surveyAnswerForm.html.twig") * * @return \Symfony\Component\HttpFoundation\Response */ public function surveyAnswerAction(Survey $survey) { $this->checkSurveyRight($survey, 'OPEN'); $status = $this->computeStatus($survey); $user = $this->tokenStorage->getToken()->getUser(); $isAnon = $user === 'anon.'; if ($status === 'published') { $postDatas = $this->request->getCurrentRequest()->request->all(); $errors = $this->validateSurveyAnswer($survey, $postDatas); if (count($errors) > 0) { $surveyAnswer = $isAnon ? null : $this->surveyManager->getSurveyAnswerBySurveyAndUser($survey, $user); $canEdit = is_null($surveyAnswer) || $survey->getAllowAnswerEdition(); $answersDatas = []; foreach ($postDatas as $questionId => $questionDatas) { if (isset($questionDatas['comment'])) { $answersDatas[$questionId]['comment'] = $questionDatas['comment']; } if (isset($questionDatas['answer']) && !empty($questionDatas['answer'])) { $answersDatas[$questionId]['answer'] = $questionDatas['answer']; } if (isset($questionDatas['choice']) && !empty($questionDatas['choice'])) { $choiceId = $questionDatas['choice']; $answersDatas[$questionId][$choiceId] = $choiceId; } if (isset($questionDatas['other'])) { $answersDatas[$questionId]['other'] = $questionDatas['other']; } foreach ($questionDatas as $key => $value) { if (is_int($key)) { $answersDatas[$questionId][$key] = $value; } } } $questionRelations = $this->surveyManager->getQuestionRelationsBySurvey($survey); foreach ($questionRelations as $relation) { $question = $relation->getQuestion(); if ($question->getType() !== 'title') { $answerData = isset($answersDatas[$question->getId()]) ? $answersDatas[$question->getId()] : []; $questionViews[$relation->getId()] = $this->displayTypedQuestion($survey, $question, $answerData, $canEdit)->getContent(); } else { $questionViews[$relation->getId()] = $this->templating->render('ClarolineSurveyBundle:Survey:titleQuestionDisplay.html.twig', ['question' => $question]); } } return ['survey' => $survey, 'questionRelations' => $questionRelations, 'questionViews' => $questionViews, 'canEdit' => $canEdit, 'errors' => $errors, 'isAnon' => $isAnon]; } $surveyAnswer = $isAnon ? null : $this->surveyManager->getSurveyAnswerBySurveyAndUser($survey, $user); $isNewAnswer = true; if (is_null($surveyAnswer)) { $surveyAnswer = new SurveyAnswer(); $surveyAnswer->setSurvey($survey); if (!$isAnon) { $surveyAnswer->setUser($user); } $surveyAnswer->setNbAnswers(1); $surveyAnswer->setAnswerDate(new \DateTime()); $this->surveyManager->persistSurveyAnswer($surveyAnswer); } else { $isNewAnswer = false; $surveyAnswer->incrementNbAnswers(); $this->surveyManager->persistSurveyAnswer($surveyAnswer); } foreach ($postDatas as $questionId => $questionResponse) { $question = $this->surveyManager->getQuestionById($questionId); if (!is_null($question)) { $questionType = $question->getType(); if ($isNewAnswer) { $questionAnswer = new QuestionAnswer(); $questionAnswer->setSurveyAnswer($surveyAnswer); $questionAnswer->setQuestion($question); if (isset($questionResponse['comment']) && !empty($questionResponse['comment'])) { $questionAnswer->setComment($questionResponse['comment']); } $this->surveyManager->persistQuestionAnswer($questionAnswer); if (($questionType === 'open_ended' || $questionType === 'open_ended_bare') && isset($questionResponse['answer']) && !empty($questionResponse['answer'])) { $openEndedAnswer = new OpenEndedQuestionAnswer(); $openEndedAnswer->setQuestionAnswer($questionAnswer); $openEndedAnswer->setContent($questionResponse['answer']); $this->surveyManager->persistOpenEndedQuestionAnswer($openEndedAnswer); } elseif ($questionType === 'multiple_choice_single' || $questionType === 'multiple_choice_multiple') { $multipleChoiceQuestion = $this->surveyManager->getMultipleChoiceQuestionByQuestion($question); if (!is_null($multipleChoiceQuestion)) { if ($questionType === 'multiple_choice_multiple') { foreach ($questionResponse as $choiceId => $response) { if ($choiceId !== 'comment' && $choiceId !== 'other') { $choice = $this->surveyManager->getChoiceById($choiceId); $choiceAnswer = new MultipleChoiceQuestionAnswer(); $choiceAnswer->setQuestionAnswer($questionAnswer); $choiceAnswer->setChoice($choice); if ($choice->isOther() && isset($questionResponse['other'])) { $choiceAnswer->setContent($questionResponse['other']); } $this->surveyManager->persistMultipleChoiceQuestionAnswer($choiceAnswer); } } } elseif ($questionType === 'multiple_choice_single' && isset($questionResponse['choice']) && !empty($questionResponse['choice'])) { $choiceId = (int) $questionResponse['choice']; $choice = $this->surveyManager->getChoiceById($choiceId); $choiceAnswer = new MultipleChoiceQuestionAnswer(); $choiceAnswer->setQuestionAnswer($questionAnswer); $choiceAnswer->setChoice($choice); if ($choice->isOther() && isset($questionResponse['other'])) { $choiceAnswer->setContent($questionResponse['other']); } $this->surveyManager->persistMultipleChoiceQuestionAnswer($choiceAnswer); } } } elseif ($questionType === 'simple_text' && isset($questionResponse['answer']) && !empty($questionResponse['answer'])) { $simpleTextAnswer = new SimpleTextQuestionAnswer(); $simpleTextAnswer->setQuestionAnswer($questionAnswer); $simpleTextAnswer->setContent($questionResponse['answer']); $this->surveyManager->persistSimpleTextQuestionAnswer($simpleTextAnswer); } } elseif ($survey->getAllowAnswerEdition()) { $questionAnswer = $this->surveyManager->getQuestionAnswerBySurveyAnswerAndQuestion($surveyAnswer, $question); if (is_null($questionAnswer)) { $questionAnswer = new QuestionAnswer(); $questionAnswer->setSurveyAnswer($surveyAnswer); $questionAnswer->setQuestion($question); $this->surveyManager->persistQuestionAnswer($questionAnswer); } if (isset($questionResponse['comment']) && !empty($questionResponse['comment'])) { $questionAnswer->setComment($questionResponse['comment']); $this->surveyManager->persistQuestionAnswer($questionAnswer); } if (($questionType === 'open_ended' || $questionType === 'open_ended_bare') && isset($questionResponse['answer']) && !empty($questionResponse['answer'])) { $openEndedAnswer = $this->surveyManager->getOpenEndedAnswerByQuestionAnswer($questionAnswer); if (is_null($openEndedAnswer)) { $openEndedAnswer = new OpenEndedQuestionAnswer(); $openEndedAnswer->setQuestionAnswer($questionAnswer); } $openEndedAnswer->setContent($questionResponse['answer']); $this->surveyManager->persistOpenEndedQuestionAnswer($openEndedAnswer); } elseif ($questionType === 'multiple_choice_single' || $questionType === 'multiple_choice_multiple') { $multipleChoiceQuestion = $this->surveyManager->getMultipleChoiceQuestionByQuestion($question); if (!is_null($multipleChoiceQuestion)) { if ($questionType === 'multiple_choice_multiple') { $this->surveyManager->deleteMultipleChoiceAnswersByQuestionAnswer($questionAnswer); foreach ($questionResponse as $choiceId => $response) { if ($choiceId !== 'comment' && $choiceId !== 'other') { $choice = $this->surveyManager->getChoiceById($choiceId); $choiceAnswer = new MultipleChoiceQuestionAnswer(); $choiceAnswer->setQuestionAnswer($questionAnswer); $choiceAnswer->setChoice($choice); if ($choice->isOther() && isset($questionResponse['other'])) { $choiceAnswer->setContent($questionResponse['other']); } $this->surveyManager->persistMultipleChoiceQuestionAnswer($choiceAnswer); } } } elseif ($questionType === 'multiple_choice_single' && isset($questionResponse['choice']) && !empty($questionResponse['choice'])) { $this->surveyManager->deleteMultipleChoiceAnswersByQuestionAnswer($questionAnswer); $choiceId = (int) $questionResponse['choice']; $choice = $this->surveyManager->getChoiceById($choiceId); $choiceAnswer = new MultipleChoiceQuestionAnswer(); $choiceAnswer->setQuestionAnswer($questionAnswer); $choiceAnswer->setChoice($choice); if ($choice->isOther() && isset($questionResponse['other'])) { $choiceAnswer->setContent($questionResponse['other']); } $this->surveyManager->persistMultipleChoiceQuestionAnswer($choiceAnswer); } } } elseif ($questionType === 'simple_text' && isset($questionResponse['answer']) && !empty($questionResponse['answer'])) { $simpleTextAnswer = new SimpleTextQuestionAnswer(); $simpleTextAnswer->setQuestionAnswer($questionAnswer); $simpleTextAnswer->setContent($questionResponse['answer']); $this->surveyManager->persistSimpleTextQuestionAnswer($simpleTextAnswer); } } } } $event = $isAnon ? new LogSurveyAnswer($survey) : new LogSurveyAnswer($survey, $user); $this->eventDispatcher->dispatch('log', $event); } if ($isAnon) { $this->get('session')->getFlashBag()->add('success', $this->translator->trans('thanks_for_answering_msg', [], 'survey')); } return new RedirectResponse($this->router->generate('claro_survey_index', ['survey' => $survey->getId()])); }