/**
  * Gets the test session for a particular deliveryExecution
  *
  * @param DeliveryExecution $deliveryExecution
  * @return \qtism\runtime\tests\AssessmentTestSession
  * @throws \common_exception_Error
  * @throws \common_exception_MissingParameter
  */
 public function getTestSession(DeliveryExecution $deliveryExecution)
 {
     if (!isset($this->cache[$deliveryExecution->getIdentifier()]['session'])) {
         $resultServer = \taoResultServer_models_classes_ResultServerStateFull::singleton();
         $compiledDelivery = $deliveryExecution->getDelivery();
         $inputParameters = $this->getRuntimeInputParameters($deliveryExecution);
         $testDefinition = \taoQtiTest_helpers_Utils::getTestDefinition($inputParameters['QtiTestCompilation']);
         $testResource = new \core_kernel_classes_Resource($inputParameters['QtiTestDefinition']);
         $sessionManager = new \taoQtiTest_helpers_SessionManager($resultServer, $testResource);
         $userId = $deliveryExecution->getUserIdentifier();
         $qtiStorage = new \taoQtiTest_helpers_TestSessionStorage($sessionManager, new BinaryAssessmentTestSeeker($testDefinition), $userId);
         $sessionId = $deliveryExecution->getIdentifier();
         if ($qtiStorage->exists($sessionId)) {
             $session = $qtiStorage->retrieve($testDefinition, $sessionId);
             if ($session instanceof UserUriAware) {
                 $session->setUserUri($userId);
             }
             $resultServerUri = $compiledDelivery->getOnePropertyValue(new \core_kernel_classes_Property(TAO_DELIVERY_RESULTSERVER_PROP));
             $resultServerObject = new \taoResultServer_models_classes_ResultServer($resultServerUri, array());
             $resultServer->setValue('resultServerUri', $resultServerUri->getUri());
             $resultServer->setValue('resultServerObject', array($resultServerUri->getUri() => $resultServerObject));
             $resultServer->setValue('resultServer_deliveryResultIdentifier', $deliveryExecution->getIdentifier());
         } else {
             $session = null;
         }
         $this->cache[$deliveryExecution->getIdentifier()] = ['session' => $session, 'storage' => $qtiStorage];
     }
     return $this->cache[$deliveryExecution->getIdentifier()]['session'];
 }
 /**
  * initalize the resultserver for a given execution
  * @param core_kernel_classes_resource processExecution
  */
 public function initResultServer($compiledDelivery, $executionIdentifier)
 {
     //starts or resume a taoResultServerStateFull session for results submission
     //retrieve the result server definition
     $resultServer = $compiledDelivery->getUniquePropertyValue(new core_kernel_classes_Property(TAO_DELIVERY_RESULTSERVER_PROP));
     //callOptions are required in the case of a LTI basic storage
     taoResultServer_models_classes_ResultServerStateFull::singleton()->initResultServer($resultServer->getUri());
     //a unique identifier for data collected through this delivery execution
     //in the case of LTI, we should use the sourceId
     taoResultServer_models_classes_ResultServerStateFull::singleton()->spawnResult($executionIdentifier, $executionIdentifier);
     common_Logger::i("Spawning/resuming result identifier related to process execution " . $executionIdentifier);
     //set up the related test taker
     //a unique identifier for the test taker
     taoResultServer_models_classes_ResultServerStateFull::singleton()->storeRelatedTestTaker(common_session_SessionManager::getSession()->getUserUri());
     //a unique identifier for the delivery
     taoResultServer_models_classes_ResultServerStateFull::singleton()->storeRelatedDelivery($compiledDelivery->getUri());
 }
 /**
  * Action to finish test attempts where the maximum time limit has been reached.
  * To end user will be sent array that contains id's of finished session.
  */
 public function endExpiredTests()
 {
     $started = \taoDelivery_models_classes_DeliveryServerService::singleton()->getResumableDeliveries();
     $resultServer = \taoResultServer_models_classes_ResultServerStateFull::singleton();
     $result = array();
     foreach ($started as $deliveryExecution) {
         $compiledDelivery = $deliveryExecution->getDelivery();
         $runtime = \taoDelivery_models_classes_DeliveryAssemblyService::singleton()->getRuntime($compiledDelivery);
         $inputParameters = \tao_models_classes_service_ServiceCallHelper::getInputValues($runtime, array());
         $testDefinition = \taoQtiTest_helpers_Utils::getTestDefinition($inputParameters['QtiTestCompilation']);
         $testResource = new \core_kernel_classes_Resource($inputParameters['QtiTestDefinition']);
         $subjectProp = new \core_kernel_classes_Property(PROPERTY_DELVIERYEXECUTION_SUBJECT);
         $delvieryExecutionSubject = $deliveryExecution->getOnePropertyValue($subjectProp);
         $sessionManager = new \taoQtiTest_helpers_SessionManager($resultServer, $testResource);
         $qtiStorage = new \taoQtiTest_helpers_TestSessionStorage($sessionManager, new BinaryAssessmentTestSeeker($testDefinition), $delvieryExecutionSubject->getUri());
         $session = $qtiStorage->retrieve($testDefinition, $deliveryExecution->getUri());
         $resultServerUri = $compiledDelivery->getOnePropertyValue(new \core_kernel_classes_Property(TAO_DELIVERY_RESULTSERVER_PROP));
         $resultServerObject = new \taoResultServer_models_classes_ResultServer($resultServerUri, array());
         $resultServer->setValue('resultServerUri', $resultServerUri->getUri());
         $resultServer->setValue('resultServerObject', array($resultServerUri->getUri() => $resultServerObject));
         $resultServer->setValue('resultServer_deliveryResultIdentifier', $deliveryExecution->getUri());
         if ($session->isRunning() === false) {
             continue;
         }
         try {
             $session->checkTimeLimits(false, false, false);
         } catch (AssessmentTestSessionException $e) {
             if (AssessmentTestSessionException::ASSESSMENT_TEST_DURATION_OVERFLOW) {
                 $testSessionMetaData = new TestSessionMetaData($session);
                 $testSessionMetaData->save(array('TEST' => array('TEST_EXIT_CODE' => TestSessionMetaData::TEST_CODE_INCOMPLETE), 'SECTION' => array('SECTION_EXIT_CODE' => TestSessionMetaData::SECTION_CODE_TIMEOUT)));
                 $session->endTestSession();
                 $deliveryExecution->setState(INSTANCE_DELIVERYEXEC_FINISHED);
                 \common_Logger::i("Expired test session {$session->getSessionId()} has finished.");
                 $result[] = $session->getSessionId();
             }
         }
     }
     echo json_encode($result);
 }
 public function setUp()
 {
     TaoPhpUnitTestRunner::initTest();
     $this->service = \taoResultServer_models_classes_ResultServerStateFull::singleton();
 }
 /**
  * Save session metadata.
  * 
  * @param array $metaData Meta data array to be saved.
  * Example:
  * array(
  *   'TEST' => array('TEST_EXIT_CODE' => 'IC'),
  *   'SECTION' => array('SECTION_EXIT_CODE' => 701),
  * )
  */
 public function save(array $metaData)
 {
     $testUri = $this->session->getTest()->getUri();
     $resultServer = \taoResultServer_models_classes_ResultServerStateFull::singleton();
     foreach ($metaData as $type => $data) {
         foreach ($data as $key => $value) {
             $metaVariable = $this->getVariable($key, $value);
             if (strcasecmp($type, 'ITEM') === 0) {
                 $itemUri = \taoQtiTest_helpers_TestRunnerUtils::getCurrentItemUri($this);
                 $occurence = $this->session->getCurrentAssessmentItemRefOccurence();
                 $transmissionId = "{$sessionId}.{$item}.{$occurence}";
                 $resultServer->storeItemVariable($testUri, $itemUri, $metaVariable, $transmissionId);
             } elseif (strcasecmp($type, 'TEST') === 0) {
                 $resultServer->storeTestVariable($testUri, $metaVariable, $this->session->getSessionId());
             } elseif (strcasecmp($type, 'SECTION') === 0) {
                 //suffix section variables with _{SECTION_IDENTIFIER}
                 $assessmentSectionId = $this->session->getCurrentAssessmentSection()->getIdentifier();
                 $metaVariable->setIdentifier($key . '_' . $assessmentSectionId);
                 $resultServer->storeTestVariable($testUri, $metaVariable, $this->session->getSessionId());
             }
         }
     }
 }
 /**
  * Action to call to comment an item.
  * 
  */
 public function comment()
 {
     if ($this->beforeAction()) {
         $testSession = $this->getTestSession();
         $resultServer = taoResultServer_models_classes_ResultServerStateFull::singleton();
         $transmitter = new taoQtiCommon_helpers_ResultTransmitter($resultServer);
         // prepare transmission Id for result server.
         $item = $testSession->getCurrentAssessmentItemRef()->getIdentifier();
         $occurence = $testSession->getCurrentAssessmentItemRefOccurence();
         $sessionId = $testSession->getSessionId();
         $transmissionId = "{$sessionId}.{$item}.{$occurence}";
         // retrieve comment's intrinsic value.
         $comment = $this->getRequestParameter('comment');
         // build variable and send it.
         $itemUri = taoQtiTest_helpers_TestRunnerUtils::getCurrentItemUri($testSession);
         $testUri = $testSession->getTest()->getUri();
         $variable = new ResponseVariable('comment', Cardinality::SINGLE, BaseType::STRING, new QtismString($comment));
         $transmitter->transmitItemVariable($variable, $transmissionId, $itemUri, $testUri);
     }
 }
 /**
  * @param AssessmentItemRef $itemRef
  *
  * @return DateTime
  */
 public function getItemStartTime($itemRef)
 {
     $itemResults = array();
     $itemStartTime = null;
     $ssid = $this->getTestSession()->getSessionId();
     $resultServer = \taoResultServer_models_classes_ResultServerStateFull::singleton();
     $collection = $resultServer->getVariables("{$ssid}.{$itemRef->getIdentifier()}.{$this->getTestSession()->getCurrentAssessmentItemRefOccurence()}");
     foreach ($collection as $vars) {
         foreach ($vars as $var) {
             if ($var->variable instanceof taoResultServer_models_classes_TraceVariable && $var->variable->getIdentifier() === 'ITEM_START_TIME_SERVER') {
                 $itemResults[] = $var->variable->getValue();
             }
         }
     }
     $itemResults = array_map(function ($ts) {
         $itemStart = new DateTime('now', new DateTimeZone('UTC'));
         $itemStart->setTimestamp($ts);
         return $itemStart;
     }, $itemResults);
     if (!empty($itemResults)) {
         $itemStartTime = min($itemResults);
     }
     return $itemStartTime;
 }
 public function getClassService()
 {
     return taoResultServer_models_classes_ResultServerStateFull::singleton();
 }
 /**
  * Loads the storage
  * @throws \common_exception_Error
  */
 protected function initStorage()
 {
     $resultServer = \taoResultServer_models_classes_ResultServerStateFull::singleton();
     $testResource = new \core_kernel_classes_Resource($this->getTestDefinitionUri());
     $sessionManager = new \taoQtiTest_helpers_SessionManager($resultServer, $testResource);
     $seeker = new BinaryAssessmentTestSeeker($this->getTestDefinition());
     $userUri = \common_session_SessionManager::getSession()->getUserUri();
     $this->storage = new \taoQtiTest_helpers_TestSessionStorage($sessionManager, $seeker, $userUri);
 }