Skip to content

Commit

Permalink
feature: change to TestVariablesRecorded, and use it in ResultTransmi…
Browse files Browse the repository at this point in the history
…ssionEventHandler
  • Loading branch information
Karol-Stelmaczonek committed Dec 7, 2023
1 parent 3b923a9 commit 3925753
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 47 deletions.
46 changes: 2 additions & 44 deletions helpers/class.TestSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@
*/

use oat\oatbox\event\EventManager;
use oat\oatbox\service\exception\InvalidServiceManagerException;
use oat\oatbox\service\ServiceManager;
use oat\taoQtiTest\helpers\TestSessionMemento;
use oat\taoQtiTest\models\classes\event\ResultTestVariablesTransmissionEvent;
use oat\taoQtiTest\models\event\QtiTestChangeEvent;
use oat\taoQtiTest\models\event\QtiTestStateChangeEvent;
use oat\taoQtiTest\models\event\ResultItemVariablesTransmissionEvent;
use oat\taoQtiTest\models\event\ResultTestVariablesAfterTransmissionEvent;
use oat\taoResultServer\models\classes\implementation\ResultServerService;
use oat\taoResultServer\models\classes\ResultStorageWrapper;
use oat\taoTests\models\event\TestExecutionPausedEvent;
use oat\taoTests\models\event\TestExecutionResumedEvent;
Expand Down Expand Up @@ -61,7 +58,6 @@
use qtism\runtime\tests\Route;
use Symfony\Component\Lock\LockInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use taoResultServer_models_classes_ReadableResultStorage as ReadableResultStorage;

/**
* A TAO Specific extension of QtiSm's AssessmentTestSession class.
Expand Down Expand Up @@ -819,33 +815,11 @@ private function triggerResultTestVariablesTransmissionEvent(
array $variables,
string $testUri = ''
): void {
$event = new ResultTestVariablesTransmissionEvent(
$this->getEventManager()->trigger(new ResultTestVariablesTransmissionEvent(
$this->getSessionId(),
$variables,
$this->getSessionId(),
$testUri
);

$this->getEventManager()->attach($event, [$this, 'attachToTransmissionEvent']);
$this->getEventManager()->trigger($event);
}

public function attachToTransmissionEvent(ResultTestVariablesTransmissionEvent $event): void
{
$scoreTotal = array_filter(
$event->getVariables(),
function ($item) {
return $item->getIdentifier() === 'SCORE_TOTAL';
}
);

if (empty($scoreTotal)) {
return;
}
$outcomeVariables = $this->getResultsStorage()->getDeliveryVariables($event->getDeliveryExecutionId());
$this->getEventManager()->trigger(new ResultTestVariablesAfterTransmissionEvent(
$event->getDeliveryExecutionId(),
$outcomeVariables,
$testUri,
$this->isManualScored()
));
}
Expand Down Expand Up @@ -927,20 +901,4 @@ protected function getSessionMemento()
{
return new TestSessionMemento($this);
}

/**
* @throws common_exception_Error
* @throws InvalidServiceManagerException
*/
private function getResultsStorage(): ReadableResultStorage
{
$resultServerService = $this->getServiceLocator()->get(ResultServerService::SERVICE_ID);
$storage = $resultServerService->getResultStorage();

if (!$storage instanceof ReadableResultStorage) {
throw new common_exception_Error('Configured result storage is not writable.');
}

return $storage;
}
}
11 changes: 10 additions & 1 deletion models/classes/event/ResultTestVariablesTransmissionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ class ResultTestVariablesTransmissionEvent implements Event
private $transmissionId;
/** @var string */
private $testUri;
/** @var bool */
private bool $isManualScored;

public function __construct(
string $deliveryExecutionId,
array $variables,
string $transmissionId,
string $testUri = ''
string $testUri = '',
bool $isManualScored = null,
) {
$this->deliveryExecutionId = $deliveryExecutionId;
$this->variables = $variables;
$this->transmissionId = $transmissionId;
$this->testUri = $testUri;
$this->isManualScored = $isManualScored;
}

