public function getQuestionStats(Question $question, $participants) { $complete = array(); foreach ($question->getAnswers() as $answer) { $trueCount = 0; $falseCount = 0; foreach ($participants as $participant) { foreach ($participant->getAnswers() as $participantAnswer) { // Do not count opinion in stats. if ($participantAnswer->getOpinion() != null) { continue; } if ($participantAnswer->getAnswer()->getId() == $answer->getId()) { if ($participantAnswer->getChecked() == true) { $trueCount++; } else { $falseCount++; } if ($question->getType() == 'SINGLE') { break; } } } } array_push($complete, new AnswerStat($trueCount, $falseCount, $answer->getContent())); } return $complete; }
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>'); }
public function load(ObjectManager $manager) { $question = new Question(); $question->setText('Test question'); $manager->persist($question); $manager->flush(); $this->addReference('test_question_1', $question); }
public static function create(Question $question, $container) { if (isset(self::$mappings[$question->getType()])) { return new self::$mappings[$question->getType()]($question, $container); } else { throw new \RuntimeException("Could not create question of type " . $question->getType()); } }
/** * Displays a form to create a new Question entity. * * @Route("/new/{id}", name="question_new") * @Method("GET") * @Template("AppBundle:Shared:new.html.twig") */ public function newAction($id) { $em = $this->getDoctrine()->getManager(); $questionset = $em->getRepository('AppBundle:Questionset')->find($id); $entity = new Question(); $entity->setQuestionset($questionset); $form = $this->createCreateForm($entity); return array('entity' => $entity, 'form' => $form->createView()); }
/** * @Route("/question/{id}/modifier", name="question_edit") */ public function editAction(Request $request, Question $question) { $category = $question->getCategory(); $form = $this->createForm(new QuestionType(), $question); $form->handleRequest($request); if ($form->isValid() && $form->isSubmitted()) { $em = $this->getDoctrine()->getEntityManager(); $em->flush(); return $this->redirect($this->generateUrl('category_show', ['id' => $category->getId()])); } return $this->render(':question:edit.html.twig', ['form' => $form->createView()]); }
public function checkCount(Question $question) { $answers = $question->getAnswers(); $countTrueAnswers = 0; foreach ($answers as $item) { $item->getCorrectly() ? $countTrueAnswers++ : null; } if ($countTrueAnswers > 3) { $this->session->getFlashBag()->add('notice', 'You have to add not more than 3 true answers for question ' . $question->getTextQuestion()); return false; } return true; }
public function loadQuestions(ObjectManager $manager) { $path = $this->container->get('kernel')->getRootDir() . '/../web/fixtures/' . 'questions.csv'; $csvData = array_map('str_getcsv', file($path)); array_shift($csvData); foreach ($csvData as $csvQuestion) { $question = new Question(); $question->setQuestion($csvQuestion[1]); $question->setAdditional($csvQuestion[2]); $manager->persist($question); } $manager->flush(); }
/** * @Route("/teacher/quiz/edit/{id}", name="editQuiz") */ public function editQuizAction($id, Request $request) { $em = $this->getDoctrine()->getManager(); $quiz = $em->getRepository('AppBundle:Quiz')->find($id); $question = new Question(); $question->setEnabled(1); $form = $this->createFormBuilder($question)->add('question', TextareaType::class)->add('save', SubmitType::class, array('label' => 'Dodaj'))->getForm(); $questrepo = $em->getRepository('AppBundle:Question'); $questions = $questrepo->findBy(array('quiz' => $quiz)); $form->handleRequest($request); if ($form->isValid()) { $question->setQuiz($quiz); $em->persist($question); $em->flush(); return $this->redirectToRoute('editQuestion', array('question' => $question->getId(), 'questions' => $questions)); } return $this->render('teacher/edit_quiz.html.twig', array('quiz' => $quiz, 'add_question' => $form->createView(), 'questions' => $questions)); }
/** * Add questions * * @param \AppBundle\Entity\Question $questions * @return Quiz */ public function addQuestion(\AppBundle\Entity\Question $questions) { $questions->setQuiz($this); $this->questions[] = $questions; return $this; }
/** * @param Question $question * @return array */ private function prepareQuestion(Question $question) { $questionData = array(); $questionData['id'] = $question->getId(); $questionData['title'] = $question->getTitle(); $questionData['content'] = $question->getContent(); $questionData['type'] = $question->getType(); $questionData['nextQuestionId'] = $question->getNextQuestionId(); $questionData['tickLength'] = $question->getTickLength(); /** * @var PotentialAnswer $potentialAnswer */ foreach ($question->getPotentialAnswers() as $potentialAnswer) { $questionData['potentialAnswers'][] = array('id' => $potentialAnswer->getId(), 'answer' => $potentialAnswer->getAnswer(), 'questionId' => $question->getId(), 'nextQuestionId' => $potentialAnswer->getNextQuestionId(), 'realAnswer' => $potentialAnswer->getRealAnswer()); } /** * @var QuestionAttachment $attachment */ foreach ($question->getAttachments() as $attachment) { $questionData['attachments'][] = array('id' => $attachment->getId(), 'title' => $attachment->getTitle(), 'path' => $attachment->getPath(), 'description' => $attachment->getDescription(), 'questionId' => $question->getId()); } return $questionData; }
/** * @Route("/newQuestion/{idQuizz}/{idQuestion}/{ajax}", name="new_question", defaults={"idQuestion" = null}) */ public function newQuestionAction($idQuizz, $idQuestion = null, $ajax = null) { $question = new Question(); $quizz = $this->getDoctrine()->getManager()->getRepository('AppBundle:Quizz')->find($idQuizz); $question->setQuizz($quizz); if ($idQuestion != null) { $question = $this->getDoctrine()->getManager()->getRepository('AppBundle:Question')->find($idQuestion); } $form = $this->createForm(new QuestionType(), $question, array('action' => $this->generateUrl('save_question', array('idQuestion' => $idQuestion)), 'attr' => array('class' => 'modifQuestionForm'))); $nbQuestion = $quizz->getQuestions()->count() + 1; if ($ajax != null) { if ($question->getId() != null) { $form = $this->setFormResponsesValues($form, $question); $nbQuestion = $quizz->getQuestions()->indexOf($question) + 1; } return $this->render(":back:shortQuestionForm.html.twig", array("form" => $form->createView(), 'nbQuestion' => $nbQuestion, 'question' => $question)); } else { return $this->render(":back:new_question.html.twig", array("form" => $form->createView(), 'nbQuestion' => $nbQuestion, 'createQuizz' => true)); } }
/** * 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; }
private function importQuestions(\SPSSReader $SPSS, Dataset $dataset) { $toFlush = array(); foreach ($SPSS->variables as $var) { if ($var->isExtended) { continue; } $index = isset($SPSS->extendedNames[$var->shortName]) ? $SPSS->extendedNames[$var->shortName] : $var->name; // -- Split question id -- $questionSplitted = explode(' ', mb_convert_encoding($var->label, 'UTF-8', 'ISO-8859-7'), 2); $questionSplitted[0] = rtrim($questionSplitted[0], '.'); $tmpSplit = explode('.', $questionSplitted[0], 2); if ($tmpSplit[0] == '3') { $tmpSplit[0] = '9'; } else { if ($tmpSplit[0] == '4') { $tmpSplit[0] = '8'; } else { if ($tmpSplit[0] == '5') { $tmpSplit[0] = '10'; } else { if ($tmpSplit[0] == '6') { $tmpSplit[0] = '12'; } else { if ($tmpSplit[0] == '7') { $tmpSplit[0] = '13'; } else { if ($tmpSplit[0] == '8') { $tmpSplit[0] = '19'; } else { if ($tmpSplit[0] == '9') { $tmpSplit[0] = '23'; } else { if ($tmpSplit[0] == '10') { $tmpSplit[0] = '24'; } else { if ($tmpSplit[0] == '11') { $tmpSplit[0] = '25'; } else { if ($tmpSplit[0] == '12') { $tmpSplit[0] = '36'; } else { if ($tmpSplit[0] == '13') { $tmpSplit[0] = '37'; } else { if ($tmpSplit[0] == '14') { $tmpSplit[0] = '38'; } else { if ($tmpSplit[0] == '15') { $tmpSplit[0] = '53'; } else { if ($tmpSplit[0] == '16') { $tmpSplit[0] = '56'; } } } } } } } } } } } } } } // C $questionSplitted[0] = implode('.', $tmpSplit); // ----------------------- $allQuestions = $this->doctrine->getRepository('AppBundle\\Entity\\Question')->findAll(); foreach ($allQuestions as $curQuestion) { $this->allQuestions[$curQuestion->getQuestionId()] = $curQuestion; } $allAnswers = $this->doctrine->getRepository('AppBundle\\Entity\\Answer')->findAll(); foreach ($allAnswers as $curAnswer) { $this->allAnswers[$curAnswer->getQuestion()->getQuestionId() . '_' . $curAnswer->getAnswerId()] = $curAnswer; } if (!isset($this->allQuestions[$questionSplitted[0]])) { $question = new Question(); $question->setQuestionId($questionSplitted[0]); $question->setQuestion($questionSplitted[1]); $question->setDataset($dataset); $this->allQuestions[$questionSplitted[0]] = $question; } else { $question = $this->allQuestions[$questionSplitted[0]]; $question->getAnswers()->clear(); } if (!$this->isDimension($question->getQuestionId())) { $this->doctrine->getManager()->persist($question); $toFlush[] = $question; $this->questions[$index] = $question; } else { $this->dimensions[$index] = $question; } foreach ($var->valueLabels as $lkey => $lval) { if (!isset($this->allAnswers[$questionSplitted[0] . '_' . $lkey])) { $answer = new Answer(); $answer->setAnswerId($lkey); $this->allAnswers[$questionSplitted[0] . '_' . $lkey] = $answer; } else { $answer = $this->allAnswers[$questionSplitted[0] . '_' . $lkey]; } $answer->setAnswer(mb_convert_encoding($lval, 'UTF-8', 'ISO-8859-7')); $answer->setQuestion($question); $question->getAnswers()->add($answer); if (!$this->isDimension($question->getQuestionId())) { $this->doctrine->getManager()->persist($answer); $toFlush[] = $answer; } } } if (count($toFlush) > 0) { $this->doctrine->getManager()->flush($toFlush); } }
/** * @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)); }
/** * @param Question $question * * @return Category */ public function addQuestion(Question $question) { $this->questions[] = $question; $question->setCategory($this); return $this; }
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']); } }
private function parseGroupFixAnswer(Question $question, User $user) { $answers = array(); $repository = $this->getDoctrine()->getRepository('AppBundle:MultiAnswer'); /** * @var MultiAnswer $multiAnswers */ $multiAnswers = $repository->findBy(array('question_id' => $question->getId(), 'created_by' => $user->getId())); /** * @var MultiAnswer $multiAnswer */ foreach ($multiAnswers as $multiAnswer) { $answers[$multiAnswer->getAnswerKey()] = $multiAnswer->getAnswer(); } return $answers; }
/** * @Route("/Question/create") */ public function createActionQuestion() { $question = new Question(); $question->setQuestionCategoryId('A Foo Bar'); $question->setQuestionTypeId('A Foo Bar'); $question->setQuestionContent('A Foo Bar'); $em = $this->getDoctrine()->getManager(); $em->persist($question); $em->flush(); $questionArray[] = $question->getId(); $questionArray[] = $question->getQuestionCategoryId(); $questionArray[] = $question->getQuestionTypeId(); $questionArray[] = $question->getQuestionContent(); return new Response(json_encode($questionArray)); }
public function __construct(\AppBundle\Entity\Question $question, $answers) { $this->statement = $question->getStatement(); $this->answers = $answers; }