Portfolio: Allow to send xAPI statement when viewing item - refs BT#18201

pull/4368/head
Angel Fernando Quiroz Campos 3 years ago
parent 45b49087b4
commit 4ce93eaf76
  1. 9
      main/inc/lib/PortfolioController.php
  2. 19
      main/inc/lib/hook/HookPortfolioItemViewed.php
  3. 8
      main/inc/lib/hook/interfaces/HookPortfolioItemViewedEventInterface.php
  4. 8
      main/inc/lib/hook/interfaces/HookPortfolioItemViewedObserverInterface.php
  5. 25
      plugin/xapi/src/Hook/XApiPortfolioItemViewedHookObserver.php
  6. 41
      plugin/xapi/src/ToolExperience/Statement/PortfolioItem.php
  7. 57
      plugin/xapi/src/ToolExperience/Statement/PortfolioItemShared.php
  8. 45
      plugin/xapi/src/ToolExperience/Statement/PortfolioItemViewed.php
  9. 6
      plugin/xapi/src/XApiPlugin.php

@ -1002,6 +1002,11 @@ class PortfolioController
{
global $interbreadcrumb;
HookPortfolioItemViewed::create()
->setEventData(['portfolio' => $item])
->notifyItemViewed()
;
$form = $this->createCommentForm($item);
$commentsRepo = $this->em->getRepository(PortfolioComment::class);
@ -2760,7 +2765,7 @@ class PortfolioController
get_lang('OriginallyPublishedAsXTitleByYUser'),
[
"<cite>{$origin->getTitle()}</cite>",
$origin->getUser()->getCompleteName()
$origin->getUser()->getCompleteName(),
]
);
}
@ -2773,7 +2778,7 @@ class PortfolioController
get_lang('OriginallyCommentedByXUserInYItem'),
[
$origin->getAuthor()->getCompleteName(),
"<cite>{$origin->getItem()->getTitle()}</cite>"
"<cite>{$origin->getItem()->getTitle()}</cite>",
]
);
}

@ -0,0 +1,19 @@
<?php
/* For licensing terms, see /license.txt */
class HookPortfolioItemViewed extends HookEvent implements HookPortfolioItemViewedEventInterface
{
protected function __construct()
{
parent::__construct('HookPortfolioItemViewed');
}
public function notifyItemViewed(): void
{
/** @var HookPortfolioItemViewedObserverInterface $observer */
foreach ($this->observers as $observer) {
$observer->hookItemViewed($this);
}
}
}

@ -0,0 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
interface HookPortfolioItemViewedEventInterface extends HookEventInterface
{
public function notifyItemViewed(): void;
}

@ -0,0 +1,8 @@
<?php
/* For licensing terms, see /license.txt */
interface HookPortfolioItemViewedObserverInterface extends HookObserverInterface
{
public function hookItemViewed(HookPortfolioItemViewedEventInterface $hookEvent);
}

@ -0,0 +1,25 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Portfolio;
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItemViewed;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
class XApiPortfolioItemViewedHookObserver extends XApiActivityHookObserver implements HookPortfolioItemViewedObserverInterface
{
/**
* @throws OptimisticLockException
* @throws ORMException
*/
public function hookItemViewed(HookPortfolioItemViewedEventInterface $hookEvent)
{
/** @var Portfolio $item */
$item = $hookEvent->getEventData()['portfolio'];
$statement = (new PortfolioItemViewed($item))->generate();
$this->saveSharedStatement($statement);
}
}

@ -0,0 +1,41 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
use Chamilo\CoreBundle\Entity\Portfolio;
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioCategory;
use Xabbuh\XApi\Model\Context;
abstract class PortfolioItem extends BaseStatement
{
protected $item;
public function __construct(Portfolio $item)
{
$this->item = $item;
}
protected function generateContext(): Context
{
$context = parent::generateContext();
$category = $this->item->getCategory();
if ($category) {
$categoryActivity = new PortfolioCategory($category);
$contextActivities = $context
->getContextActivities()
->withAddedCategoryActivity(
$categoryActivity->generate()
)
;
$context = $context->withContextActivities($contextActivities);
}
return $context;
}
}

