/**
  * 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);
     }
 }
Beispiel #2
0
 /**
  * Save Answers.
  *
  * @param $choices array list of answers (checked radioboxes values)
  * @param $arrPaper array array of saved paper
  */
 public function saveAnswers($choices, $arrPaper)
 {
     //Then, save the answers
     foreach ($choices as $key => $propo) {
         //retrieve data from $key : question_id - period_id and $propo : proposition_id
         $atmp = explode('-', $key);
         $quest_id = $atmp[0];
         $per_id = $atmp[1];
         //get proposition
         $proposition = $this->om->getRepository('CPASimUSanteSimupollBundle:Proposition')->findOneById($propo);
         // get paper
         $thepaper = $arrPaper[$per_id];
         // echo '$per_id'.$per_id;echo '</pre>';var_dump(is_object($arrPaper[$per_id]));echo '</pre><br>';
         if (is_object($arrPaper[$per_id])) {
             //save answer
             $answer = new Answer();
             $answer->setPaper($thepaper);
             $answer->setQuestion($proposition->getQuestion());
             $answer->setAnswer($proposition->getId() . ';');
             $answer->setMark($proposition->getMark());
             $this->om->persist($answer);
         }
     }
     $this->om->flush();
 }