From ea927826d35cd3ddda1e85a810997bc48d1e161f Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Mon, 8 Feb 2021 20:24:30 -0500 Subject: [PATCH] XAPI: Include attachments in portfolio statements - refs BT#18201 --- .../Statement/BaseStatement.php | 58 +++++++++++++++++++ .../Statement/PortfolioItemCommented.php | 15 ++++- .../Statement/PortfolioItemShared.php | 15 ++++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/plugin/xapi/src/ToolExperience/Statement/BaseStatement.php b/plugin/xapi/src/ToolExperience/Statement/BaseStatement.php index 6811cb59d9..2ad87da35b 100644 --- a/plugin/xapi/src/ToolExperience/Statement/BaseStatement.php +++ b/plugin/xapi/src/ToolExperience/Statement/BaseStatement.php @@ -4,10 +4,16 @@ namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement; +use Chamilo\CoreBundle\Entity\PortfolioAttachment; use Chamilo\PluginBundle\XApi\ToolExperience\Activity\Course as CourseActivity; use Chamilo\PluginBundle\XApi\ToolExperience\Activity\Site as SiteActivity; +use Chamilo\UserBundle\Entity\User; +use Xabbuh\XApi\Model\Attachment; use Xabbuh\XApi\Model\Context; use Xabbuh\XApi\Model\ContextActivities; +use Xabbuh\XApi\Model\IRI; +use Xabbuh\XApi\Model\IRL; +use Xabbuh\XApi\Model\LanguageMap; use Xabbuh\XApi\Model\Statement; use Xabbuh\XApi\Model\StatementId; use Xabbuh\XApi\Model\Uuid; @@ -50,4 +56,56 @@ abstract class BaseStatement new ContextActivities(null, $groupingActivities) ); } + + /** + * @param array|PortfolioAttachment[] $portfolioAttachments + * @param \Chamilo\UserBundle\Entity\User $user + * + * @return array|Attachment[] + */ + protected function generateAttachments(array $portfolioAttachments, User $user): array + { + if (empty($portfolioAttachments)) { + return []; + } + + $attachments = []; + + $userDirectory = \UserManager::getUserPathById($user->getId(), 'system'); + $attachmentsDirectory = $userDirectory.'portfolio_attachments/'; + + $langIso = api_get_language_isocode(); + + $cidreq = api_get_cidreq(); + $baseUrl = api_get_path(WEB_CODE_PATH).'portfolio/index.php?'.($cidreq ? $cidreq.'&' : ''); + + foreach ($portfolioAttachments as $portfolioAttachment) { + $attachmentFilename = $attachmentsDirectory.$portfolioAttachment->getPath(); + + $display = LanguageMap::create( + ['und' => $portfolioAttachment->getFilename()] + ); + $description = null; + + if ($portfolioAttachment->getComment()) { + $description = LanguageMap::create( + [$langIso => $portfolioAttachment->getComment()] + ); + } + + $attachments[] = new Attachment( + IRI::fromString('http://id.tincanapi.com/attachment/supporting_media'), + mime_content_type($attachmentFilename), + $portfolioAttachment->getSize(), + hash_file('sha256', $attachmentFilename), + $display, + $description, + IRL::fromString( + $baseUrl.http_build_query(['action' => 'download', 'file' => $portfolioAttachment->getPath()]) + ) + ); + } + + return $attachments; + } } diff --git a/plugin/xapi/src/ToolExperience/Statement/PortfolioItemCommented.php b/plugin/xapi/src/ToolExperience/Statement/PortfolioItemCommented.php index 106d1870e0..ae2399a0fe 100644 --- a/plugin/xapi/src/ToolExperience/Statement/PortfolioItemCommented.php +++ b/plugin/xapi/src/ToolExperience/Statement/PortfolioItemCommented.php @@ -4,6 +4,7 @@ namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement; +use Chamilo\CoreBundle\Entity\PortfolioAttachment; use Chamilo\CoreBundle\Entity\PortfolioComment as PortfolioCommentEntity; use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioComment as PortfolioCommentActivity; use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioItem as PortfolioItemActivity; @@ -35,6 +36,14 @@ class PortfolioItemCommented extends BaseStatement $context = $this->generateContext(); + $em = \Database::getManager(); + $commentAttachments = $em->getRepository(PortfolioAttachment::class)->findFromComment($this->comment); + + $attachments = $this->generateAttachments( + $commentAttachments, + $this->comment->getAuthor() + ); + if ($commentParent) { $repliedVerb = new RepliedVerb(); @@ -54,7 +63,8 @@ class PortfolioItemCommented extends BaseStatement null, $this->comment->getDate(), null, - $context->withContextActivities($contextActivities) + $context->withContextActivities($contextActivities), + $attachments ); } else { $itemShared = new PortfolioItemShared($portfolioItem); @@ -66,7 +76,8 @@ class PortfolioItemCommented extends BaseStatement ->withVerb($commentedVerb->generate()) ->withStored($this->comment->getDate()) ->withResult($statementResult) - ->withContext($context); + ->withContext($context) + ->withAttachments($attachments); } } } diff --git a/plugin/xapi/src/ToolExperience/Statement/PortfolioItemShared.php b/plugin/xapi/src/ToolExperience/Statement/PortfolioItemShared.php index f680357baa..820a401104 100644 --- a/plugin/xapi/src/ToolExperience/Statement/PortfolioItemShared.php +++ b/plugin/xapi/src/ToolExperience/Statement/PortfolioItemShared.php @@ -5,10 +5,14 @@ namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement; use Chamilo\CoreBundle\Entity\Portfolio as PortfolioEntity; +use Chamilo\CoreBundle\Entity\PortfolioAttachment; use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioCategory as PortfolioCategoryActivity; use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioItem as PortfolioItemActivity; use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User as UserActor; use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Shared as SharedVerb; +use Xabbuh\XApi\Model\Attachment; +use Xabbuh\XApi\Model\IRI; +use Xabbuh\XApi\Model\LanguageMap; use Xabbuh\XApi\Model\Statement; /** @@ -50,6 +54,14 @@ class PortfolioItemShared extends BaseStatement $context = $context->withContextActivities($contextActivities); } + $em = \Database::getManager(); + $itemAttachments = $em->getRepository(PortfolioAttachment::class)->findFromItem($this->portfolioItem); + + $attachments = $this->generateAttachments( + $itemAttachments, + $this->portfolioItem->getUser() + ); + return new Statement( null, $userActor->generate(), @@ -59,7 +71,8 @@ class PortfolioItemShared extends BaseStatement null, $this->portfolioItem->getCreationDate(), null, - $context + $context, + $attachments ); } }