public function fillQuestionnaire($questionnaire, Request $request)
 {
     $data = json_decode($request->getContent(), true);
     $ip = $request->getClientIp();
     $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
     $qb->select('count(p.id)')->from('AppBundle:Participant', 'p')->join('p.questionnaire', 'q')->where('q.id = ?1')->andWhere('p.ip = ?2')->setParameter(1, $questionnaire->getId())->setParameter(2, $ip);
     $count = $qb->getQuery()->getSingleScalarResult();
     // Construct participant.
     $participant = new Participant(new \DateTime());
     // Did this participant come from an url?
     if (isset($data['urlId'])) {
         $url = $this->getUrl($data['urlId']);
         // Already filled out or url is null.
         if ($url == null || $url->getParticipant() != null) {
             return null;
         }
         $participant->setUrl($url);
     } else {
         if ($count >= 1) {
             // Do not save repeating ip's
             return null;
         }
     }
     if (isset($data['answers'])) {
         $fullAnswers = array();
         // Iterate over the questionnaire data.
         foreach ($questionnaire->getQuestions() as $question) {
             $other = false;
             foreach ($question->getAnswers() as $answer) {
                 if ($other) {
                     break;
                 }
                 $constructed = new ParticipantAnswer($participant, $answer, $question);
                 foreach ($data['answers'] as $participantAnswer) {
                     if (isset($participantAnswer['other']) && $question->getId() == $participantAnswer['questionId']) {
                         $constructed->setOpinion($participantAnswer['other']);
                         $other = true;
                         break;
                     } else {
                         if (isset($participantAnswer['questionId']) && $question->getType() == 'OPEN' && $question->getId() == $participantAnswer['questionId']) {
                             $constructed->setTextAnswer($participantAnswer['textAnswer']);
                             break;
                         } else {
                             if (isset($participantAnswer['id']) && $answer->getId() == $participantAnswer['id']) {
                                 $constructed->setChecked($participantAnswer['checked']);
                                 break;
                             }
                         }
                     }
                 }
                 array_push($fullAnswers, $constructed);
             }
         }
         $participant->setQuestionnaire($questionnaire);
         $participant->setIp($ip);
         $participant->setAnswers($fullAnswers);
         // Persist participant.
         $em = $this->getDoctrine()->getManager();
         $em->persist($participant);
         $em->flush();
     }
     return null;
 }