public function getName(): string
Expand All @@ -71,4 +75,9 @@ public function getTestUri(): string
{
return $this->testUri;
}

public function isManualScored(): bool
{
return $this->isManualScored;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@

use oat\oatbox\event\Event;

class ResultTestVariablesAfterTransmissionEvent implements Event
class TestVariablesRecorded implements Event
{
private string $deliveryExecutionId;

private array $variables;
private bool $isManualScored;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@

namespace oat\taoQtiTest\models\classes\eventHandler\ResultTransmissionEventHandler;

use oat\oatbox\event\EventManager;
use oat\oatbox\service\ServiceManager;
use oat\tao\model\service\InjectionAwareService;
use oat\taoDelivery\model\execution\DeliveryServerService;
use oat\taoQtiTest\models\classes\event\ResultTestVariablesTransmissionEvent;
use oat\taoQtiTest\models\event\ResultItemVariablesTransmissionEvent;
use oat\taoQtiTest\models\event\TestVariablesRecorded;
use taoQtiCommon_helpers_ResultTransmitter;
use oat\oatbox\service\exception\InvalidServiceManagerException;
use oat\taoResultServer\models\classes\implementation\ResultServerService;
use taoResultServer_models_classes_ReadableResultStorage as ReadableResultStorage;

class ResultTransmissionEventHandler extends InjectionAwareService implements
Api\ResultTransmissionEventHandlerInterface
Expand All @@ -45,6 +51,8 @@ public function transmitResultItemVariable(ResultItemVariablesTransmissionEvent
}

/**
* @param ResultTestVariablesTransmissionEvent $event
* @throws InvalidServiceManagerException
* @throws \taoQtiCommon_helpers_ResultTransmissionException
*/
public function transmitResultTestVariable(ResultTestVariablesTransmissionEvent $event): void
Expand All @@ -54,6 +62,12 @@ public function transmitResultTestVariable(ResultTestVariablesTransmissionEvent
$event->getTransmissionId(),
$event->getTestUri()
);

if (empty($this->containsScoreTotal($event))) {
return;
}

$this->triggerTestVariablesRecorded($event);
}

private function buildTransmitter($deliveryExecutionId): taoQtiCommon_helpers_ResultTransmitter
Expand All @@ -64,4 +78,59 @@ private function buildTransmitter($deliveryExecutionId): taoQtiCommon_helpers_Re

return new taoQtiCommon_helpers_ResultTransmitter($resultStore);
}

public function getEventManager()
{
return $this->getServiceLocator()->get(EventManager::SERVICE_ID);
}

public function getServiceLocator()
{
return ServiceManager::getServiceManager();
}

/**
* @throws common_exception_Error
* @throws InvalidServiceManagerException
*/
private function getResultsStorage(): ReadableResultStorage
{
$resultServerService = $this->getServiceLocator()->get(ResultServerService::SERVICE_ID);
$storage = $resultServerService->getResultStorage();

if (!$storage instanceof ReadableResultStorage) {
throw new common_exception_Error('Configured result storage is not writable.');
}

return $storage;
}

/**
* @param ResultTestVariablesTransmissionEvent $event
* @return array
*/
private function containsScoreTotal(ResultTestVariablesTransmissionEvent $event): array
{
return array_filter(
$event->getVariables(),
function ($item) {
return $item->getIdentifier() === 'SCORE_TOTAL';
}
);
}

/**
* @param ResultTestVariablesTransmissionEvent $event
* @return void
* @throws InvalidServiceManagerException
*/
private function triggerTestVariablesRecorded(ResultTestVariablesTransmissionEvent $event): void
{
$outcomeVariables = $this->getResultsStorage()->getDeliveryVariables($event->getDeliveryExecutionId());
$this->getEventManager()->trigger(new TestVariablesRecorded(
$event->getDeliveryExecutionId(),
$outcomeVariables,
$event->isManualScored()
));
}
}

0 comments on commit 3925753

Please sign in to comment.