Plugin: XAPI: Add statement when portfolio items/comments are scored - refs BT#18201

pull/4501/head
Angel Fernando Quiroz Campos 3 years ago
parent b0145600e8
commit 2d8720a9ad
  1. 10
      main/inc/lib/PortfolioController.php
  2. 19
      main/inc/lib/hook/HookPortfolioCommentScored.php
  3. 19
      main/inc/lib/hook/HookPortfolioItemScored.php
  4. 8
      main/inc/lib/hook/interfaces/HookPortfolioCommentScoredEventInterface.php
  5. 8
      main/inc/lib/hook/interfaces/HookPortfolioCommentScoredObserverInterface.php
  6. 8
      main/inc/lib/hook/interfaces/HookPortfolioItemScoredEventInterface.php
  7. 8
      main/inc/lib/hook/interfaces/HookPortfolioItemScoredObserverInterface.php
  8. 19
      plugin/xapi/src/Hook/XApiPortfolioCommentScoredHookObserver.php
  9. 19
      plugin/xapi/src/Hook/XApiPortfolioItemScoredHookObserver.php
  10. 62
      plugin/xapi/src/ToolExperience/Statement/PortfolioCommentScored.php
  11. 53
      plugin/xapi/src/ToolExperience/Statement/PortfolioItemScored.php
  12. 16
      plugin/xapi/src/ToolExperience/Verb/Scored.php
  13. 12
      plugin/xapi/src/XApiPlugin.php

@ -2407,6 +2407,11 @@ class PortfolioController
$em->persist($item);
$em->flush();
HookPortfolioItemScored::create()
->setEventData(['item' => $item])
->notifyItemScored()
;
Display::addFlash(
Display::return_message(get_lang('PortfolioItemGraded'), 'success')
);
@ -2476,6 +2481,11 @@ class PortfolioController
$em->persist($comment);
$em->flush();
HookPortfolioCommentScored::create()
->setEventData(['comment' => $comment])
->notifyCommentScored()
;
Display::addFlash(
Display::return_message(get_lang('PortfolioCommentGraded'), 'success')
);

@ -0,0 +1,19 @@
<?php
/* For licensing terms, see /license.txt */
class HookPortfolioCommentScored extends HookEvent implements HookPortfolioCommentScoredEventInterface
{
protected function __construct()
{
parent::__construct('HookPortfolioCommentScored');
}
public function notifyCommentScored()
{
/** @var HookPortfolioCommentScoredObserverInterface $observer */
foreach ($this->observers as $observer) {
$observer->hookCommentScored($this);
}
}
}

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

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

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

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

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

@ -0,0 +1,19 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\PortfolioComment;
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioCommentScored;
class XApiPortfolioCommentScoredHookObserver extends XApiActivityHookObserver implements HookPortfolioCommentScoredObserverInterface
{
public function hookCommentScored(HookPortfolioCommentScoredEventInterface $hookEvent)
{
/** @var PortfolioComment $comment */
$comment = $hookEvent->getEventData()['comment'];
$statement = (new PortfolioCommentScored($comment))->generate();
$this->saveSharedStatement($statement);
}
}

@ -0,0 +1,19 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Portfolio;
use Chamilo\PluginBundle\XApi\ToolExperience\Statement\PortfolioItemScored;
class XApiPortfolioItemScoredHookObserver extends XApiActivityHookObserver implements HookPortfolioItemScoredObserverInterface
{
public function hookItemScored(HookPortfolioItemScoredEventInterface $hookEvent)
{
/** @var Portfolio $item */
$item = $hookEvent->getEventData()['item'];
$statement = (new PortfolioItemScored($item))->generate();
$this->saveSharedStatement($statement);
}
}

@ -0,0 +1,62 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\XApi\ToolExperience\Statement;
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
use Chamilo\CoreBundle\Entity\PortfolioComment;
use Chamilo\PluginBundle\XApi\ToolExperience\Activity\PortfolioComment as PortfolioCommentActivity;
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User;
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Scored;
use Xabbuh\XApi\Model\Result;
use Xabbuh\XApi\Model\Score;
use Xabbuh\XApi\Model\Statement;
class PortfolioCommentScored extends BaseStatement
{
use PortfolioAttachmentsTrait;
/** @var PortfolioComment */
private $comment;
public function __construct(PortfolioComment $comment)
{
$this->comment = $comment;
}
public function generate(): Statement
{
$user = api_get_user_entity(api_get_user_id());
$commentAttachments = \Database::getManager()
->getRepository(PortfolioAttachment::class)
->findFromComment($this->comment)
;
$maxScore = (float) api_get_course_setting('portfolio_max_score');
$rawScore = $this->comment->getScore();
$scaled = $maxScore ? ($rawScore / $maxScore) : 0;
$actor = new User($user);
$verb = new Scored();
$object = new PortfolioCommentActivity($this->comment);
$context = $this->generateContext();
$attachments = $this->generateAttachments($commentAttachments, $this->comment->getAuthor());
$score = new Score($scaled, $rawScore, 0, $maxScore);
$result = new Result($score);
return new Statement(
$this->generateStatementId('portfolio-comment'),
$actor->generate(),
$verb->generate(),
$object->generate(),
$result,
null,
api_get_utc_datetime(null, false, true),
null,
$context,
$attachments
);
}
}

