/**
  * json request, save the paper data.
  *
  * @EXT\Route(
  *      "/validate/{sid}/{page}/{all}",
  *      name="cpasimusante_simupoll_paper_save",
  *      defaults={ "page" = 1, "all" = 0 },
  *      requirements={},
  *      options={"expose"=true}
  * )
  *
  * @param int  $sid  id of Simupoll
  * @param int  $page for the pagination, page destination
  * @param bool $all  for use or not use the pagination
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function paperSaveAction(Request $request, $sid, $page, $all)
 {
     $em = $this->getDoctrine()->getManager();
     $session = $request->getSession();
     if ($request->isMethod('POST')) {
         $simupoll = $em->getRepository('CPASimUSanteSimupollBundle:Simupoll')->findOneById($sid);
         $user = $this->container->get('security.token_storage')->getToken()->getUser();
         $uid = $user->getId();
         //get NumPaper for the simupoll / user
         $maxNumPaper = $this->paperManager->getMaxNumPaper($sid, $uid);
         //Verify if it exists a not finished paper for the user
         //$paper = $this->paperManager->gePaper($sid, $uid);
         //list of answers
         $choices = $request->request->get('choice');
         $next_category = $request->request->get('next');
         $current_category = $request->request->get('current');
         //if there is no paper
         //  if (count($paper) == 0) {
         //Create paper
         $paper = new Paper();
         $paper->setUser($user);
         $paper->setStart(new \DateTime());
         $paper->setSimupoll($simupoll);
         $paper->setNumPaper((int) $maxNumPaper + 1);
         $em->persist($paper);
         $em->flush();
         //$this->paperManager->savePaper($user, $simupoll, $period, (int) $maxNumPaper + 1);
         //Save responses
         foreach ($choices as $choice) {
             $proposition = $em->getRepository('CPASimUSanteSimupollBundle:Proposition')->findOneById($choice[1]);
             $answer = new Answer();
             $answer->setPaper($paper);
             $answer->setQuestion($proposition->getQuestion());
             $answer->setAnswer($proposition->getId() . ';');
             $answer->setMark($proposition->getMark());
             $em->persist($answer);
         }
         $em->flush();
         //Set session
         $session->set('simupaper' . $sid, $paper->getId());
         $data = $choices;
         return new JsonResponse($data);
     }
 }
 /**
  * Save Paper.
  *
  * @return Paper $paper
  */
 public function savePaper($user, $simupoll, $period, $maxNumPaper)
 {
     $paper = new Paper();
     $paper->setUser($user);
     $paper->setStart(new \DateTime());
     $paper->setSimupoll($simupoll);
     $paper->setPeriod($period);
     $paper->setNumPaper($maxNumPaper);
     $this->om->persist($paper);
     $this->om->flush();
     return $paper;
 }