1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\DataTransformer; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Core\DataTransformer\DataTransformerInterface; |
10
|
|
|
use Chamilo\CoreBundle\ApiResource\CourseTool; |
11
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
12
|
|
|
use Chamilo\CoreBundle\Tool\AbstractTool; |
13
|
|
|
use Chamilo\CoreBundle\Tool\ToolChain; |
14
|
|
|
use Chamilo\CoreBundle\Traits\CourseFromRequestTrait; |
15
|
|
|
use Chamilo\CourseBundle\Entity\CTool; |
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
|
19
|
|
|
class CourseToolDataTranformer implements DataTransformerInterface |
20
|
|
|
{ |
21
|
|
|
use CourseFromRequestTrait; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
protected RequestStack $requestStack, |
25
|
|
|
protected EntityManagerInterface $entityManager, |
26
|
|
|
protected readonly ToolChain $toolChain, |
27
|
|
|
) {} |
28
|
|
|
|
29
|
|
|
public function transform($object, string $to, array $context = []) |
30
|
|
|
{ |
31
|
|
|
\assert($object instanceof CTool); |
32
|
|
|
|
33
|
|
|
$tool = $object->getTool(); |
34
|
|
|
|
35
|
|
|
$toolModel = $this->toolChain->getToolFromName( |
36
|
|
|
$tool->getName() |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$course = $this->getCourse(); |
40
|
|
|
|
41
|
|
|
$cTool = new CourseTool(); |
42
|
|
|
$cTool->iid = $object->getIid(); |
43
|
|
|
$cTool->name = $object->getName(); |
44
|
|
|
$cTool->visibility = $object->getVisibility(); |
45
|
|
|
$cTool->resourceNode = $object->resourceNode; |
46
|
|
|
$cTool->illustrationUrl = $object->illustrationUrl; |
47
|
|
|
$cTool->url = $this->generateToolUrl($toolModel, $course); |
48
|
|
|
$cTool->tool = $toolModel; |
49
|
|
|
|
50
|
|
|
return $cTool; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function generateToolUrl(AbstractTool $tool, Course $course): string |
54
|
|
|
{ |
55
|
|
|
$link = $tool->getLink(); |
56
|
|
|
|
57
|
|
|
if (strpos($link, 'nodeId')) { |
58
|
|
|
$nodeId = (string) $course->getResourceNode()->getId(); |
59
|
|
|
$link = str_replace(':nodeId', $nodeId, $link); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $link.'?' |
63
|
|
|
.http_build_query([ |
64
|
|
|
'cid' => $this->getCourse()->getId(), |
65
|
|
|
'sid' => $this->getSession()?->getId(), |
66
|
|
|
'gid' => 0, |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function supportsTransformation($data, string $to, array $context = []): bool |
71
|
|
|
{ |
72
|
|
|
return $data instanceof CTool && CourseTool::class === $to; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|