/**
  * 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'];
 }
 /**
  * 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);
 }