|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chamilo\CoreBundle\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use Chamilo\CoreBundle\Entity\AbstractResource; |
|
9
|
|
|
use Chamilo\CoreBundle\Entity\ResourceIllustrationInterface; |
|
10
|
|
|
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository; |
|
11
|
|
|
use Chamilo\CoreBundle\Repository\ResourceNodeRepository; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
14
|
|
|
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
|
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
final class ResourceNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use NormalizerAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED'; |
|
23
|
|
|
|
|
24
|
|
|
private ResourceNodeRepository $resourceNodeRepository; |
|
25
|
|
|
private IllustrationRepository $illustrationRepository; |
|
26
|
|
|
private RequestStack $requestStack; |
|
27
|
|
|
private UrlGeneratorInterface $generator; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ResourceNodeRepository $resourceNodeRepository, IllustrationRepository $illustrationRepository, RequestStack $requestStack, UrlGeneratorInterface $generator) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->resourceNodeRepository = $resourceNodeRepository; |
|
32
|
|
|
$this->requestStack = $requestStack; |
|
33
|
|
|
$this->generator = $generator; |
|
34
|
|
|
$this->illustrationRepository = $illustrationRepository; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param AbstractResource $object |
|
39
|
|
|
*/ |
|
40
|
|
|
public function normalize($object, ?string $format = null, array $context = []): array | string | int | float | bool | ArrayObject | null |
|
41
|
|
|
{ |
|
42
|
|
|
$context[self::ALREADY_CALLED] = true; |
|
43
|
|
|
|
|
44
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
45
|
|
|
|
|
46
|
|
|
$getFile = $request->get('getFile'); |
|
47
|
|
|
|
|
48
|
|
|
$courseId = (int) $request->get('cid'); |
|
49
|
|
|
if (empty($courseId)) { |
|
50
|
|
|
// Try with cid from session |
|
51
|
|
|
$courseId = (int) $request->getSession()->get('cid'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$sessionId = (int) $request->get('sid'); |
|
55
|
|
|
if (empty($sessionId)) { |
|
56
|
|
|
$sessionId = (int) $request->getSession()->get('sid'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$groupId = (int) $request->get('gid'); |
|
60
|
|
|
if (empty($groupId)) { |
|
61
|
|
|
$groupId = (int) $request->getSession()->get('gid'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($object->hasResourceNode()) { |
|
65
|
|
|
$resourceNode = $object->getResourceNode(); |
|
66
|
|
|
|
|
67
|
|
|
$params = [ |
|
68
|
|
|
'id' => $resourceNode->getId(), |
|
69
|
|
|
'cid' => $courseId, |
|
70
|
|
|
'sid' => $sessionId, |
|
71
|
|
|
'gid' => $groupId, |
|
72
|
|
|
'tool' => $resourceNode->getResourceType()->getTool()->getName(), |
|
73
|
|
|
'type' => $resourceNode->getResourceType()->getName(), |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
|
|
//if ($getFile) { |
|
77
|
|
|
// Get all links from resource. |
|
78
|
|
|
$object->setResourceLinkListFromEntity(); |
|
79
|
|
|
//} |
|
80
|
|
|
|
|
81
|
|
|
$object->contentUrl = $this->generator->generate('chamilo_core_resource_view', $params); |
|
82
|
|
|
$object->downloadUrl = $this->generator->generate('chamilo_core_resource_download', $params); |
|
83
|
|
|
error_log($object->getResourceName()); |
|
84
|
|
|
// Get illustration of a resource, instead of looking for the node children to get the illustration. |
|
85
|
|
|
if ($object instanceof ResourceIllustrationInterface) { |
|
86
|
|
|
error_log('check illustration'); |
|
87
|
|
|
$object->illustrationUrl = $this->illustrationRepository->getIllustrationUrl($object); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// This gets the file contents, usually use to get HTML/Text data to be edited. |
|
91
|
|
|
if ($getFile && |
|
92
|
|
|
$resourceNode->hasResourceFile() && |
|
93
|
|
|
$resourceNode->hasEditableTextContent() |
|
94
|
|
|
) { |
|
95
|
|
|
$object->contentFile = $this->resourceNodeRepository->getResourceNodeFileContent( |
|
96
|
|
|
$resourceNode |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->normalizer->normalize($object, $format, $context); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function supportsNormalization($data, ?string $format = null, array $context = []): bool |
|
105
|
|
|
{ |
|
106
|
|
|
if (isset($context[self::ALREADY_CALLED])) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $data instanceof AbstractResource; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|