protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Are you sure you want to import symfony pack questions into the database ?', false); if (!$helper->ask($input, $output, $question)) { return; } $output->writeln('<info>=== Importing Symfony Pack Questions ===</info>'); $yaml = new Parser(); $filesToParse = ['architecture', 'automated-tests', 'bundles', 'cache-http', 'command-line', 'controllers', 'dependency-injection', 'forms', 'http', 'misc', 'php', 'routing', 'security', 'standardization', 'symfony3', 'templating', 'validation']; $em = $this->getContainer()->get('doctrine.orm.entity_manager'); foreach ($filesToParse as $fileName) { $file = $yaml->parse(file_get_contents('app/Resources/symfony-pack-questions/' . $fileName . '.yml')); $category = new Category(); $category->setName($file['category']); $em->persist($category); foreach ($file['questions'] as $question) { $questionEntity = new Question(); $questionEntity->setCategory($category); $questionEntity->setStatement($question['question']); $em->persist($questionEntity); foreach ($question['answers'] as $answer) { $answerEntity = new Answer(); $answerEntity->setStatement($answer['value']); $answerEntity->setVeracity($answer['correct']); $answerEntity->setQuestion($questionEntity); $em->persist($answerEntity); } } } $em->flush(); $output->writeln('<info>=== Import of Symfony Pack Questions Successful ===</info>'); }
/** * @param Request $request * @return JsonResponse */ public function submitAnswerAction(Request $request) { $answer = $request->get('answer'); $questionId = $request->get('questionId'); $type = $request->get('type'); if (empty($answer)) { $answer = 'Keine Antwort'; } if (!empty($questionId)) { $repository = $this->getDoctrine()->getRepository('AppBundle:Question'); $question = $repository->findOneBy(array('id' => $questionId)); $repository = $this->getDoctrine()->getRepository('AppBundle:User'); $user = $repository->findOneBy(array('id' => 1)); $questionAnswer = new Answer(); $questionAnswer->setAnswer($answer); $questionAnswer->setCreatedAt(new \DateTime()); $questionAnswer->setQuestion($question); $questionAnswer->setUser($user); $em = $this->getDoctrine()->getManager(); $em->persist($questionAnswer); $em->flush(); return new JsonResponse(array('success' => 1, 'message' => 'answer saved')); } return new JsonResponse(array('success' => 0, 'message' => 'some data is missing')); }
public function load(ObjectManager $manager) { $answer = new Answer(); $answer->setText('Test answer'); $answer->setQuestion($this->getReference('test_question_1')); $manager->persist($answer); $manager->flush(); }
public function insertIncorrect(Question $question) { $em = $this->doctrine->getManager(); $answer = new Answer(); $answer->setQuestion($question); $answer->setCorrectly(true); $answer->setTextAnswer('There is no correct answer'); $em->persist($answer); return; }
/** * @Route("/question/{id}/reponse/ajouter", name="answer_add") */ public function addAction(Request $request, Question $question) { $answer = new Answer(); $answer->setQuestion($question); $form = $this->createForm(new AnswerType(), $answer); $form->handleRequest($request); if ($form->isValid() && $form->isSubmitted()) { $em = $this->getDoctrine()->getEntityManager(); $em->persist($answer); $em->flush(); return $this->redirect($this->generateUrl('question_show', ['id' => $question->getId()])); } return $this->render(':answer:add.html.twig', ['form' => $form->createView(), 'answer' => $answer]); }
private function handleRequest(Answer $entity, Request $request) { $data = $request->getContent(); if (isset($data['question'])) { $entity->setQuestion($this->findQuestion($data['question'])); } 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']); } }
/** * Create an empty question object and persist it. * * @param $questionnaire - questionnaire to which to add the question to. * @return Question newly created and persisted question. */ private function createEmptyQuestion($questionnaire) { // Create a new question. $question = new Question(); $question->setType('SINGLE'); $question->setQuestionnaire($questionnaire); // Get the entity manager. $em = $this->getDoctrine()->getManager(); // Create a empty answer and persist it. $answer = new Answer(); $answer->setQuestion($question); $em->persist($answer); // Assign the empty answer. $question->addAnswer($answer); // Persist the question. $em->persist($question); $em->flush(); return $question; }
/** * @param Answer $answer * * @return Question */ public function addAnswer(Answer $answer) { $this->answers[] = $answer; $answer->setQuestion($this); return $this; }
/** * @Route("/teacher/quiz/question/{question}", name="editQuestion") */ public function editQuestionAction(Request $request, $question) { $em = $this->getDoctrine()->getManager(); $question = $em->getRepository('AppBundle:Question')->find($question); $answers = $em->getRepository('AppBundle:Answer')->createQueryBuilder('a'); $answers = $answers->where('a.question=' . $question->getId())->andWhere('a.enabled = 1')->getQuery()->getResult(); $quiz = $question->getQuiz(); $questions = $em->getRepository('AppBundle:Question')->findByQuiz($quiz); $image = $em->getRepository('AppBundle:QuestionImage')->findOneByQuestion($question); if ($image) { $image = $image->getWebPath(); } $correct = $em->getRepository('AppBundle:Answer')->createQueryBuilder('c'); $correct = $correct->where('c.points > 0')->andWhere('c.question=' . $question->getId())->andWhere('c.enabled = 1')->getQuery()->getResult(); $answer = new Answer(); $answer->setEnabled(1); $answer->setQuestion($question); $form = $this->get('form.factory')->createNamedBuilder('answer', FormType::class, $answer)->add('answer')->add('points')->add('save', SubmitType::class, array('label' => "Dodaj odpowiedź"))->getForm(); $form->handleRequest($request); // if($form->isValid()){ // if($answer->getPoints()>=1 && $correct){ // $this->addFlash('notice','W tym pytaniu istnieje już poprawna odpowiedź!'); // return $this->render('teacher/edit_question.html.twig', array( // 'question' => $question, // 'questions' => $questions, // 'quiz' => $quiz, // 'answers'=>$answers, // 'add_answer'=>$form->createView(), // 'add_question'=>$formq->createView(), // 'add_image'=>$formfile->createView(), // 'image'=>$image, // )); // } // $em->persist($answer); // $em->flush(); // $answers = $em->getRepository('AppBundle:Answer')->findByQuestion($question); // } $question_new = new Question(); $formq = $this->get('form.factory')->createNamedBuilder('question', FormType::class, $question_new)->add('question', TextareaType::class)->add('save', SubmitType::class, array('label' => 'Dodaj'))->getForm(); $formq->handleRequest($request); if ($formq->isValid()) { $question_new->setEnabled(1); $question_new->setQuiz($quiz); $em->persist($question_new); $em->flush(); return $this->redirectToRoute('editQuestion', array('question' => $question_new->getId())); } $file = new QuestionImage(); $formfile = $this->get('form.factory')->createNamedBuilder('image', FormType::class, $file)->add('file')->getForm(); $file->setQuestion($question); if ($request->request->has('image')) { $formfile->handleRequest($request); } if ($formfile->isValid()) { $file->upload(); $em->persist($file); $em->flush(); $this->addFlash('notice', 'Pomyślnie dodano obrazek do pytania.'); } if ($form->isValid()) { if ($answer->getPoints() >= 1 && $correct) { $this->addFlash('notice', 'W tym pytaniu istnieje już poprawna odpowiedź!'); return $this->render('teacher/edit_question.html.twig', array('question' => $question, 'questions' => $questions, 'quiz' => $quiz, 'answers' => $answers, 'add_answer' => $form->createView(), 'add_question' => $formq->createView(), 'add_image' => $formfile->createView(), 'image' => $image)); } $em->persist($answer); $em->flush(); $answers = $em->getRepository('AppBundle:Answer')->createQueryBuilder('a'); $answers = $answers->where('a.question=' . $question->getId())->andWhere('a.enabled = 1')->getQuery()->getResult(); } return $this->render('teacher/edit_question.html.twig', array('question' => $question, 'questions' => $questions, 'quiz' => $quiz, 'answers' => $answers, 'add_answer' => $form->createView(), 'add_question' => $formq->createView(), 'add_image' => $formfile->createView(), 'image' => $image)); }
/** * Add answers * * @param \AppBundle\Entity\Answer $answers * @return Question */ public function addAnswer(\AppBundle\Entity\Answer $answers) { $answers->setQuestion($this); $this->answers[] = $answers; return $this; }
/** * @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)); }