@ -0,0 +1,53 @@
<?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 as PortfolioItemActivity;
use Chamilo\PluginBundle\XApi\ToolExperience\Actor\User;
use Chamilo\PluginBundle\XApi\ToolExperience\Verb\Scored;
use Xabbuh\XApi\Model\Result;
use Xabbuh\XApi\Model\Score;
use Xabbuh\XApi\Model\Statement;
class PortfolioItemScored extends PortfolioItem
{
use PortfolioAttachmentsTrait;
public function generate(): Statement
{
$user = api_get_user_entity(api_get_user_id());
$itemAttachments = \Database::getManager()
->getRepository(PortfolioAttachment::class)
->findFromItem($this->item)
;
$maxScore = (float) api_get_course_setting('portfolio_max_score');
$rawScore = $this->item->getScore();
$scaled = $maxScore ? ($rawScore / $maxScore) : 0;
$actor = new User($user);
$verb = new Scored();
$object = new PortfolioItemActivity($this->item);
$context = $this->generateContext();
$attachments = $this->generateAttachments($itemAttachments, $this->item->getUser());
$score = new Score($scaled, $rawScore, 0, $maxScore);
$result = new Result($score);
return new Statement(
$this->generateStatementId('portfolio-item'),
$actor->generate(),
$verb->generate(),
$object->generate(),
$result,
null,
api_get_utc_datetime(null, false, true),
null,
$context,
$attachments
);
}
}

@ -0,0 +1,16 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\XApi\ToolExperience\Verb;
class Scored extends BaseVerb
{
public function __construct()
{
parent::__construct(
'http://adlnet.gov/expapi/verbs/scored',
'Scored'
);
}
}

@ -150,6 +150,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$portfolioItemCommentedHook = XApiPortfolioItemCommentedHookObserver::create();
$portfolioItemHighlightedHook = XApiPortfolioItemHighlightedHookObserver::create();
$portfolioDownloaded = XApiPortfolioDownloadedHookObserver::create();
$portfolioItemScoredHook = XApiPortfolioItemScoredHookObserver::create();
$portfolioCommentedScoredHook = XApiPortfolioCommentScoredHookObserver::create();
HookLearningPathItemViewed::create()->detach($learningPathItemViewedHook);
HookLearningPathEnd::create()->detach($learningPathEndHook);
@ -160,6 +162,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface
HookPortfolioItemCommented::create()->detach($portfolioItemCommentedHook);
HookPortfolioItemHighlighted::create()->detach($portfolioItemHighlightedHook);
HookPortfolioDownloaded::create()->detach($portfolioDownloaded);
HookPortfolioItemScored::create()->detach($portfolioItemScoredHook);
HookPortfolioCommentScored::create()->detach($portfolioCommentedScoredHook);
return 1;
}
@ -247,6 +251,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$portfolioItemViewedHook = XApiPortfolioItemViewedHookObserver::create();
$portfolioItemHighlightedHook = XApiPortfolioItemHighlightedHookObserver::create();
$portfolioDownloadedHook = XApiPortfolioDownloadedHookObserver::create();
$portfolioItemScoredHook = XApiPortfolioItemScoredHookObserver::create();
$portfolioCommentScoredHook = XApiPortfolioCommentScoredHookObserver::create();
$learningPathItemViewedEvent = HookLearningPathItemViewed::create();
$learningPathEndEvent = HookLearningPathEnd::create();
@ -257,6 +263,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$portfolioItemViewedEvent = HookPortfolioItemViewed::create();
$portfolioItemHighlightedEvent = HookPortfolioItemHighlighted::create();
$portfolioDownloadedEvent = HookPortfolioDownloaded::create();
$portfolioItemScoredEvent = HookPortfolioItemScored::create();
$portfolioCommentScoredEvent = HookPortfolioCommentScored::create();
if ('true' === $this->get(self::SETTING_LRS_LP_ITEM_ACTIVE)) {
$learningPathItemViewedEvent->attach($learningPathItemViewedHook);
@ -288,12 +296,16 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$portfolioItemViewedEvent->attach($portfolioItemViewedHook);
$portfolioItemHighlightedEvent->attach($portfolioItemHighlightedHook);
$portfolioDownloadedEvent->attach($portfolioDownloadedHook);
$portfolioItemScoredEvent->attach($portfolioItemScoredHook);
$portfolioCommentScoredEvent->attach($portfolioCommentScoredHook);
} else {
$portfolioItemAddedEvent->detach($portfolioItemAddedHook);
$portfolioItemCommentedEvent->detach($portfolioItemCommentedHook);
$portfolioItemViewedEvent->detach($portfolioItemViewedHook);
$portfolioItemHighlightedEvent->detach($portfolioItemHighlightedHook);
$portfolioDownloadedEvent->detach($portfolioDownloadedHook);
$portfolioItemScoredEvent->detach($portfolioItemScoredHook);
$portfolioCommentScoredEvent->detach($portfolioCommentScoredHook);
}
return $this;

Loading…
Cancel
Save