message.ajax.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_count_message':
  12. $userId = api_get_user_id();
  13. $invitations = [];
  14. $group_pending_invitations = 0;
  15. // Setting notifications
  16. $count_unread_message = 0;
  17. if (api_get_setting('allow_message_tool') === 'true') {
  18. // get count unread message and total invitations
  19. $count_unread_message = MessageManager::getCountNewMessagesFromDB($userId);
  20. }
  21. if (api_get_setting('allow_social_tool') === 'true') {
  22. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(
  23. $userId
  24. );
  25. $usergroup = new UserGroup();
  26. $group_pending_invitations = $usergroup->get_groups_by_user(
  27. $userId,
  28. GROUP_USER_PERMISSION_PENDING_INVITATION,
  29. false
  30. );
  31. if (!empty($group_pending_invitations)) {
  32. $group_pending_invitations = count($group_pending_invitations);
  33. } else {
  34. $group_pending_invitations = 0;
  35. }
  36. $invitations = [
  37. 'ms_friends' => $number_of_new_messages_of_friend,
  38. 'ms_groups' => $group_pending_invitations,
  39. 'ms_inbox' => $count_unread_message,
  40. ];
  41. }
  42. header('Content-type:application/json');
  43. echo json_encode($invitations);
  44. break;
  45. case 'send_message':
  46. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  47. $messageContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  48. if (empty($subject) || empty($messageContent)) {
  49. echo Display::return_message(get_lang('ErrorSendingMessage'), 'error');
  50. exit;
  51. }
  52. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  53. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  54. // Add course info
  55. if (!empty($courseId)) {
  56. $courseInfo = api_get_course_info_by_id($courseId);
  57. if (!empty($courseInfo)) {
  58. if (empty($sessionId)) {
  59. $courseNotification = sprintf(get_lang('ThisEmailWasSentViaCourseX'), $courseInfo['title']);
  60. } else {
  61. $sessionInfo = api_get_session_info($sessionId);
  62. if (!empty($sessionInfo)) {
  63. $courseNotification = sprintf(
  64. get_lang('ThisEmailWasSentViaCourseXInSessionX'),
  65. $courseInfo['title'],
  66. $sessionInfo['name']
  67. );
  68. }
  69. }
  70. $messageContent .= '<br /><br />'.$courseNotification;
  71. }
  72. }
  73. $result = MessageManager::send_message($_REQUEST['user_id'], $subject, $messageContent);
  74. if ($result) {
  75. echo Display::return_message(get_lang('MessageHasBeenSent'), 'confirmation');
  76. } else {
  77. echo Display::return_message(get_lang('ErrorSendingMessage'), 'confirmation');
  78. }
  79. break;
  80. case 'send_invitation':
  81. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  82. $invitationContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  83. SocialManager::sendInvitationToUser($_REQUEST['user_id'], $subject, $invitationContent);
  84. break;
  85. case 'find_users':
  86. if (api_is_anonymous()) {
  87. echo '';
  88. break;
  89. }
  90. $repo = UserManager::getRepository();
  91. $users = $repo->findUsersToSendMessage(
  92. api_get_user_id(),
  93. $_REQUEST['q'],
  94. $_REQUEST['page_limit']
  95. );
  96. $showEmail = api_get_setting('show_email_addresses') === 'true';
  97. $return = ['items' => []];
  98. /** @var User $user */
  99. foreach ($users as $user) {
  100. $userName = UserManager::formatUserFullName($user, true);
  101. if ($showEmail) {
  102. $userName .= " ({$user->getEmail()})";
  103. }
  104. $return['items'][] = [
  105. 'text' => $userName,
  106. 'id' => $user->getId(),
  107. ];
  108. }
  109. header('Content-type:application/json');
  110. echo json_encode($return);
  111. break;
  112. default:
  113. echo '';
  114. }
  115. exit;