notify_by_mail.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * This script generates four session categories.
  5. *
  6. * @package chamilo.plugin.advanced_subscription
  7. */
  8. /**
  9. * Init.
  10. */
  11. require_once __DIR__.'/../config.php';
  12. $plugin = AdvancedSubscriptionPlugin::create();
  13. $now = api_get_utc_datetime();
  14. $weekAgo = api_get_utc_datetime('-1 week');
  15. $sessionExtraField = new ExtraField('session');
  16. $sessionExtraFieldValue = new ExtraFieldValue('session');
  17. /**
  18. * Get session list.
  19. */
  20. $joinTables = Database::get_main_table(TABLE_MAIN_SESSION).' s INNER JOIN '.
  21. Database::get_main_table(TABLE_MAIN_SESSION_USER).' su ON s.id = su.session_id INNER JOIN '.
  22. Database::get_main_table(TABLE_MAIN_USER_REL_USER).' uu ON su.user_id = uu.user_id INNER JOIN '.
  23. Database::get_main_table(TABLE_ADVANCED_SUBSCRIPTION_QUEUE).' asq ON su.session_id = asq.session_id AND su.user_id = asq.user_id';
  24. $columns = 's.id AS session_id, uu.friend_user_id AS superior_id, uu.user_id AS student_id, asq.id AS queue_id, asq.status AS status';
  25. $conditions = [
  26. 'where' => [
  27. 's.access_start_date >= ? AND uu.relation_type = ? AND asq.updated_at <= ?' => [
  28. $now,
  29. USER_RELATION_TYPE_BOSS,
  30. $weekAgo,
  31. ],
  32. ],
  33. 'order' => 's.id',
  34. ];
  35. $queueList = Database::select($columns, $joinTables, $conditions);
  36. /**
  37. * Remind students.
  38. */
  39. $sessionInfoList = [];
  40. foreach ($queueList as $queueItem) {
  41. if (!isset($sessionInfoList[$queueItem['session_id']])) {
  42. $sessionInfoList[$queueItem['session_id']] = api_get_session_info($queueItem['session_id']);
  43. }
  44. }
  45. foreach ($queueList as $queueItem) {
  46. switch ($queueItem['status']) {
  47. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_START:
  48. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED:
  49. $data = ['sessionId' => $queueItem['session_id']];
  50. $data['session'] = api_get_session_info($queueItem['session_id']);
  51. $data['student'] = api_get_user_info($queueItem['student_id']);
  52. $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_REMINDER_STUDENT);
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. /**
  59. * Remind superiors.
  60. */
  61. // Get recommended number of participants
  62. $sessionRecommendedNumber = [];
  63. foreach ($queueList as $queueItem) {
  64. $row =
  65. $sessionExtraFieldValue->get_values_by_handler_and_field_variable(
  66. $queueItem['session_id'],
  67. 'recommended_number_of_participants'
  68. );
  69. $sessionRecommendedNumber[$queueItem['session_id']] = $row['value'];
  70. }
  71. // Group student by superior and session
  72. $queueBySuperior = [];
  73. foreach ($queueList as $queueItem) {
  74. $queueBySuperior[$queueItem['session_id']][$queueItem['superior_id']][$queueItem['student_id']]['status'] = $queueItem['status'];
  75. }
  76. foreach ($queueBySuperior as $sessionId => $superiorStudents) {
  77. $data = [
  78. 'sessionId' => $sessionId,
  79. 'session' => $sessionInfoList[$sessionId],
  80. 'students' => [],
  81. ];
  82. $dataUrl = [
  83. 'action' => 'confirm',
  84. 'sessionId' => $sessionId,
  85. 'currentUserId' => 0,
  86. 'newStatus' => ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED,
  87. 'studentUserId' => 0,
  88. 'is_connected' => true,
  89. 'profile_completed' => 0,
  90. ];
  91. foreach ($superiorStudents as $superiorId => $students) {
  92. $data['superior'] = api_get_user_info($superiorId);
  93. // Check if superior has at least one student
  94. if (count($students) > 0) {
  95. foreach ($students as $studentId => $studentInfo) {
  96. if ($studentInfo['status'] == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_START) {
  97. $data['students'][$studentId] = api_get_user_info($studentId);
  98. $dataUrl['studentUserId'] = $studentId;
  99. $dataUrl['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED;
  100. $data['students'][$studentId]['acceptUrl'] = $plugin->getQueueUrl($dataUrl);
  101. $dataUrl['newStatus'] = ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_DISAPPROVED;
  102. $data['students'][$studentId]['rejectUrl'] = $plugin->getQueueUrl($dataUrl);
  103. }
  104. }
  105. if (is_array($data['students']) && count($data['students']) > 0) {
  106. // Check if superior have more than recommended
  107. if ($sessionRecommendedNumber[$sessionId] >= count($students)) {
  108. // Is greater or equal than recommended
  109. $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_REMINDER_SUPERIOR);
  110. } else {
  111. // Is less than recommended
  112. $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_REMINDER_SUPERIOR_MAX);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Remind admins.
  120. */
  121. $admins = UserManager::get_all_administrators();
  122. $isWesternNameOrder = api_is_western_name_order();
  123. foreach ($admins as &$admin) {
  124. $admin['complete_name'] = $isWesternNameOrder ?
  125. $admin['firstname'].', '.$admin['lastname'] : $admin['lastname'].', '.$admin['firstname']
  126. ;
  127. }
  128. unset($admin);
  129. $queueByAdmin = [];
  130. foreach ($queueList as $queueItem) {
  131. if ($queueItem['status'] == ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED) {
  132. $queueByAdmin[$queueItem['session_id']]['students'][$queueItem['student_id']]['user_id'] = $queueItem['student_id'];
  133. }
  134. }
  135. $data = [
  136. 'admins' => $admins,
  137. ];
  138. foreach ($queueByAdmin as $sessionId => $studentInfo) {
  139. $data['sessionId'] = $sessionId;
  140. $data['admin_view_url'] = api_get_path(WEB_PLUGIN_PATH).
  141. 'advanced_subscription/src/admin_view.php?s='.$data['sessionId'];
  142. $data['session'] = $sessionInfoList[$sessionId];
  143. $data['students'] = $studentInfo['students'];
  144. $plugin->sendMail($data, ADVANCED_SUBSCRIPTION_ACTION_REMINDER_ADMIN);
  145. }