Skip to content

Commit

Permalink
Merge pull request #2429 from oat-sa/backport/INF-235-ags
Browse files Browse the repository at this point in the history
feature: add new event ResultTestVariablesAfterTransmissionEvent (#2425)
  • Loading branch information
Karol-Stelmaczonek authored Dec 13, 2023
2 parents 1958ffd + 06b04ec commit 006e6e0
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 5 deletions.
21 changes: 20 additions & 1 deletion helpers/class.TestSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
16 changes: 12 additions & 4 deletions models/classes/event/ResultTestVariablesTransmissionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,4 +75,9 @@ public function getTestUri(): string
{
return $this->testUri;
}

public function isManualScored(): bool
{
return $this->isManualScored;
}
}
62 changes: 62 additions & 0 deletions models/classes/event/TestVariablesRecorded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\taoQtiTest\models\event;

use oat\oatbox\event\Event;

class TestVariablesRecorded implements Event
{
private string $deliveryExecutionId;
private array $variables;
private bool $isManualScored;

public function __construct(
string $deliveryExecutionId,
array $variables,
bool $isManualScored
) {
$this->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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -45,6 +53,8 @@ public function transmitResultItemVariable(ResultItemVariablesTransmissionEvent
}

/**
* @param ResultTestVariablesTransmissionEvent $event
* @throws InvalidServiceManagerException
* @throws \taoQtiCommon_helpers_ResultTransmissionException
*/
public function transmitResultTestVariable(ResultTestVariablesTransmissionEvent $event): void
Expand All @@ -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
Expand All @@ -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()
));
}
}

0 comments on commit 006e6e0

Please sign in to comment.