/**
  * @param Section $section
  * @param array   $ids
  *
  * @return array $contributions
  */
 public function compareContributions(Section $section, $ids)
 {
     $contributions = $this->contributionRepository->findyBySectionAndIds($section, $ids);
     $titleDiff = new HtmlDiff($contributions[0]->getTitle(), $contributions[1]->getTitle(), false);
     $textDiff = new HtmlDiff($contributions[0]->getText(), $contributions[1]->getText(), true);
     $contribution = new Contribution();
     $contribution->setText($textDiff->outputDiff()->toString());
     $contribution->setTitle($titleDiff->outputDiff()->toString());
     $contribution->setContributor($contributions[1]->getContributor());
     $contribution->setCreationDate($contributions[1]->getCreationDate());
     $contributions[1] = $contribution;
     return $contributions;
 }
 private function createActiveContributions($tempSections)
 {
     $this->log('Creating active contributions for old (temporary) sections...');
     $em = $this->container->get('doctrine.orm.entity_manager');
     $sectionRepository = $this->container->get('icap.wiki.section_repository');
     foreach ($tempSections as $tempSection) {
         $section = $sectionRepository->findOneBy(array('id' => $tempSection['id']));
         $user = $em->getReference('ClarolineCoreBundle:User', $tempSection['user_id']);
         $activeContribution = new Contribution();
         $activeContribution->setTitle($tempSection['title']);
         $activeContribution->setText($tempSection['text']);
         $activeContribution->setCreationDate(new \DateTime($tempSection['creation_date']));
         $activeContribution->setSection($section);
         $activeContribution->setContributor($user);
         $section->setActiveContribution($activeContribution);
         $em->persist($section);
         $em->flush();
     }
 }
