Session.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace Chamilo\CoreBundle\Entity;
  4. use Chamilo\CourseBundle\Entity\CStudentPublication;
  5. use Chamilo\UserBundle\Entity\User;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Chamilo\CoreBundle\Entity\SessionCategory;
  10. //use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. //use Gedmo\Mapping\Annotation as Gedmo;
  12. /**
  13. * Session
  14. * UniqueEntity("name")
  15. * @ORM\Table(
  16. * name="session",
  17. * uniqueConstraints={@ORM\UniqueConstraint(name="name", columns={"name"})},
  18. * indexes={
  19. * @ORM\Index(name="idx_id_coach", columns={"id_coach"}),
  20. * @ORM\Index(name="idx_id_session_admin_id", columns={"session_admin_id"})
  21. * }
  22. * )
  23. * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Entity\Repository\SessionRepository")
  24. */
  25. class Session
  26. {
  27. const VISIBLE = 1;
  28. const READ_ONLY = 2;
  29. const INVISIBLE = 3;
  30. const AVAILABLE = 4;
  31. const STUDENT = 0;
  32. const DRH = 1;
  33. const COACH = 2;
  34. /**
  35. * @var integer
  36. *
  37. * @ORM\Column(name="id", type="integer", nullable=false, unique=false)
  38. * @ORM\Id
  39. * @ORM\GeneratedValue(strategy="AUTO")
  40. */
  41. private $id;
  42. /**
  43. * @var string
  44. *
  45. * @ORM\Column(name="name", type="string", length=150, nullable=false, unique=false)
  46. */
  47. private $name;
  48. /**
  49. * @var string
  50. *
  51. * @ORM\Column(name="description", type="text", nullable=true, unique=false)
  52. */
  53. private $description;
  54. /**
  55. * @var string
  56. *
  57. * @ORM\Column(name="show_description", type="boolean", nullable=true)
  58. */
  59. private $showDescription;
  60. /**
  61. * @var integer
  62. *
  63. * @ORM\Column(name="duration", type="integer", nullable=true)
  64. */
  65. private $duration;
  66. /**
  67. * @var integer
  68. *
  69. * @ORM\Column(name="nbr_courses", type="smallint", nullable=true, unique=false)
  70. */
  71. private $nbrCourses;
  72. /**
  73. * @var integer
  74. *
  75. * @ORM\Column(name="nbr_users", type="integer", nullable=true, unique=false)
  76. */
  77. private $nbrUsers;
  78. /**
  79. * @var integer
  80. *
  81. * @ORM\Column(name="nbr_classes", type="integer", nullable=true, unique=false)
  82. */
  83. private $nbrClasses;
  84. /**
  85. * @var integer
  86. *
  87. * @ORM\Column(name="session_admin_id", type="integer", nullable=true, unique=false)
  88. */
  89. private $sessionAdminId;
  90. /**
  91. * @var integer
  92. *
  93. * @ORM\Column(name="visibility", type="integer", nullable=false, unique=false)
  94. */
  95. private $visibility;
  96. /**
  97. * @var integer
  98. *
  99. * @ORM\Column(name="promotion_id", type="integer", nullable=true, unique=false)
  100. */
  101. private $promotionId;
  102. /**
  103. * @var \DateTime
  104. *
  105. * @ORM\Column(name="display_start_date", type="datetime", nullable=true, unique=false)
  106. */
  107. private $displayStartDate;
  108. /**
  109. * @var \DateTime
  110. *
  111. * @ORM\Column(name="display_end_date", type="datetime", nullable=true, unique=false)
  112. */
  113. private $displayEndDate;
  114. /**
  115. * @var \DateTime
  116. *
  117. * @ORM\Column(name="access_start_date", type="datetime", nullable=true, unique=false)
  118. */
  119. private $accessStartDate;
  120. /**
  121. * @var \DateTime
  122. *
  123. * @ORM\Column(name="access_end_date", type="datetime", nullable=true, unique=false)
  124. */
  125. private $accessEndDate;
  126. /**
  127. * @var \DateTime
  128. *
  129. * @ORM\Column(name="coach_access_start_date", type="datetime", nullable=true, unique=false)
  130. */
  131. private $coachAccessStartDate;
  132. /**
  133. * @var \DateTime
  134. *
  135. * @ORM\Column(name="coach_access_end_date", type="datetime", nullable=true, unique=false)
  136. */
  137. private $coachAccessEndDate;
  138. /**
  139. * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CItemProperty", mappedBy="session")
  140. **/
  141. //private $items;
  142. /**
  143. * @ORM\ManyToOne(targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="sessionAsGeneralCoach")
  144. * @ORM\JoinColumn(name="id_coach", referencedColumnName="id")
  145. **/
  146. private $generalCoach;
  147. /**
  148. * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\SessionCategory", inversedBy="session")
  149. * @ORM\JoinColumn(name="session_category_id", referencedColumnName="id")
  150. **/
  151. private $category;
  152. /**
  153. * @var ArrayCollection
  154. * @ORM\OneToMany(targetEntity="SessionRelCourse", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
  155. **/
  156. protected $courses;
  157. /**
  158. * @var ArrayCollection
  159. * @ORM\OneToMany(targetEntity="SessionRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
  160. **/
  161. protected $users;
  162. /**
  163. * @var ArrayCollection
  164. * @ORM\OneToMany(targetEntity="SessionRelCourseRelUser", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
  165. **/
  166. protected $userCourseSubscriptions;
  167. /**
  168. * @var Course
  169. **/
  170. protected $currentCourse;
  171. /**
  172. * @var boolean
  173. * @ORM\Column(name="send_subscription_notification", type="boolean", nullable=false, options={"default":false})
  174. */
  175. private $sendSubscriptionNotification;
  176. /**
  177. * @var ArrayCollection
  178. * @ORM\OneToMany(targetEntity="Chamilo\CourseBundle\Entity\CStudentPublication", mappedBy="session", cascade={"persist"}, orphanRemoval=true)
  179. */
  180. private $studentPublications;
  181. /**
  182. * Constructor
  183. */
  184. public function __construct()
  185. {
  186. $this->items = new ArrayCollection();
  187. $this->nbrClasses = 0;
  188. $this->nbrUsers = 0;
  189. $this->nbrUsers = 0;
  190. $this->displayStartDate = new \DateTime();
  191. $this->displayEndDate = new \DateTime();
  192. $this->accessStartDate = new \DateTime();
  193. $this->accessEndDate = new \DateTime();
  194. $this->coachAccessStartDate = new \DateTime();
  195. $this->coachAccessEndDate = new \DateTime();
  196. $this->visibility = 1;
  197. $this->courses = new ArrayCollection();
  198. $this->users = new ArrayCollection();
  199. $this->userCourseSubscriptions = new ArrayCollection();
  200. $this->showDescription = 0;
  201. $this->category = null;
  202. $this->studentPublications = new ArrayCollection();
  203. }
  204. /**
  205. * @return int
  206. */
  207. public function getDuration()
  208. {
  209. return $this->duration;
  210. }
  211. /**
  212. * @param int $duration
  213. */
  214. public function setDuration($duration)
  215. {
  216. $this->duration = $duration;
  217. }
  218. /**
  219. * @return string
  220. */
  221. public function getShowDescription()
  222. {
  223. return $this->showDescription;
  224. }
  225. /**
  226. * @param string $showDescription
  227. */
  228. public function setShowDescription($showDescription)
  229. {
  230. $this->showDescription = $showDescription;
  231. }
  232. /**
  233. * @return string
  234. */
  235. public function __toString()
  236. {
  237. return (string) $this->getName();
  238. }
  239. /**
  240. * Get id
  241. *
  242. * @return integer
  243. */
  244. public function getId()
  245. {
  246. return $this->id;
  247. }
  248. /**
  249. * @param int $id
  250. */
  251. public function setId($id)
  252. {
  253. $this->id = $id;
  254. }
  255. /**
  256. * @return ArrayCollection
  257. */
  258. public function getUsers()
  259. {
  260. return $this->users;
  261. }
  262. /**
  263. * @param $users
  264. */
  265. public function setUsers($users)
  266. {
  267. $this->users = new ArrayCollection();
  268. foreach ($users as $user) {
  269. $this->addUser($user);
  270. }
  271. }
  272. /**
  273. * @param SessionRelUser $user
  274. */
  275. public function addUser(SessionRelUser $user)
  276. {
  277. $user->setSession($this);
  278. if (!$this->hasUser($user)) {
  279. $this->users[] = $user;
  280. }
  281. }
  282. /**
  283. * @param int $status
  284. * @param User $user
  285. */
  286. public function addUserInSession($status, User $user)
  287. {
  288. $sessionRelUser = new SessionRelUser();
  289. $sessionRelUser->setSession($this);
  290. $sessionRelUser->setUser($user);
  291. $sessionRelUser->setRelationType($status);
  292. $this->addUser($sessionRelUser);
  293. }
  294. /**
  295. * @param SessionRelUser $subscription
  296. * @return bool
  297. */
  298. public function hasUser(SessionRelUser $subscription)
  299. {
  300. if ($this->getUsers()->count()) {
  301. $criteria = Criteria::create()->where(
  302. Criteria::expr()->eq("user", $subscription->getUser())
  303. )->andWhere(
  304. Criteria::expr()->eq("session", $subscription->getSession())
  305. )->andWhere(
  306. Criteria::expr()->eq("relationType", $subscription->getRelationType())
  307. );
  308. $relation = $this->getUsers()->matching($criteria);
  309. return $relation->count() > 0;
  310. }
  311. return false;
  312. }
  313. /**
  314. * @return ArrayCollection
  315. */
  316. public function getCourses()
  317. {
  318. return $this->courses;
  319. }
  320. /**
  321. * @param $courses
  322. */
  323. public function setCourses($courses)
  324. {
  325. $this->courses = new ArrayCollection();
  326. foreach ($courses as $course) {
  327. $this->addCourses($course);
  328. }
  329. }
  330. /**
  331. * @param SessionRelCourse $course
  332. */
  333. public function addCourses(SessionRelCourse $course)
  334. {
  335. $course->setSession($this);
  336. $this->courses[] = $course;
  337. }
  338. /**
  339. * @param Course $course
  340. *
  341. * @return bool
  342. */
  343. public function hasCourse(Course $course)
  344. {
  345. if ($this->getCourses()->count()) {
  346. $criteria = Criteria::create()->where(
  347. Criteria::expr()->eq("course", $course)
  348. );
  349. $relation = $this->getCourses()->matching($criteria);
  350. return $relation->count() > 0;
  351. }
  352. return false;
  353. }
  354. /**
  355. * Remove $course
  356. *
  357. * @param SessionRelCourse $course
  358. */
  359. public function removeCourses($course)
  360. {
  361. foreach ($this->courses as $key => $value) {
  362. if ($value->getId() == $course->getId()) {
  363. unset($this->courses[$key]);
  364. }
  365. }
  366. }
  367. /**
  368. * @param User $user
  369. * @param Course $course
  370. * @param int $status if not set it will check if the user is registered
  371. * with any status
  372. *
  373. * @return bool
  374. */
  375. public function hasUserInCourse(User $user, Course $course, $status = null)
  376. {
  377. $relation = $this->getUserInCourse($user, $course, $status);
  378. return $relation->count() > 0;
  379. }
  380. /**
  381. * @param User $user
  382. * @param Course $course
  383. *
  384. * @return bool
  385. */
  386. public function hasStudentInCourse(User $user, Course $course)
  387. {
  388. return $this->hasUserInCourse($user, $course, self::STUDENT);
  389. }
  390. /**
  391. * @param User $user
  392. * @param Course $course
  393. *
  394. * @return bool
  395. */
  396. public function hasCoachInCourseWithStatus(User $user, Course $course)
  397. {
  398. return $this->hasUserInCourse($user, $course, self::COACH);
  399. }
  400. /**
  401. * @param User $user
  402. * @param Course $course
  403. * @param string $status
  404. *
  405. * @return \Doctrine\Common\Collections\Collection|static
  406. */
  407. public function getUserInCourse(User $user, Course $course, $status = null)
  408. {
  409. $criteria = Criteria::create()->where(
  410. Criteria::expr()->eq("course", $course)
  411. )->andWhere(
  412. Criteria::expr()->eq("user", $user)
  413. );
  414. if (!is_null($status)) {
  415. $criteria->andWhere(
  416. Criteria::expr()->eq("status", $status)
  417. );
  418. }
  419. return $this->getUserCourseSubscriptions()->matching($criteria);
  420. }
  421. /**
  422. * Set name
  423. *
  424. * @param string $name
  425. * @return Session
  426. */
  427. public function setName($name)
  428. {
  429. $this->name = $name;
  430. return $this;
  431. }
  432. /**
  433. * Get name
  434. *
  435. * @return string
  436. */
  437. public function getName()
  438. {
  439. return $this->name;
  440. }
  441. /**
  442. * Set description
  443. *
  444. * @param string $description
  445. * @return Groups
  446. */
  447. public function setDescription($description)
  448. {
  449. $this->description = $description;
  450. return $this;
  451. }
  452. /**
  453. * Get description
  454. *
  455. * @return string
  456. */
  457. public function getDescription()
  458. {
  459. return $this->description;
  460. }
  461. /**
  462. * Set nbrCourses
  463. *
  464. * @param integer $nbrCourses
  465. * @return Session
  466. */
  467. public function setNbrCourses($nbrCourses)
  468. {
  469. $this->nbrCourses = $nbrCourses;
  470. return $this;
  471. }
  472. /**
  473. * Get nbrCourses
  474. *
  475. * @return integer
  476. */
  477. public function getNbrCourses()
  478. {
  479. return $this->nbrCourses;
  480. }
  481. /**
  482. * Set nbrUsers
  483. *
  484. * @param integer $nbrUsers
  485. * @return Session
  486. */
  487. public function setNbrUsers($nbrUsers)
  488. {
  489. $this->nbrUsers = $nbrUsers;
  490. return $this;
  491. }
  492. /**
  493. * Get nbrUsers
  494. *
  495. * @return integer
  496. */
  497. public function getNbrUsers()
  498. {
  499. return $this->nbrUsers;
  500. }
  501. /**
  502. * Set nbrClasses
  503. *
  504. * @param integer $nbrClasses
  505. * @return Session
  506. */
  507. public function setNbrClasses($nbrClasses)
  508. {
  509. $this->nbrClasses = $nbrClasses;
  510. return $this;
  511. }
  512. /**
  513. * Get nbrClasses
  514. *
  515. * @return integer
  516. */
  517. public function getNbrClasses()
  518. {
  519. return $this->nbrClasses;
  520. }
  521. /**
  522. * Set sessionAdminId
  523. *
  524. * @param integer $sessionAdminId
  525. * @return Session
  526. */
  527. public function setSessionAdminId($sessionAdminId)
  528. {
  529. $this->sessionAdminId = $sessionAdminId;
  530. return $this;
  531. }
  532. /**
  533. * Get sessionAdminId
  534. *
  535. * @return integer
  536. */
  537. public function getSessionAdminId()
  538. {
  539. return $this->sessionAdminId;
  540. }
  541. /**
  542. * Set visibility
  543. *
  544. * @param integer $visibility
  545. * @return Session
  546. */
  547. public function setVisibility($visibility)
  548. {
  549. $this->visibility = $visibility;
  550. return $this;
  551. }
  552. /**
  553. * Get visibility
  554. *
  555. * @return integer
  556. */
  557. public function getVisibility()
  558. {
  559. return $this->visibility;
  560. }
  561. /**
  562. * Set promotionId
  563. *
  564. * @param integer $promotionId
  565. * @return Session
  566. */
  567. public function setPromotionId($promotionId)
  568. {
  569. $this->promotionId = $promotionId;
  570. return $this;
  571. }
  572. /**
  573. * Get promotionId
  574. *
  575. * @return integer
  576. */
  577. public function getPromotionId()
  578. {
  579. return $this->promotionId;
  580. }
  581. /**
  582. * Set displayStartDate
  583. *
  584. * @param \DateTime $displayStartDate
  585. * @return Session
  586. */
  587. public function setDisplayStartDate($displayStartDate)
  588. {
  589. $this->displayStartDate = $displayStartDate;
  590. return $this;
  591. }
  592. /**
  593. * Get displayStartDate
  594. *
  595. * @return \DateTime
  596. */
  597. public function getDisplayStartDate()
  598. {
  599. return $this->displayStartDate;
  600. }
  601. /**
  602. * Set displayEndDate
  603. *
  604. * @param \DateTime $displayEndDate
  605. * @return Session
  606. */
  607. public function setDisplayEndDate($displayEndDate)
  608. {
  609. $this->displayEndDate = $displayEndDate;
  610. return $this;
  611. }
  612. /**
  613. * Get displayEndDate
  614. *
  615. * @return \DateTime
  616. */
  617. public function getDisplayEndDate()
  618. {
  619. return $this->displayEndDate;
  620. }
  621. /**
  622. * Set accessStartDate
  623. *
  624. * @param \DateTime $accessStartDate
  625. * @return Session
  626. */
  627. public function setAccessStartDate($accessStartDate)
  628. {
  629. $this->accessStartDate = $accessStartDate;
  630. return $this;
  631. }
  632. /**
  633. * Get accessStartDate
  634. *
  635. * @return \DateTime
  636. */
  637. public function getAccessStartDate()
  638. {
  639. return $this->accessStartDate;
  640. }
  641. /**
  642. * Set accessEndDate
  643. *
  644. * @param \DateTime $accessEndDate
  645. * @return Session
  646. */
  647. public function setAccessEndDate($accessEndDate)
  648. {
  649. $this->accessEndDate = $accessEndDate;
  650. return $this;
  651. }
  652. /**
  653. * Get accessEndDate
  654. *
  655. * @return \DateTime
  656. */
  657. public function getAccessEndDate()
  658. {
  659. return $this->accessEndDate;
  660. }
  661. /**
  662. * Set coachAccessStartDate
  663. *
  664. * @param \DateTime $coachAccessStartDate
  665. * @return Session
  666. */
  667. public function setCoachAccessStartDate($coachAccessStartDate)
  668. {
  669. $this->coachAccessStartDate = $coachAccessStartDate;
  670. return $this;
  671. }
  672. /**
  673. * Get coachAccessStartDate
  674. *
  675. * @return \DateTime
  676. */
  677. public function getCoachAccessStartDate()
  678. {
  679. return $this->coachAccessStartDate;
  680. }
  681. /**
  682. * Set coachAccessEndDate
  683. *
  684. * @param \DateTime $coachAccessEndDate
  685. * @return Session
  686. */
  687. public function setCoachAccessEndDate($coachAccessEndDate)
  688. {
  689. $this->coachAccessEndDate = $coachAccessEndDate;
  690. return $this;
  691. }
  692. /**
  693. * Get coachAccessEndDate
  694. *
  695. * @return \DateTime
  696. */
  697. public function getCoachAccessEndDate()
  698. {
  699. return $this->coachAccessEndDate;
  700. }
  701. /**
  702. * Get id
  703. * @return User
  704. */
  705. public function getGeneralCoach()
  706. {
  707. return $this->generalCoach;
  708. }
  709. /**
  710. * @param $coach
  711. */
  712. public function setGeneralCoach($coach)
  713. {
  714. $this->generalCoach = $coach;
  715. }
  716. /**
  717. * @return mixed
  718. * @return SessionCategory
  719. */
  720. public function getCategory()
  721. {
  722. return $this->category;
  723. }
  724. /**
  725. * @param $category
  726. * @return $this
  727. */
  728. public function setCategory($category)
  729. {
  730. $this->category = $category;
  731. return $this;
  732. }
  733. /**
  734. * @return array
  735. */
  736. public static function getStatusList()
  737. {
  738. return array(
  739. self::VISIBLE => 'status_visible',
  740. self::READ_ONLY => 'status_read_only',
  741. self::INVISIBLE => 'status_invisible',
  742. self::AVAILABLE => 'status_available',
  743. );
  744. }
  745. /**
  746. * Check if session is visible
  747. * @return bool
  748. */
  749. public function isActive()
  750. {
  751. $now = new \Datetime('now');
  752. if ($now > $this->getAccessStartDate()) {
  753. return true;
  754. }
  755. return false;
  756. }
  757. /**
  758. * @param Course $course
  759. */
  760. public function addCourse(Course $course)
  761. {
  762. $entity = new SessionRelCourse();
  763. $entity->setCourse($course);
  764. $this->addCourses($entity);
  765. }
  766. /**
  767. * @return ArrayCollection
  768. */
  769. public function getUserCourseSubscriptions()
  770. {
  771. return $this->userCourseSubscriptions;
  772. }
  773. /**
  774. * @param ArrayCollection $userCourseSubscriptions
  775. */
  776. public function setUserCourseSubscriptions($userCourseSubscriptions)
  777. {
  778. $this->userCourseSubscriptions = new ArrayCollection();
  779. foreach ($userCourseSubscriptions as $item) {
  780. $this->addUserCourseSubscription($item);
  781. }
  782. }
  783. /**
  784. * @param SessionRelCourseRelUser $subscription
  785. */
  786. public function addUserCourseSubscription(SessionRelCourseRelUser $subscription)
  787. {
  788. $subscription->setSession($this);
  789. if (!$this->hasUserCourseSubscription($subscription)) {
  790. $this->userCourseSubscriptions[] = $subscription;
  791. }
  792. }
  793. /**
  794. * @param int $status
  795. * @param User $user
  796. * @param Course $course
  797. */
  798. public function addUserInCourse($status, User $user, Course $course)
  799. {
  800. $userRelCourseRelSession = new SessionRelCourseRelUser();
  801. $userRelCourseRelSession->setCourse($course);
  802. $userRelCourseRelSession->setUser($user);
  803. $userRelCourseRelSession->setSession($this);
  804. $userRelCourseRelSession->setStatus($status);
  805. $this->addUserCourseSubscription($userRelCourseRelSession);
  806. }
  807. /**
  808. * @param SessionRelCourseRelUser $subscription
  809. * @return bool
  810. */
  811. public function hasUserCourseSubscription(SessionRelCourseRelUser $subscription)
  812. {
  813. if ($this->getUserCourseSubscriptions()->count()) {
  814. $criteria = Criteria::create()->where(
  815. Criteria::expr()->eq("user", $subscription->getUser())
  816. )->andWhere(
  817. Criteria::expr()->eq("course", $subscription->getCourse())
  818. )->andWhere(
  819. Criteria::expr()->eq("session", $subscription->getSession())
  820. );
  821. $relation = $this->getUserCourseSubscriptions()->matching($criteria);
  822. return $relation->count() > 0;
  823. }
  824. return false;
  825. }
  826. /**
  827. * @return Course
  828. */
  829. public function getCurrentCourse()
  830. {
  831. return $this->currentCourse;
  832. }
  833. /**
  834. * @param Course $course
  835. * @return $this
  836. */
  837. public function setCurrentCourse(Course $course)
  838. {
  839. // If the session is registered in the course session list.
  840. if ($this->getCourses()->contains($course->getId())) {
  841. $this->currentCourse = $course;
  842. }
  843. return $this;
  844. }
  845. /**
  846. * Set $sendSubscriptionNotification
  847. * @param boolean $sendNotification
  848. * @return \Chamilo\CoreBundle\Entity\Session
  849. */
  850. public function setSendSubscriptionNotification($sendNotification)
  851. {
  852. $this->sendSubscriptionNotification = $sendNotification;
  853. return $this;
  854. }
  855. /**
  856. * Get $sendSubscriptionNotification
  857. * @return boolean
  858. */
  859. public function getSendSubscriptionNotification()
  860. {
  861. return $this->sendSubscriptionNotification;
  862. }
  863. /**
  864. * Get user from course by status
  865. * @param \Chamilo\CoreBundle\Entity\Course $course
  866. * @param string $status
  867. * @return \Doctrine\Common\Collections\Collection|static
  868. */
  869. public function getUserCourseSubscriptionsByStatus(Course $course, $status)
  870. {
  871. $criteria = Criteria::create()
  872. ->where(
  873. Criteria::expr()->eq("course", $course)
  874. )
  875. ->andWhere(
  876. Criteria::expr()->eq("status", $status)
  877. );
  878. return $this->userCourseSubscriptions->matching($criteria);
  879. }
  880. public function getBuyCoursePluginPrice()
  881. {
  882. // start buycourse validation
  883. // display the course price and buy button if the buycourses plugin is enabled and this course is configured
  884. $plugin = \BuyCoursesPlugin::create();
  885. $isThisCourseInSale = $plugin->buyCoursesForGridCatalogValidator($this->id, \BuyCoursesPlugin::PRODUCT_TYPE_SESSION);
  886. $return = [];
  887. if ($isThisCourseInSale) {
  888. // set the Price label
  889. $return['html'] = $isThisCourseInSale['html'];
  890. // set the Buy button instead register.
  891. if ($isThisCourseInSale['verificator']) {
  892. $return['buy_button'] = $plugin->returnBuyCourseButton($this->id, \BuyCoursesPlugin::PRODUCT_TYPE_SESSION);
  893. }
  894. }
  895. // end buycourse validation
  896. return $return;
  897. }
  898. /**
  899. * @param ArrayCollection $studentPublications
  900. * @return Session
  901. */
  902. public function setStudentPublications(ArrayCollection $studentPublications)
  903. {
  904. $this->studentPublications = new ArrayCollection();
  905. foreach ($studentPublications as $studentPublication) {
  906. $this->addStudentPublication($studentPublication);
  907. }
  908. return $this;
  909. }
  910. /**
  911. * @param CStudentPublication $studentPublication
  912. * @return Session
  913. */
  914. public function addStudentPublication(CStudentPublication $studentPublication)
  915. {
  916. $this->studentPublications[] = $studentPublication;
  917. return $this;
  918. }
  919. /**
  920. * Get studentPublications
  921. * @return ArrayCollection
  922. */
  923. public function getStudentPublications()
  924. {
  925. return $this->studentPublications;
  926. }
  927. }