notify_by_mail.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * This script generates four session categories.
  5. * @package chamilo.plugin.advanced_subscription
  6. */
  7. /**
  8. * Init
  9. */
  10. require_once __DIR__.'/../config.php';
  11. $plugin = AdvancedSubscriptionPlugin::create();
  12. $now = api_get_utc_datetime();
  13. $weekAgo = api_get_utc_datetime('-1 week');
  14. $sessionExtraField = new ExtraField('session');
  15. $sessionExtraFieldValue = new ExtraFieldValue('session');
  16. /**
  17. * Get session list
  18. */
  19. $joinTables = Database::get_main_table(TABLE_MAIN_SESSION).' s INNER JOIN '.
  20. Database::get_main_table(TABLE_MAIN_SESSION_USER).' su ON s.id = su.session_id INNER JOIN '.
  21. Database::get_main_table(TABLE_MAIN_USER_REL_USER).' uu ON su.user_id = uu.user_id INNER JOIN '.
  22. Database::get_main_table(TABLE_ADVANCED_SUBSCRIPTION_QUEUE).' asq ON su.session_id = asq.session_id AND su.user_id = asq.user_id';
  23. $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';
  24. $conditions = array(
  25. 'where' => array(
  26. 's.access_start_date >= ? AND uu.relation_type = ? AND asq.updated_at <= ?' => array(
  27. $now,
  28. USER_RELATION_TYPE_BOSS,
  29. $weekAgo,
  30. )
  31. ),
  32. 'order' => 's.id',
  33. );
  34. $queueList = Database::select($columns, $joinTables, $conditions);
  35. /**
  36. * Remind students
  37. */
  38. $sessionInfoList = array();
  39. foreach ($queueList as $queueItem) {
  40. if (!isset($sessionInfoList[$queueItem['session_id']])) {
  41. $sessionInfoList[$queueItem['session_id']] = api_get_session_info($queueItem['session_id']);
  42. }
  43. }
  44. foreach ($queueList as $queueItem) {
  45. switch ($queueItem['status']) {
  46. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_START:
  47. // No break
  48. case ADVANCED_SUBSCRIPTION_QUEUE_STATUS_BOSS_APPROVED:
  49. $data = array('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 = array();
  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 = array();
  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 = array(
  78. 'sessionId' => $sessionId,
  79. 'session' => $sessionInfoList[$sessionId],
  80. 'students' => array(),
  81. );
  82. $dataUrl = array(
  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 = array();
  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 = array(
  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. }