Esempio n. 1
0
 /**
  * Gives new answers to existing Questionnaire Entity
  *
  * @Route("/new/{id}", requirements={"id" = "\d+"}, name="answer_post_new")
  * @Method({"GET", "POST", "DELETE"})
  *
  * NOTE: the Method annotation is optional, but it's a recommended practice
  * to constraint the HTTP methods each controller responds to (by default
  * it responds to all methods).
  */
 public function newAction(Questionnaire $questionnaire, Statement $statement, Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     // $repository = $this->getDoctrine()->getRepository('AppBundle:Statement');
     // $statements = $repository->findAll();
     //
     // $statementCollection = new Answer;
     //
     // foreach ($statements as $statement) {
     //
     //   $statementCollection->getStatement()->add($statement);
     // }
     //
     // $collection = $this->createForm(new AnswerType, $statementCollection);
     //
     // return $this->render('Questionnaire/Answer/new.html.twig', array(
     //   'collection' => $collection->createView()
     // ));
     // $repo_statement = $this->getDoctrine()->getRepository('AppBundle:Statement');
     // $repo_questionnaire = $this->getDoctrine()->getRepository('AppBundle:Questionnaire');
     // $questionnaire = $repo_questionnaire->findOneById($id);
     //
     // if (is_null($questionnaire)) {
     //     throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
     // }
     //
     // // $statement = $repo_statement->findOneBy(
     // //     array('questionnaire'=>$questionnaire,'statement'=>$this->getStatement())
     // // );
     foreach ($questionnaire->getstatements() as $key => $statement) {
         $repo_statement = $this->getDoctrine()->getRepository('AppBundle:Statement');
         $statement = $repo_statement->findOneBy(array('questionnaire' => $questionnaire, 'statement' => $this->getStatement()));
         $answer[$key] = new Answer($key);
         $form[$key] = $this->createForm(new AnswerType(), $answer[$key])->createView();
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $form->bindRequest($request);
         // the isSubmitted() method is completely optional because the other
         // isValid() method already checks whether the form is submitted.
         // However, we explicitly add it to improve code readability.
         // See http://symfony.com/doc/current/best_practices/forms.html#handling-form-submits
         if ($form->isSubmitted() && $form->isValid()) {
             $questionnaireAns = $form["questionnaireAns"]->getData();
             foreach ($questionnaireAns as $questionnaire) {
                 $id = $questionnaire->getId();
                 $dbQuestionnaire = $em->getRepository('AppBundle:Questionnaire')->find($id);
                 if (!$dbQuestionnaire) {
                     throw $this - createNotFoundException('No $dbQuestionnaire found for id ' . $id);
                 }
                 $dbQuestionnaire->addAnswer($answer);
             }
             $statements = $form["statements"]->getData();
             foreach ($statements as $statement) {
                 $id = $statement->getId();
                 $dbStatement = $em->getRepository('AppBundle:Statement')->find($id);
                 if (!$dbStatement) {
                     throw $this - createNotFoundException('No dbStatement found for id ' . $id);
                 }
                 $dbStatement->addAnswer($answer);
             }
             $em->persist($answer);
             $em->flush();
             return $this->redirectToRoute('answer');
         }
     }
     return $this->render('Questionnaire/Answer/new.html.twig', array('questionnaire' => $questionnaire, 'statement' => $statement, 'answer' => $answer, 'form' => $form->createView()));
 }