Skip to content

Commit

Permalink
Plugin: XAPI: log stored statements instead of request statements - r…
Browse files Browse the repository at this point in the history
…efs BT#18201

Stored statements have ID to save
  • Loading branch information
AngelFQC committed Aug 29, 2022
1 parent 2fd3dbf commit 76eb407
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
2 changes: 0 additions & 2 deletions plugin/xapi/README.md
Expand Up @@ -85,6 +85,4 @@ ALTER TABLE xapi_internal_log ADD CONSTRAINT FK_C1C667ACA76ED395 FOREIGN KEY (us
ALTER TABLE xapi_internal_log ADD CONSTRAINT FK_C1C667ACA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE;
ALTER TABLE xapi_tool_launch ADD CONSTRAINT FK_E18CB58391D79BD3 FOREIGN KEY (c_id) REFERENCES course (id) ON DELETE CASCADE;
ALTER TABLE xapi_tool_launch ADD CONSTRAINT FK_E18CB583613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE;

ALTER TABLE xapi_internal_log CHANGE statement_id statement_id varchar(255) NULL;
```
6 changes: 3 additions & 3 deletions plugin/xapi/src/Entity/InternalLog.php
Expand Up @@ -30,7 +30,7 @@ class InternalLog
/**
* @var string
*
* @ORM\Column(name="statement_id", type="string", nullable=true)
* @ORM\Column(name="statement_id", type="string")
*/
private $statementId;
/**
Expand Down Expand Up @@ -105,12 +105,12 @@ public function setUser(User $user): InternalLog
return $this;
}

public function getStatementId(): ?string
public function getStatementId(): string
{
return $this->statementId;
}

public function setStatementId(?string $statementId): InternalLog
public function setStatementId(string $statementId): InternalLog
{
$this->statementId = $statementId;

Expand Down
43 changes: 35 additions & 8 deletions plugin/xapi/src/Lrs/StatementsController.php
Expand Up @@ -7,6 +7,8 @@
use Chamilo\PluginBundle\XApi\Lrs\Util\InternalLogUtil;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Xabbuh\XApi\Common\Exception\NotFoundException;
use Xabbuh\XApi\Model\StatementId;
use Xabbuh\XApi\Serializer\Symfony\ActorSerializer;
use Xabbuh\XApi\Serializer\Symfony\Serializer;
use Xabbuh\XApi\Serializer\Symfony\SerializerFactory;
Expand Down Expand Up @@ -89,11 +91,15 @@ public function put(): Response
)
;

InternalLogUtil::saveStatementForInternalLog($statement);

$putStatementController = new StatementPutController($this->statementRepository);

return $putStatementController->putStatement($this->httpRequest, $statement);
$response = $putStatementController->putStatement($this->httpRequest, $statement);

$this->saveLog(
[$this->httpRequest->query->get('statementId')]
);

return $response;
}

public function post(): Response
Expand All @@ -109,12 +115,33 @@ public function post(): Response
->deserializeStatements($content)
;

foreach ($statements as $statement) {
InternalLogUtil::saveStatementForInternalLog($statement);
}

$postStatementController = new StatementPostController($this->statementRepository);

return $postStatementController->postStatements($this->httpRequest, $statements);
$response = $postStatementController->postStatements($this->httpRequest, $statements);

$this->saveLog(
json_decode($response->getContent(), false)
);

return $response;
}

/**
* @param array<string> $statementsId
*
* @return void
*/
private function saveLog(array $statementsId)
{
foreach ($statementsId as $statementId) {
try {
$storedStatement = $this->statementRepository->findStatementById(
StatementId::fromString($statementId)
);

InternalLogUtil::saveStatementForInternalLog($storedStatement);
} catch (NotFoundException $e) {
}
}
}
}

0 comments on commit 76eb407

Please sign in to comment.