diff --git a/helpers/class.TestSession.php b/helpers/class.TestSession.php index 0e92210aa7..79951097c1 100644 --- a/helpers/class.TestSession.php +++ b/helpers/class.TestSession.php @@ -44,6 +44,7 @@ use qtism\data\processing\OutcomeProcessing; use qtism\data\rules\OutcomeRuleCollection; use qtism\data\rules\SetOutcomeValue; +use qtism\data\state\OutcomeDeclaration; use qtism\runtime\common\OutcomeVariable; use qtism\runtime\common\ProcessingException; use qtism\runtime\processing\OutcomeProcessingEngine; @@ -772,10 +773,28 @@ private function triggerResultTestVariablesTransmissionEvent( $this->getSessionId(), $variables, $this->getSessionId(), - $testUri + $testUri, + $this->isManualScored() )); } + private function isManualScored(): bool + { + /** @var AssessmentItemRef $itemRef */ + foreach ($this->getRoute()->getAssessmentItemRefs() as $itemRef) { + foreach ($itemRef->getComponents() as $component) { + if ( + $component instanceof OutcomeDeclaration + && $component->isExternallyScored() + ) { + return true; + } + } + } + + return false; + } + /** * @param TestSessionMemento $sessionMemento */ diff --git a/models/classes/event/ResultTestVariablesTransmissionEvent.php b/models/classes/event/ResultTestVariablesTransmissionEvent.php index 605f5198bb..51ba39b1a4 100644 --- a/models/classes/event/ResultTestVariablesTransmissionEvent.php +++ b/models/classes/event/ResultTestVariablesTransmissionEvent.php @@ -34,18 +34,21 @@ class ResultTestVariablesTransmissionEvent implements Event private $transmissionId; /** @var string */ private $testUri; + /** @var bool */ + private bool $isManualScored; public function __construct( string $deliveryExecutionId, - array $variables, + 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 @@ -72,4 +75,9 @@ public function getTestUri(): string { return $this->testUri; } + + public function isManualScored(): bool + { + return $this->isManualScored; + } } diff --git a/models/classes/event/TestVariablesRecorded.php b/models/classes/event/TestVariablesRecorded.php new file mode 100644 index 0000000000..6f8c39e3cc --- /dev/null +++ b/models/classes/event/TestVariablesRecorded.php @@ -0,0 +1,62 @@ +deliveryExecutionId = $deliveryExecutionId; + $this->variables = $variables; + $this->isManualScored = $isManualScored; + } + + public function getName(): string + { + return self::class; + } + + public function getDeliveryExecutionId(): string + { + return $this->deliveryExecutionId; + } + + public function getVariables(): array + { + return $this->variables; + } + + public function getIsManualScored(): bool + { + return $this->isManualScored; + } +} diff --git a/models/classes/eventHandler/ResultTransmissionEventHandler/ResultTransmissionEventHandler.php b/models/classes/eventHandler/ResultTransmissionEventHandler/ResultTransmissionEventHandler.php index c99d5e3d66..e0264f7814 100644 --- a/models/classes/eventHandler/ResultTransmissionEventHandler/ResultTransmissionEventHandler.php +++ b/models/classes/eventHandler/ResultTransmissionEventHandler/ResultTransmissionEventHandler.php @@ -22,11 +22,19 @@ namespace oat\taoQtiTest\models\classes\eventHandler\ResultTransmissionEventHandler; +use common_exception_Error; +use oat\oatbox\event\EventManager; +use oat\oatbox\service\ServiceManager; +use oat\oatbox\service\ServiceNotFoundException; 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 { @@ -45,6 +53,8 @@ public function transmitResultItemVariable(ResultItemVariablesTransmissionEvent } /** + * @param ResultTestVariablesTransmissionEvent $event + * @throws InvalidServiceManagerException * @throws \taoQtiCommon_helpers_ResultTransmissionException */ public function transmitResultTestVariable(ResultTestVariablesTransmissionEvent $event): void @@ -54,6 +64,12 @@ public function transmitResultTestVariable(ResultTestVariablesTransmissionEvent $event->getTransmissionId(), $event->getTestUri() ); + + if (!$this->containsScoreTotal($event)) { + return; + } + + $this->triggerTestVariablesRecorded($event); } private function buildTransmitter($deliveryExecutionId): taoQtiCommon_helpers_ResultTransmitter @@ -64,4 +80,58 @@ 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(); + } + + /** + * @return ReadableResultStorage + * @throws ServiceNotFoundException + * @throws common_exception_Error + */ + 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; + } + + private function containsScoreTotal(ResultTestVariablesTransmissionEvent $event): bool + { + $scoreTotal = array_filter( + $event->getVariables(), + function ($item) { + return $item->getIdentifier() === 'SCORE_TOTAL'; + } + ); + + return !empty($scoreTotal); + } + + /** + * @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() + )); + } }