Exemple #3
0
 /**
  * Creates a new non persisted contribution and sets it as section's active contribution.
  *
  * @param \Claroline\CoreBundle\Entity\User $user
  */
 public function setNewActiveContributionToSection(User $user = null)
 {
     $oldActiveContribution = $this->getActiveContribution();
     $newActiveContribution = new Contribution();
     $newActiveContribution->setSection($this);
     if ($oldActiveContribution === null) {
         if ($user === null) {
             $user = $this->getAuthor();
         }
     } else {
         if ($user === null) {
             $user = $oldActiveContribution->getContributor();
         }
         if ($user === null) {
             $user = $this->getAuthor();
         }
         $newActiveContribution->setTitle($oldActiveContribution->getTitle());
         $newActiveContribution->setText($oldActiveContribution->getText());
     }
     $newActiveContribution->setContributor($user);
     $this->setActiveContribution($newActiveContribution);
 }
 /**
  * Imports wiki object from array
  * (see WikiImporter for structure and description).
  *
  * @param array $data
  * @param $rootPath
  * @param $loggedUser
  *
  * @return Wiki
  */
 public function importWiki(array $data, $rootPath, $loggedUser)
 {
     $wiki = new Wiki();
     if (isset($data['data'])) {
         $wikiData = $data['data'];
         $wiki->setMode($wikiData['options']['mode']);
         $sectionsMap = array();
         foreach ($wikiData['sections'] as $section) {
             $entitySection = new Section();
             $entitySection->setWiki($wiki);
             $entitySection->setDeleted($section['deleted']);
             $entitySection->setDeletionDate($section['deletion_date']);
             $entitySection->setCreationDate($section['creation_date']);
             $author = null;
             if ($section['author'] !== null) {
                 $author = $this->userRepository->findOneByUsername($section['author']);
             }
             if ($author === null) {
                 $author = $loggedUser;
             }
             $entitySection->setAuthor($author);
             $parentSection = null;
             if ($section['parent_id'] !== null) {
                 $parentSection = $sectionsMap[$section['parent_id']];
                 $entitySection->setParent($parentSection);
             }
             if ($section['is_root']) {
                 $wiki->setRoot($entitySection);
                 $this->om->persist($wiki);
             }
             foreach ($section['contributions'] as $contribution) {
                 $contributionData = $contribution['contribution'];
                 $entityContribution = new Contribution();
                 $entityContribution->setSection($entitySection);
                 $entityContribution->setTitle($contributionData['title']);
                 $entityContribution->setCreationDate($contributionData['creation_date']);
                 $contributor = null;
                 if ($contributionData['contributor'] !== null) {
                     $contributor = $this->userRepository->findOneByUsername($contributionData['contributor']);
                 }
                 if ($contributor === null) {
                     $contributor = $loggedUser;
                 }
                 $entityContribution->setContributor($contributor);
                 $text = file_get_contents($rootPath . DIRECTORY_SEPARATOR . $contributionData['path']);
                 $entityContribution->setText($text);
                 if ($contributionData['is_active']) {
                     $entitySection->setActiveContribution($entityContribution);
                     if ($parentSection !== null) {
                         $this->sectionRepository->persistAsLastChildOf($entitySection, $parentSection);
                     } else {
                         $this->sectionRepository->persistAsFirstChild($entitySection);
                     }
                 }
                 $this->om->persist($entityContribution);
             }
             $sectionsMap[$section['id']] = $entitySection;
         }
     }
     return $wiki;
 }
 /**
  * Update (or create) a section when creating a new contribution.
  *
  * @param ParamFetcher $paramFetcher
  * @param Wiki         $wiki
  * @param Section      $section
  *
  * @return mixed[]
  *
  * @Route(
  *     requirements={ "wiki" = "\d+", "section" = "\d+" }
  * )
  * @QueryParam(
  *     name="visible",
  *     requirements="(true|false)",
  *     description="Sets the visibility of the section"
  * )
  * @QueryParam(
  *     name="isBrother",
  *     requirements="(true|false|undefined)",
  *     nullable=true,
  *     description="Should the section be treated as sibling of the reference section ?"
  * )
  * @RequestParam(
  *     name="title",
  *     nullable=true,
  *     description="Title of the new contribution"
  * )
  * @RequestParam(
  *     name="text",
  *     nullable=true,
  *     description="Content of the new contribution"
  * )
  * @RequestParam(
  *     name="contributor",
  *     requirements="\d+",
  *     description="ID of the contributor"
  * )
  * @RequestParam(
  *     name="parentSectionId",
  *     requirements="\d+",
  *     description="ID of the parent section"
  * )
  */
 public function postWikiSectionContributionAction(ParamFetcher $paramFetcher, Wiki $wiki, Section $section = null)
 {
     $this->checkAccess('OPEN', $wiki);
     $createSection = empty($section);
     $em = $this->getDoctrine()->getManager();
     $sectionRepository = $this->get('icap.wiki.section_repository');
     $contributor = $this->getDoctrine()->getRepository('ClarolineCoreBundle:User')->findOneById($paramFetcher->get('contributor'));
     $collection = $collection = new ResourceCollection([$wiki->getResourceNode()]);
     $isAdmin = $this->isUserGranted('EDIT', $wiki, $collection);
     // Section ID in URL is equal to 0, we need to create a new section
     if ($createSection) {
         $section = new Section();
         $section->setWiki($wiki);
         $section->setAuthor($contributor);
         $section->setIsWikiAdmin($isAdmin);
         $parentSectionId = $paramFetcher->get('parentSectionId');
         $parent = $this->getSection($wiki, $parentSectionId);
         $sectionRepository->persistAsLastChildOf($section, $parent);
     }
     $contribution = new Contribution();
     $contribution->setTitle($paramFetcher->get('title'));
     $contribution->setText($paramFetcher->get('text'));
     $contribution->setSection($section);
     $contribution->setContributor($contributor);
     $contribution->setCreationDate(new \DateTime());
     $section->setActiveContribution($contribution);
     // Adjust section visibility
     $visible = $paramFetcher->get('visible') === 'true';
     $visibility = $visible && $wiki->getMode() === 0 || $visible && $isAdmin;
     $section->setVisible($visibility);
     $em->persist($section);
     $em->flush();
     if ($createSection) {
         $this->dispatchSectionCreateEvent($wiki, $section);
     } else {
         $unitOfWork = $em->getUnitOfWork();
         $unitOfWork->computeChangeSets();
         $changeSet = $unitOfWork->getEntityChangeSet($section);
         $this->dispatchSectionUpdateEvent($wiki, $section, $changeSet);
     }
     return ['section' => ['id' => $section->getId(), 'visible' => $section->getVisible()], 'sections' => $sectionRepository->buildSectionTree($wiki, $isAdmin, $contributor), 'contribution' => ['id' => $contribution->getId(), 'is_active' => true, 'contributor' => ['last_name' => $contribution->getContributor()->getLastName(), 'first_name' => $contribution->getContributor()->getFirstName(), 'user_name' => $contribution->getContributor()->getUserName()], 'creation_date' => $contribution->getCreationDate(), 'text' => $contribution->getText(), 'title' => $contribution->getTitle()]];
 }