XAPI: Manage portfolio events - refs BT#18201

pull/3766/head^2
Angel Fernando Quiroz Campos 5 years ago
parent 669f76f6c1
commit 003fd7dc1b
  1. 1
      plugin/xapi/lang/english.php
  2. 1
      plugin/xapi/lang/french.php
  3. 1
      plugin/xapi/lang/spanish.php
  4. 12
      plugin/xapi/src/Hook/XApiActivityHookObserver.php
  5. 103
      plugin/xapi/src/Hook/XApiPortfolioItemAddedHookObserver.php
  6. 117
      plugin/xapi/src/Hook/XApiPortfolioItemCommentedHookObserver.php
  7. 22
      plugin/xapi/src/XApiPlugin.php

@ -18,6 +18,7 @@ $strings['lrs_lp_item_viewed_active'] = 'Learning path item viewed';
$strings['lrs_lp_end_active'] = 'Learning path ended';
$strings['lrs_quiz_active'] = 'Quiz ended';
$strings['lrs_quiz_question_active'] = 'Quiz question answered';
$strings['lrs_portfolio_active'] = 'Portfolio events';
$strings['NoActivities'] = 'No activities added yet';
$strings['ActivityTitle'] = 'Activity';

@ -18,6 +18,7 @@ $strings['lrs_lp_item_viewed_active'] = 'Élément de parcours visionné';
$strings['lrs_lp_end_active'] = 'Parcours terminé';
$strings['lrs_quiz_active'] = 'Exercice terminé';
$strings['lrs_quiz_question_active'] = 'Question d\'exercice répondue';
$strings['lrs_portfolio_active'] = 'Événements de portfolio';
$strings['NoActivities'] = 'Aucune activité ajoutée pour l\'instant';
$strings['ActivityTitle'] = 'Activité';

@ -18,6 +18,7 @@ $strings['lrs_lp_item_viewed_active'] = 'Visualización de contenido de lección
$strings['lrs_lp_end_active'] = 'Finalización de lección';
$strings['lrs_quiz_active'] = 'Finalización de ejercicio';
$strings['lrs_quiz_question_active'] = 'Resolución de pregunta en ejercicio';
$strings['lrs_portfolio_active'] = 'Eventos en portafolio';
$strings['NoActivities'] = 'No hay actividades aún';
$strings['ActivityTitle'] = 'Actividad';

@ -19,7 +19,7 @@ abstract class XApiActivityHookObserver extends HookObserver
use XApiStatementTrait;
/**
* @var \Chamilo\CoreBundle\Entity\Course
* @var \Chamilo\CoreBundle\Entity\Course|null
*/
protected $course;
/**
@ -135,10 +135,12 @@ abstract class XApiActivityHookObserver extends HookObserver
{
$platform = api_get_setting('Institution').' - '.api_get_setting('siteName');
$groupingActivities = [
$this->generateActivityFromSite(),
$this->generateActivityFromCourse($this->course, $this->session),
];
$groupingActivities = [];
$groupingActivities[] = $this->generateActivityFromSite();
if ($this->course) {
$groupingActivities[] = $this->generateActivityFromCourse($this->course, $this->session);
}
$context = new Context();

@ -0,0 +1,103 @@
<?php
/* For licensing terms, see /license.txt */
use Xabbuh\XApi\Common\Exception\StatementIdAlreadyExistsException;
use Xabbuh\XApi\Model\Activity;
use Xabbuh\XApi\Model\Definition;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\LanguageMap;
use Xabbuh\XApi\Model\Verb;
/**
* Class XApiPortfolioItemAddedHookObserver.
*/
class XApiPortfolioItemAddedHookObserver extends XApiActivityHookObserver
implements HookPortfolioItemAddedObserverInterface
{
/**
* @var \Chamilo\CoreBundle\Entity\Portfolio
*/
private $item;
/**
* @inheritDoc
*/
public function hookItemAdded(HookPortfolioItemAddedEventInterface $hookEvent)
{
$this->item = $hookEvent->getEventData()['portfolio'];
$this->user = $this->item->getUser();
$this->course = $this->item->getCourse();
$this->session = $this->item->getSession();
try {
$statement = $this->createStatement(
$this->item->getCreationDate()
);
} catch (StatementIdAlreadyExistsException $e) {
return;
}
$this->saveSharedStatement($statement);
}
/**
* @inheritDoc
*/
protected function getId()
{
return $this->generateId(
XApiPlugin::DATA_TYPE_PORTFOLIO_ITEM,
$this->item->getId()
);
}
/**
* @inheritDoc
*/
protected function getActor()
{
return $this->generateActor(
$this->item->getUser()
);
}
/**
* @inheritDoc
*/
protected function getVerb()
{
$languageMap = XApiPlugin::create()->getLangMap('shared');
return new Verb(
IRI::fromString(XApiPlugin::VERB_SHARED),
LanguageMap::create($languageMap)
);
}
/**
* @inheritDoc
*/
protected function getActivity()
{
$languageIso = api_get_language_isocode($this->course->getCourseLanguage());
$id = $this->plugin->generateIri($this->item->getId(), 'portfolio-item');
return new Activity(
$id,
new Definition(
LanguageMap::create([$languageIso => $this->item->getTitle()])
)
);
}
/**
* @inheritDoc
*/
protected function getActivityResult()
{
return null;
}
}

