parent
29ce57d1cc
commit
00c0822b10
@ -0,0 +1,230 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\PluginBundle\Entity\XApi; |
||||||
|
|
||||||
|
use Chamilo\UserBundle\Entity\User; |
||||||
|
use DateTime; |
||||||
|
use Doctrine\ORM\Mapping as ORM; |
||||||
|
|
||||||
|
/** |
||||||
|
* @ORM\Table(name="xapi_internal_log") |
||||||
|
* @ORM\Entity() |
||||||
|
*/ |
||||||
|
class InternalLog |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var int |
||||||
|
* |
||||||
|
* @ORM\Column(name="id", type="integer") |
||||||
|
* @ORM\Id() |
||||||
|
* @ORM\GeneratedValue() |
||||||
|
*/ |
||||||
|
private $id; |
||||||
|
/** |
||||||
|
* @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User") |
||||||
|
* @ORM\JoinColumn(name="user_id", referencedColumnName="id") |
||||||
|
*/ |
||||||
|
private $user; |
||||||
|
/** |
||||||
|
* @var string |
||||||
|
* |
||||||
|
* @ORM\Column(name="statement_id", type="string") |
||||||
|
*/ |
||||||
|
private $statementId; |
||||||
|
/** |
||||||
|
* @var string |
||||||
|
* |
||||||
|
* @ORM\Column(name="verb", type="string") |
||||||
|
*/ |
||||||
|
private $verb; |
||||||
|
/** |
||||||
|
* @var string |
||||||
|
* |
||||||
|
* @ORM\Column(name="activity_id", type="string") |
||||||
|
*/ |
||||||
|
private $activityId; |
||||||
|
/** |
||||||
|
* @var string|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="activity_name", type="string", nullable=true) |
||||||
|
*/ |
||||||
|
private $activityName; |
||||||
|
/** |
||||||
|
* @var string|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="activity_description", type="string", nullable=true) |
||||||
|
*/ |
||||||
|
private $activityDescription; |
||||||
|
/** |
||||||
|
* @var float|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="score_scaled", type="float", nullable=true) |
||||||
|
*/ |
||||||
|
private $scoreScaled; |
||||||
|
/** |
||||||
|
* @var float|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="score_raw", type="float", nullable=true) |
||||||
|
*/ |
||||||
|
private $scoreRaw; |
||||||
|
/** |
||||||
|
* @var float|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="score_min", type="float", nullable=true) |
||||||
|
*/ |
||||||
|
private $scoreMin; |
||||||
|
/** |
||||||
|
* @var float|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="score_max", type="float", nullable=true) |
||||||
|
*/ |
||||||
|
private $scoreMax; |
||||||
|
/** |
||||||
|
* @var DateTime|null |
||||||
|
* |
||||||
|
* @ORM\Column(name="created_at", type="datetime", nullable=true) |
||||||
|
*/ |
||||||
|
private $createdAt; |
||||||
|
|
||||||
|
public function getId(): int |
||||||
|
{ |
||||||
|
return $this->id; |
||||||
|
} |
||||||
|
|
||||||
|
public function getUser(): User |
||||||
|
{ |
||||||
|
return $this->user; |
||||||
|
} |
||||||
|
|
||||||
|
public function setUser(User $user): InternalLog |
||||||
|
{ |
||||||
|
$this->user = $user; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getStatementId(): string |
||||||
|
{ |
||||||
|
return $this->statementId; |
||||||
|
} |
||||||
|
|
||||||
|
public function setStatementId(string $statementId): InternalLog |
||||||
|
{ |
||||||
|
$this->statementId = $statementId; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getVerb(): string |
||||||
|
{ |
||||||
|
return $this->verb; |
||||||
|
} |
||||||
|
|
||||||
|
public function setVerb(string $verb): InternalLog |
||||||
|
{ |
||||||
|
$this->verb = $verb; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getActivityId(): string |
||||||
|
{ |
||||||
|
return $this->activityId; |
||||||
|
} |
||||||
|
|
||||||
|
public function setActivityId(string $activityId): InternalLog |
||||||
|
{ |
||||||
|
$this->activityId = $activityId; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getActivityName(): ?string |
||||||
|
{ |
||||||
|
return $this->activityName; |
||||||
|
} |
||||||
|
|
||||||
|
public function setActivityName(?string $activityName): InternalLog |
||||||
|
{ |
||||||
|
$this->activityName = $activityName; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return string|null |
||||||
|
*/ |
||||||
|
public function getActivityDescription(): ?string |
||||||
|
{ |
||||||
|
return $this->activityDescription; |
||||||
|
} |
||||||
|
|
||||||
|
public function setActivityDescription(?string $activityDescription): InternalLog |
||||||
|
{ |
||||||
|
$this->activityDescription = $activityDescription; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getScoreScaled(): ?float |
||||||
|
{ |
||||||
|
return $this->scoreScaled; |
||||||
|
} |
||||||
|
|
||||||
|
public function setScoreScaled(?float $scoreScaled): InternalLog |
||||||
|
{ |
||||||
|
$this->scoreScaled = $scoreScaled; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getScoreRaw(): ?float |
||||||
|
{ |
||||||
|
return $this->scoreRaw; |
||||||
|
} |
||||||
|
|
||||||
|
public function setScoreRaw(?float $scoreRaw): InternalLog |
||||||
|
{ |
||||||
|
$this->scoreRaw = $scoreRaw; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getScoreMin(): ?float |
||||||
|
{ |
||||||
|
return $this->scoreMin; |
||||||
|
} |
||||||
|
|
||||||
|
public function setScoreMin(?float $scoreMin): InternalLog |
||||||
|
{ |
||||||
|
$this->scoreMin = $scoreMin; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getScoreMax(): ?float |
||||||
|
{ |
||||||
|
return $this->scoreMax; |
||||||
|
} |
||||||
|
|
||||||
|
public function setScoreMax(?float $scoreMax): InternalLog |
||||||
|
{ |
||||||
|
$this->scoreMax = $scoreMax; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function getCreatedAt(): ?DateTime |
||||||
|
{ |
||||||
|
return $this->createdAt; |
||||||
|
} |
||||||
|
|
||||||
|
public function setCreatedAt(?DateTime $createdAt): InternalLog |
||||||
|
{ |
||||||
|
$this->createdAt = $createdAt; |
||||||
|
|
||||||
|
return $this; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,118 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\PluginBundle\XApi\Lrs\Util; |
||||||
|
|
||||||
|
use Chamilo\PluginBundle\Entity\XApi\InternalLog; |
||||||
|
use Database; |
||||||
|
use Doctrine\ORM\ORMException; |
||||||
|
use UserManager; |
||||||
|
use Xabbuh\XApi\Model\Activity; |
||||||
|
use Xabbuh\XApi\Model\Actor; |
||||||
|
use Xabbuh\XApi\Model\Agent; |
||||||
|
use Xabbuh\XApi\Model\IRL; |
||||||
|
use Xabbuh\XApi\Model\Statement; |
||||||
|
use XApiPlugin; |
||||||
|
|
||||||
|
class InternalLogUtil |
||||||
|
{ |
||||||
|
public static function saveStatementForInternalLog(Statement $statement) |
||||||
|
{ |
||||||
|
if (null === $user = self::getUserFromActor($statement->getActor())) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
$internalLog = new InternalLog(); |
||||||
|
$internalLog->setUser($user); |
||||||
|
|
||||||
|
$languageIso = api_get_language_isocode(); |
||||||
|
|
||||||
|
$internalLog->setVerb( |
||||||
|
XApiPlugin::extractVerbInLanguage($statement->getVerb()->getDisplay(), $languageIso) |
||||||
|
); |
||||||
|
|
||||||
|
$statementObject = $statement->getObject(); |
||||||
|
|
||||||
|
if (!$statementObject instanceof Activity) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
$internalLog->setStatementId($statement->getId()->getValue()); |
||||||
|
|
||||||
|
if (null !== $definition = $statementObject->getDefinition()) { |
||||||
|
$internalLog->setActivityId( |
||||||
|
$statementObject->getId()->getValue() |
||||||
|
); |
||||||
|
|
||||||
|
if (null !== $nameInLanguages = $definition->getName()) { |
||||||
|
$internalLog->setActivityName( |
||||||
|
XApiPlugin::extractVerbInLanguage($nameInLanguages, $languageIso) |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
if (null !== $descriptionInLanguage = $definition->getDescription()) { |
||||||
|
$internalLog->setActivityDescription( |
||||||
|
XApiPlugin::extractVerbInLanguage($descriptionInLanguage, $languageIso) |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (null !== $statementResult = $statement->getResult()) { |
||||||
|
if (null !== $score = $statementResult->getScore()) { |
||||||
|
$internalLog |
||||||
|
->setScoreScaled( |
||||||
|
$score->getScaled() |
||||||
|
) |
||||||
|
->setScoreRaw( |
||||||
|
$score->getRaw() |
||||||
|
) |
||||||
|
->setScoreMin( |
||||||
|
$score->getMin() |
||||||
|
) |
||||||
|
->setScoreMax( |
||||||
|
$score->getMax() |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (null !== $created = $statement->getCreated()) { |
||||||
|
$internalLog->setCreatedAt($created); |
||||||
|
} |
||||||
|
|
||||||
|
$em = Database::getManager(); |
||||||
|
$em->persist($internalLog); |
||||||
|
$em->flush(); |
||||||
|
} |
||||||
|
|
||||||
|
private static function getUserFromActor(Actor $actor) |
||||||
|
{ |
||||||
|
if (!$actor instanceof Agent) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
$actorIri = $actor->getInverseFunctionalIdentifier(); |
||||||
|
|
||||||
|
if (null === $actorIri) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
$userRepo = UserManager::getRepository(); |
||||||
|
|
||||||
|
$user = null; |
||||||
|
|
||||||
|
if (null !== $mbox = $actorIri->getMbox()) { |
||||||
|
if ((null !== $parts = explode(':', $mbox->getValue(), 2)) && !empty($parts[1])) { |
||||||
|
$user = $userRepo->findOneBy(['email' => $parts[1]]); |
||||||
|
} |
||||||
|
} elseif (null !== $account = $actorIri->getAccount()) { |
||||||
|
$chamiloIrl = IRL::fromString(api_get_path(WEB_PATH)); |
||||||
|
|
||||||
|
if ($account->getHomePage()->equals($chamiloIrl)) { |
||||||
|
$user = $userRepo->findOneBy(['username' => $account->getName()]); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return $user; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue