XAPI: Throw exception before send statement already sent - refs BT#16742

pull/3680/head
Angel Fernando Quiroz Campos 5 years ago
parent c6de6cf41a
commit 5ac6ee2a6c
  1. 29
      plugin/xapi/src/Hook/XApiActivityHookObserver.php
  2. 12
      plugin/xapi/src/Hook/XApiLearningPathEndHook.php
  3. 8
      plugin/xapi/src/Hook/XApiLearningPathItemViewedHook.php
  4. 8
      plugin/xapi/src/Hook/XApiQuizEndHook.php
  5. 16
      plugin/xapi/src/Hook/XApiQuizQuestionAnsweredHook.php

@ -3,6 +3,7 @@
use Chamilo\PluginBundle\Entity\XApi\SharedStatement;
use Xabbuh\XApi\Common\Exception\ConflictException;
use Xabbuh\XApi\Common\Exception\StatementIdAlreadyExistsException;
use Xabbuh\XApi\Common\Exception\XApiException;
use Xabbuh\XApi\Model\Context;
use Xabbuh\XApi\Model\Statement;
@ -80,12 +81,20 @@ abstract class XApiActivityHookObserver extends HookObserver
}
/**
* @throws \Xabbuh\XApi\Common\Exception\StatementIdAlreadyExistsException
*
* @return \Xabbuh\XApi\Model\Statement
*/
protected function createStatement()
{
$id = $this->getId();
if ($this->statementAlreadyShared($id->getValue())) {
throw new StatementIdAlreadyExistsException($id->getValue());
}
return new Statement(
$this->getId(),
$id,
$this->getActor(),
$this->getVerb(),
$this->getActivity(),
@ -102,6 +111,24 @@ abstract class XApiActivityHookObserver extends HookObserver
*/
abstract protected function getId();
/**
* @param string $uuid
*
* @return bool
*/
protected function statementAlreadyShared($uuid)
{
$sharedStmt = Database::getManager()
->getRepository(SharedStatement::class)
->findOneByUuid($uuid);
if ($sharedStmt) {
return true;
}
return false;
}
/**
* @return \Xabbuh\XApi\Model\Agent
*/

@ -49,17 +49,13 @@ class XApiLearningPathEndHook extends XApiActivityHookObserver implements HookLe
$this->course = api_get_course_entity($this->lpView->getCId());
$this->session = api_get_session_entity($this->lpView->getSessionId());
$statement = $this->createStatement();
if ($this->isStatementAlreadySent($statement)) {
return;
}
try {
$statement = $this->sendStatementToLrs($statement);
$statement = $this->createStatement();
$sharedStmt = $this->sendStatementToLrs($statement);
$this->saveSharedStatement(
$statement->getId(),
$sharedStmt->getId(),
XApiPlugin::DATA_TYPE_LP_VIEW,
$this->lpView->getId()
);

@ -58,13 +58,13 @@ class XApiLearningPathItemViewedHook
$this->course = api_get_course_entity($this->lpView->getCId());
$this->session = api_get_session_entity($this->lpView->getSessionId());
$statement = $this->createStatement();
try {
$statement = $this->sendStatementToLrs($statement);
$statement = $this->createStatement();
$sharedStmt = $this->sendStatementToLrs($statement);
$this->saveSharedStatement(
$statement->getId(),
$sharedStmt->getId(),
XApiPlugin::DATA_TYPE_LP_ITEM_VIEW,
$this->lpItemView->getId()
);

@ -46,13 +46,13 @@ class XApiQuizEndHook extends XApiActivityHookObserver implements HookQuizEndObs
$this->course = api_get_course_entity($this->exe->getCId());
$this->session = api_get_session_entity($this->exe->getSessionId());
$statement = $this->createStatement();
try {
$statement = $this->sendStatementToLrs($statement);
$statement = $this->createStatement();
$sharedStmt = $this->sendStatementToLrs($statement);
$this->saveSharedStatement(
$statement->getId(),
$sharedStmt->getId(),
XApiPlugin::DATA_TYPE_EXERCISE,
$this->exe->getExeId()
);

@ -57,17 +57,17 @@ class XApiQuizQuestionAnsweredHook
$this->course = api_get_course_entity($this->exe->getCId());
$this->session = api_get_session_entity($this->exe->getSessionId());
$statement = $this
->createStatement()
->withCreated(
$this->attempt->getTms()
);
try {
$statement = $this->sendStatementToLrs($statement);
$statement = $this
->createStatement()
->withCreated(
$this->attempt->getTms()
);
$sharedStmt = $this->sendStatementToLrs($statement);
$this->saveSharedStatement(
$statement->getId(),
$sharedStmt->getId(),
XApiPlugin::DATA_TYPE_ATTEMPT,
$this->attempt->getId()
);

Loading…
Cancel
Save