Ejemplo n.º 1
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));
 }