Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
13.33% |
6 / 45 |
|
16.67% |
2 / 12 |
CRAP | |
0.00% |
0 / 1 |
| LTIDeliveryTool | |
13.33% |
6 / 45 |
|
16.67% |
2 / 12 |
161.47 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLaunchUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryFromLink | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| linkDeliveryExecution | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getFinishUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| startDelivery | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| getAuthorizationProvider | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getLinkedDeliveryExecutions | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| linkLtiResultId | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getLtiLaunchData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| storeLtiContext | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLtiContextRepository | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\ltiDeliveryProvider\model; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use core_kernel_classes_Resource as Resource; |
| 27 | use oat\ltiDeliveryProvider\model\execution\LtiContextRepositoryInterface; |
| 28 | use oat\ltiDeliveryProvider\model\execution\LtiDeliveryExecutionService; |
| 29 | use oat\ltiDeliveryProvider\model\navigation\LtiNavigationService; |
| 30 | use oat\oatbox\mutex\LockTrait; |
| 31 | use oat\oatbox\service\ConfigurableService; |
| 32 | use oat\oatbox\service\ServiceManager; |
| 33 | use oat\oatbox\session\SessionService; |
| 34 | use oat\oatbox\user\User; |
| 35 | use oat\taoDelivery\model\authorization\AuthorizationProvider; |
| 36 | use oat\taoDelivery\model\authorization\AuthorizationService; |
| 37 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 38 | use oat\taoDelivery\model\execution\StateServiceInterface; |
| 39 | use oat\taoLti\models\classes\LtiException; |
| 40 | use oat\taoLti\models\classes\LtiLaunchData; |
| 41 | use oat\taoLti\models\classes\LtiService; |
| 42 | use oat\taoLti\models\classes\TaoLtiSession; |
| 43 | |
| 44 | class LTIDeliveryTool extends ConfigurableService |
| 45 | { |
| 46 | use LockTrait; |
| 47 | |
| 48 | public const TOOL_INSTANCE = 'http://www.tao.lu/Ontologies/TAOLTI.rdf#LTIToolDelivery'; |
| 49 | public const PROPERTY_LINK_DELIVERY = 'http://www.tao.lu/Ontologies/TAOLTI.rdf#LinkDelivery'; |
| 50 | |
| 51 | private const EXTENSION = 'ltiDeliveryProvider'; |
| 52 | private const MODULE = 'DeliveryTool'; |
| 53 | private const ACTION = 'launch'; |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * @return static |
| 58 | * |
| 59 | * @deprecated Added fo backward compatibility. Use service locator instead. |
| 60 | */ |
| 61 | public static function singleton(): self |
| 62 | { |
| 63 | return ServiceManager::getServiceManager()->get(static::class); |
| 64 | } |
| 65 | |
| 66 | public function getLaunchUrl($parameters = []) |
| 67 | { |
| 68 | $fullAction = self::ACTION . '/' . base64_encode(json_encode($parameters)); |
| 69 | return _url($fullAction, self::MODULE, self::EXTENSION); |
| 70 | } |
| 71 | |
| 72 | public function getDeliveryFromLink() |
| 73 | { |
| 74 | $remoteLink = LtiService::singleton()->getLtiSession()->getLtiLinkResource(); |
| 75 | return $remoteLink->getOnePropertyValue(new core_kernel_classes_Property(static::PROPERTY_LINK_DELIVERY)); |
| 76 | } |
| 77 | |
| 78 | public function linkDeliveryExecution(Resource $link, $userUri, Resource $deliveryExecution) |
| 79 | { |
| 80 | $link = $this->getServiceLocator()->get(LtiDeliveryExecutionService::SERVICE_ID) |
| 81 | ->createDeliveryExecutionLink($userUri, $link->getUri(), $deliveryExecution->getUri()); |
| 82 | |
| 83 | return !is_null($link); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param DeliveryExecution $deliveryExecution |
| 88 | * @return mixed |
| 89 | */ |
| 90 | public function getFinishUrl(DeliveryExecution $deliveryExecution) |
| 91 | { |
| 92 | /** @var LtiNavigationService $ltiNavigationService */ |
| 93 | $ltiNavigationService = $this->getServiceLocator()->get(LtiNavigationService::SERVICE_ID); |
| 94 | |
| 95 | return $ltiNavigationService->getReturnUrl($this->getLtiLaunchData(), $deliveryExecution); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Start a new delivery execution |
| 100 | * |
| 101 | * @throws \common_exception_Unauthorized |
| 102 | */ |
| 103 | public function startDelivery(Resource $delivery, Resource $link, User $user): DeliveryExecution |
| 104 | { |
| 105 | $lock = $this->createLock(__METHOD__ . $delivery->getUri() . $user->getIdentifier(), 30); |
| 106 | $lock->acquire(true); |
| 107 | |
| 108 | $this->getAuthorizationProvider()->verifyStartAuthorization($delivery->getUri(), $user); |
| 109 | |
| 110 | /** @var LtiAssignment $assignmentService */ |
| 111 | $assignmentService = $this->getServiceLocator()->get(LtiAssignment::SERVICE_ID); |
| 112 | if (!$assignmentService->isDeliveryExecutionAllowed($delivery->getUri(), $user)) { |
| 113 | $lock->release(); |
| 114 | throw new \common_exception_Unauthorized(__('User is not authorized to run this delivery')); |
| 115 | } |
| 116 | $stateService = $this->getServiceLocator()->get(StateServiceInterface::SERVICE_ID); |
| 117 | $deliveryExecution = $stateService->createDeliveryExecution($delivery->getUri(), $user, $delivery->getLabel()); |
| 118 | $this->linkLtiResultId($deliveryExecution); |
| 119 | $this->getServiceLocator()->get(LtiDeliveryExecutionService::SERVICE_ID) |
| 120 | ->createDeliveryExecutionLink($user->getIdentifier(), $link->getUri(), $deliveryExecution->getIdentifier()); |
| 121 | $lock->release(); |
| 122 | |
| 123 | $this->storeLtiContext($this->getLtiLaunchData(), $deliveryExecution); |
| 124 | |
| 125 | return $deliveryExecution; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Gives you the authorization provider for the given execution. |
| 130 | * |
| 131 | * @return AuthorizationProvider |
| 132 | */ |
| 133 | protected function getAuthorizationProvider() |
| 134 | { |
| 135 | $authService = $this->getServiceLocator()->get(AuthorizationService::SERVICE_ID); |
| 136 | return $authService->getAuthorizationProvider(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns an array of DeliveryExecution |
| 141 | * |
| 142 | * @param string $userId |
| 143 | * @return array |
| 144 | */ |
| 145 | public function getLinkedDeliveryExecutions(Resource $delivery, Resource $link, $userId) |
| 146 | { |
| 147 | /** @var LtiDeliveryExecutionService $deliveryExecutionService */ |
| 148 | $deliveryExecutionService = $this->getServiceLocator()->get(LtiDeliveryExecutionService::SERVICE_ID); |
| 149 | $executions = $deliveryExecutionService->getLinkedDeliveryExecutions($delivery, $link, $userId); |
| 150 | return $executions; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Link `lis_result_sourcedid` to delivery execution |
| 155 | * in order to be able to retrieve delivery execution by lis_result_sourcedid |
| 156 | * |
| 157 | * @throws \common_exception_Error |
| 158 | * @throws \oat\taoLti\models\classes\LtiException |
| 159 | * @throws \oat\taoLti\models\classes\LtiVariableMissingException |
| 160 | */ |
| 161 | protected function linkLtiResultId(DeliveryExecution $deliveryExecution) |
| 162 | { |
| 163 | $executionIdentifier = $deliveryExecution->getIdentifier(); |
| 164 | // lis_outcome_service_url This value should not change from one launch to the next and in general, |
| 165 | // the TP can expect that there is a one-to-one mapping between |
| 166 | // the lis_outcome_service_url and a particular oauth_consumer_key. |
| 167 | // This value might change if there was a significant re-configuration |
| 168 | // of the TC system or if the TC moved from one domain to another. |
| 169 | $launchData = $this->getLtiLaunchData(); |
| 170 | $resultIdentifier = $launchData->hasVariable('lis_result_sourcedid') |
| 171 | ? $launchData->getVariable('lis_result_sourcedid') |
| 172 | : $executionIdentifier; |
| 173 | |
| 174 | /** @var LtiResultAliasStorage $ltiResultIdStorage */ |
| 175 | $ltiResultIdStorage = $this->getServiceLocator()->get(LtiResultAliasStorage::SERVICE_ID); |
| 176 | $ltiResultIdStorage->storeResultAlias($executionIdentifier, $resultIdentifier); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @throws LtiException |
| 181 | */ |
| 182 | protected function getLtiLaunchData(): LtiLaunchData |
| 183 | { |
| 184 | $session = $this->getServiceLocator()->get(SessionService::SERVICE_ID)->getCurrentSession(); |
| 185 | if (!$session instanceof TaoLtiSession) { |
| 186 | throw new LtiException('Not an LTI session.'); |
| 187 | } |
| 188 | |
| 189 | return $session->getLaunchData(); |
| 190 | } |
| 191 | |
| 192 | private function storeLtiContext(LtiLaunchData $ltiLaunchData, $deliveryExecution): void |
| 193 | { |
| 194 | $this->getLtiContextRepository()->save($ltiLaunchData, $deliveryExecution); |
| 195 | } |
| 196 | |
| 197 | private function getLtiContextRepository(): LtiContextRepositoryInterface |
| 198 | { |
| 199 | return $this->getServiceManager() |
| 200 | ->getContainer() |
| 201 | ->get(LtiContextRepositoryInterface::class); |
| 202 | } |
| 203 | } |