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\SessionCategory; |
13
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelCourse; |
14
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelUser; |
15
|
|
|
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser; |
16
|
|
|
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
17
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
20
|
|
|
|
21
|
|
|
#[AsController] |
22
|
|
|
class CreateSessionWithUsersAndCoursesAction |
23
|
|
|
{ |
24
|
|
|
public function __construct( |
25
|
|
|
private EntityManagerInterface $em, |
26
|
|
|
private ValidatorInterface $validator, |
27
|
|
|
private UserRepository $userRepo, |
28
|
|
|
private CourseRepository $courseRepo |
29
|
|
|
) {} |
30
|
|
|
|
31
|
|
|
public function __invoke(CreateSessionWithUsersAndCoursesInput $data): Session |
32
|
|
|
{ |
33
|
|
|
$this->validator->validate($data); |
34
|
|
|
|
35
|
|
|
$session = new Session(); |
36
|
|
|
$session |
37
|
|
|
->setTitle($data->getTitle()) |
38
|
|
|
->setDescription($data->getDescription() ?? '') |
39
|
|
|
->setVisibility($data->getVisibility() ?? 1) |
40
|
|
|
->setNbrCourses(count($data->getCourseIds())) |
41
|
|
|
->setNbrUsers(count($data->getStudentIds()) + count($data->getTutorIds())) |
42
|
|
|
->setShowDescription($data->getShowDescription() ?? false) |
43
|
|
|
->setDuration($data->getDuration() ?? 0) |
44
|
|
|
->setDisplayStartDate($data->getDisplayStartDate() ?? new \DateTime()) |
45
|
|
|
->setDisplayEndDate($data->getDisplayEndDate() ?? new \DateTime()) |
46
|
|
|
->setAccessStartDate($data->getAccessStartDate() ?? new \DateTime()) |
47
|
|
|
->setAccessEndDate($data->getAccessEndDate() ?? new \DateTime()) |
48
|
|
|
->setCoachAccessStartDate($data->getCoachAccessStartDate() ?? new \DateTime()) |
49
|
|
|
->setCoachAccessEndDate($data->getCoachAccessEndDate() ?? new \DateTime()) |
50
|
|
|
->setValidityInDays($data->getValidityInDays() ?? 0); |
51
|
|
|
|
52
|
|
|
if ($data->getCategory()) { |
|
|
|
|
53
|
|
|
$category = $this->em->getRepository(SessionCategory::class)->find($data->getCategory()); |
54
|
|
|
if (!$category) { |
55
|
|
|
throw new \RuntimeException("Invalid category ID: " . $data->getCategory()); |
56
|
|
|
} |
57
|
|
|
$session->setCategory($category); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->em->persist($session); |
61
|
|
|
|
62
|
|
|
$relCourses = []; |
63
|
|
|
$courses = []; |
64
|
|
|
foreach ($data->getCourseIds() as $courseId) { |
65
|
|
|
$course = $this->courseRepo->find($courseId); |
66
|
|
|
if (!$course) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$courses[$courseId] = $course; |
71
|
|
|
|
72
|
|
|
$relCourse = new SessionRelCourse(); |
73
|
|
|
$relCourse->setSession($session); |
74
|
|
|
$relCourse->setCourse($course); |
75
|
|
|
$relCourse->setNbrUsers(0); |
76
|
|
|
$this->em->persist($relCourse); |
77
|
|
|
|
78
|
|
|
$relCourses[$courseId] = $relCourse; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($data->getStudentIds() as $userId) { |
82
|
|
|
$user = $this->userRepo->find($userId); |
83
|
|
|
if (!$user) { |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$rel = new SessionRelUser(); |
88
|
|
|
$rel |
89
|
|
|
->setSession($session) |
90
|
|
|
->setUser($user) |
91
|
|
|
->setRelationType(Session::STUDENT); |
92
|
|
|
|
93
|
|
|
$this->em->persist($rel); |
94
|
|
|
|
95
|
|
|
foreach ($courses as $courseId => $course) { |
96
|
|
|
$relCourseUser = new SessionRelCourseRelUser(); |
97
|
|
|
$relCourseUser |
98
|
|
|
->setSession($session) |
99
|
|
|
->setUser($user) |
100
|
|
|
->setCourse($course) |
101
|
|
|
->setStatus(Session::STUDENT) |
102
|
|
|
->setVisibility(1) |
103
|
|
|
->setProgress(0) |
104
|
|
|
->setLegalAgreement(0); |
105
|
|
|
|
106
|
|
|
$this->em->persist($relCourseUser); |
107
|
|
|
|
108
|
|
|
if (isset($relCourses[$courseId])) { |
109
|
|
|
$relCourses[$courseId]->setNbrUsers( |
110
|
|
|
$relCourses[$courseId]->getNbrUsers() + 1 |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($data->getTutorIds() as $userId) { |
117
|
|
|
$user = $this->userRepo->find($userId); |
118
|
|
|
if (!$user) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$rel = new SessionRelUser(); |
123
|
|
|
$rel |
124
|
|
|
->setSession($session) |
125
|
|
|
->setUser($user) |
126
|
|
|
->setRelationType(Session::SESSION_ADMIN); |
127
|
|
|
|
128
|
|
|
$this->em->persist($rel); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->em->flush(); |
132
|
|
|
|
133
|
|
|
return $session; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: