Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
LtiResultAliasStorage | |
0.00% |
0 / 46 |
|
0.00% |
0 / 8 |
132 | |
0.00% |
0 / 1 |
getTableName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
storeResultAlias | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
getResultAlias | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getDeliveryExecutionId | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
install | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getPersistence | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
deleteDeliveryExecutionData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getQueryBuilder | |
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; |
23 | |
24 | use oat\oatbox\service\ConfigurableService; |
25 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDelete; |
26 | use oat\taoDelivery\model\execution\Delete\DeliveryExecutionDeleteRequest; |
27 | use oat\taoResultServer\models\classes\ResultAliasServiceInterface; |
28 | |
29 | /** |
30 | * Class LtiResultAliasStorage |
31 | * @package oat\ltiDeliveryProvider\model |
32 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
33 | */ |
34 | class LtiResultAliasStorage extends ConfigurableService implements DeliveryExecutionDelete |
35 | { |
36 | public const OPTION_PERSISTENCE = 'persistence'; |
37 | |
38 | public const SERVICE_ID = 'ltiDeliveryProvider/LtiResultIdStorage'; |
39 | |
40 | public const TABLE_NAME = 'lti_result_identifiers'; |
41 | public const DELIVERY_EXECUTION_ID = 'delivery_execution_id'; |
42 | public const RESULT_ID = 'result_id'; |
43 | |
44 | /** @var \common_persistence_SqlPersistence */ |
45 | protected $persistence; |
46 | |
47 | /** |
48 | * @return string |
49 | */ |
50 | public function getTableName() |
51 | { |
52 | return self::TABLE_NAME; |
53 | } |
54 | |
55 | /** |
56 | * Add record to the storage |
57 | * @param string $deliveryExecutionId |
58 | * @param string $resultId |
59 | * @return boolean |
60 | */ |
61 | public function storeResultAlias($deliveryExecutionId, $resultId) |
62 | { |
63 | $data = [ |
64 | self::DELIVERY_EXECUTION_ID => $deliveryExecutionId, |
65 | self::RESULT_ID => $resultId, |
66 | ]; |
67 | $queryBuilder = $this->getQueryBuilder(); |
68 | $queryBuilder->delete($this->getTableName()); |
69 | $queryBuilder->where(self::RESULT_ID . '=:' . self::RESULT_ID); |
70 | $queryBuilder->orWhere(self::DELIVERY_EXECUTION_ID . '=:' . self::DELIVERY_EXECUTION_ID); |
71 | $queryBuilder->setParameters([ |
72 | self::RESULT_ID => $resultId, |
73 | self::DELIVERY_EXECUTION_ID => $deliveryExecutionId |
74 | ]); |
75 | $queryBuilder->execute(); |
76 | return $this->getPersistence()->insert(self::TABLE_NAME, $data) === 1; |
77 | } |
78 | |
79 | /** |
80 | * @see ResultAliasServiceInterface::getResultAlias |
81 | */ |
82 | public function getResultAlias($deliveryExecutionId) |
83 | { |
84 | $queryBuilder = $this->getQueryBuilder(); |
85 | $queryBuilder->select(self::RESULT_ID); |
86 | $queryBuilder->where('t.' . self::DELIVERY_EXECUTION_ID . '=?'); |
87 | $queryBuilder->setParameters([$deliveryExecutionId]); |
88 | $stmt = $this->persistence->query($queryBuilder->getSQL(), $queryBuilder->getParameters()); |
89 | $result = $stmt->fetch(\PDO::FETCH_COLUMN); |
90 | return $result === false ? [] : [$result]; |
91 | } |
92 | |
93 | /** |
94 | * @see ResultAliasServiceInterface::getDeliveryExecutionId |
95 | * |
96 | * Should return null if not found, but as there is no aggregation of alias |
97 | * services yet, we need mimic the oat\taoResultServer\models\classes::ResultAliasService |
98 | * behaviour here |
99 | */ |
100 | public function getDeliveryExecutionId($aliasId) |
101 | { |
102 | $queryBuilder = $this->getQueryBuilder(); |
103 | $queryBuilder->select(self::DELIVERY_EXECUTION_ID); |
104 | $queryBuilder->where('t.' . self::RESULT_ID . '=?'); |
105 | $queryBuilder->setParameters([$aliasId]); |
106 | $stmt = $this->persistence->query($queryBuilder->getSQL(), $queryBuilder->getParameters()); |
107 | $data = $stmt->fetch(\PDO::FETCH_ASSOC); |
108 | return isset($data[self::DELIVERY_EXECUTION_ID]) |
109 | ? $data[self::DELIVERY_EXECUTION_ID] |
110 | : $aliasId; |
111 | } |
112 | |
113 | /** |
114 | * Create table in database |
115 | * @param $persistence |
116 | */ |
117 | public static function install($persistence) |
118 | { |
119 | $migration = new \oat\ltiDeliveryProvider\scripts\dbMigrations\LtiResultAliasStorage_v1(); |
120 | $migration->apply($persistence); |
121 | } |
122 | |
123 | /** |
124 | * @return \common_persistence_SqlPersistence |
125 | */ |
126 | public function getPersistence() |
127 | { |
128 | $persistenceId = $this->getOption(self::OPTION_PERSISTENCE); |
129 | if (is_null($this->persistence)) { |
130 | $this->persistence = $this->getServiceManager() |
131 | ->get(\common_persistence_Manager::SERVICE_ID) |
132 | ->getPersistenceById($persistenceId); |
133 | } |
134 | |
135 | return $this->persistence; |
136 | } |
137 | |
138 | /** |
139 | * @inheritdoc |
140 | */ |
141 | public function deleteDeliveryExecutionData(DeliveryExecutionDeleteRequest $request) |
142 | { |
143 | $queryBuilder = $this->getQueryBuilder(); |
144 | $queryBuilder |
145 | ->delete(static::TABLE_NAME) |
146 | ->where(static::DELIVERY_EXECUTION_ID . '=:deliveryExecutionId') |
147 | ->setParameter('deliveryExecutionId', $request->getDeliveryExecution()->getIdentifier()); |
148 | |
149 | return $this->persistence->exec($queryBuilder->getSQL(), $queryBuilder->getParameters()); |
150 | } |
151 | |
152 | /** |
153 | * @return \Doctrine\DBAL\Query\QueryBuilder |
154 | */ |
155 | private function getQueryBuilder() |
156 | { |
157 | return $this->getPersistence()->getPlatForm()->getQueryBuilder()->from($this->getTableName(), 't'); |
158 | } |
159 | } |