HookResubscription.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Hook to limit session resubscriptions.
  5. *
  6. * @author Imanol Losada Oriol <imanol.losada@beeznest.com>
  7. *
  8. * @package chamilo.plugin.resubscription
  9. */
  10. class HookResubscription extends HookObserver implements HookResubscribeObserverInterface
  11. {
  12. /**
  13. * Class constructor.
  14. */
  15. public function __construct()
  16. {
  17. parent::__construct(
  18. 'plugin/resubscription/src/Resubscription.php',
  19. 'resubscription'
  20. );
  21. }
  22. /**
  23. * Limit session resubscription when a Chamilo user is resubscribed to a session.
  24. *
  25. * @param HookCreateUserEventInterface $hook The hook
  26. */
  27. public function hookResubscribe(HookResubscribeEventInterface $hook)
  28. {
  29. $data = $hook->getEventData();
  30. if ($data['type'] === HOOK_EVENT_TYPE_PRE) {
  31. $resubscriptionLimit = Resubscription::create()->get('resubscription_limit');
  32. // Initialize variables as a calendar year by default
  33. $limitDateFormat = 'Y-01-01';
  34. $limitDate = gmdate($limitDateFormat);
  35. $resubscriptionOffset = "1 year";
  36. // No need to use a 'switch' with only two options so an 'if' is enough.
  37. // However this could change if the number of options increases
  38. if ($resubscriptionLimit === 'natural_year') {
  39. $limitDateFormat = 'Y-m-d';
  40. $limitDate = gmdate($limitDateFormat);
  41. $limitDate = gmdate($limitDateFormat, strtotime("$limitDate -$resubscriptionOffset"));
  42. }
  43. $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_SESSION)."ON id = session_id";
  44. // User sessions and courses
  45. $userSessions = Database::select(
  46. 'session_id, date_end',
  47. Database::get_main_table(TABLE_MAIN_SESSION_USER).$join,
  48. [
  49. 'where' => [
  50. 'user_id = ? AND date_end >= ?' => [
  51. api_get_user_id(),
  52. $limitDate,
  53. ],
  54. ],
  55. 'order' => 'date_end DESC',
  56. ]
  57. );
  58. $userSessionCourses = [];
  59. foreach ($userSessions as $userSession) {
  60. $userSessionCourseResult = Database::select(
  61. 'c_id',
  62. Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
  63. [
  64. 'where' => [
  65. 'session_id = ?' => [
  66. $userSession['session_id'],
  67. ],
  68. ],
  69. ]
  70. );
  71. foreach ($userSessionCourseResult as $userSessionCourse) {
  72. if (!isset($userSessionCourses[$userSessionCourse['c_id']])) {
  73. $userSessionCourses[$userSessionCourse['c_id']] = $userSession['date_end'];
  74. }
  75. }
  76. }
  77. // Current session and courses
  78. $currentSessionCourseResult = Database::select(
  79. 'c_id',
  80. Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
  81. [
  82. 'where' => [
  83. 'session_id = ?' => [
  84. $data['session_id'],
  85. ],
  86. ],
  87. ]
  88. );
  89. // Check if current course code matches with one of the users
  90. foreach ($currentSessionCourseResult as $currentSessionCourse) {
  91. if (isset($userSessionCourses[$currentSessionCourse['c_id']])) {
  92. $endDate = $userSessionCourses[$currentSessionCourse['c_id']];
  93. $resubscriptionDate = gmdate($limitDateFormat, strtotime($endDate." +$resubscriptionOffset"));
  94. $icon = Display::return_icon('students.gif', get_lang('Student'));
  95. $canResubscribeFrom = sprintf(
  96. get_plugin_lang('CanResubscribeFromX', 'resubscription'),
  97. $resubscriptionDate
  98. );
  99. throw new Exception(Display::label($icon.' '.$canResubscribeFrom, "info"));
  100. }
  101. }
  102. }
  103. }
  104. }