Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| KvLtiDeliveryExecutionService | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| getPersistence | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getLinkedDeliveryExecutions | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| createDeliveryExecutionLink | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| deleteDeliveryExecutionData | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| saveLinkReference | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryExecutionLinks | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 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\execution\implementation; |
| 23 | |
| 24 | use oat\ltiDeliveryProvider\model\execution\LtiDeliveryExecutionService as LtiDeliveryExecutionServiceInterface; |
| 25 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDeleteRequest; |
| 26 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\taoDelivery\model\execution\ServiceProxy; |
| 29 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionState; |
| 30 | |
| 31 | /** |
| 32 | * Class KvLtiDeliveryExecutionService |
| 33 | * Key value implementation of the LtiDeliveryExecutionServiceInterface |
| 34 | * @package oat\ltiDeliveryProvider\model\execution |
| 35 | */ |
| 36 | class KvLtiDeliveryExecutionService extends AbstractLtiDeliveryExecutionService |
| 37 | { |
| 38 | public const OPTION_PERSISTENCE = 'persistence'; |
| 39 | |
| 40 | public const LTI_DE_LINK_LINK = 'kvlti_ll_'; |
| 41 | |
| 42 | public const LINKS_OF_DELIVERY_EXECUTION = 'kvlti_links_de_'; |
| 43 | |
| 44 | /** |
| 45 | * @var \common_persistence_KeyValuePersistence |
| 46 | */ |
| 47 | private $persistence; |
| 48 | |
| 49 | /** |
| 50 | * @return \common_persistence_KeyValuePersistence|\common_persistence_Persistence |
| 51 | */ |
| 52 | protected function getPersistence() |
| 53 | { |
| 54 | if (is_null($this->persistence)) { |
| 55 | $persistenceOption = $this->getOption(self::OPTION_PERSISTENCE); |
| 56 | $this->persistence = (is_object($persistenceOption)) |
| 57 | ? $persistenceOption |
| 58 | : \common_persistence_KeyValuePersistence::getPersistence($persistenceOption); |
| 59 | } |
| 60 | return $this->persistence; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns an array of DeliveryExecution |
| 65 | * |
| 66 | * @param \core_kernel_classes_Resource $delivery |
| 67 | * @param \core_kernel_classes_Resource $link |
| 68 | * @param string $userId |
| 69 | * |
| 70 | * @throws \common_exception_NotFound |
| 71 | * |
| 72 | * @return DeliveryExecution[] |
| 73 | */ |
| 74 | public function getLinkedDeliveryExecutions( |
| 75 | \core_kernel_classes_Resource $delivery, |
| 76 | \core_kernel_classes_Resource $link, |
| 77 | $userId |
| 78 | ) { |
| 79 | $data = $this->getPersistence()->get(self::LTI_DE_LINK_LINK . $link->getUri() . $userId); |
| 80 | $ltiDeliveryExecutionLinks = KvLTIDeliveryExecutionLink::unSerialize($data); |
| 81 | |
| 82 | $results = []; |
| 83 | foreach ($ltiDeliveryExecutionLinks as $ltiDeliveryExecutionLink) { |
| 84 | /** @var DeliveryExecution $deliveryExecution */ |
| 85 | $deliveryExecution = $this->getServiceLocator()->get(ServiceProxy::SERVICE_ID)->getDeliveryExecution( |
| 86 | $ltiDeliveryExecutionLink->getDeliveryExecutionId() |
| 87 | ); |
| 88 | |
| 89 | if ($delivery->equals($deliveryExecution->getDelivery())) { |
| 90 | $results[] = $deliveryExecution; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $results; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param string $userUri |
| 99 | * @param string $link |
| 100 | * @param string $deliveryExecutionUri |
| 101 | * |
| 102 | * @throws \common_Exception |
| 103 | * |
| 104 | * @return KvLTIDeliveryExecutionLink |
| 105 | */ |
| 106 | public function createDeliveryExecutionLink($userUri, $link, $deliveryExecutionUri) |
| 107 | { |
| 108 | $persistence = $this->getPersistence(); |
| 109 | $ltiDeliveryExecutionLink = new KvLTIDeliveryExecutionLink($userUri, $deliveryExecutionUri, $link); |
| 110 | $key = self::LTI_DE_LINK_LINK . $link . $userUri; |
| 111 | |
| 112 | $ltiDeliveryExecutions = json_decode($persistence->get($key), true); |
| 113 | |
| 114 | if (!is_array($ltiDeliveryExecutions)) { |
| 115 | $ltiDeliveryExecutions = []; |
| 116 | } |
| 117 | |
| 118 | $ltiDeliveryExecutions[] = $ltiDeliveryExecutionLink->jsonSerialize(); |
| 119 | $persistence->set($key, json_encode($ltiDeliveryExecutions)); |
| 120 | $this->saveLinkReference($link, $userUri, $deliveryExecutionUri); |
| 121 | |
| 122 | return $ltiDeliveryExecutionLink; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @inheritdoc |
| 127 | */ |
| 128 | public function deleteDeliveryExecutionData(DeliveryExecutionDeleteRequest $request) |
| 129 | { |
| 130 | $userUri = $request->getDeliveryExecution()->getUserIdentifier(); |
| 131 | $deUri = $request->getDeliveryExecution()->getIdentifier(); |
| 132 | $deleted = []; |
| 133 | |
| 134 | $linksOfDelivery = $this->getDeliveryExecutionLinks($userUri, $deUri); |
| 135 | |
| 136 | foreach ($linksOfDelivery as $link) { |
| 137 | $deleted[] = $this->getPersistence()->del(self::LTI_DE_LINK_LINK . $link . $userUri); |
| 138 | } |
| 139 | |
| 140 | $this->getPersistence()->del(self::LINKS_OF_DELIVERY_EXECUTION . $userUri . $deUri); |
| 141 | |
| 142 | return !in_array(false, $deleted); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param $link |
| 147 | * @param $userUri |
| 148 | * @param $deliveryExecutionUri |
| 149 | * @return bool |
| 150 | * @throws \common_Exception |
| 151 | */ |
| 152 | protected function saveLinkReference($link, $userUri, $deliveryExecutionUri) |
| 153 | { |
| 154 | $linksOfExecutionAndUser = $this->getPersistence()->get( |
| 155 | static::LINKS_OF_DELIVERY_EXECUTION . $userUri . $deliveryExecutionUri |
| 156 | ); |
| 157 | |
| 158 | if (is_null($linksOfExecutionAndUser)) { |
| 159 | $linksOfExecutionAndUser = []; |
| 160 | } else { |
| 161 | $linksOfExecutionAndUser = json_decode($linksOfExecutionAndUser, true); |
| 162 | } |
| 163 | |
| 164 | $linksOfExecutionAndUser[] = $link; |
| 165 | |
| 166 | return $this->getPersistence()->set( |
| 167 | static::LINKS_OF_DELIVERY_EXECUTION . $userUri . $deliveryExecutionUri, |
| 168 | json_encode($linksOfExecutionAndUser) |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param $userUri |
| 174 | * @param $deliveryExecutionUri |
| 175 | * @return array |
| 176 | */ |
| 177 | protected function getDeliveryExecutionLinks($userUri, $deliveryExecutionUri) |
| 178 | { |
| 179 | $linksOfExecutionAndUser = $this->getPersistence()->get( |
| 180 | self::LINKS_OF_DELIVERY_EXECUTION . $userUri . $deliveryExecutionUri |
| 181 | ); |
| 182 | |
| 183 | if (empty($linksOfExecutionAndUser)) { |
| 184 | $linksOfExecutionAndUser = []; |
| 185 | } else { |
| 186 | $linksOfExecutionAndUser = json_decode($linksOfExecutionAndUser, true); |
| 187 | } |
| 188 | |
| 189 | return $linksOfExecutionAndUser; |
| 190 | } |
| 191 | } |