Exemplo n.º 1
0
 /**
  * Creates a new exercise paper for a given user.
  *
  * @param Exercise $exercise
  * @param User     $user
  *
  * @return Paper
  */
 public function createPaper(Exercise $exercise, User $user = null)
 {
     // Get the number of the new Paper
     $paperNum = 1;
     if ($user) {
         $lastPaper = $this->om->getRepository('UJMExoBundle:Paper')->findOneBy(['user' => $user, 'exercise' => $exercise], ['start' => 'DESC']);
         if ($lastPaper) {
             $paperNum = $lastPaper->getNumPaper() + 1;
         }
     }
     // Generate the list of Steps and Questions for the Paper
     $order = '';
     if (!empty($lastPaper) && $exercise->getKeepSteps()) {
         // Get steps order from the last user Paper
         $order = $lastPaper->getOrdreQuestion();
     } else {
         // Generate paper step order
         $questions = $this->pickQuestions($exercise);
         foreach ($questions as $question) {
             $order .= $question->getId() . ';';
         }
     }
     // Create the new Paper entity
     $paper = new Paper();
     $paper->setExercise($exercise);
     $paper->setUser($user);
     $paper->setNumPaper($paperNum);
     $paper->setOrdreQuestion($order);
     $paper->setAnonymous($exercise->getAnonymous());
     $this->om->persist($paper);
     $this->om->flush();
     return $paper;
 }
 /**
  * To create a paper in order to take an assessment
  *
  * @access public
  *
  * @param integer $id id of exercise
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function exercisePaperAction($id)
 {
     $exerciseSer = $this->container->get('ujm.exercise_services');
     $user = $this->container->get('security.token_storage')->getToken()->getUser();
     if (!is_object($user)) {
         return $this->redirect($this->generateUrl('ujm_exercise_open', array('exerciseId' => $id)));
     }
     $uid = $user->getId();
     $em = $this->getDoctrine()->getManager();
     $exercise = $em->getRepository('UJMExoBundle:Exercise')->find($id);
     $exoAdmin = $exerciseSer->isExerciseAdmin($exercise);
     $this->checkAccess($exercise);
     $workspace = $exercise->getResourceNode()->getWorkspace();
     if ($exerciseSer->controlDate($exoAdmin, $exercise) === true && ($exercise->getPublished() === true || $exoAdmin === true)) {
         $session = $this->getRequest()->getSession();
         $dql = 'SELECT max(p.numPaper) FROM UJM\\ExoBundle\\Entity\\Paper p ' . 'WHERE p.exercise=' . $id . ' AND p.user='******'UJMExoBundle:Paper')->getPaper($user->getId(), $id);
         //if not exist a paper no finished
         if (count($paper) == 0) {
             if ($exerciseSer->controlMaxAttemps($exercise, $user, $exoAdmin) === false) {
                 return $this->redirect($this->generateUrl('ujm_paper_list', array('exoID' => $id)));
             }
             $paper = new Paper();
             $paper->setNumPaper((int) $maxNumPaper[0][1] + 1);
             $paper->setExercise($exercise);
             $paper->setUser($user);
             $paper->setStart(new \Datetime());
             $paper->setArchive(0);
             $paper->setInterupt(1);
             if ($exercise->getNbQuestion() > 0 && $exercise->getKeepSameQuestion() == true) {
                 $papers = $this->getDoctrine()->getManager()->getRepository('UJMExoBundle:Paper')->getExerciseUserPapers($user->getId(), $id);
                 if (count($papers) == 0) {
                     $tab = $this->prepareInteractionsPaper($id, $exercise);
                     $interactions = $tab['interactions'];
                     $orderInter = $tab['orderInter'];
                     $tabOrderInter = $tab['tabOrderInter'];
                 } else {
                     $lastPaper = $papers[count($papers) - 1];
                     $orderInter = $lastPaper->getOrdreQuestion();
                     $tabOrderInter = explode(';', $lastPaper->getOrdreQuestion());
                     unset($tabOrderInter[count($tabOrderInter) - 1]);
                     $interactions[0] = $em->getRepository('UJMExoBundle:Interaction')->find($tabOrderInter[0]);
                 }
             } else {
                 $tab = $this->prepareInteractionsPaper($id, $exercise);
                 $interactions = $tab['interactions'];
                 $orderInter = $tab['orderInter'];
                 $tabOrderInter = $tab['tabOrderInter'];
             }
             $paper->setOrdreQuestion($orderInter);
             $em->persist($paper);
             $em->flush();
         } else {
             $paper = $paper[0];
             if (!$exercise->getDispButtonInterrupt()) {
                 return $this->forceFinishExercise($paper);
             }
             $tabOrderInter = explode(';', $paper->getOrdreQuestion());
             unset($tabOrderInter[count($tabOrderInter) - 1]);
             $interactions[0] = $em->getRepository('UJMExoBundle:Interaction')->find($tabOrderInter[0]);
         }
         $session->set('tabOrderInter', $tabOrderInter);
         $session->set('paper', $paper->getId());
         $session->set('exerciseID', $id);
         $typeInter = $interactions[0]->getType();
         //To display selectioned question
         return $this->displayQuestion(1, $interactions[0], $typeInter, $exercise->getDispButtonInterrupt(), $exercise->getMaxAttempts(), $workspace, $paper);
     } else {
         return $this->redirect($this->generateUrl('ujm_paper_list', array('exoID' => $id)));
     }
 }