message.ajax.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. $_dont_save_user_course_access = true;
  5. /**
  6. * Responses to AJAX calls.
  7. */
  8. require_once __DIR__.'/../global.inc.php';
  9. $action = $_GET['a'];
  10. switch ($action) {
  11. case 'get_notifications_inbox':
  12. $userId = api_get_user_id();
  13. $listInbox = [];
  14. if (api_get_setting('allow_message_tool') == 'true') {
  15. $list = MessageManager::getMessageData(
  16. 0,
  17. 10,
  18. null,
  19. null,
  20. ['actions' => ['read'], 'type' => MessageManager::MESSAGE_TYPE_INBOX]
  21. );
  22. foreach ($list as $row) {
  23. $user = api_get_user_info($row['0']);
  24. $temp['title'] = $row['1'];
  25. $temp['date'] = $row['2'];
  26. $temp['fullname'] = $user['complete_name'];
  27. $temp['email'] = $user['email'];
  28. $temp['url'] = $row['1'];
  29. $listInbox[] = $temp;
  30. }
  31. }
  32. header('Content-type:application/json');
  33. echo json_encode($listInbox);
  34. break;
  35. case 'get_notifications_friends':
  36. $userId = api_get_user_id();
  37. $listInvitations = [];
  38. $temp = [];
  39. if (api_get_setting('allow_social_tool') == 'true') {
  40. $list = SocialManager::get_list_invitation_of_friends_by_user_id($userId, 3);
  41. foreach ($list as $row) {
  42. $user = api_get_user_info($row['user_sender_id']);
  43. $temp['title'] = $row['title'];
  44. $temp['content'] = $row['content'];
  45. $temp['date'] = $row['send_date'];
  46. $temp['user_id'] = $user['id'];
  47. $temp['fullname'] = $user['complete_name'];
  48. $temp['email'] = $user['email'];
  49. $temp['avatar'] = $user['avatar_small'];
  50. $listInvitations[] = $temp;
  51. }
  52. }
  53. header('Content-type:application/json');
  54. echo json_encode($listInvitations);
  55. break;
  56. case 'get_count_message':
  57. $userId = api_get_user_id();
  58. $invitations = [];
  59. // Setting notifications
  60. $count_unread_message = 0;
  61. if (api_get_setting('allow_message_tool') === 'true') {
  62. // get count unread message and total invitations
  63. $count_unread_message = MessageManager::getCountNewMessagesFromDB($userId);
  64. }
  65. if (api_get_setting('allow_social_tool') === 'true') {
  66. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(
  67. $userId
  68. );
  69. $usergroup = new UserGroup();
  70. $group_pending_invitations = $usergroup->get_groups_by_user(
  71. $userId,
  72. GROUP_USER_PERMISSION_PENDING_INVITATION,
  73. false
  74. );
  75. if (!empty($group_pending_invitations)) {
  76. $group_pending_invitations = count($group_pending_invitations);
  77. } else {
  78. $group_pending_invitations = 0;
  79. }
  80. $invitations = [
  81. 'ms_friends' => $number_of_new_messages_of_friend,
  82. 'ms_groups' => $group_pending_invitations,
  83. 'ms_inbox' => $count_unread_message,
  84. ];
  85. }
  86. header('Content-type:application/json');
  87. echo json_encode($invitations);
  88. break;
  89. case 'send_message':
  90. api_block_anonymous_users(false);
  91. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  92. $messageContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  93. if (empty($subject) || empty($messageContent)) {
  94. echo Display::return_message(get_lang('There was an error while trying to send the message.'), 'error');
  95. exit;
  96. }
  97. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  98. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  99. // Add course info
  100. if (!empty($courseId)) {
  101. $courseInfo = api_get_course_info_by_id($courseId);
  102. if (!empty($courseInfo)) {
  103. if (empty($sessionId)) {
  104. $courseNotification = sprintf(get_lang('This e-mail was sent via course %s'), $courseInfo['title']);
  105. } else {
  106. $sessionInfo = api_get_session_info($sessionId);
  107. if (!empty($sessionInfo)) {
  108. $courseNotification = sprintf(
  109. get_lang('This e-mail was sent via course %sInSessionX'),
  110. $courseInfo['title'],
  111. $sessionInfo['name']
  112. );
  113. }
  114. }
  115. $messageContent .= '<br /><br />'.$courseNotification;
  116. }
  117. }
  118. $result = MessageManager::send_message($_REQUEST['user_id'], $subject, $messageContent);
  119. if ($result) {
  120. echo Display::return_message(get_lang('Your message has been sent.'), 'confirmation');
  121. } else {
  122. echo Display::return_message(get_lang('There was an error while trying to send the message.'), 'confirmation');
  123. }
  124. break;
  125. case 'send_invitation':
  126. api_block_anonymous_users(false);
  127. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  128. $invitationContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  129. SocialManager::sendInvitationToUser($_REQUEST['user_id'], $subject, $invitationContent);
  130. break;
  131. case 'find_users':
  132. if (api_is_anonymous()) {
  133. echo '';
  134. break;
  135. }
  136. $repo = UserManager::getRepository();
  137. $users = $repo->findUsersToSendMessage(
  138. api_get_user_id(),
  139. $_REQUEST['q'],
  140. $_REQUEST['page_limit']
  141. );
  142. $showEmail = api_get_setting('show_email_addresses') === 'true';
  143. $return = ['items' => []];
  144. /** @var User $user */
  145. foreach ($users as $user) {
  146. $userName = UserManager::formatUserFullName($user, true);
  147. if ($showEmail) {
  148. $userName .= " ({$user->getEmail()})";
  149. }
  150. $return['items'][] = [
  151. 'text' => $userName,
  152. 'id' => $user->getId(),
  153. ];
  154. }
  155. header('Content-type:application/json');
  156. echo json_encode($return);
  157. break;
  158. default:
  159. echo '';
  160. }
  161. exit;