Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| GetActiveDeliveryExecution | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __invoke | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
56 | |||
| getDelivery | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNumberOfActiveActions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTool | |
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) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\ltiDeliveryProvider\model\actions; |
| 23 | |
| 24 | use oat\tao\model\actionQueue\AbstractQueuedAction; |
| 25 | use oat\ltiDeliveryProvider\model\LTIDeliveryTool; |
| 26 | use oat\ltiDeliveryProvider\controller\DeliveryTool; |
| 27 | use oat\ltiDeliveryProvider\model\execution\LtiDeliveryExecutionService; |
| 28 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 29 | use oat\taoDelivery\model\execution\DeliveryServerService; |
| 30 | use oat\taoLti\models\classes\LtiException; |
| 31 | use oat\taoLti\models\classes\LtiService; |
| 32 | use oat\taoDelivery\model\execution\Counter\DeliveryExecutionCounterInterface; |
| 33 | use oat\taoDelivery\model\AttemptServiceInterface; |
| 34 | |
| 35 | /** |
| 36 | * Class GetActiveDeliveryExecution |
| 37 | * @package oat\ltiProctoring\model\actions |
| 38 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 39 | */ |
| 40 | class GetActiveDeliveryExecution extends AbstractQueuedAction |
| 41 | { |
| 42 | protected $delivery; |
| 43 | |
| 44 | public function __construct(\core_kernel_classes_Resource $delivery = null) |
| 45 | { |
| 46 | $this->delivery = $delivery; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param $params |
| 51 | * @return DeliveryExecution |
| 52 | * @throws LtiException |
| 53 | * @throws \common_exception_Error |
| 54 | * @throws \common_exception_NotFound |
| 55 | * @throws \common_exception_Unauthorized |
| 56 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 57 | * @throws \oat\taoLti\models\classes\LtiVariableMissingException |
| 58 | */ |
| 59 | public function __invoke($params) |
| 60 | { |
| 61 | $active = null; |
| 62 | |
| 63 | if ($this->delivery !== null) { |
| 64 | $remoteLink = LtiService::singleton()->getLtiSession()->getLtiLinkResource(); |
| 65 | $user = \common_session_SessionManager::getSession()->getUser(); |
| 66 | |
| 67 | $launchData = LtiService::singleton()->getLtiSession()->getLaunchData(); |
| 68 | /** @var LtiDeliveryExecutionService $deliveryExecutionService */ |
| 69 | $deliveryExecutionService = $this->getServiceManager()->get(LtiDeliveryExecutionService::SERVICE_ID); |
| 70 | |
| 71 | if ( |
| 72 | $launchData->hasVariable(DeliveryTool::PARAM_FORCE_RESTART) |
| 73 | && $launchData->getVariable(DeliveryTool::PARAM_FORCE_RESTART) == 'true' |
| 74 | ) { |
| 75 | // ignore existing executions to force restart |
| 76 | $executions = []; |
| 77 | } else { |
| 78 | $executions = $deliveryExecutionService->getLinkedDeliveryExecutions( |
| 79 | $this->delivery, |
| 80 | $remoteLink, |
| 81 | $user->getIdentifier() |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** @var AttemptServiceInterface $attemptService */ |
| 86 | $attemptService = $this->getServiceLocator()->get(AttemptServiceInterface::SERVICE_ID); |
| 87 | $satesToExclude = $attemptService->getStatesToExclude(); |
| 88 | //filter sates which should not be treated as an attempt |
| 89 | $executions = array_filter($executions, function ($execution) use ($satesToExclude) { |
| 90 | return !in_array($execution->getState()->getUri(), $satesToExclude); |
| 91 | }); |
| 92 | |
| 93 | if (empty($executions)) { |
| 94 | $active = $this->getTool()->startDelivery($this->delivery, $remoteLink, $user); |
| 95 | } else { |
| 96 | $resumable = $this->getServiceLocator()->get(DeliveryServerService::SERVICE_ID)->getResumableStates(); |
| 97 | foreach ($executions as $deliveryExecution) { |
| 98 | if (in_array($deliveryExecution->getState()->getUri(), $resumable)) { |
| 99 | $active = $deliveryExecution; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } else { |
| 105 | $this->logNotice('Attempt to invoke action `' . $this->getId() . '` without delivery'); |
| 106 | } |
| 107 | |
| 108 | return $active; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Needed for statistics |
| 113 | */ |
| 114 | public function getDelivery() |
| 115 | { |
| 116 | return $this->delivery; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @return int |
| 121 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 122 | */ |
| 123 | public function getNumberOfActiveActions() |
| 124 | { |
| 125 | /** @var LtiDeliveryExecutionService $deliveryExecutionService */ |
| 126 | $deliveryExecutionService = $this->getServiceManager()->get(DeliveryExecutionCounterInterface::SERVICE_ID); |
| 127 | return $deliveryExecutionService->count(DeliveryExecution::STATE_ACTIVE); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @return LTIDeliveryTool |
| 132 | */ |
| 133 | protected function getTool() |
| 134 | { |
| 135 | return $this->getServiceLocator()->get(LTIDeliveryTool::class); |
| 136 | } |
| 137 | } |