*/ namespace oat\taoQtiTest\models\runner\communicator; use oat\taoQtiTest\models\ExtendedStateService; use oat\taoQtiTest\models\runner\QtiRunnerMessageService; use oat\taoQtiTest\models\runner\QtiRunnerServiceContext; use qtism\runtime\tests\AssessmentTestSessionState; use Zend\ServiceManager\ServiceLocatorAwareInterface; use Zend\ServiceManager\ServiceLocatorAwareTrait; /** * Class TestStateChannel * * Describes the test session state * * @package oat\taoQtiTest\models\runner\communicator */ class TestStateChannel implements ServiceLocatorAwareInterface, CommunicationChannel { use ServiceLocatorAwareTrait; const CHANNEL_NAME = 'teststate'; /** * Get name of channel * @return string */ public function getName() { return self::CHANNEL_NAME; } /** * Generate output message based on test session state * @param QtiRunnerServiceContext $context Current runner context * @param array $data * @return array */ public function process(QtiRunnerServiceContext $context, array $data = []) { $result = null; $session = $context->getTestSession(); $state = $session->getState(); $sessionId = $session->getSessionId(); $messageService = $this->getServiceLocator()->get(QtiRunnerMessageService::SERVICE_ID); $stateService = $this->getServiceLocator()->get(ExtendedStateService::SERVICE_ID); $events = $stateService->getEvents($sessionId); $ids = []; foreach ($events as $event) { if ($event['type'] == self::CHANNEL_NAME) { $ids[] = $event['id']; if (isset($event['data']['state'])) { $state = $event['data']['state']; } else { \common_Logger::w('The state is missing from the ' . self::CHANNEL_NAME . ' event context'); } if (isset($event['data']['message'])) { //translate the message to the current user language. $message = __($event['data']['message']); } else { $message = $messageService->getStateMessage($context->getTestSession()); \common_Logger::w('The message is missing from the ' . self::CHANNEL_NAME . ' event context'); } if (!$result || $state == AssessmentTestSessionState::CLOSED) { if ($state == AssessmentTestSessionState::CLOSED) { $type = 'close'; } elseif ($state == AssessmentTestSessionState::SUSPENDED) { $type = 'pause'; } else { $type = null; } if (is_null($type)) { \common_Logger::w('Inconsistent ' . self::CHANNEL_NAME . ' event'); } else { $result = [ 'type' => $type, 'code' => $state, 'message' => $message, ]; } } } } if (count($ids)) { $stateService->removeEvents($sessionId, $ids); } return $result; } }