/** * 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); }
/** * Search for records based on an entity * * @param Question $entity * @param string $sort * @param string $direction * @return \Doctrine\ORM\Query * * @author João Paulo Cercal <*****@*****.**> * @version 0.1 */ public function getQuery(Question $entity, $sort, $direction) { $queryBuilder = $this->createQueryBuilder('ck'); $data = array('createdBy' => $entity->getCreatedBy(), 'googleGroupsId' => $entity->getGoogleGroupsId(), 'title' => $entity->getTitle(), 'difficulty' => $entity->getDifficulty(), 'comment' => $entity->getComment(), 'updatedBy' => $entity->getUpdatedBy()); if (!empty($data['questionType'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.questionType', ':questionType'))->setParameter('questionType', $data['questionType']); } if (!empty($data['answer'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.answer', ':answer'))->setParameter('answer', $data['answer']); } if (!empty($data['category'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.category', ':category'))->setParameter('category', $data['category']); } if (!empty($data['tag'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.tag', ':tag'))->setParameter('tag', $data['tag']); } if (!empty($data['deletedBy'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.deletedBy', ':deletedBy'))->setParameter('deletedBy', $data['deletedBy']); } if (!empty($data['updatedBy'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.updatedBy', ':updatedBy'))->setParameter('updatedBy', $data['updatedBy']); } if (!empty($data['createdBy'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.createdBy', ':createdBy'))->setParameter('createdBy', $data['createdBy']); } if (!empty($data['googleGroupsId'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.googleGroupsId', ':googleGroupsId'))->setParameter('googleGroupsId', $data['googleGroupsId']); } if (!empty($data['title'])) { $queryBuilder->andWhere($queryBuilder->expr()->like('ck.title', ':title'))->setParameter('title', "%{$data['title']}%"); } if (!empty($data['difficulty'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.difficulty', ':difficulty'))->setParameter('difficulty', $data['difficulty']); } if (!empty($data['comment'])) { $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.comment', ':comment'))->setParameter('comment', $data['comment']); } if (!empty($data['deletedAt'])) { $queryBuilder->andWhere($queryBuilder->expr()->between('ck.deletedAt', ':deletedAtFrom', ':deletedAtTo'))->setParameter('deletedAtFrom', $data['deletedAt'])->setParameter('deletedAtTo', $data['deletedAt']); } if (!empty($data['updatedAt'])) { $queryBuilder->andWhere($queryBuilder->expr()->between('ck.updatedAt', ':updatedAtFrom', ':updatedAtTo'))->setParameter('updatedAtFrom', $data['updatedAt'])->setParameter('updatedAtTo', $data['updatedAt']); } if (!empty($data['createdAt'])) { $queryBuilder->andWhere($queryBuilder->expr()->between('ck.createdAt', ':createdAtFrom', ':createdAtTo'))->setParameter('createdAtFrom', $data['createdAt'])->setParameter('createdAtTo', $data['createdAt']); } $queryBuilder->andWhere($queryBuilder->expr()->eq('ck.deleted', ':deleted'))->setParameter('deleted', false); return $queryBuilder->orderBy($sort, $direction)->getQuery(); }
/** * @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(); }
/** * Get subject * * @param Question $question * * @return string */ protected function getSubject(Question $question) { $container = $this->getContainer(); return sprintf('[%s] %s: %s', $container->getParameter('cekurte_zcpe_google_group_name'), $container->getParameter('cekurte_zcpe_google_group_subject'), $question->getGoogleGroupsId()); }