Ejemplo n.º 1
0
 /**
  * To know if an user is the creator of Simupoll.
  *
  *
  * @param \CPASimUSante\SimupollBundle\Entity\Simupoll $simupoll
  *
  * @return bool
  */
 public function isGrantedAccess($simupoll, $access)
 {
     $collection = new ResourceCollection(array($simupoll->getResourceNode()));
     if ($this->securityAuth->isGranted($access, $collection)) {
         return true;
     } else {
         return false;
     }
 }
Ejemplo n.º 2
0
 /**
  * @param string   $permission
  * @param Simupoll $simupoll
  *
  * @return bool
  */
 protected function isUserGranted($permission, Simupoll $simupoll, $collection = null)
 {
     if ($collection === null) {
         $collection = new ResourceCollection(array($simupoll->getResourceNode()));
     }
     $checkPermission = false;
     if ($this->get('security.authorization_checker')->isGranted($permission, $collection)) {
         $checkPermission = true;
     }
     return $checkPermission;
 }
Ejemplo n.º 3
0
 /**
  * When opening the Simupoll resource.
  *
  * @EXT\Route("/open/{id}", name="cpasimusante_simupoll_open", requirements={"id" = "\d+"}, options={"expose"=true})
  * @EXT\ParamConverter("simupoll", class="CPASimUSanteSimupollBundle:Simupoll", options={"id" = "id"})
  * @EXT\Template("CPASimUSanteSimupollBundle:Simupoll:open.html.twig")
  *
  * @param Simupoll $simupoll
  *
  * @return array
  */
 public function openAction($simupoll)
 {
     $em = $this->getDoctrine()->getManager();
     $workspace = $simupoll->getResourceNode()->getWorkspace();
     $user = $this->container->get('security.token_storage')->getToken()->getUser();
     //can user access ?
     $this->checkAccess('OPEN', $simupoll);
     //can user edit ?
     $simupollAdmin = $this->container->get('cpasimusante_simupoll.services.simupoll')->isGrantedAccess($simupoll, 'ADMINISTRATE');
     //can user manage exercise
     $allowToCompose = 0;
     if (is_object($user) && $simupollAdmin === true) {
         $allowToCompose = 1;
     }
     //is there a period set or is this the right period to answer ?
     $opened = $this->periodManager->getOpenedPeriod($simupoll->getId());
     //is there any response to this simupoll
     $hasresponse = $this->simupollManager->hasResponse($simupoll);
     //is there any question for this simupoll
     $hasquestion = $this->simupollManager->hasQuestion($simupoll);
     //$hasquestion = ($simupoll->getHasPaper() !== true) ? false : true;
     //is there any category for this simupoll
     $hascategory = $this->simupollManager->hasCategory($simupoll);
     return array('_resource' => $simupoll, '_workspace' => $workspace, 'opened' => $opened, 'allowToCompose' => $allowToCompose, 'simupollAdmin' => $simupollAdmin, 'hasresponse' => $hasresponse, 'hasquestion' => $hasquestion, 'hascategory' => $hascategory);
 }
