Skip to content

Commit

Permalink
XAPI: Throw exception before send statement already sent - refs BT#16742
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Nov 27, 2020
1 parent c6de6cf commit 5ac6ee2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
29 changes: 28 additions & 1 deletion plugin/xapi/src/Hook/XApiActivityHookObserver.php
Expand Up @@ -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;
Expand Down Expand Up @@ -80,12 +81,20 @@ protected function saveSharedStatement(StatementId $uuid, $dataType, $dataId)
}

/**
* @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(),
Expand All @@ -102,6 +111,24 @@ protected function createStatement()
*/
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
*/
Expand Down
12 changes: 4 additions & 8 deletions plugin/xapi/src/Hook/XApiLearningPathEndHook.php
Expand Up @@ -49,17 +49,13 @@ public function notifyLearningPathEnd(HookLearningPathEndEventInterface $event)
$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()
);
Expand Down
8 changes: 4 additions & 4 deletions plugin/xapi/src/Hook/XApiLearningPathItemViewedHook.php
Expand Up @@ -58,13 +58,13 @@ public function hookLearningPathItemViewed(HookLearningPathItemViewedEventInterf
$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()
);
Expand Down
8 changes: 4 additions & 4 deletions plugin/xapi/src/Hook/XApiQuizEndHook.php
Expand Up @@ -46,13 +46,13 @@ public function hookQuizEnd(HookQuizEndEventInterface $hookEvent)
$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()
);
Expand Down
16 changes: 8 additions & 8 deletions plugin/xapi/src/Hook/XApiQuizQuestionAnsweredHook.php
Expand Up @@ -57,17 +57,17 @@ public function hookQuizQuestionAnswered(HookQuizQuestionAnsweredEventInterface
$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()
);
Expand Down

0 comments on commit 5ac6ee2

Please sign in to comment.