message.ajax.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Setting notifications
  15. $count_unread_message = 0;
  16. if (api_get_setting('allow_message_tool') === 'true') {
  17. // get count unread message and total invitations
  18. $count_unread_message = MessageManager::getCountNewMessagesFromDB($userId);
  19. }
  20. if (api_get_setting('allow_social_tool') === 'true') {
  21. $number_of_new_messages_of_friend = SocialManager::get_message_number_invitation_by_user_id(
  22. $userId
  23. );
  24. $usergroup = new UserGroup();
  25. $group_pending_invitations = $usergroup->get_groups_by_user(
  26. $userId,
  27. GROUP_USER_PERMISSION_PENDING_INVITATION,
  28. false
  29. );
  30. if (!empty($group_pending_invitations)) {
  31. $group_pending_invitations = count($group_pending_invitations);
  32. } else {
  33. $group_pending_invitations = 0;
  34. }
  35. $invitations = [
  36. 'ms_friends' => $number_of_new_messages_of_friend,
  37. 'ms_groups' => $group_pending_invitations,
  38. 'ms_inbox' => $count_unread_message,
  39. ];
  40. }
  41. header('Content-type:application/json');
  42. echo json_encode($invitations);
  43. break;
  44. case 'send_message':
  45. api_block_anonymous_users(false);
  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. api_block_anonymous_users(false);
  82. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  83. $invitationContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  84. SocialManager::sendInvitationToUser($_REQUEST['user_id'], $subject, $invitationContent);
  85. break;
  86. case 'find_users':
  87. if (api_is_anonymous()) {
  88. echo '';
  89. break;
  90. }
  91. $repo = UserManager::getRepository();
  92. $users = $repo->findUsersToSendMessage(
  93. api_get_user_id(),
  94. $_REQUEST['q'],
  95. $_REQUEST['page_limit']
  96. );
  97. $showEmail = api_get_setting('show_email_addresses') === 'true';
  98. $return = ['items' => []];
  99. /** @var User $user */
  100. foreach ($users as $user) {
  101. $userName = UserManager::formatUserFullName($user, true);
  102. if ($showEmail) {
  103. $userName .= " ({$user->getEmail()})";
  104. }
  105. $return['items'][] = [
  106. 'text' => $userName,
  107. 'id' => $user->getId(),
  108. ];
  109. }
  110. header('Content-type:application/json');
  111. echo json_encode($return);
  112. break;
  113. default:
  114. echo '';
  115. }
  116. exit;