message.ajax.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use Chamilo\UserBundle\Entity\Repository\UserRepository;
  5. /**
  6. * Responses to AJAX calls
  7. */
  8. $action = $_GET['a'];
  9. switch ($action) {
  10. case 'send_message':
  11. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  12. $messageContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  13. if (empty($subject) || empty($messageContent)) {
  14. echo Display::display_error_message(get_lang('ErrorSendingMessage'));
  15. exit;
  16. }
  17. $result = MessageManager::send_message($_REQUEST['user_id'], $subject, $messageContent);
  18. if ($result) {
  19. echo Display::display_confirmation_message(get_lang('MessageHasBeenSent'));
  20. } else {
  21. echo Display::display_error_message(get_lang('ErrorSendingMessage'));
  22. }
  23. break;
  24. case 'send_invitation':
  25. $subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : null;
  26. $invitationContent = isset($_REQUEST['content']) ? trim($_REQUEST['content']) : null;
  27. SocialManager::sendInvitationToUser($_REQUEST['user_id'], $subject, $invitationContent);
  28. break;
  29. case 'find_users':
  30. if (api_is_anonymous()) {
  31. echo '';
  32. break;
  33. }
  34. /** @var UserRepository $repo */
  35. $repo = Database::getManager()
  36. ->getRepository('ChamiloUserBundle:User');
  37. $users = $repo->findUsersToSendMessage(
  38. api_get_user_id(),
  39. $_REQUEST['q'],
  40. $_REQUEST['page_limit']
  41. );
  42. $showEmail = api_get_setting('show_email_addresses') === 'true';
  43. $return = ['items' => []];
  44. /** @var User $user */
  45. foreach ($users as $user) {
  46. $userName = $user->getCompleteName();
  47. if ($showEmail) {
  48. $userName .= " ({$user->getEmail()})";
  49. }
  50. $return['items'][] = [
  51. 'text' => $userName,
  52. 'id' => $user->getId()
  53. ];
  54. }
  55. echo json_encode($return);
  56. break;
  57. default:
  58. echo '';
  59. }
  60. exit;