1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Controller\Api; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Validator\ValidatorInterface; |
10
|
|
|
use Chamilo\CoreBundle\Dto\CreateSessionWithUsersAndCoursesInput; |
11
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
12
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelCourse; |
13
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelUser; |
14
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser; |
15
|
|
|
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
16
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
18
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
19
|
|
|
|
20
|
|
|
#[AsController] |
21
|
|
|
class CreateSessionWithUsersAndCoursesAction |
22
|
|
|
{ |
23
|
|
|
public function __construct( |
24
|
|
|
private EntityManagerInterface $em, |
25
|
|
|
private ValidatorInterface $validator, |
26
|
|
|
private UserRepository $userRepo, |
27
|
|
|
private CourseRepository $courseRepo |
28
|
|
|
) {} |
29
|
|
|
|
30
|
|
|
public function __invoke(CreateSessionWithUsersAndCoursesInput $data): Session |
31
|
|
|
{ |
32
|
|
|
$this->validator->validate($data); |
33
|
|
|
|
34
|
|
|
$session = new Session(); |
35
|
|
|
$session |
36
|
|
|
->setTitle($data->getTitle()) |
37
|
|
|
->setDescription($data->getDescription() ?? '') |
38
|
|
|
->setVisibility($data->getVisibility() ?? 1) |
39
|
|
|
->setNbrCourses(count($data->getCourseIds())) |
40
|
|
|
->setNbrUsers(count($data->getStudentIds()) + count($data->getTutorIds())); |
41
|
|
|
|
42
|
|
|
$this->em->persist($session); |
43
|
|
|
|
44
|
|
|
$relCourses = []; |
45
|
|
|
foreach ($data->getCourseIds() as $courseId) { |
46
|
|
|
$course = $this->courseRepo->find($courseId); |
47
|
|
|
if (!$course) continue; |
48
|
|
|
|
49
|
|
|
$relCourse = new SessionRelCourse(); |
50
|
|
|
$relCourse->setSession($session); |
51
|
|
|
$relCourse->setCourse($course); |
52
|
|
|
$relCourse->setNbrUsers(0); |
53
|
|
|
$this->em->persist($relCourse); |
54
|
|
|
|
55
|
|
|
$relCourses[$courseId] = $relCourse; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($data->getStudentIds() as $userId) { |
59
|
|
|
$user = $this->userRepo->find($userId); |
60
|
|
|
if (!$user) continue; |
61
|
|
|
|
62
|
|
|
$rel = new SessionRelUser(); |
63
|
|
|
$rel |
64
|
|
|
->setSession($session) |
65
|
|
|
->setUser($user) |
66
|
|
|
->setRelationType(Session::STUDENT); |
67
|
|
|
|
68
|
|
|
$this->em->persist($rel); |
69
|
|
|
|
70
|
|
|
foreach ($data->getCourseIds() as $courseId) { |
71
|
|
|
$course = $this->courseRepo->find($courseId); |
72
|
|
|
if (!$course) continue; |
73
|
|
|
|
74
|
|
|
$relCourseUser = new SessionRelCourseRelUser(); |
75
|
|
|
$relCourseUser |
76
|
|
|
->setSession($session) |
77
|
|
|
->setUser($user) |
78
|
|
|
->setCourse($course) |
79
|
|
|
->setStatus(Session::STUDENT) |
80
|
|
|
->setVisibility(1) |
81
|
|
|
->setProgress(0) |
82
|
|
|
->setLegalAgreement(0); |
83
|
|
|
|
84
|
|
|
$this->em->persist($relCourseUser); |
85
|
|
|
|
86
|
|
|
if (isset($relCourses[$courseId])) { |
87
|
|
|
$relCourses[$courseId]->setNbrUsers( |
88
|
|
|
$relCourses[$courseId]->getNbrUsers() + 1 |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($data->getTutorIds() as $userId) { |
95
|
|
|
$user = $this->userRepo->find($userId); |
96
|
|
|
if (!$user) continue; |
97
|
|
|
|
98
|
|
|
$rel = new SessionRelUser(); |
99
|
|
|
$rel |
100
|
|
|
->setSession($session) |
101
|
|
|
->setUser($user) |
102
|
|
|
->setRelationType(Session::SESSION_ADMIN); |
103
|
|
|
|
104
|
|
|
$this->em->persist($rel); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->em->flush(); |
108
|
|
|
|
109
|
|
|
return $session; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|