Course.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. namespace AppBundle\Entity\Booking;
  3. use AppBundle\Annotation\ExportSplitFields;
  4. use AppBundle\Enum\Education\PratiqueEnum;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Dunglas\ApiBundle\Annotation\Iri;
  8. use Recurr\Rule;
  9. use Recurr\Weekday;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use AppBundle\Validator\Constraints\Booking as OpentalentEventAssert;
  13. use AppBundle\Entity\AccessAndFunction\Access;
  14. use AppBundle\Entity\Education\Education;
  15. use AppBundle\Entity\Education\EducationCurriculum;
  16. /**
  17. * Cours
  18. *
  19. * @Iri("http://schema.org/Cours")
  20. * @OpentalentEventAssert\Course()
  21. */
  22. #[ORM\Entity(repositoryClass: 'AppBundle\Entity\Booking\Repository\CourseRepository')]
  23. class Course extends AbstractBooking
  24. {
  25. /**
  26. * @var ArrayCollection<CourseRecur>
  27. * "course",
  28. * "course_bookingrecur",
  29. * "planning_list",
  30. * "access_details_practicalcourses",
  31. * "edu_stu_courses_courses",
  32. * "student_registration_courses",
  33. * "planning_detail",
  34. * "course_details",
  35. * "course_informations_edit"
  36. * })
  37. */
  38. #[Assert\Valid]
  39. #[ORM\OneToMany(targetEntity: 'CourseRecur', mappedBy: 'event', cascade: ['persist'], orphanRemoval: true)]
  40. #[Groups(['bookingrecur', 'course', 'course_bookingrecur', 'planning_list', 'access_details_practicalcourses', 'edu_stu_courses_courses', 'student_registration_courses', 'planning_detail', 'course_details', 'course_informations_edit'])]
  41. protected $eventRecur;
  42. /**
  43. * @var ArrayCollection<Course>
  44. */
  45. #[ORM\OneToMany(targetEntity: 'Course', mappedBy: 'parent', orphanRemoval: true)]
  46. #[Groups(['course_timeline'])]
  47. private $timeline;
  48. /**
  49. * @var \AppBundle\Entity\Booking\Course
  50. */
  51. #[ORM\ManyToOne(targetEntity: 'Course', inversedBy: 'timeline')]
  52. #[Groups(['course'])]
  53. private $parent;
  54. /**
  55. * @var string
  56. */
  57. #[Assert\Type(type: 'string')]
  58. #[Assert\Choice(callback: ['\AppBundle\Enum\Booking\CourseTimelineTypeEnum', 'toArray'], multiple: false, min: 1)]
  59. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  60. #[Groups(['course'])]
  61. private $type;
  62. /**
  63. * @var string
  64. */
  65. #[ORM\ManyToOne(targetEntity: 'AppBundle\Entity\Education\Education')]
  66. #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
  67. #[Groups(['course', 'planning_list', 'attendancebooking_list_course', 'generate_attendance', 'presence_attendance', 'course_details', 'edu_stu_courses_courses', 'student_registration_courses', 'course_informations_edit', 'planning_detail', 'student_registration_courses', 'education_input_list_access', 'worksbyusers_db_work', 'access_attendance_detail_attendancebookings', 'education_student_next_year_access'])]
  68. private $education;
  69. /**
  70. * @var ArrayCollection<EducationCurriculum>
  71. */
  72. #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\Education\EducationCurriculum')]
  73. #[Groups(['course', 'course_educationcurriculum', 'planning_list', 'planning_detail', 'attendancebooking_list_course', 'generate_attendance', 'access_details_practicalcourses', 'presence_attendance', 'course_details', 'edu_stu_courses_courses', 'student_registration_courses', 'worksbyusers_db_work', 'accesses_courseteacher_show_practicalcourses', 'course_informations_edit', 'education_input_list_access'])]
  74. private $educationCurriculum;
  75. /**
  76. * @var ArrayCollection<Access>
  77. */
  78. #[ORM\ManyToMany(targetEntity: 'AppBundle\Entity\AccessAndFunction\Access', inversedBy: 'courses')]
  79. #[ORM\JoinTable(name: 'course_student', joinColumns: [], inverseJoinColumns: [])]
  80. #[ORM\JoinColumn(name: 'course_id', referencedColumnName: 'id')]
  81. #[ORM\JoinColumn(name: 'student_id', referencedColumnName: 'id')]
  82. #[Groups(['course_access', 'access_details_practicalcourses', 'presence_attendance', 'course_details', 'edu_stu_courses_courses', 'student_registration_courses', 'planning_detail', 'course_informations_edit'])]
  83. private $students;
  84. /**
  85. * @var bool
  86. */
  87. #[ORM\Column(type: 'boolean', options: ['default' => false])]
  88. #[Assert\Type(type: 'boolean')]
  89. #[Assert\NotNull]
  90. #[Groups(['course', 'planning_list', 'planning_detail', 'education_input_list_access'])]
  91. private $isCanceled = false;
  92. /**
  93. * @var bool
  94. */
  95. #[ORM\Column(type: 'boolean', options: ['default' => false])]
  96. #[Assert\Type(type: 'boolean')]
  97. #[Assert\NotNull]
  98. #[Groups(['course', 'planning_detail'])]
  99. private $isDuplicate = false;
  100. /**
  101. * var ArrayCollection<AttendanceBooking>
  102. */
  103. #[Assert\Valid]
  104. #[ORM\OneToMany(targetEntity: 'AppBundle\Entity\Booking\AttendanceBooking', cascade: ['persist'], mappedBy: 'course', orphanRemoval: true)]
  105. #[ORM\JoinColumn(nullable: false)]
  106. #[Groups(['course_attendancebooking', 'planning_list', 'presence_attendance', 'student_registration_courses', 'planning_detail', 'course_details'])]
  107. private $attendanceBooking;
  108. /**
  109. * @var string
  110. * @ExportSplitFields({"name","education.educationCategory.label","educationComplementTemplate","educationCurricullumsTemplate","timeTemplate","organizersTemplate","place.name","room.name"})
  111. */
  112. #[Groups(['student_list_coursesfiltered', 'education_student_next_year_access'])]
  113. private $fullLabelTemplate;
  114. /**
  115. * @var string
  116. */
  117. #[Groups(['template'])]
  118. private $timeTemplate;
  119. /**
  120. * @var string
  121. */
  122. #[Groups(['template'])]
  123. private $educationComplementTemplate;
  124. /**
  125. * @var string
  126. */
  127. #[Groups(['template'])]
  128. private $educationCurricullumsTemplate;
  129. /**
  130. * @var string
  131. */
  132. #[Groups(['template'])]
  133. private $organizersTemplate;
  134. /**
  135. * @var string
  136. */
  137. #[Groups(['planning_list'])]
  138. private $onlyOneStudentName;
  139. /**
  140. * @var ArrayCollection<EventUser>
  141. */
  142. #[Assert\Valid]
  143. #[ORM\OneToMany(targetEntity: 'Work', mappedBy: 'course', cascade: ['persist'], orphanRemoval: true)]
  144. #[Groups(['course'])]
  145. private $work;
  146. public function __construct()
  147. {
  148. parent::__construct();
  149. $this->eventRecur = new ArrayCollection();
  150. $this->educationCurriculum = new ArrayCollection();
  151. $this->students = new ArrayCollection();
  152. $this->timeline = new ArrayCollection();
  153. $this->attendanceBooking = new ArrayCollection();
  154. }
  155. /**
  156. * Adds timeline.
  157. *
  158. * @param Course $course
  159. *
  160. * @return $this
  161. */
  162. public function addTimeline(Course $course) {
  163. $this->timeline[] = $course;
  164. }
  165. /**
  166. * Removes timeline.
  167. *
  168. * @param Course $course
  169. *
  170. * @return $this
  171. */
  172. public function removeTimeline(Course $course) {
  173. $key = array_search($course, $this->timeline, true);
  174. if (false !== $key) {
  175. unset($this->timeline[$key]);
  176. }
  177. return $this;
  178. }
  179. /**
  180. * Gets timeline.
  181. *
  182. * @return ArrayCollection<Course>
  183. */
  184. public function getTimeline() {
  185. return $this->timeline;
  186. }
  187. /**
  188. * Get parent
  189. *
  190. * @return Course
  191. */
  192. public function getParent() {
  193. return $this->parent;
  194. }
  195. /**
  196. * Set parent
  197. *
  198. * @param Course $parent
  199. * @return $this
  200. */
  201. public function setParent(Course $parent=null) {
  202. $this->parent = $parent;
  203. return $this;
  204. }
  205. /**
  206. * Get type
  207. *
  208. * @return string
  209. */
  210. public function getType() {
  211. return $this->type;
  212. }
  213. /**
  214. * Set type
  215. *
  216. * @param string $type
  217. * @return $this
  218. */
  219. public function setType($type) {
  220. $this->type = $type;
  221. return $this;
  222. }
  223. /**
  224. * Sets isCanceled.
  225. *
  226. * @param bool $isCanceled
  227. *
  228. * @return $this
  229. */
  230. public function setIsCanceled($isCanceled)
  231. {
  232. $this->isCanceled = $isCanceled;
  233. return $this;
  234. }
  235. /**
  236. * Gets isCanceled.
  237. *
  238. * @return bool
  239. */
  240. public function getIsCanceled()
  241. {
  242. return $this->isCanceled;
  243. }
  244. /**
  245. * Sets isDuplicate.
  246. *
  247. * @param bool $isDuplicate
  248. *
  249. * @return $this
  250. */
  251. public function setIsDuplicate($isDuplicate)
  252. {
  253. $this->isDuplicate = $isDuplicate;
  254. return $this;
  255. }
  256. /**
  257. * Gets isDuplicate.
  258. *
  259. * @return bool
  260. */
  261. public function getIsDuplicate()
  262. {
  263. return $this->isDuplicate;
  264. }
  265. /**
  266. * Set education
  267. *
  268. * @param Education $education
  269. *
  270. * @return Course
  271. */
  272. public function setEducation(Education $education = null)
  273. {
  274. $this->education = $education;
  275. return $this;
  276. }
  277. /**
  278. * Get education
  279. *
  280. * @return Education
  281. */
  282. public function getEducation()
  283. {
  284. return $this->education;
  285. }
  286. /**
  287. * Add educationCurriculum
  288. *
  289. * @param EducationCurriculum $educationCurriculum
  290. *
  291. * @return Course
  292. */
  293. public function addEducationCurriculum(EducationCurriculum $educationCurriculum)
  294. {
  295. $this->educationCurriculum[] = $educationCurriculum;
  296. return $this;
  297. }
  298. /**
  299. * Remove educationCurriculum
  300. *
  301. * @param EducationCurriculum $educationCurriculum
  302. */
  303. public function removeEducationCurriculum(EducationCurriculum $educationCurriculum)
  304. {
  305. $this->educationCurriculum->removeElement($educationCurriculum);
  306. }
  307. /**
  308. * Get educationCurriculum
  309. *
  310. * @return \Doctrine\Common\Collections\Collection
  311. */
  312. public function getEducationCurriculum()
  313. {
  314. return $this->educationCurriculum;
  315. }
  316. /**
  317. * Reset education curriculum
  318. *
  319. */
  320. public function resetEducationCurriculum()
  321. {
  322. foreach($this->getEducationCurriculum() as $educationCurriculum){
  323. $this->removeEducationCurriculum($educationCurriculum);
  324. }
  325. $this->educationCurriculum->clear();
  326. }
  327. /**
  328. * Add student
  329. *
  330. * @param Access $student
  331. *
  332. * @return Course
  333. */
  334. public function addStudent(Access $student)
  335. {
  336. $this->students[] = $student;
  337. return $this;
  338. }
  339. /**
  340. * Remove student
  341. *
  342. * @param Access $student
  343. */
  344. public function removeStudent(Access $student)
  345. {
  346. $this->students->removeElement($student);
  347. }
  348. /**
  349. * Get students
  350. *
  351. * @return ArrayCollection<Access>
  352. */
  353. public function getStudents()
  354. {
  355. return $this->students;
  356. }
  357. /**
  358. * Add attendanceBooking
  359. *
  360. * @param \AppBundle\Entity\Booking\AttendanceBooking $attendanceBooking
  361. *
  362. * @return AbstractBooking
  363. */
  364. public function addAttendanceBooking(\AppBundle\Entity\Booking\AttendanceBooking $attendanceBooking)
  365. {
  366. $attendanceBooking->setCourse($this);
  367. $this->attendanceBooking[] = $attendanceBooking;
  368. return $this;
  369. }
  370. /**
  371. * Remove attendanceBooking
  372. *
  373. * @param \AppBundle\Entity\Booking\AttendanceBooking $attendanceBooking
  374. */
  375. public function removeAttendanceBooking(\AppBundle\Entity\Booking\AttendanceBooking $attendanceBooking)
  376. {
  377. $this->attendanceBooking->removeElement($attendanceBooking);
  378. }
  379. /**
  380. * Get attendanceBooking
  381. *
  382. * @return \Doctrine\Common\Collections\Collection
  383. */
  384. public function getAttendanceBooking()
  385. {
  386. return $this->attendanceBooking;
  387. }
  388. /**
  389. * {@inheritdoc}
  390. */
  391. public function getParticipants() {
  392. $participants = new ArrayCollection();
  393. array_map(function (Access $a) use ($participants) { $participants->add($a->getPerson());}, $this->getStudents()->toArray());
  394. return $participants;
  395. }
  396. /**
  397. * @return array
  398. */
  399. public function getTimeTemplate(){
  400. $dateTimeStart = $this->getDatetimeStart();
  401. if(!empty($dateTimeStart)){
  402. $days = [];
  403. if(!empty($this->getEventRecur()) && !empty($this->getEventRecur()->get(0))){
  404. $rule = $this->getEventRecur()->get(0)->getRule();
  405. $rrule = new Rule($rule);
  406. $string_days = [
  407. 0 => 'Monday',
  408. 1 => 'Tuesday',
  409. 2 => 'Wednesday',
  410. 3 => 'Thursday',
  411. 4 => 'Friday',
  412. 5 => 'Saturday',
  413. 6 => 'Sunday'
  414. ];
  415. /** @var Weekday $weekDay */
  416. foreach ($rrule->getByDayTransformedToWeekdays() as $weekDays){
  417. $days[] = $string_days[$weekDays->weekday];
  418. }
  419. }else{
  420. $days[] = $dateTimeStart->format('l');
  421. }
  422. $timezone = 'Europe/Paris';
  423. $dateTimeStart->setTimezone(new \DateTimeZone($timezone));
  424. $time = [];
  425. foreach ($days as $day){
  426. $time[] = ['value' => $day , 'translate' => true];
  427. }
  428. $time[] = $dateTimeStart->format('à H:i');
  429. return $time;
  430. }else{
  431. return [];
  432. }
  433. }
  434. /**
  435. * @return string
  436. */
  437. public function getEducationComplementTemplate(){
  438. return $this->getEducation() ? $this->getEducation()->getEducationComplementTemplate() : '';
  439. }
  440. /**
  441. * @return array
  442. */
  443. public function getEducationCurricullumsTemplate(){
  444. $educationCurricullums = [];
  445. foreach ($this->getEducationCurriculum() as $edc){
  446. $educationCurricullums[] = ( $edc->getCycle() && $edc->getCycle()->getLabel() !== 'NO_CYCLE' ? $edc->getCycle()->getLabel() : '' );
  447. $educationCurricullums[] = ['value' => $edc->getYear(), 'translate' => true];
  448. $educationCurricullums[] = $edc->getLevel();
  449. $educationCurricullums[] = ', ';
  450. }
  451. return $educationCurricullums;
  452. }
  453. /**
  454. * @return string
  455. */
  456. public function getOrganizersTemplate(){
  457. $organizers = [];
  458. foreach($this->getOrganizer() as $organizer){
  459. $organizers[] = $organizer->getPerson()->getFullNameTemplate();
  460. }
  461. return ' avec ' . implode(", ", $organizers);
  462. }
  463. /**
  464. * Gets full label.
  465. *
  466. * @return array
  467. */
  468. public function getFullLabelTemplate()
  469. {
  470. if(is_null($this->getEducation())){
  471. return [];
  472. }
  473. $fullLabelTemplate = [
  474. $this->getName(),
  475. $this->getEducation()->getEducationCategory()->getLabel(),
  476. $this->getEducationComplementTemplate()
  477. ];
  478. $fullLabelTemplate = array_merge($fullLabelTemplate, $this->getEducationCurricullumsTemplate(),$this->getTimeTemplate(), [
  479. $this->getOrganizersTemplate(),
  480. $this->getRoom() ? $this->getRoom()->getName() : '',
  481. $this->getPlace() ? $this->getPlace()->getName() : ''
  482. ] );
  483. return $fullLabelTemplate;
  484. }
  485. /**
  486. * @return string
  487. */
  488. public function getOnlyOneStudentName()
  489. {
  490. return $this->onlyOneStudentName;
  491. }
  492. /**
  493. * @param string $name
  494. */
  495. public function setOnlyOneStudentName($name)
  496. {
  497. $this->onlyOneStudentName = $name;
  498. }
  499. /**
  500. * Add Work
  501. *
  502. * @param Work $work
  503. *
  504. * @return AbstractBooking
  505. */
  506. public function addWork(Work $work)
  507. {
  508. $work->setCourse($this);
  509. $this->work[] = $work;
  510. return $this;
  511. }
  512. /**
  513. * Get work
  514. *
  515. * @return \Doctrine\Common\Collections\Collection
  516. */
  517. public function getWork()
  518. {
  519. return $this->work;
  520. }
  521. }