Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 122 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryTool | |
0.00% |
0 / 122 |
|
0.00% |
0 / 10 |
930 | |
0.00% |
0 / 1 |
| run | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
182 | |||
| launchQueue | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| checkCapacity | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| launch1p3 | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getLearnerUrl | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
30 | |||
| getTool | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDelivery | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getActiveDeliveryExecution | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| configureI18n | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getConcurringSessionService | |
0.00% |
0 / 1 |
|
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-2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\ltiDeliveryProvider\controller; |
| 22 | |
| 23 | use common_ext_ExtensionsManager; |
| 24 | use common_Logger; |
| 25 | use common_session_SessionManager; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\ltiDeliveryProvider\model\execution\LtiDeliveryExecutionService; |
| 28 | use oat\ltiDeliveryProvider\model\LtiAssignment; |
| 29 | use oat\ltiDeliveryProvider\model\LTIDeliveryTool; |
| 30 | use oat\ltiDeliveryProvider\model\LtiLaunchDataService; |
| 31 | use oat\ltiDeliveryProvider\model\navigation\LtiNavigationService; |
| 32 | use oat\tao\model\actionQueue\ActionFullException; |
| 33 | use oat\taoDelivery\model\Capacity\CapacityInterface; |
| 34 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 35 | use oat\taoLti\controller\ToolModule; |
| 36 | use oat\taoLti\models\classes\LtiException; |
| 37 | use oat\taoLti\models\classes\LtiMessages\LtiErrorMessage; |
| 38 | use oat\taoLti\models\classes\LtiRoles; |
| 39 | use oat\taoLti\models\classes\LtiService; |
| 40 | use oat\taoLti\models\classes\LtiVariableMissingException; |
| 41 | use oat\taoQtiTest\model\Service\ConcurringSessionService; |
| 42 | use oat\taoQtiTest\models\QtiTestExtractionFailedException; |
| 43 | use tao_helpers_I18n; |
| 44 | use tao_helpers_Uri; |
| 45 | |
| 46 | use function GuzzleHttp\Psr7\stream_for; |
| 47 | |
| 48 | class DeliveryTool extends ToolModule |
| 49 | { |
| 50 | /** |
| 51 | * Setting this parameter to 'true' will prevent resuming a testsession in progress |
| 52 | * and will start a new testsession whenever the lti tool is launched |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | public const PARAM_FORCE_RESTART = 'custom_force_restart'; |
| 57 | /** |
| 58 | * Setting this parameter to 'true' will prevent the thank you screen to be shown after |
| 59 | * the test and skip directly to the return url |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | public const PARAM_SKIP_THANKYOU = 'custom_skip_thankyou'; |
| 64 | |
| 65 | /** |
| 66 | * Setting this parameter to 'true' will prevent the 'You have already taken this test' |
| 67 | * screen to be shown skip directly to the return url |
| 68 | * @var string |
| 69 | */ |
| 70 | public const PARAM_SKIP_OVERVIEW = 'custom_skip_overview'; |
| 71 | |
| 72 | /** |
| 73 | * Setting this parameter to a string will show this string as the title of the thankyou |
| 74 | * page. (no effect if PARAM_SKIP_THANKYOU is set to 'true') |
| 75 | * |
| 76 | * @var string |
| 77 | */ |
| 78 | public const PARAM_THANKYOU_MESSAGE = 'custom_message'; |
| 79 | |
| 80 | /** |
| 81 | * (non-PHPdoc) |
| 82 | * @see ToolModule::run() |
| 83 | * |
| 84 | * @throws LtiException |
| 85 | * @throws \InterruptedActionException |
| 86 | * @throws \ResolverException |
| 87 | * @throws \common_exception_Error |
| 88 | * @throws \common_exception_IsAjaxAction |
| 89 | * @throws \common_exception_NotFound |
| 90 | * @throws LtiVariableMissingException |
| 91 | */ |
| 92 | public function run() |
| 93 | { |
| 94 | $compiledDelivery = $this->getDelivery(); |
| 95 | |
| 96 | if (is_null($compiledDelivery) || !$compiledDelivery->exists()) { |
| 97 | if ($this->hasAccess(LinkConfiguration::class, 'configureDelivery')) { |
| 98 | // user authorised to select the Delivery |
| 99 | $this->redirect(tao_helpers_Uri::url('configureDelivery', 'LinkConfiguration', null)); |
| 100 | } else { |
| 101 | // user NOT authorised to select the Delivery |
| 102 | throw new LtiException( |
| 103 | __('This tool has not yet been configured, please contact your instructor'), |
| 104 | LtiErrorMessage::ERROR_INVALID_PARAMETER |
| 105 | ); |
| 106 | } |
| 107 | } else { |
| 108 | $session = common_session_SessionManager::getSession(); |
| 109 | |
| 110 | if (is_null($session)) { |
| 111 | throw new LtiException(__('Test Session not found')); |
| 112 | } |
| 113 | |
| 114 | $user = $session->getUser(); |
| 115 | $ltiRoles = [LtiRoles::CONTEXT_LEARNER, LtiRoles::CONTEXT_LTI1P3_LEARNER]; |
| 116 | |
| 117 | $isLearner = !is_null($user) && count(array_intersect($ltiRoles, $user->getRoles())) > 0; |
| 118 | |
| 119 | $isDryRun = !$isLearner && in_array(LtiRoles::CONTEXT_LTI1P3_INSTRUCTOR, $user->getRoles(), true); |
| 120 | |
| 121 | if ($isLearner || $isDryRun) { |
| 122 | if ($this->hasAccess(DeliveryRunner::class, 'runDeliveryExecution')) { |
| 123 | try { |
| 124 | $activeExecution = $this->getActiveDeliveryExecution($compiledDelivery); |
| 125 | $this->getConcurringSessionService()->pauseActiveDeliveryExecutionsForUser($activeExecution); |
| 126 | $this->redirect($this->getLearnerUrl($compiledDelivery, $activeExecution)); |
| 127 | } catch (QtiTestExtractionFailedException $e) { |
| 128 | common_Logger::i($e->getMessage()); |
| 129 | throw new LtiException($e->getMessage()); |
| 130 | } catch (ActionFullException $e) { |
| 131 | $this->redirect(_url('launchQueue', 'DeliveryTool', null, [ |
| 132 | 'position' => $e->getPosition(), |
| 133 | 'delivery' => $compiledDelivery->getUri(), |
| 134 | ])); |
| 135 | } |
| 136 | } else { |
| 137 | common_Logger::e('Lti learner has no access to delivery runner'); |
| 138 | $this->returnError(__('Access to this functionality is restricted'), false); |
| 139 | } |
| 140 | } elseif ($this->hasAccess(LinkConfiguration::class, 'configureDelivery')) { |
| 141 | $this->redirect( |
| 142 | _url( |
| 143 | 'showDelivery', |
| 144 | 'LinkConfiguration', |
| 145 | null, |
| 146 | [ |
| 147 | 'uri' => $compiledDelivery->getUri() |
| 148 | ] |
| 149 | ) |
| 150 | ); |
| 151 | } else { |
| 152 | $this->returnError(__('Access to this functionality is restricted to students'), false); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @throws LtiException |
| 159 | * @throws \common_exception_Error |
| 160 | * @throws \common_ext_ExtensionException |
| 161 | */ |
| 162 | public function launchQueue() |
| 163 | { |
| 164 | $this->configureI18n(); |
| 165 | |
| 166 | $delivery = $this->getDelivery(); |
| 167 | if (!$delivery->exists()) { |
| 168 | throw new LtiException( |
| 169 | __('Delivery does not exist. Please contact your instructor.'), |
| 170 | LtiErrorMessage::ERROR_INVALID_PARAMETER |
| 171 | ); |
| 172 | } |
| 173 | $runUrl = _url('run', 'DeliveryTool', null, ['delivery' => $delivery->getUri()]); |
| 174 | $config = $this->getServiceLocator()->get('ltiDeliveryProvider/LaunchQueue')->getConfig(); |
| 175 | $config['runUrl'] = $runUrl; |
| 176 | $config['capacityCheckUrl'] = _url('checkCapacity', 'DeliveryTool'); |
| 177 | $this->defaultData(); |
| 178 | $this->setData('delivery', $delivery); |
| 179 | $this->setData('position', intval($this->getRequestParameter('position'))); |
| 180 | $this->setData('client_params', $config); |
| 181 | $this->setView('learner/launchQueue.tpl'); |
| 182 | } |
| 183 | |
| 184 | public function checkCapacity() |
| 185 | { |
| 186 | /** @var CapacityInterface $capacityService */ |
| 187 | $capacityService = $this->getServiceLocator()->get(CapacityInterface::SERVICE_ID); |
| 188 | $capacity = $capacityService->getCapacity(); |
| 189 | $payload = [ |
| 190 | 'id' => '', |
| 191 | 'status' => 0, |
| 192 | ]; |
| 193 | if ($capacity === -1 || $capacity > 0) { |
| 194 | $payload['status'] = 1; |
| 195 | } |
| 196 | |
| 197 | return $this->getPsrResponse()->withBody(stream_for(json_encode($payload))) |
| 198 | ->withHeader('Content-Type', 'application/json'); |
| 199 | } |
| 200 | |
| 201 | public function launch1p3(): void |
| 202 | { |
| 203 | $message = $this->getValidatedLtiMessagePayload(); |
| 204 | |
| 205 | LtiService::singleton()->startLti1p3Session($message); |
| 206 | $this->forward('run', null, null, $_GET); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * @param core_kernel_classes_Resource $delivery |
| 211 | * @param DeliveryExecution|null $activeExecution |
| 212 | * |
| 213 | * @return string |
| 214 | * @throws LtiException |
| 215 | * @throws \common_exception_Error |
| 216 | */ |
| 217 | protected function getLearnerUrl(\core_kernel_classes_Resource $delivery, DeliveryExecution $activeExecution = null) |
| 218 | { |
| 219 | $currentSession = \common_session_SessionManager::getSession(); |
| 220 | $user = $currentSession->getUser(); |
| 221 | if ($activeExecution === null) { |
| 222 | $activeExecution = $this->getActiveDeliveryExecution($delivery); |
| 223 | } |
| 224 | |
| 225 | if ($activeExecution !== null) { |
| 226 | return _url( |
| 227 | 'runDeliveryExecution', |
| 228 | 'DeliveryRunner', |
| 229 | null, |
| 230 | [ |
| 231 | 'deliveryExecution' => $activeExecution->getIdentifier() |
| 232 | ] |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | /** @var LtiAssignment $assignmentService */ |
| 237 | $assignmentService = $this->getServiceLocator()->get(LtiAssignment::SERVICE_ID); |
| 238 | |
| 239 | if (!$assignmentService->isDeliveryExecutionAllowed($delivery->getUri(), $user)) { |
| 240 | throw new LtiException( |
| 241 | __('User is not authorized to run this delivery'), |
| 242 | LtiErrorMessage::ERROR_LAUNCH_FORBIDDEN |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | if ($user->getLaunchData()->hasVariable(self::PARAM_SKIP_OVERVIEW)) { |
| 247 | $executionService = $this->getServiceLocator()->get(LtiDeliveryExecutionService::SERVICE_ID); |
| 248 | $executions = $executionService->getLinkedDeliveryExecutions( |
| 249 | $delivery, |
| 250 | $currentSession->getLtiLinkResource(), |
| 251 | $user->getIdentifier() |
| 252 | ); |
| 253 | $lastDE = end($executions); |
| 254 | /** @var LtiNavigationService $ltiNavigationService */ |
| 255 | $ltiNavigationService = $this->getServiceLocator()->get(LtiNavigationService::SERVICE_ID); |
| 256 | $url = $ltiNavigationService->getReturnUrl($user->getLaunchData(), $lastDE); |
| 257 | } else { |
| 258 | $url = _url( |
| 259 | 'ltiOverview', |
| 260 | 'DeliveryRunner', |
| 261 | null, |
| 262 | ['delivery' => $delivery->getUri()] |
| 263 | ); |
| 264 | } |
| 265 | return $url; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * (non-PHPdoc) |
| 270 | * @see ToolModule::getTool() |
| 271 | */ |
| 272 | protected function getTool() |
| 273 | { |
| 274 | return $this->getServiceLocator()->get(LTIDeliveryTool::class); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Returns the delivery associated with the current link |
| 279 | * either from url or from the remote_link if configured |
| 280 | * returns null if none found |
| 281 | * |
| 282 | * @return core_kernel_classes_Resource |
| 283 | * @throws LtiException |
| 284 | * @throws \common_exception_Error |
| 285 | */ |
| 286 | protected function getDelivery() |
| 287 | { |
| 288 | //passed as a parameter |
| 289 | if ($this->hasRequestParameter('delivery')) { |
| 290 | $returnValue = new core_kernel_classes_Resource($this->getRequestParameter('delivery')); |
| 291 | } else { |
| 292 | $launchData = LtiService::singleton()->getLtiSession()->getLaunchData(); |
| 293 | |
| 294 | /** @var LtiLaunchDataService $launchDataService */ |
| 295 | $launchDataService = $this->getServiceLocator()->get(LtiLaunchDataService::SERVICE_ID); |
| 296 | $returnValue = $launchDataService->findDeliveryFromLaunchData($launchData); |
| 297 | } |
| 298 | |
| 299 | return $returnValue; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * @param core_kernel_classes_Resource $delivery |
| 304 | * |
| 305 | * @return mixed|null|DeliveryExecution |
| 306 | */ |
| 307 | protected function getActiveDeliveryExecution(\core_kernel_classes_Resource $delivery) |
| 308 | { |
| 309 | return $this->getServiceLocator() |
| 310 | ->get(LtiDeliveryExecutionService::SERVICE_ID) |
| 311 | ->getActiveDeliveryExecution($delivery); |
| 312 | } |
| 313 | |
| 314 | private function configureI18n(): void |
| 315 | { |
| 316 | $extension = $this->getServiceLocator() |
| 317 | ->get(common_ext_ExtensionsManager::SERVICE_ID) |
| 318 | ->getExtensionById('ltiDeliveryProvider'); |
| 319 | |
| 320 | tao_helpers_I18n::init($extension, DEFAULT_ANONYMOUS_INTERFACE_LANG); |
| 321 | } |
| 322 | |
| 323 | private function getConcurringSessionService(): ConcurringSessionService |
| 324 | { |
| 325 | return $this->getPsrContainer()->get(ConcurringSessionService::class); |
| 326 | } |
| 327 | } |