1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
namespace Chamilo\PluginBundle\XApi\Lrs; |
6
|
|
|
|
7
|
|
|
use Chamilo\PluginBundle\XApi\Lrs\Util\InternalLogUtil; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Xabbuh\XApi\Common\Exception\NotFoundException; |
11
|
|
|
use Xabbuh\XApi\Model\Statement; |
12
|
|
|
use Xabbuh\XApi\Model\StatementId; |
13
|
|
|
use Xabbuh\XApi\Serializer\Symfony\ActorSerializer; |
14
|
|
|
use Xabbuh\XApi\Serializer\Symfony\Serializer; |
15
|
|
|
use Xabbuh\XApi\Serializer\Symfony\SerializerFactory; |
16
|
|
|
use XApi\LrsBundle\Controller\StatementGetController; |
17
|
|
|
use XApi\LrsBundle\Controller\StatementHeadController; |
18
|
|
|
use XApi\LrsBundle\Controller\StatementPostController; |
19
|
|
|
use XApi\LrsBundle\Controller\StatementPutController; |
20
|
|
|
use XApi\LrsBundle\Model\StatementsFilterFactory; |
21
|
|
|
use XApi\Repository\Doctrine\Mapping\Statement as StatementEntity; |
22
|
|
|
use XApi\Repository\Doctrine\Repository\StatementRepository; |
23
|
|
|
use XApiPlugin; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class StatementsController. |
27
|
|
|
* |
28
|
|
|
* @package Chamilo\PluginBundle\XApi\Lrs |
29
|
|
|
*/ |
30
|
|
|
class StatementsController extends BaseController |
31
|
|
|
{ |
32
|
|
|
public function get(): Response |
33
|
|
|
{ |
34
|
|
|
$pluginEm = XApiPlugin::getEntityManager(); |
35
|
|
|
|
36
|
|
|
$serializer = Serializer::createSerializer(); |
37
|
|
|
$factory = new SerializerFactory($serializer); |
38
|
|
|
|
39
|
|
|
$getStatementController = new StatementGetController( |
40
|
|
|
new StatementRepository( |
41
|
|
|
$pluginEm->getRepository(StatementEntity::class) |
42
|
|
|
), |
43
|
|
|
$factory->createStatementSerializer(), |
44
|
|
|
$factory->createStatementResultSerializer(), |
45
|
|
|
new StatementsFilterFactory( |
46
|
|
|
new ActorSerializer($serializer) |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return $getStatementController->getStatement($this->httpRequest); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function head(): Response |
54
|
|
|
{ |
55
|
|
|
$pluginEm = XApiPlugin::getEntityManager(); |
56
|
|
|
|
57
|
|
|
$serializer = Serializer::createSerializer(); |
58
|
|
|
$factory = new SerializerFactory($serializer); |
59
|
|
|
|
60
|
|
|
$headStatementController = new StatementHeadController( |
61
|
|
|
new StatementRepository( |
62
|
|
|
$pluginEm->getRepository(StatementEntity::class) |
63
|
|
|
), |
64
|
|
|
$factory->createStatementSerializer(), |
65
|
|
|
$factory->createStatementResultSerializer(), |
66
|
|
|
new StatementsFilterFactory( |
67
|
|
|
new ActorSerializer($serializer) |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return $headStatementController->getStatement($this->httpRequest); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var StatementRepository |
76
|
|
|
*/ |
77
|
|
|
private $statementRepository; |
78
|
|
|
/** |
79
|
|
|
* @var \Symfony\Component\Serializer\Serializer|\Symfony\Component\Serializer\SerializerInterface |
80
|
|
|
*/ |
81
|
|
|
private $serializer; |
82
|
|
|
/** |
83
|
|
|
* @var SerializerFactory |
84
|
|
|
*/ |
85
|
|
|
private $serializerFactory; |
86
|
|
|
|
87
|
|
|
public function __construct(Request $httpRequest) |
88
|
|
|
{ |
89
|
|
|
parent::__construct($httpRequest); |
90
|
|
|
|
91
|
|
|
$pluginEm = XApiPlugin::getEntityManager(); |
92
|
|
|
|
93
|
|
|
$this->statementRepository = new StatementRepository( |
94
|
|
|
$pluginEm->getRepository(StatementEntity::class) |
95
|
|
|
); |
96
|
|
|
$this->serializer = Serializer::createSerializer(); |
97
|
|
|
$this->serializerFactory = new SerializerFactory($this->serializer); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function get(): Response |
101
|
|
|
{ |
102
|
|
|
$getStatementController = new StatementGetController( |
103
|
|
|
$this->statementRepository, |
104
|
|
|
$this->serializerFactory->createStatementSerializer(), |
105
|
|
|
$this->serializerFactory->createStatementResultSerializer(), |
106
|
|
|
new StatementsFilterFactory( |
107
|
|
|
new ActorSerializer($this->serializer) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $getStatementController->getStatement($this->httpRequest); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function head(): Response |
115
|
|
|
{ |
116
|
|
|
$headStatementController = new StatementHeadController( |
117
|
|
|
$this->statementRepository, |
118
|
|
|
$this->serializerFactory->createStatementSerializer(), |
119
|
|
|
$this->serializerFactory->createStatementResultSerializer(), |
120
|
|
|
new StatementsFilterFactory( |
121
|
|
|
new ActorSerializer($this->serializer) |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $headStatementController->getStatement($this->httpRequest); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function put(): Response |
129
|
|
|
{ |
130
|
|
|
$statement = $this->serializerFactory |
131
|
|
|
->createStatementSerializer() |
132
|
|
|
->deserializeStatement( |
133
|
|
|
$this->httpRequest->getContent() |
134
|
|
|
) |
135
|
|
|
; |
136
|
|
|
|
137
|
|
|
$putStatementController = new StatementPutController($this->statementRepository); |
138
|
|
|
|
139
|
|
|
$response = $putStatementController->putStatement($this->httpRequest, $statement); |
140
|
|
|
|
141
|
|
|
$this->saveLog( |
142
|
|
|
[$this->httpRequest->query->get('statementId')] |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $response; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function post(): Response |
149
|
|
|
{ |
150
|
|
|
$pluginEm = XApiPlugin::getEntityManager(); |
151
|
|
|
|
152
|
|
|
$postStatementController = new StatementPostController( |
153
|
|
|
new StatementRepository( |
154
|
|
|
$pluginEm->getRepository(StatementEntity::class) |
155
|
|
|
) |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$content = $this->httpRequest->getContent(); |
159
|
|
|
|
160
|
|
|
if (substr($content, 0, 1) !== '[') { |
161
|
|
|
$content = "[$content]"; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$statements = $this->deserializeStatements($content); |
165
|
|
|
|
166
|
|
|
return $postStatementController->postStatements($this->httpRequest, $statements); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
private function deserializeStatement(string $content = ''): Statement |
170
|
|
|
{ |
171
|
|
|
$factory = new SerializerFactory(Serializer::createSerializer()); |
172
|
|
|
|
173
|
|
|
return $factory->createStatementSerializer()->deserializeStatement($content); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
private function deserializeStatements(string $content = ''): array |
177
|
|
|
{ |
178
|
|
|
$factory = new SerializerFactory(Serializer::createSerializer()); |
179
|
|
|
|
180
|
|
|
return $factory->createStatementSerializer()->deserializeStatements($content); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array<string> $statementsId |
185
|
|
|
* |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
private function saveLog(array $statementsId) |
189
|
|
|
{ |
190
|
|
|
foreach ($statementsId as $statementId) { |
191
|
|
|
try { |
192
|
|
|
$storedStatement = $this->statementRepository->findStatementById( |
193
|
|
|
StatementId::fromString($statementId) |
194
|
|
|
); |
195
|
|
|
|
196
|
|
|
InternalLogUtil::saveStatementForInternalLog($storedStatement); |
197
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|