Skip to content

Commit

Permalink
Portfolio: Allow reuse comments - refs BT#18201
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Sep 9, 2022
1 parent be7b972 commit bc12c8a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
26 changes: 26 additions & 0 deletions main/inc/ajax/portfolio.ajax.php
Expand Up @@ -3,6 +3,7 @@
/* For licensing terms, see /license.txt */

use Chamilo\CoreBundle\Entity\Portfolio;
use Chamilo\CoreBundle\Entity\PortfolioComment;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -18,6 +19,7 @@
$em = Database::getManager();

$item = null;
$comment = null;

if ($httpRequest->query->has('item')) {
/** @var Portfolio $item */
Expand All @@ -27,6 +29,13 @@
);
}

if ($httpRequest->query->has('comment')) {
$comment = $em->find(
PortfolioComment::class,
$httpRequest->query->getInt('comment')
);
}

$httpResponse = Response::create();

switch ($action) {
Expand All @@ -48,6 +57,23 @@
]
);
break;
case 'find_template_comment':
if (!$comment) {
$httpResponse->setStatusCode(Response::HTTP_NOT_FOUND);
break;
}

if (!$comment->isTemplate() || $comment->getAuthor() !== $currentUser) {
$httpResponse->setStatusCode(Response::HTTP_FORBIDDEN);
break;
}

$httpResponse = JsonResponse::create(
[
'content' => $comment->getContent(),
]
);
break;
}

$httpResponse->send();
82 changes: 81 additions & 1 deletion main/inc/lib/PortfolioController.php
Expand Up @@ -1148,9 +1148,20 @@ public function view(Portfolio $item)
},
'childClose' => '</div></article>',
'nodeDecorator' => function ($node) use ($commentsRepo, $clockIcon, $item) {
$commentActions = [];
/** @var PortfolioComment $comment */
$comment = $commentsRepo->find($node['id']);

if ($this->commentBelongsToOwner($comment)) {
$commentActions[] = Display::url(
Display::return_icon(
$comment->isTemplate() ? 'wizard.png' : 'wizard_na.png',
$comment->isTemplate() ? get_lang('RemoveAsTemplate') : get_lang('AddAsTemplate')
),
$this->baseUrl.http_build_query(['action' => 'template_comment', 'id' => $comment->getId()])
);
}

$commentActions[] = Display::url(
Display::return_icon('discuss.png', get_lang('ReplyToThisComment')),
'#',
Expand Down Expand Up @@ -2604,6 +2615,29 @@ public function markAsTemplate(Portfolio $item)
exit;
}

public function markAsTemplateComment(PortfolioComment $comment)
{
if (!$this->commentBelongsToOwner($comment)) {
api_not_allowed(true);
}

$comment->setIsTemplate(
!$comment->isTemplate()
);

Database::getManager()->flush();

Display::addFlash(
Display::return_message(
$comment->isTemplate() ? get_lang('PortfolioCommentSetAsTemplate') : get_lang('PortfolioCommentUnsetAsTemplate'),
'success'
)
);

header("Location: $this->baseUrl".http_build_query(['action' => 'view', 'id' => $comment->getItem()->getId()]));
exit;
}

/**
* @param bool $showHeader
*/
Expand Down Expand Up @@ -2745,6 +2779,11 @@ private function itemBelongToOwner(Portfolio $item): bool
return true;
}

private function commentBelongsToOwner(PortfolioComment $comment): bool
{
return $comment->getAuthor() === $this->owner;
}

private function createFormTagFilter(bool $listByUser = false): FormValidator
{
$extraField = new ExtraField('portfolio');
Expand Down Expand Up @@ -3070,8 +3109,30 @@ private function createCommentForm(Portfolio $item): string
{
$formAction = $this->baseUrl.http_build_query(['action' => 'view', 'id' => $item->getId()]);

$templates = $this->em
->getRepository(PortfolioComment::class)
->findBy(
[
'isTemplate' => true,
'author' => $this->owner,
]
);

$form = new FormValidator('frm_comment', 'post', $formAction);
$form->addHeader(get_lang('AddNewComment'));
$form->addSelectFromCollection(
'template',
[
get_lang('Template'),
null,
'<span id="portfolio-spinner" class="fa fa-fw fa-spinner fa-spin" style="display: none;"
aria-hidden="true" aria-label="'.get_lang('Loading').'"></span>',
],
$templates,
[],
true,
'getExcerpt'
);
$form->addHtmlEditor('content', get_lang('Comments'), true, false, ['ToolbarSet' => 'Minimal']);
$form->addHidden('item', $item->getId());
$form->addHidden('parent', 0);
Expand Down Expand Up @@ -3118,7 +3179,26 @@ private function createCommentForm(Portfolio $item): string
exit;
}

return $form->returnForm();
$js = '<script>
$(function() {
$(\'#frm_comment_template\').on(\'change\', function () {
$(\'#portfolio-spinner\').show();
$.getJSON(_p.web_ajax + \'portfolio.ajax.php?a=find_template_comment&comment=\' + this.value)
.done(function(response) {
CKEDITOR.instances.content.setData(response.content);
})
.fail(function () {
CKEDITOR.instances.content.setData(\'\');
})
.always(function() {
$(\'#portfolio-spinner\').hide();
});
});
});
</script>';

return $form->returnForm().$js;
}

private function generateAttachmentList($post, bool $includeHeader = true): string
Expand Down
1 change: 1 addition & 0 deletions main/install/configuration.dist.php
Expand Up @@ -1062,6 +1062,7 @@
ALTER TABLE portfolio_comment ADD CONSTRAINT FK_C2C17DA2126F525E FOREIGN KEY (item_id) REFERENCES portfolio (id) ON DELETE CASCADE;
ALTER TABLE portfolio_comment ADD CONSTRAINT FK_C2C17DA2A977936C FOREIGN KEY (tree_root) REFERENCES portfolio_comment (id) ON DELETE CASCADE;
ALTER TABLE portfolio_comment ADD CONSTRAINT FK_C2C17DA2727ACA70 FOREIGN KEY (parent_id) REFERENCES portfolio_comment (id) ON DELETE CASCADE;
ALTER TABLE portfolio_comment ADD is_template TINYINT(1) DEFAULT '0' NOT NULL;
ALTER TABLE portfolio_category ADD parent_id INT(11) NOT NULL DEFAULT 0;
*/
// In 1.11.8, before enabling this feature, you also need to:
Expand Down
11 changes: 11 additions & 0 deletions main/portfolio/index.php
Expand Up @@ -296,6 +296,17 @@

$controller->markAsTemplate($item);
break;
case 'template_comment':
$id = $httpRequest->query->getInt('id');

$comment = $em->find(PortfolioComment::class, $id);

if (empty($comment)) {
break;
}

$controller->markAsTemplateComment($comment);
break;
case 'list':
default:
$controller->index($httpRequest);
Expand Down
19 changes: 19 additions & 0 deletions src/Chamilo/CoreBundle/Entity/PortfolioComment.php
Expand Up @@ -114,6 +114,13 @@ class PortfolioComment
*/
private $score;

/**
* @var bool
*
* @ORM\Column(name="is_template", type="boolean", options={"default": false})
*/
private $isTemplate = false;

/**
* PortfolioComment constructor.
*/
Expand Down Expand Up @@ -249,4 +256,16 @@ public function getLvl(): int
{
return $this->lvl;
}

public function isTemplate(): bool
{
return $this->isTemplate;
}

public function setIsTemplate(bool $isTemplate): PortfolioComment
{
$this->isTemplate = $isTemplate;

return $this;
}
}

0 comments on commit bc12c8a

Please sign in to comment.