MessagesWebService.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class for manage the messages web service
  5. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  6. * @package chamilo.webservices.messages
  7. */
  8. class MessagesWebService extends WebService
  9. {
  10. const SERVICE_NAME = 'MsgREST';
  11. /**
  12. * Generate the api key for a user
  13. * @param int $userId The user id
  14. * @return string The api key
  15. */
  16. public function generateApiKey($userId)
  17. {
  18. $apiKey = UserManager::get_api_keys($userId, self::SERVICE_NAME);
  19. if (empty($apiKey)) {
  20. UserManager::add_api_key($userId, self::SERVICE_NAME);
  21. $apiKey = UserManager::get_api_keys($userId, self::SERVICE_NAME);
  22. }
  23. return current($apiKey);
  24. }
  25. /**
  26. * Get the user api key
  27. * @param string $username The user name
  28. * @return string The api key
  29. */
  30. public function getApiKey($username)
  31. {
  32. $userInfo = api_get_user_info_from_username($username);
  33. $userId = $userInfo['user_id'];
  34. if ($this->apiKey !== null) {
  35. return $this->apiKey;
  36. } else {
  37. $this->apiKey = $this->generateApiKey($userId);
  38. return $this->apiKey;
  39. }
  40. }
  41. /**
  42. * Check if the api is valid for a user
  43. * @param string $username The username
  44. * @param string $apiKeyToValidate The api key
  45. * @return boolean Whether the api belongs to the user return true. Otherwise return false
  46. */
  47. public static function isValidApiKey($username, $apiKeyToValidate)
  48. {
  49. $userInfo = api_get_user_info_from_username($username);
  50. $userId = $userInfo['user_id'];
  51. $apiKeys = UserManager::get_api_keys($userId, self::SERVICE_NAME);
  52. if (!empty($apiKeys)) {
  53. $apiKey = current($apiKeys);
  54. if ($apiKey == $apiKeyToValidate) {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. /**
  61. * Get the count of new messages for a user
  62. * @param string $username The username
  63. * @param int $lastId The id of the last received message
  64. * @return int The count fo new messages
  65. */
  66. public function countNewMessages($username, $lastId = 0)
  67. {
  68. $userInfo = api_get_user_info_from_username($username);
  69. $userId = $userInfo['user_id'];
  70. return MessageManager::countMessagesFromLastReceivedMessage($userId, $lastId);
  71. }
  72. /**
  73. * Get the list of new messages for a user
  74. * @param string $username The username
  75. * @param int $lastId The id of the last received message
  76. * @return array the new message list
  77. */
  78. public function getNewMessages($username, $lastId = 0)
  79. {
  80. $messages = array();
  81. $userInfo = api_get_user_info_from_username($username);
  82. $userId = $userInfo['user_id'];
  83. $lastMessages = MessageManager::getMessagesFromLastReceivedMessage($userId, $lastId);
  84. foreach ($lastMessages as $message) {
  85. $hasAttachments = MessageManager::hasAttachments($message['id']);
  86. $messages[] = array(
  87. 'id' => $message['id'],
  88. 'title' => $message['title'],
  89. 'sender' => array(
  90. 'id' => $message['user_id'],
  91. 'lastname' => $message['lastname'],
  92. 'firstname' => $message['firstname'],
  93. 'completeName' => api_get_person_name($message['firstname'], $message['lastname']),
  94. ),
  95. 'sendDate' => $message['send_date'],
  96. 'content' => $message['content'],
  97. 'hasAttachments' => $hasAttachments,
  98. 'platform' => array(
  99. 'website' => api_get_path(WEB_PATH),
  100. 'messagingTool' => api_get_path(WEB_PATH) . 'main/messages/inbox.php'
  101. )
  102. );
  103. }
  104. return $messages;
  105. }
  106. }