message.ajax.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. use Chamilo\UserBundle\Entity\User;
  7. use Chamilo\UserBundle\Entity\Repository\UserRepository;
  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. $total_invitations = 0;
  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::get_number_of_messages(true);
  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. $total_invitations = intval($number_of_new_messages_of_friend) + $group_pending_invitations + intval($count_unread_message);
  37. }
  38. //$total_invitations = !empty($total_invitations) ? Display::badge($total_invitations) : '';
  39. echo $total_invitations;
  40. break;
  41. case 'send_message':
  42. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  43. $messageContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  44. if (empty($subject) || empty($messageContent)) {
  45. echo Display::return_message(get_lang('ErrorSendingMessage'), 'error');
  46. exit;
  47. }
  48. $result = MessageManager::send_message($_REQUEST['user_id'], $subject, $messageContent);
  49. if ($result) {
  50. echo Display::return_message(get_lang('MessageHasBeenSent'), 'confirmation');
  51. } else {
  52. echo Display::return_message(get_lang('ErrorSendingMessage'), 'confirmation');
  53. }
  54. break;
  55. case 'send_invitation':
  56. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  57. $invitationContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  58. SocialManager::sendInvitationToUser($_REQUEST['user_id'], $subject, $invitationContent);
  59. break;
  60. case 'find_users':
  61. if (api_is_anonymous()) {
  62. echo '';
  63. break;
  64. }
  65. /** @var UserRepository $repo */
  66. $repo = Database::getManager()
  67. ->getRepository('ChamiloUserBundle:User');
  68. $users = $repo->findUsersToSendMessage(
  69. api_get_user_id(),
  70. $_REQUEST['q'],
  71. $_REQUEST['page_limit']
  72. );
  73. $showEmail = api_get_setting('show_email_addresses') === 'true';
  74. $return = ['items' => []];
  75. /** @var User $user */
  76. foreach ($users as $user) {
  77. $userName = $user->getCompleteName();
  78. if ($showEmail) {
  79. $userName .= " ({$user->getEmail()})";
  80. }
  81. $return['items'][] = [
  82. 'text' => $userName,
  83. 'id' => $user->getId()
  84. ];
  85. }
  86. echo json_encode($return);
  87. break;
  88. default:
  89. echo '';
  90. }
  91. exit;