Portfolio: Allow delete comments - refs BT#18201

pull/4470/head
Angel Fernando Quiroz Campos 3 years ago
parent 7838a8ca9a
commit 0c8825b91d
  1. 30
      main/inc/lib/PortfolioController.php
  2. 9
      main/portfolio/index.php
  3. 18
      src/Chamilo/CoreBundle/Entity/Repository/PortfolioAttachmentRepository.php

@ -1266,6 +1266,10 @@ class PortfolioController
Display::return_icon('edit.png', get_lang('Edit')),
$this->baseUrl.http_build_query(['action' => 'edit_comment', 'id' => $comment->getId()])
);
$commentActions[] = Display::url(
Display::return_icon('delete.png', get_lang('Delete')),
$this->baseUrl.http_build_query(['action' => 'delete_comment', 'id' => $comment->getId()])
);
}
$nodeHtml = '<div class="pull-right">'.implode(PHP_EOL, $commentActions).'</div>'.PHP_EOL
@ -2961,6 +2965,32 @@ class PortfolioController
);
}
/**
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\ORMException
*/
public function deleteComment(PortfolioComment $comment)
{
if (!$this->commentBelongsToOwner($comment)) {
api_not_allowed(true);
}
$this->em->remove($comment);
$this->em
->getRepository(PortfolioAttachment::class)
->removeFromComment($comment);
$this->em->flush();
Display::addFlash(
Display::return_message(get_lang('CommentDeleted'), 'success')
);
header("Location: $this->baseUrl");
exit;
}
private function isAllowed(): bool
{
$isSubscribedInCourse = false;

@ -314,6 +314,15 @@ switch ($action) {
$controller->editComment($comment);
}
break;
case 'delete_comment':
$id = $httpRequest->query->getInt('id');
$comment = $em->find(PortfolioComment::class, $id);
if (!empty($comment)) {
$controller->deleteComment($comment);
}
break;
case 'tags':
case 'edit_tag':

@ -8,6 +8,8 @@ use Chamilo\CoreBundle\Entity\Portfolio;
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
use Chamilo\CoreBundle\Entity\PortfolioComment;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
/**
* Class PortfolioAttachmentRepository.
@ -26,6 +28,9 @@ class PortfolioAttachmentRepository extends EntityRepository
);
}
/**
* @return array<int, PortfolioComment>
*/
public function findFromComment(PortfolioComment $comment): array
{
return $this->findBy(
@ -35,4 +40,17 @@ class PortfolioAttachmentRepository extends EntityRepository
]
);
}
/**
* @throws OptimisticLockException
* @throws ORMException
*/
public function removeFromComment(PortfolioComment $comment)
{
$comments = $this->findFromComment($comment);
foreach ($comments as $comment) {
$this->_em->remove($comment);
}
}
}

Loading…
Cancel
Save