@ -0,0 +1,117 @@
<?php
/* For licensing terms, see /license.txt */
use Xabbuh\XApi\Common\Exception\StatementIdAlreadyExistsException;
use Xabbuh\XApi\Model\Activity;
use Xabbuh\XApi\Model\Definition;
use Xabbuh\XApi\Model\IRI;
use Xabbuh\XApi\Model\LanguageMap;
use Xabbuh\XApi\Model\Verb;
/**
* Class XApiPortfolioItemCommentedHookObserver.
*/
class XApiPortfolioItemCommentedHookObserver extends XApiActivityHookObserver
implements HookPortfolioItemCommentedObserverInterface
{
/**
* @var \Chamilo\CoreBundle\Entity\PortfolioComment
*/
private $comment;
/**
* @inheritDoc
*/
public function hookItemCommented(HookPortfolioItemCommentedEventInterface $hookEvent)
{
$this->comment = $hookEvent->getEventData()['comment'];
$this->user = $this->comment->getAuthor();
$this->course = $this->comment->getItem()->getCourse();
$this->session = $this->comment->getItem()->getSession();
try {
$statement = $this->createStatement(
$this->comment->getDate()
);
} catch (StatementIdAlreadyExistsException $e) {
return;
}
$this->saveSharedStatement($statement);
}
/**
* @inheritDoc
*/
protected function getId()
{
return $this->generateId(
XApiPlugin::DATA_TYPE_PORTFOLIO_COMMENT,
$this->comment->getId()
);
}
/**
* @inheritDoc
*/
protected function getActor()
{
return $this->generateActor(
$this->comment->getAuthor()
);
}
/**
* @inheritDoc
*/
protected function getVerb()
{
$languageMap = XApiPlugin::create()->getLangMap('commented');
return new Verb(
IRI::fromString(XApiPlugin::VERB_COMMENTED),
LanguageMap::create($languageMap)
);
}
/**
* @inheritDoc
*/
protected function getActivity()
{
if ($this->comment->getParent()) {
$parent = $this->comment->getParent();
$id = $this->plugin->generateIri($parent->getId(), 'portfolio-comment');
$titleMap = $this->plugin->getLangMap('AReplyOnAPortfolioComment');
} else {
$item = $this->comment->getItem();
$id = $this->plugin->generateIri($item->getId(), 'portfolio-item');
$languageIso = api_get_language_isocode($this->course->getCourseLanguage());
$titleMap = [$languageIso => $item->getTitle()];
}
return new Activity(
$id,
new Definition(
LanguageMap::create($titleMap)
)
);
}
/**
* @inheritDoc
*/
protected function getActivityResult()
{
return new \Xabbuh\XApi\Model\Result(
null,
null,
null,
$this->comment->getContent()
);
}
}

