*/ class LtiAssignment extends ConfigurableService { use OntologyAwareTrait; use LoggerAwareTrait; const LTI_MAX_ATTEMPTS_VARIABLE = 'custom_max_attempts'; /** * @deprecated Use LtiAssignmentAuthorizationService::SERVICE_ID instead */ const LTI_SERVICE_ID = 'ltiDeliveryProvider/assignment'; const SERVICE_ID = 'ltiDeliveryProvider/assignment'; /** * @param string $deliveryIdentifier * @param User $user * @return bool */ public function isDeliveryExecutionAllowed($deliveryIdentifier, User $user) { $delivery = $this->getResource($deliveryIdentifier); return $this->verifyToken($delivery, $user); } /** * Check Max. number of executions * * @param \core_kernel_classes_Resource $delivery * @param User $user * @return bool * @throws LtiException * @throws \common_exception_Error * @throws \core_kernel_persistence_Exception * @throws \oat\taoLti\models\classes\LtiVariableMissingException */ protected function verifyToken(\core_kernel_classes_Resource $delivery, User $user) { $propMaxExec = $delivery->getOnePropertyValue($this->getProperty(DeliveryContainerService::PROPERTY_MAX_EXEC)); $maxExec = is_null($propMaxExec) ? 0 : $propMaxExec->literal; $currentSession = $this->getServiceLocator()->get(SessionService::SERVICE_ID)->getCurrentSession(); if ($currentSession instanceof TaoLtiSession) { $launchData = $currentSession->getLaunchData(); if ($launchData->hasVariable(self::LTI_MAX_ATTEMPTS_VARIABLE)) { $val = $launchData->getVariable(self::LTI_MAX_ATTEMPTS_VARIABLE); if (!is_numeric($val)) { throw new LtiClientException( __('"max_attempts" variable must me numeric.'), LtiErrorMessage::ERROR_INVALID_PARAMETER ); } $maxExec = (int) $val; } } //check Tokens $usedTokens = count($this->getServiceLocator()->get(AttemptServiceInterface::SERVICE_ID) ->getAttempts($delivery->getUri(), $user)); if (($maxExec != 0) && ($usedTokens >= $maxExec)) { $this->logDebug("Attempt to start the compiled delivery " . $delivery->getUri() . " without tokens"); throw new LtiClientException( __('Attempts limit has been reached.'), LtiErrorMessage::ERROR_LAUNCH_FORBIDDEN ); } return true; } }