public function createAction(Request $request)
 {
     $questionnaireId = $request->request->getInt('questionnaireId');
     if (!empty($questionnaireId)) {
         $repository = $this->getDoctrine()->getRepository('AppBundle:Questionnaire');
         $questionnaire = $repository->findOneBy(array('id' => $questionnaireId));
         // Save user:
         $repository = $this->getDoctrine()->getRepository('AppBundle:User');
         $user = $repository->findOneBy(array('id' => 1));
         $question = new Question();
         $question->setUser($user);
         $question->setCreatedAt(new \DateTime());
         $question->setContent($request->get('content'));
         $question->setDescription($request->get('description'));
         $question->setQuestionnaire($questionnaire);
         $question->setTitle($request->get('title'));
         $question->setType($request->get('type'));
         $em = $this->getDoctrine()->getManager();
         $em->persist($question);
         // Now let's deal with potential answers and a potential file:
         $em->flush();
         return new JsonResponse(array('success' => 1, 'message' => 'question saved'));
     }
 }
 private function handleRequest(Question $entity, Request $request)
 {
     $data = $request->getContent();
     if (isset($data['title'])) {
         $entity->setTitle($data['title']);
     }
     if (isset($data['content'])) {
         $entity->setContent($data['content']);
     }
     if (isset($data['created_by'])) {
         $entity->setCreatedBy($data['created_by']);
     }
 }
Exemple #3
0
 /**
  * @Route("/testid{id}/test", name="testpage")
  */
 public function showTestAction(Request $request, $id)
 {
     $test = $this->getDoctrine()->getRepository('AppBundle:Test')->find($id);
     //for complete test
     if (!$this->isGranted("ROLE_ADMIN")) {
         $user = $this->getUser();
         if (!$user->getTests()->isEmpty()) {
             foreach ($user->getTests()->getValues() as $userTest) {
                 if ($userTest === $test) {
                     return $this->redirectToRoute('userTest', array('id' => $user->getId(), 'testId' => $test->getId()));
                 }
             }
         }
         return $this->render('tests/test.html.twig', array('test' => $test));
     }
     //for admin edit
     $image = new Image();
     $form = $this->createFormBuilder($image)->add('file', 'file', array('label' => 'Изображение:', 'required' => false))->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $em = $this->getDoctrine()->getManager();
         $question = new Question();
         $question->setContent($request->get('_description'));
         $question->setTest($test);
         $test->addQuestion($question);
         $ans = $request->get('_answer');
         for ($i = 0; $i < count($ans); $i++) {
             if (!empty($ans[$i]['content'])) {
                 $answer = new Answer();
                 $answer->setContent($ans[$i]['content']);
                 $answer->setRating($ans[$i]['rating']);
                 $answer->setQuestion($question);
                 $question->addAnswer($answer);
             } else {
                 continue;
             }
         }
         if (!$form->get('file')->isEmpty()) {
             $question->setImage($image);
             $em->persist($test);
             $em->flush();
             $image->upload($test->getId() . '/' . $question->getId());
             $image->setPath($test->getId() . '/' . $question->getId() . '/' . $form->get('file')->getData()->getClientOriginalName());
             $em->persist($image);
             $em->flush();
             $minRating = $this->get('calculate')->calculateMinRating($test);
             $maxRating = $this->get('calculate')->calculateMaxRating($test);
             return $this->render('tests/test.html.twig', array('test' => $test, 'uploadForm' => $form->createView(), 'maxRating' => $maxRating, 'minRating' => $minRating));
         }
         $em->persist($test);
         $em->flush();
         $minRating = $this->get('calculate')->calculateMinRating($test);
         $maxRating = $this->get('calculate')->calculateMaxRating($test);
         return $this->render('tests/test.html.twig', array('test' => $test, 'uploadForm' => $form->createView(), 'maxRating' => $maxRating, 'minRating' => $minRating));
     }
     $minRating = $this->get('calculate')->calculateMinRating($test);
     $maxRating = $this->get('calculate')->calculateMaxRating($test);
     if (!$test) {
         throw $this->createNotFoundException('No found test for id' . $id);
     }
     return $this->render('tests/test.html.twig', array('test' => $test, 'uploadForm' => $form->createView(), 'maxRating' => $maxRating, 'minRating' => $minRating));
 }