@ -32,11 +32,14 @@ class XApiPlugin extends Plugin implements HookPluginInterface
const SETTING_LRS_LP_ACTIVE = 'lrs_lp_end_active';
const SETTING_LRS_QUIZ_ACTIVE = 'lrs_quiz_active';
const SETTING_LRS_QUIZ_QUESTION_ACTIVE = 'lrs_quiz_question_active';
const SETTING_LRS_PORTFOLIO_ACTIVE = 'lrs_portfolio_active';
const VERB_TERMINATED = 'http://adlnet.gov/expapi/verbs/terminated';
const VERB_COMPLETED = 'http://adlnet.gov/expapi/verbs/completed';
const VERB_ANSWERED = 'http://adlnet.gov/expapi/verbs/answered';
const VERB_VIEWED = 'http://id.tincanapi.com/verb/viewed';
const VERB_SHARED = 'http://adlnet.gov/expapi/verbs/shared';
const VERB_COMMENTED = 'http://adlnet.gov/expapi/verbs/commented';
const IRI_QUIZ = 'http://adlnet.gov/expapi/activities/assessment';
const IRI_QUIZ_QUESTION = 'http://adlnet.gov/expapi/activities/question';
@ -48,6 +51,8 @@ class XApiPlugin extends Plugin implements HookPluginInterface
const DATA_TYPE_EXERCISE = 'e_exercise';
const DATA_TYPE_LP_ITEM_VIEW = 'lp_item_view';
const DATA_TYPE_LP_VIEW = 'lp_view';
const DATA_TYPE_PORTFOLIO_ITEM = 'portfolio_item';
const DATA_TYPE_PORTFOLIO_COMMENT = 'portfolio_comment';
const TYPE_QUIZ = 'quiz';
const TYPE_QUIZ_QUESTION = 'quiz_question';
@ -77,6 +82,7 @@ class XApiPlugin extends Plugin implements HookPluginInterface
self::SETTING_LRS_LP_ACTIVE => 'boolean',
self::SETTING_LRS_QUIZ_ACTIVE => 'boolean',
self::SETTING_LRS_QUIZ_QUESTION_ACTIVE => 'boolean',
self::SETTING_LRS_PORTFOLIO_ACTIVE => 'boolean',
];
parent::__construct(
@ -151,12 +157,16 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$quizQuestionAnsweredHook = XApiQuizQuestionAnsweredHookObserver::create();
$quizEndHook = XApiQuizEndHookObserver::create();
$createCourseHook = XApiCreateCourseHookObserver::create();
$portfolioItemAddedHook = XApiPortfolioItemAddedHookObserver::create();
$portfolioItemCommentedHook = XApiPortfolioItemCommentedHookObserver::create();
HookLearningPathItemViewed::create()->detach($learningPathItemViewedHook);
HookLearningPathEnd::create()->detach($learningPathEndHook);
HookQuizQuestionAnswered::create()->detach($quizQuestionAnsweredHook);
HookQuizEnd::create()->detach($quizEndHook);
HookCreateCourse::create()->detach($createCourseHook);
HookPortfolioItemAdded::create()->detach($portfolioItemAddedHook);
HookPortfolioItemCommented::create()->detach($portfolioItemCommentedHook);
return 1;
}
@ -224,11 +234,15 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$learningPathEndHook = XApiLearningPathEndHookObserver::create();
$quizQuestionAnsweredHook = XApiQuizQuestionAnsweredHookObserver::create();
$quizEndHook = XApiQuizEndHookObserver::create();
$portfolioItemAddedHook = XApiPortfolioItemAddedHookObserver::create();
$portfolioItemCommentedHook = XApiPortfolioItemCommentedHookObserver::create();
$learningPathItemViewedEvent = HookLearningPathItemViewed::create();
$learningPathEndEvent = HookLearningPathEnd::create();
$quizQuestionAnsweredEvent = HookQuizQuestionAnswered::create();
$quizEndEvent = HookQuizEnd::create();
$portfolioItemAddedEvent = HookPortfolioItemAdded::create();
$portfolioItemCommentedEvent = HookPortfolioItemCommented::create();
if ('true' === $this->get(self::SETTING_LRS_LP_ITEM_ACTIVE)) {
$learningPathItemViewedEvent->attach($learningPathItemViewedHook);
@ -254,6 +268,14 @@ class XApiPlugin extends Plugin implements HookPluginInterface
$quizEndEvent->detach($quizEndHook);
}
if ('true' === $this->get(self::SETTING_LRS_PORTFOLIO_ACTIVE)) {
$portfolioItemAddedEvent->attach($portfolioItemAddedHook);
$portfolioItemCommentedEvent->attach($portfolioItemCommentedHook);
} else {
$portfolioItemAddedEvent->detach($portfolioItemAddedHook);
$portfolioItemCommentedEvent->attach($portfolioItemCommentedHook);
}
return $this;
}

Loading…
Cancel
Save