Example #1
0
 public function load(ObjectManager $manager)
 {
     foreach ($this->quiz as $key => $q) {
         $quiz = new Quiz();
         $quiz->setName($q['name']);
         $quiz->setNbQuestions($q['nbQuestions']);
         $manager->persist($quiz);
         $this->addReference('quiz_' . $key, $quiz);
     }
     $manager->flush();
 }
Example #2
0
 /**
  * @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];
 }