Ejemplo n.º 4
0
 /**
  * Prepare the data to display the paper.
  *
  * @EXT\Route(
  *      "/open/{id}/{page}/{all}",
  *      name="cpasimusante_simupoll_paper",
  *      defaults={ "page" = 1, "all" = 0 },
  *      requirements={},
  *      options={"expose"=true}
  * )
  * @EXT\Template("CPASimUSanteSimupollBundle:Paper:open.html.twig")
  */
 public function openAction(Request $request, Simupoll $simupoll)
 {
     $sessionFlashBag = $this->session->getFlashBag();
     $workspace = $simupoll->getResourceNode()->getWorkspace();
     $session = $request->getSession();
     $em = $this->getDoctrine()->getManager();
     //can user access this page ?
     $this->checkAccess('OPEN', $simupoll);
     $user = $this->container->get('security.token_storage')->getToken()->getUser();
     $uid = $user->getId();
     $sid = $simupoll->getId();
     //list of papers
     $arrPapersIds = [];
     //list of periods for the Simupoll
     $arrPeriodsIds = [];
     $categoryBounds = [];
     $answers = null;
     $maxNumPaper = 0;
     $current_page = 1;
     $total_page = 1;
     $previous_category = -1;
     $current_category = 0;
     $next_category = -1;
     //get all periods for the Simupoll
     $periods = $this->periodManager->getPeriods($sid);
     // echo '<pre>period id:';var_dump($periods['id']);echo '</pre>';
     // echo '<pre>period title:';var_dump($periods['title']);echo '</pre>';
     // echo '<pre>period current:';var_dump($periods['current']);echo '</pre>';
     /*
              //1 - is the simupoll opened now ? = between start-stop in Period
              //TODO : should not be needed, if the answer button is not shown
              $period_start = '';
              $period_stop = '';
              $period_id = 0;
              $period = null;
              $openedPeriod = $em->getRepository('CPASimUSanteSimupollBundle:Period')
                  ->getOpenedPeriodForSimupoll($sid);
              if ($openedPeriod != null) {
                  $period_id = $openedPeriod[0]->getId();
                  $period_start = $openedPeriod[0]->getStart();
                  $period_stop = $openedPeriod[0]->getStop();
                  $period = $openedPeriod[0];
              } else {
                  //get out
                  return $this->redirect($this->generateUrl('cpasimusante_simupoll_open', array('id' => $sid)));
              }
     */
     //2 - Retrieve the data to drive how the questions are displayed.
     //(all questions, from categories ...) ?
     $displayOrganization = $em->getRepository('CPASimUSanteSimupollBundle:Organization')->findOneBySimupoll($simupoll);
     $choice = 0;
     $choiceData = '';
     //if a display organization has been set
     if (isset($displayOrganization)) {
         $choice = $displayOrganization->getChoice();
         $choiceData = $displayOrganization->getChoiceData();
         //if admin has chosen to display several categories at a time.
         if ($choice == 2) {
             $categoryBounds = explode(',', $choiceData);
             $total_page = count($categoryBounds);
             //default values
             $current_category = $categoryBounds[0];
             //did user choose several categories
             if (isset($categoryBounds[1])) {
                 $next_category = $categoryBounds[1];
             }
         }
     } else {
         //no display organization set : get out
         $sessionFlashBag->add('error', $this->translator->trans('organization_not_set', [], 'resource'));
         return $this->redirect($this->generateUrl('cpasimusante_simupoll_open', ['id' => $sid]));
     }
     // echo '$choice=';echo $choice;echo '<br>$current_category=';echo $current_category;echo '<br>$next_category=';echo $next_category.'<br>';
     //echo '<pre>$periods[id]=<br>';var_dump($periods['id']);echo '</pre>';echo ' user_id='.$uid;echo ' simupoll_id='.$sid;
     //3 - Get paper ids (list of id)
     //periodIds >= paperIds = sessionIds
     //paper already created, and simupaper session exists
     //$session->get('simupaper') = paperid1-periodid1;paperid2-periodid2...
     if ($session->get('simupaper' . $sid) !== null) {
         $tmps = explode(';', $session->get('simupaper' . $sid));
         foreach ($tmps as $tmp) {
             $t = explode('-', $tmp);
             $arrPapersIds[$t[1]] = $t[0];
             $arrPeriodsIds[] = $t[1];
         }
         $maxNumPaper = count($tmps);
         // echo '<br>session exists<br>';echo $session->get('simupaper'.$sid);echo '<br><pre>$arrPapersIds =<br>';var_dump(array_keys($arrPapersIds));echo '</pre>';
         // if (count($arrPapersIds) != count($session_papersIds))
     } else {
         $papers = $em->getRepository('CPASimUSanteSimupollBundle:Paper')->findByUserAndSimupoll($uid, $sid);
         if ($papers != []) {
             foreach ($papers as $paper) {
                 $periodId = $paper->getPeriod()->getId();
                 $arrPapersIds[$periodId] = $paper->getId();
                 $arrPeriodsIds[] = $periodId;
             }
         }
         // echo '<br>session does not exist<br>';echo '<pre>$arrPapersIds =<br>';var_dump($arrPapersIds);echo '</pre>';
         $maxNumPaper = count($papers);
     }
     /*
              //3 - Get paper id
              //paper already created, and simupaper session exists
              if ($session->get('simupaper') != null) {
                  $paperId = $session->get('simupaper');
                  $paper = $em->getRepository('CPASimUSanteSimupollBundle:Paper')
                      ->findOneBy(array('user'=>$uid, 'id' => $paperId, 'period'=>$period_id));
                      //->getPaper($uid, $paperId, $period_id);
                  echo ' a ';
                  //we don't have simupaper session
              } else {
                  $paper = $em->getRepository('CPASimUSanteSimupollBundle:Paper')
                      ->findOneBy(array('user'=>$uid, 'period'=>$period_id));//
                      //->getCurrentPaperInPeriod($uid, $sid, $period_id);
                      //->getCurrentPaper($uid, $sid, $period_start, $period_stop);
                  // no simupaper session but a paper already exists (new login)
                  echo ' b ';
                  if ($paper != array()) {
                      $paperId = $paper->getId();
                  }
                  echo ' c='.$paperId.'<br>';
              }
              echo '<pre>';var_dump(gettype($paper));echo '</pre>';
     */
     //4 - the save part
     if ($request->isMethod('POST')) {
         $next_category = $request->request->get('next');
         $current_category = $request->request->get('current');
         // echo '<br>IN POST 1- current_category '.$current_category.' next_category '.$next_category.'<br><b>current_page='.$current_page;echo '</b><br>';
         //4 - 1 save paper
         $arrPaper = [];
         $tmp = [];
         //for each period
         foreach ($periods['id'] as $key => $per_id) {
             //echo $per_id.' -key='.$key.'<br>';
             //if paper has not been saved
             if (!array_key_exists($per_id, $arrPapersIds)) {
                 //echo 'new paper, period='.$per_id.'<br>';
                 $maxNumPaper = $maxNumPaper + 1;
                 //save the paper
                 $paper = $this->paperManager->savePaper($user, $simupoll, $periods['entity'][$key], $maxNumPaper);
                 $arrPaper[$per_id] = $paper;
                 //session data
                 $tmp[] = $paper->getId() . '-' . $per_id;
             } else {
                 //echo 'paper exists, period='.$per_id.'<br>';
                 $arrPaper[$per_id] = $em->getRepository('CPASimUSanteSimupollBundle:Paper')->findOneById($arrPapersIds[$per_id]);
                 //session data
                 $tmp[] = $arrPapersIds[$per_id] . '-' . $per_id;
             }
         }
         //echo '<pre>$arrPaper =<br>';var_dump(array_keys($arrPaper));echo '</pre>';
         //die();
         //Set session, with simupoll id to avoid bug if several simupoll are set
         $session->set('simupaper' . $sid, implode(';', $tmp));
         //list of answers
         $choices = $request->request->get('choice');
         //4 - 2 Save user answers
         if ($choices != null) {
             //get questions for the selected categories
             $questionList = $em->getRepository('CPASimUSanteSimupollBundle:Question')->getQuestionsWithinCategories($sid, $current_category, $next_category);
             //4 - 2 -1 First, remove the answers already saved
             $qids = [];
             if ($questionList != null) {
                 //get question id array
                 foreach ($questionList as $ql) {
                     $qids[] = $ql['id'];
                 }
                 if ($qids !== []) {
                     //remove old answers, for each paper = corresponding to each period
                     foreach ($arrPapersIds as $perId => $paperId) {
                         //remove values ONLY for curent period
                         if (isset($periods['currentid'][$perId]) && $periods['currentid'][$perId]) {
                             $em->getRepository('CPASimUSanteSimupollBundle:Answer')->deleteOldAnswersInCategories($paperId, $qids);
                         }
                     }
                 }
             }
             //4 - 2 - 2 Then, save the answers
             //$this->paperManager->saveAnswers($choices, $arrPaper);
             foreach ($choices as $key => $propo) {
                 //retrieve data from $key : (question_id - period_id) and $propo : proposition_id
                 $atmp = explode('-', $key);
                 $quest_id = $atmp[0];
                 $per_id = $atmp[1];
                 //get proposition
                 $proposition = $em->getRepository('CPASimUSanteSimupollBundle:Proposition')->findOneById($propo);
                 // get paper
                 //$thepaper = $arrPaper[$per_id];
                 // echo '$per_id'.$per_id;echo '<br></pre>';var_dump(is_object($arrPaper[$per_id]));echo '</pre>';
                 //save answer
                 if (is_object($arrPaper[$per_id])) {
                     $answer = $this->paperManager->saveAnswer($arrPaper[$per_id], $proposition->getQuestion(), $proposition->getId() . ';', $proposition->getMark());
                     /*$answer = new Answer();
                       $answer->setPaper($thepaper);
                       $answer->setQuestion($proposition->getQuestion());
                       $answer->setAnswer($proposition->getId().';');
                       $answer->setMark($proposition->getMark());
                       $em->persist($answer);*/
                     // echo "========<br>";
                     // echo "ANSWER:<br>";
                     // echo "question ".$proposition->getQuestion()->getId().':'.$proposition->getQuestion()->getTitle().'<br>';
                     // echo "answer ".$proposition->getId().' '.$proposition->getMark().'<br>';
                     // echo "========<br>";
                 }
             }
             //The simupoll has been answered to at least once
             if ($simupoll->getHasPaper() !== true) {
                 $simupoll->setHasPaper(true);
                 $em->persist($simupoll);
             }
             $em->flush();
         }
     }
     //echo '2- current_category '.$current_category.' next_category '.$next_category.'<br><b>current_page='.$current_page;echo '</b><br>';
     //5 - get general data for display questions in categories
     //get new bounds
     $tmp_current = $current_category;
     $tmp_next = $next_category;
     $tmp_answers = [];
     if ($choice == 2) {
         if ($next_category == -1) {
             $current_page = $total_page;
         }
         if ($current_category == -1) {
             $current_page = 1;
         }
         if ($request->isMethod('POST')) {
             $direction = $request->request->get('direction');
             //echo 'next ';echo '<pre>next=';var_dump($request->request->get('next'));echo '</pre>';echo '<br>';echo 'direction ';var_dump($direction);echo '<br>';
             //click on Next button
             if ($direction != 'prev') {
                 //get current pos. of "next"
                 $k = array_search($next_category, $categoryBounds);
                 if ($k !== false) {
                     //echo 'k='.$k.'<br>';echo '3next - current_category '.$current_category.' next_category '.$next_category.'<br><b>current_page='.$current_page;echo '</b><br>';
                     $current_category = $next_category;
                     $current_page = $k + 1;
                     //not the last position
                     if (isset($categoryBounds[$k + 1])) {
                         $next_category = $categoryBounds[$k + 1];
                         //echo 'isset<br>';
                         //last pos
                     } else {
                         $next_category = -1;
                     }
                     // $next_category already -1
                 } else {
                     $next_category = -1;
                     //echo 'next, k=false<br>';
                 }
                 //click on Previous button
             } else {
                 $k = array_search($current_category, $categoryBounds);
                 if ($k !== false) {
                     $next_category = $current_category;
                     $current_page = $k;
                     //echo 'k='.$k.'<br>';echo '3prev- current_category '.$current_category.' next_category '.$next_category.'<br><b>current_page='.$current_page;echo '</b><br>';
                     //not the first position
                     if (isset($categoryBounds[$k - 1])) {
                         $current_category = $categoryBounds[$k - 1];
                         //echo 'isset<br>';
                         //last pos
                     } else {
                         $current_category = -1;
                     }
                 } else {
                     $current_category = -1;
                     //echo 'prev, k=false<br>';
                 }
             }
         }
         //echo '4- current_category '.$current_category.' next_category '.$next_category.'<br><b>current_page='.$current_page;echo '</b><br>';
         //manage bounds
         //find categories between the bounds
         $categories = $em->getRepository('CPASimUSanteSimupollBundle:Category')->getCategoriesBetweenLft($sid, $current_category, $next_category);
         //echo '<pre>cats=';foreach($categories as $c){var_dump($c->getId().' '.$c->getName());}echo '</pre>'; //OK
         //find questions and answers for these categories
         $questions = $em->getRepository('CPASimUSanteSimupollBundle:Question')->getQuestionsWithCategories($sid, $current_category, $next_category);
         $answers = $this->paperManager->getAnswers($arrPapersIds, $sid, $answers, $current_category, $next_category);
         /*
         $answers = array();
         foreach ($arrPapersIds as $paperId) {
             $answers = $this->paperManager
                 ->getAnswerDataForPaperInCategorylist($sid, $paperId, $answers, $current_category, $next_category);
         }
         */
         //$answers = array_merge($tmp_answers);
         //echo '<pre>$answers partial =';var_dump($answers);echo '</pre>'; //OK
         //case : all questions shown
     } else {
         //get all categories
         $categories = $em->getRepository('CPASimUSanteSimupollBundle:Category')->findBy(['simupoll' => $simupoll], ['lft' => 'ASC']);
         /*
                      foreach ($arrPapersIds as $paperId) {
                          $tmp_answers = $this->paperManager
                              ->getAnswerDataForPaperInCategorylist($sid, $paperId, $tmp_answers, $current_category, $next_category);
                      }
         */
         $tmp_answers = $this->paperManager->getAnswers($arrPapersIds, $sid, $tmp_answers, $current_category, $next_category);
         $answers = array_merge($tmp_answers);
         //echo '$answers all =<br><pre>';var_dump($answers);echo '</pre><br>';
         //get all questions and propositions
         $questions = $em->getRepository('CPASimUSanteSimupollBundle:Question')->getQuestionsWithCategories($sid);
     }
     // echo '5- current_category '.$current_category.' next_category '.$next_category.'<br><b>current_page='.$current_page;echo '</b><br>';
     // echo '<pre>$arrPapersIds';var_dump($arrPapersIds);echo '</pre>';
     // echo '<pre>$periods';var_dump($periods['current']);echo '</pre>';
     return ['choice' => $choice, 'pids' => $arrPapersIds, 'page' => $current_page, 'total' => $total_page, 'categories' => $categories, 'questions' => $questions, 'answers' => $answers, 'next' => $next_category, 'current' => $current_category, 'periods' => $periods, 'workspace' => $workspace, '_resource' => $simupoll];
 }