示例#1
0
 /**
  * @Route("/ajax/sendAnswer/", name="ajax_send_answer")
  */
 public function sendAnswerAjaxAction(Request $request)
 {
     $dane = json_decode(file_get_contents('php://input'), true);
     $em = $this->getDoctrine()->getManager();
     $attempt = $em->getRepository('AppBundle:Attempt')->find($dane['attempt']);
     $answer = $em->getRepository('AppBundle:Answer')->find($dane['id']);
     $u_answer = new UserAnswer();
     $u_answer->setAttempt($attempt);
     $u_answer->setAnswer($answer);
     $em->persist($u_answer);
     $em->flush();
     $attempt->setQuestion(null);
     $em->flush();
     $response = new JsonResponse();
     $response->setData(array('success' => true));
     return $response;
 }
 /**
  * @Route("/section/{section}/question/{page}", name="question")
  */
 public function questionAction(Section $section, $page, Request $request)
 {
     $question = $this->getQuestion($section, $page);
     if ($request->getMethod() == 'POST' && $request->get('answer') != null) {
         $user = $this->container->get('doctrine')->getManager()->getRepository('AppBundle\\Entity\\User')->findOneBy(array('sessionId' => $request->getSession()->getId()));
         $answer = $this->container->get('doctrine')->getManager()->getRepository('AppBundle\\Entity\\Answer')->find($request->get('answer'));
         if (!$answer || $answer->getQuestion() != $question) {
             throw new \Exception('Invalid answer selected: ' . $request->get('answer'));
         }
         $userAnswer = $this->container->get('doctrine')->getManager()->createQuery('SELECT ua FROM AppBundle\\Entity\\UserAnswer ua JOIN ua.answer a WHERE ua.user = :user AND a.question = :question')->setParameter('user', $user)->setParameter('question', $question)->getOneOrNullResult();
         if (!$userAnswer) {
             $userAnswer = new UserAnswer();
         }
         $userAnswer->setUser($user);
         $userAnswer->setAnswer($answer);
         $this->container->get('doctrine')->getManager()->persist($userAnswer);
         $this->container->get('doctrine')->getManager()->flush($userAnswer);
         return new RedirectResponse($this->container->get('router')->generate('answer', array('section' => $section->getId(), 'page' => $page)));
     }
     return $this->render('AppBundle::question.html.twig', array('question' => $question, 'page' => $page, 'hasPrevious' => $page <= 1 ? false : true, 'section' => $section));
 }
 public function postUserAnswerAction()
 {
     $em = $this->getDoctrine()->getEntityManager();
     $request = $this->getRequest();
     $this->testId = $request->request->get('testid');
     $this->userId = $request->request->get('userid');
     $this->dataAnswers = $request->request->get('answer');
     $this->user = $em->find("AppBundle\\Entity\\User", $this->userId);
     $this->test = $em->find("AppBundle\\Entity\\Test", $this->testId);
     // print_r($test);
     foreach ($this->dataAnswers as $answer) {
         $answerid = current($answer);
         $questionid = key($answer);
         $this->userAnswers[$questionid][] = $answerid;
         $userAnswer = new UserAnswer();
         $answer = $em->find("AppBundle\\Entity\\Answer", $answerid);
         $question = $em->find("AppBundle\\Entity\\Question", $questionid);
         $userAnswer->setUser($this->user);
         $userAnswer->setQuestion($question);
         $userAnswer->setTest($this->test);
         $userAnswer->setAnswer($answer);
         $em->persist($userAnswer);
     }
     $em->flush();
     $em->clear();
     $tmp = $this->updateStatistic();
     return ["success" => "true", "message" => "Спасибо! Ваши ответы на вопросы успешно сохранены, мы с Вами свяжемся в ближайшее время!"];
 }
 private function importAnswers(\SPSSReader $SPSS, Dataset $dataset)
 {
     // Loop through the answers
     $SPSS->loadData();
     for ($case = 0; $case < $SPSS->header->numberOfCases; $case++) {
         $user = new User();
         $user->setSessionId(microtime());
         $user->setAutoGenerated(true);
         $toFlush = array($user);
         foreach ($SPSS->variables as $var) {
             if ($var->isExtended) {
                 continue;
             }
             $index = isset($SPSS->extendedNames[$var->shortName]) ? $SPSS->extendedNames[$var->shortName] : $var->name;
             if (isset($this->dimensions[$index])) {
                 // This is a dimension attribute
                 // Check if the dimension is a valid profile dimension
                 if ($this->isValidProfileDimension($this->dimensions[$index]->getQuestionId())) {
                     // Set the profile dimension
                     $dimension = $this->dimensionIds[$this->dimensions[$index]->getQuestionId()];
                     $setter = 'set' . ucfirst($dimension);
                     $user->{$setter}($this->mapProfileDimensionValue($dimension, $var->data[$case] === 'NaN' ? '' : $var->data[$case]));
                 }
             } else {
                 if ($this->questions[$index]) {
                     // Create a UserAnswer
                     $userAnswer = new UserAnswer();
                     $userAnswer->setDataset($dataset);
                     $userAnswer->setUser($user);
                     if ($var->data[$case] === 'NaN' || $var->data[$case] == '') {
                         continue;
                     }
                     if (!isset($this->allAnswers[$this->questions[$index]->getQuestionId() . '_' . $var->data[$case]])) {
                         continue;
                         //throw new \Exception('Could not find user answer for '.$index.' ('.$var->data[$case].') given');
                     }
                     $answer = $this->allAnswers[$this->questions[$index]->getQuestionId() . '_' . $var->data[$case]];
                     $userAnswer->setAnswer($answer);
                     $this->doctrine->getManager()->persist($userAnswer);
                     $toFlush[] = $userAnswer;
                 } else {
                     throw new \Exception('Answer to a non-existing question! ' . $index);
                 }
             }
         }
         $this->doctrine->getManager()->persist($user);
         $this->doctrine->getManager()->flush($toFlush);
         foreach ($toFlush as $curEntity) {
             $this->doctrine->getManager()->detach($curEntity);
             // To save memory
         }
     }
 }