/** * Lists all Question entities. * * @Route("/questions") * @Method("GET") * * @return JsonResponse * * @author João Paulo Cercal <*****@*****.**> * @version 0.1 */ public function questionsAction() { $repository = $this->get('doctrine')->getRepository('CekurteZCPEBundle:Question'); $entityFilter = new Question(); $entityFilter->setDeleted(false); $entityFilter->setApproved(true); $result = $repository->getQuery($entityFilter, 'ck.id', 'asc')->getResult(); $data = array(); foreach ($result as $item) { $dataItem = array('id' => $item->getId(), 'google_groups_id' => $item->getGoogleGroupsId(), 'question' => $item->getTitle(), 'question_type' => array('id' => $item->getQuestionType()->getId(), 'type' => $item->getQuestionType()->getTitle()), 'difficulty' => $item->getDifficulty(), 'comment' => $item->getComment(), 'created_at' => $item->getCreatedAt(), 'created_by' => $item->getCreatedBy()->getName()); foreach ($item->getCategory() as $row) { $dataItem['categories'][] = $row->getId(); } foreach ($item->getQuestionHasAnswer() as $row) { $dataItem['alternatives'][] = array('id' => $row->getAnswer()->getId(), 'is_correct' => $row->isCorrect(), 'alternative' => $row->getAnswer()->getTitle()); } $data[] = $dataItem; } return new JsonResponse($data); }
/** * @Route("/admin/parser-database-content") * @Method("GET") * @Template("CekurteZCPEBundle:Default:parser.html.twig") */ public function parserDatabaseContentAction() { set_time_limit(0); $em = $this->get('doctrine')->getManager(); $itens = $em->getRepository('CekurteZCPEBundle:Parser')->findAll(); foreach ($itens as $item) { $subject = $item->getSubject(); $content = $item->getContent(); if (preg_match_all("/[\r\n]+[^:alpha]{1}\\s*[\\:\\)\\/]{1}\\s*(.*)/", $content, $matches)) { $answers = end($matches); $answersComplete = $matches[0]; $question = $content; foreach ($answersComplete as $answer) { $question = str_replace($answer, '', $question); } } if (preg_match_all("/[\r\n]+[^:alpha]{1}\\s*[\\:\\)\\/]{1}\\s*(.*)/", $content, $matches)) { $answers = end($matches); $answersComplete = $matches[0]; $question = $content; foreach ($answersComplete as $answer) { $question = str_replace($answer, '', $question); } } $questionType = 'Single Choice'; if (preg_match_all("/.*(\\_{5}).*/", $content, $matches)) { $questionType = 'Text'; } if (preg_match("/\\(\\s*choose\\s*(\\d+)\\)/i", $content, $matches)) { $choose = (int) end($matches); if ($choose > 1) { $questionType = 'Multiple Choice'; } $question = str_replace($matches[0], '', $question); } if (preg_match("/\\d{1,4}/", $subject, $matches)) { $googleGroupsId = (int) $matches[0]; } $category = array(); if (preg_match("/(Categoria|Category){1}\\:{1}\\s+(.*)\\./i", $content, $matches)) { $category = explode(', ', end($matches)); } $entity = $em->getRepository('CekurteZCPEBundle:Question')->findOneBy(array('googleGroupsId' => $googleGroupsId)); if (!$entity instanceof Question) { $entity = new Question(); $entity->setDeleted(false)->setCreatedBy($this->getUser())->setCreatedAt($item->getCreatedAt())->setGoogleGroupsId($googleGroupsId)->setGoogleGroupsAuthor($item->getAuthor())->setTitle(trim(nl2br($question)))->setComment($content); $questionTypeEntity = $em->getRepository('CekurteZCPEBundle:QuestionType')->findOneBy(array('title' => trim($questionType))); if ($questionTypeEntity instanceof QuestionType) { $entity->setQuestionType($questionTypeEntity); } if (!empty($category)) { foreach ($category as $title) { $categoryEntity = $em->getRepository('CekurteZCPEBundle:Category')->findOneBy(array('title' => trim($title))); if ($categoryEntity instanceof Category) { $entity->addCategory($categoryEntity); } } } if (!empty($answers)) { $em->persist($entity); $em->flush(); $entity->setApproved(false)->setImportedFromGoogleGroups(true)->setDifficulty(0)->setEmailHasSent(true); $em->persist($entity); $em->flush(); foreach ($answers as $answer) { $answerEntity = new Answer(); $answerEntity->setTitle($answer)->setDeleted(false)->setCreatedBy($this->getUser())->setCreatedAt(new \DateTime('NOW')); $em->persist($answerEntity); $em->flush(); $answerEntityRel = new QuestionHasAnswer(); $answerEntityRel->setAnswer($answerEntity)->setQuestion($entity)->setCorrect(false); $em->persist($answerEntityRel); $em->flush(); } } } } return array(); }