@ -4,12 +4,12 @@
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 Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioItem;
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User;
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItem as PortfolioItemStatement;
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Shared;
use Database;
use Xabbuh\XApi\Model\Statement;
/**
@ -17,47 +17,24 @@ use Xabbuh\XApi\Model\Statement;
*
* @package Chamilo\PluginBundle\XApi\ToolExperience\Statement
*/
class PortfolioItemShared extends BaseStatement
class PortfolioItemShared extends PortfolioItemStatement
{
/**
* @var \Chamilo\CoreBundle\Entity\Portfolio
*/
private $portfolioItem;
public function __construct(PortfolioEntity $item)
{
$this->portfolioItem = $item;
}
public function generate(): Statement
{
$userActor = new UserActor(
$this->portfolioItem->getUser()
);
$sharedVerb = new SharedVerb();
$itemActivity = new PortfolioItemActivity($this->portfolioItem);
$itemAuthor = $this->item->getUser();
$context = $this->generateContext();
if ($this->portfolioItem->getCategory()) {
$categoryActivity = new PortfolioCategoryActivity($this->portfolioItem->getCategory());
$contextActivities = $context
->getContextActivities()
->withAddedCategoryActivity(
$categoryActivity->generate()
);
$userActor = new User($itemAuthor);
$sharedVerb = new Shared();
$itemActivity = new PortfolioItem($this->item);
$context = $context->withContextActivities($contextActivities);
}
$context = $this->generateContext();
$em = \Database::getManager();
$itemAttachments = $em->getRepository(PortfolioAttachment::class)->findFromItem($this->portfolioItem);
$itemAttachments = Database::getManager()
->getRepository(PortfolioAttachment::class)
->findFromItem($this->item)
;
$attachments = $this->generateAttachments(
$itemAttachments,
$this->portfolioItem->getUser()
);
$attachments = $this->generateAttachments($itemAttachments, $itemAuthor);
return new Statement(
$this->generateStatementId('portfolio-item'),
@ -66,7 +43,7 @@ class PortfolioItemShared extends BaseStatement
$itemActivity->generate(),
null,
null,
$this->portfolioItem->getCreationDate(),
$this->item->getCreationDate(),
null,
$context,
$attachments

@ -0,0 +1,45 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioItem;
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User;
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItem as PortfolioItemStatement;
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Viewed;
use Database;
use Xabbuh\XApi\Model\Statement;
class PortfolioItemViewed extends PortfolioItemStatement
{
public function generate(): Statement
{
$itemAuthor = $this->item->getUser();
$itemAttachments = Database::getManager()
->getRepository(PortfolioAttachment::class)
->findFromItem($this->item)
;
$actor = new User($itemAuthor);
$verb = new Viewed();
$object = new PortfolioItem($this->item);
$context = $this->generateContext();
$attachments = $this->generateAttachments($itemAttachments, $itemAuthor);
return new Statement(
$this->generateStatementId('portfolio-item'),
$actor->generate(),
$verb->generate(),
$object->generate(),
null,
null,
$this->item->getCreationDate(),
null,
$context,
$attachments
);
}
}

@ -220,6 +220,7 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$quizEndHook = XApiQuizEndHookObserver::create();
$portfolioItemAddedHook = XApiPortfolioItemAddedHookObserver::create();
$portfolioItemCommentedHook = XApiPortfolioItemCommentedHookObserver::create();
$portfolioItemViewedHook = XApiPortfolioItemViewedHookObserver::create();
$learningPathItemViewedEvent = HookLearningPathItemViewed::create();
$learningPathEndEvent = HookLearningPathEnd::create();
@ -227,6 +228,7 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$quizEndEvent = HookQuizEnd::create();
$portfolioItemAddedEvent = HookPortfolioItemAdded::create();
$portfolioItemCommentedEvent = HookPortfolioItemCommented::create();
$portfolioItemViewedEvent = HookPortfolioItemViewed::create();
if ('true' === $this->get(self::SETTING_LRS_LP_ITEM_ACTIVE)) {
$learningPathItemViewedEvent->attach($learningPathItemViewedHook);
@ -255,9 +257,11 @@ class XApiPlugin extends Plugin implements HookPluginInterface
if ('true' === $this->get(self::SETTING_LRS_PORTFOLIO_ACTIVE)) {
$portfolioItemAddedEvent->attach($portfolioItemAddedHook);
$portfolioItemCommentedEvent->attach($portfolioItemCommentedHook);
$portfolioItemViewedEvent->attach($portfolioItemViewedHook);
} else {
$portfolioItemAddedEvent->detach($portfolioItemAddedHook);
$portfolioItemCommentedEvent->attach($portfolioItemCommentedHook);
$portfolioItemCommentedEvent->detach($portfolioItemCommentedHook);
$portfolioItemViewedEvent->detach($portfolioItemViewedHook);
}
return $this;

Loading…
Cancel
Save