public function saveAction(Session $s) { $session = $this->get('session')->get('session'); $em = $this->getDoctrine()->getManager(); $s->setEndAt(new \Datetime()); $em->persist($s); $em->flush(); foreach ($session['session']['questions'] as $key => $value) { $question = $em->getRepository('KdvQuizBundle:Question')->find($value['question']); if ($question->getType() == "multiple_text") { foreach ($value['answers'] as $k => $text) { $sa = new SessionAnswer(); $sa->setQuestion($question); $sa->setText($text); $sa->setSession($s); $em->persist($sa); $em->flush(); } } else { $sa = new SessionAnswer(); $sa->setQuestion($question); $answer = $em->getRepository('KdvQuizBundle:Answer')->find($value['answers']); $sa->setAnswer($answer); $sa->setSession($s); $em->persist($sa); $em->flush(); } } return $this->redirect($this->generateUrl('kdv_quiz_session_view', ['session' => $s->getId()])); }
/** * @Template() */ public function indexAction(Quiz $quiz = null) { $em = $this->getDoctrine()->getManager(); if ($quiz == null) { $this->createNotFoundException('Quiz is not find.'); } // New session $session = $this->get('session')->get('session'); if (!isset($session['session']['session_id'])) { $s = new Session(); $s->setBeginAt(new \DateTime())->setUserId(1); $em->persist($s); $em->flush(); $session['session']['session_id'] = $s->getId(); $session['session']['quiz_id'] = $quiz->getId(); } $this->get('session')->set('session', $session); // dump($this->get('session')->get('session')); // Id des questions $questions = $em->getRepository('KdvQuizBundle:Question')->findBy(['quiz' => $quiz->getId()]); foreach ($questions as $key => $value) { $random[] = $value->getId(); } if (isset($session['session']['questions'])) { foreach ($random as $key => $value) { foreach ($session['session']['questions'] as $key2 => $value2) { if ($value2['question'] == $value) { unset($random[$key]); } } } } if (count($random) > 0) { $val = array_rand($random, 1); $question = $em->getRepository('KdvQuizBundle:Question')->createQueryBuilder('q')->where('q.quiz =:quiz')->andWhere('q.id =:id')->setParameter('quiz', $quiz)->setParameter('id', $random[$val])->getQuery()->getSingleResult(); } else { $question = null; } return ['question' => $question, 'questionNumber' => isset($session['session']['questions']) ? count($session['session']['questions']) + 1 : 1]; }