/**
  * @Route(
  *      "/{resourceId}/drops/detail/correction/{state}/{correctionId}/document/{documentId}",
  *      name="innova_collecticiel_drops_detail_comment",
  *      requirements={"documentId" = "\d+", "resourceId" = "\d+", "correctionId" = "\d+", "state" = "show|edit|preview"},
  *      defaults={"page" = 1}
  * )
  * @Route(
  *      "/{resourceId}/drops/detail/correction/{state}/{correctionId}/{page}",
  *      name="innova_collecticiel_drops_detail_correction_paginated",
  *      requirements={"resourceId" = "\d+", "correctionId" = "\d+", "page" = "\d+", "state" = "show|edit|preview"}
  * )
  * @ParamConverter("dropzone", class="InnovaCollecticielBundle:Dropzone", options={"id" = "resourceId"})
  * @ParamConverter("document", class="InnovaCollecticielBundle:Document", options={"id" = "documentId"})
  * @ParamConverter("user", options={
  *      "authenticatedUser" = true,
  *      "messageEnabled" = true,
  *      "messageTranslationKey" = "Correct an evaluation requires authentication. Please login.",
  *      "messageTranslationDomain" = "innova_collecticiel"
  * })
  * @Template()
  */
 public function dropsDetailCommentAction(Dropzone $dropzone, $state, $correctionId, $page, User $user, Document $document)
 {
     $documentOri = new Document();
     $documentOri = $document;
     $em = $this->getDoctrine()->getManager();
     $this->get('innova.manager.dropzone_voter')->isAllowToOpen($dropzone);
     $correction = $this->getDoctrine()->getRepository('InnovaCollecticielBundle:Correction')->getCorrectionAndDropAndUserAndDocuments($dropzone, $correctionId);
     $countCorrection = count($correction);
     // Parcours des documents sélectionnés
     foreach ($correction->getDrop()->getDocuments() as $document) {
         if ($document->getId() == $documentOri->getId()) {
             $documentId = $document->getId();
             // Ajout pour avoir les commentaires et qui les a lu.
             // Lire les commentaires et les passer à la vue
             $comments = $this->getDoctrine()->getRepository('InnovaCollecticielBundle:Comment')->findBy(array('document' => $documentId));
             // Parcours des commentaires des documents sélectionnés
             foreach ($comments as $comment) {
                 $commentId = $comment->getId();
                 //                echo "Comment = " . $commentId . " - " . $correction->getUser()->getId();
                 $comments_read = $this->getDoctrine()->getRepository('InnovaCollecticielBundle:CommentRead')->findBy(array('comment' => $commentId, 'user' => $correction->getUser()->getId()));
                 // Nombre de lectures du commentaire pour cet utilisateur pour ce commentaire du document
                 $countCommentRead = count($comments_read);
                 //                    $countCommentRead = 0; // Pour les tests, à enlever
                 // Ce commentaire n'avait pas été lu.
                 // Donc, maintenant, il va l'être,
                 // je dois donc insérer une occurrence dans la table CommentRead";
                 if ($countCommentRead == 0) {
                     $comment_read_add = new CommentRead();
                     $comment_read_add->setComment($comment);
                     $comment_read_add->setUser($user);
                     $em->persist($comment_read_add);
                     $unitOfWork = $em->getUnitOfWork();
                     $unitOfWork->computeChangeSets();
                     $dropzoneChangeSet = $unitOfWork->getEntityChangeSet($dropzone);
                     $event = new LogCommentReadCreateEvent($dropzone, $dropzoneChangeSet, $comment_read_add);
                     $this->dispatch($event);
                 }
             }
             $em->flush();
             // Fin ajout.
         }
     }
     $userId = $this->get('security.token_storage')->getToken()->getUser()->getId();
     if ($state == 'preview') {
         if ($correction->getDrop()->getUser()->getId() != $userId) {
             throw new AccessDeniedException();
         }
     } else {
         //            $this->get('innova.manager.dropzone_voter')->isAllowToEdit($dropzone);
     }
     //$this->checkUserGradeAvailable($dropzone);
     /*
     if (!$dropzone->getPeerReview()) {
         return $this->redirect(
             $this->generateUrl(
                 'innova_collecticiel_drops_detail_correction_standard',
                 array(
                     'resourceId' => $dropzone->getId(),
                     'state' => $state,
                     'correctionId' => $correctionId
                 )
             )
         );
     }
     */
     /** @var Correction $correction */
     $edit = $state == 'edit';
     if ($correction == null) {
         throw new NotFoundHttpException();
     }
     if ($edit === true and $correction->getEditable() === false) {
         throw new AccessDeniedException();
     }
     $em = $this->getDoctrine()->getManager();
     $pager = $this->getCriteriaPager($dropzone);
     try {
         $pager->setCurrentPage($page);
     } catch (NotValidCurrentPageException $e) {
         throw new NotFoundHttpException();
     }
     $oldData = array();
     $grades = array();
     if ($correction !== null) {
         $grades = $em->getRepository('InnovaCollecticielBundle:Grade')->findByCriteriaAndCorrection($pager->getCurrentPageResults(), $correction);
         foreach ($grades as $grade) {
             $oldData[$grade->getCriterion()->getId()] = $grade->getValue() >= $dropzone->getTotalCriteriaColumn() ? $dropzone->getTotalCriteriaColumn() - 1 : $grade->getValue();
         }
     }
     $formComment = $this->createForm(new CommentType(new Comment(), null));
     $form = $this->createForm(new CorrectionCriteriaPageType(), $oldData, array('edit' => $edit, 'criteria' => $pager->getCurrentPageResults(), 'totalChoice' => $dropzone->getTotalCriteriaColumn()));
     if ($edit) {
         if ($this->getRequest()->isMethod('POST') and $correction !== null) {
             $form->handleRequest($this->getRequest());
             if ($form->isValid()) {
                 $data = $form->getData();
                 foreach ($data as $criterionId => $value) {
                     $this->persistGrade($grades, $criterionId, $value, $correction);
                 }
                 if ($correction->getFinished()) {
                     $totalGrade = $this->get('innova.manager.correction_manager')->calculateCorrectionTotalGrade($dropzone, $correction);
                     $correction->setTotalGrade($totalGrade);
                     $em->persist($correction);
                     $em->flush();
                 }
                 $goBack = $form->get('goBack')->getData();
                 if ($goBack == 1) {
                     $pageNumber = max($page - 1, 0);
                     return $this->redirect($this->generateUrl('innova_collecticiel_drops_detail_correction_paginated', array('resourceId' => $dropzone->getId(), 'state' => 'edit', 'correctionId' => $correction->getId(), 'page' => $pageNumber)));
                 } else {
                     if ($pager->getCurrentPage() < $pager->getNbPages()) {
                         return $this->redirect($this->generateUrl('innova_collecticiel_drops_detail_correction_paginated', array('resourceId' => $dropzone->getId(), 'state' => 'edit', 'correctionId' => $correction->getId(), 'page' => $page + 1)));
                     } else {
                         return $this->redirect($this->generateUrl('innova_collecticiel_drops_detail_correction_comment', array('resourceId' => $dropzone->getId(), 'state' => 'edit', 'correctionId' => $correction->getId())));
                     }
                 }
             }
         }
     }
     // Appel de la vue qui va gérer l'ajout des commentaires. InnovaERV.
     $view = 'InnovaCollecticielBundle:Correction:correctCriteria.html.twig';
     if ($state == 'show' || $state == 'edit') {
         return $this->render($view, array('workspace' => $dropzone->getResourceNode()->getWorkspace(), '_resource' => $dropzone, 'dropzone' => $dropzone, 'correction' => $correction, 'pager' => $pager, 'form' => $form->createView(), 'formComment' => $formComment->createView(), 'admin' => true, 'edit' => $edit, 'state' => $state, 'document' => $documentOri, 'comments' => $comments, 'user' => $user));
     } else {
         if ($state == 'preview') {
             return $this->render($view, array('workspace' => $dropzone->getResourceNode()->getWorkspace(), '_resource' => $dropzone, 'dropzone' => $dropzone, 'correction' => $correction, 'pager' => $pager, 'form' => $form->createView(), 'formComment' => $formComment->createView(), 'admin' => false, 'edit' => false, 'state' => $state));
         }
     }
 }
 /**
  * @param Dropzone    $dropzone
  * @param mixed       $dropzoneChangeSet
  * @param CommentRead $comment_read
  */
 public function __construct(Dropzone $dropzone, $dropzoneChangeSet, CommentRead $comment_read)
 {
     $details = array('dropzone' => array('id' => $dropzone->getId(), 'changeSet' => $dropzoneChangeSet), 'comment_read' => array('id' => $comment_read->getId(), 'comment' => $comment_read->getComment(), 'user' => $comment_read->getUser()));
     parent::__construct($dropzone->getResourceNode(), $details);
 }