XAPI: Include attachments in portfolio statements - refs BT#18201

pull/3766/head^2
Angel Fernando Quiroz Campos 5 years ago
parent 3abb6deb62
commit ea927826d3
  1. 58
      plugin/xapi/src/ToolExperience/Statement/BaseStatement.php
  2. 15
      plugin/xapi/src/ToolExperience/Statement/PortfolioItemCommented.php
  3. 15
      plugin/xapi/src/ToolExperience/Statement/PortfolioItemShared.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;
}
}

@ -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);
}
}
}

@ -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
);
}
}

Loading…
Cancel
Save