Пример #1
0
 /**
  * @Security("has_role('ROLE_SUPER_ADMIN')")
  * @Route("/testid{testId}/test/question{id}", name="editQuestion")
  */
 public function editQuestionAction(Request $request, $testId, $id)
 {
     $test = $this->getDoctrine()->getRepository('AppBundle:Test')->find($testId);
     $question = $this->getDoctrine()->getRepository('AppBundle:Question')->find($id);
     $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->setContent($request->get('_description'));
         $ans = $request->get('_answer');
         $questionAns = $question->getAnswers()->toArray();
         $counter = 0;
         foreach ($questionAns as $answer) {
             if (!empty($ans[$counter]['content'])) {
                 $answer->setContent($ans[$counter]['content']);
                 $answer->setRating($ans[$counter]['rating']);
                 $em->persist($answer);
                 $counter++;
             } else {
                 continue;
             }
         }
         if (count($ans) > count($questionAns)) {
             for ($i = $counter; $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->removeImages();
             $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->redirectToRoute('testpage', array('id' => $testId));
         }
         $em->flush();
         return $this->redirectToRoute('testpage', array('id' => $testId));
     }
     $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, 'questionEdit' => $question));
 }