cm_webservice_inbox.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. require_once __DIR__.'/cm_webservice.php';
  8. /**
  9. * Description of cm_soap_inbox.
  10. *
  11. * @author marcosousa
  12. */
  13. class WSCMInbox extends WSCM
  14. {
  15. public function unreadMessage($username, $password)
  16. {
  17. if ($this->verifyUserPass($username, $password) == "valid") {
  18. $table_message = Database::get_main_table(TABLE_MESSAGE);
  19. $user_id = UserManager::get_user_id_from_username($username);
  20. $condition_msg_status = ' msg_status = 1 '; // define('MESSAGE_STATUS_UNREAD', '1');
  21. $sql_query = "SELECT COUNT(*) as number_messages
  22. FROM $table_message
  23. WHERE $condition_msg_status AND user_receiver_id=".$user_id;
  24. $sql_result = Database::query($sql_query);
  25. $result = Database::fetch_array($sql_result);
  26. return $result['number_messages'];
  27. }
  28. return "0";
  29. }
  30. public function get_message_id(
  31. $username,
  32. $password,
  33. $from,
  34. $number_of_items
  35. ) {
  36. if ($this->verifyUserPass($username, $password) == "valid") {
  37. $user_id = UserManager::get_user_id_from_username($username);
  38. $table_message = Database::get_main_table(TABLE_MESSAGE);
  39. $sql_query = "SELECT id FROM $table_message ".
  40. " WHERE user_receiver_id=".$user_id." AND msg_status IN (0,1)".
  41. " ORDER BY send_date LIMIT $from,$number_of_items";
  42. $sql_result = Database::query($sql_query);
  43. $message = "#";
  44. while ($result = Database::fetch_row($sql_result)) {
  45. $message .= $result[0]."#";
  46. }
  47. return $message;
  48. } else {
  49. return get_lang('Login failed - incorrect login or password.');
  50. }
  51. }
  52. public function get_message_data($username, $password, $message_id, $field)
  53. {
  54. if ($this->verifyUserPass($username, $password) == "valid") {
  55. $htmlcode = false;
  56. $user_id = UserManager::get_user_id_from_username($username);
  57. switch ($field) {
  58. case 'sender':
  59. $field_table = "user_sender_id";
  60. break;
  61. case 'title':
  62. $htmlcode = true;
  63. $field_table = "title";
  64. break;
  65. case 'date':
  66. $field_table = "send_date";
  67. break;
  68. case 'status':
  69. $field_table = "msg_status";
  70. break;
  71. case 'content':
  72. $this->set_message_as_read($user_id, $message_id);
  73. $htmlcode = true;
  74. $field_table = "content";
  75. break;
  76. default:
  77. $field_table = "title";
  78. }
  79. $table_message = Database::get_main_table(TABLE_MESSAGE);
  80. $sql_query = "SELECT ".$field_table." FROM $table_message ".
  81. " WHERE user_receiver_id=".$user_id." AND id=".$message_id;
  82. $sql_result = Database::query($sql_query);
  83. $result = Database::fetch_row($sql_result);
  84. return $htmlcode ? html_entity_decode($result[0]) : $result[0];
  85. } else {
  86. return get_lang('Login failed - incorrect login or password.');
  87. }
  88. }
  89. public function get_message_id_sent(
  90. $username,
  91. $password,
  92. $from,
  93. $number_of_items
  94. ) {
  95. if ($this->verifyUserPass($username, $password) == "valid") {
  96. $user_id = UserManager::get_user_id_from_username($username);
  97. $table_message = Database::get_main_table(TABLE_MESSAGE);
  98. $sql_query = "SELECT id FROM $table_message
  99. WHERE user_sender_id=".$user_id." AND msg_status=".MESSAGE_STATUS_OUTBOX."
  100. ORDER BY send_date LIMIT $from,$number_of_items";
  101. $sql_result = Database::query($sql_query);
  102. $message = "#";
  103. while ($result = Database::fetch_row($sql_result)) {
  104. $message .= $result[0]."#";
  105. }
  106. return $message;
  107. } else {
  108. return get_lang('Login failed - incorrect login or password.');
  109. }
  110. }
  111. public function get_message_data_sent($username, $password, $id, $field)
  112. {
  113. if ($this->verifyUserPass($username, $password) == "valid") {
  114. $htmlcode = false;
  115. switch ($field) {
  116. case 'sender':
  117. $field_table = "user_sender_id";
  118. break;
  119. case 'title':
  120. $htmlcode = true;
  121. $field_table = "title";
  122. break;
  123. case 'date':
  124. $field_table = "send_date";
  125. break;
  126. case 'status':
  127. $field_table = "msg_status";
  128. break;
  129. case 'content':
  130. $htmlcode = true;
  131. $field_table = "content";
  132. break;
  133. default:
  134. $field_table = "title";
  135. }
  136. $user_id = UserManager::get_user_id_from_username($username);
  137. $table_message = Database::get_main_table(TABLE_MESSAGE);
  138. $sql_query = "SELECT ".$field_table." FROM $table_message ".
  139. " WHERE user_sender_id=".$user_id." AND id=".$id;
  140. $sql_result = Database::query($sql_query);
  141. $result = Database::fetch_row($sql_result);
  142. return $htmlcode ? html_entity_decode($result[0]) : $result[0];
  143. } else {
  144. return get_lang('Login failed - incorrect login or password.');
  145. }
  146. }
  147. public function message_send(
  148. $username,
  149. $password,
  150. $receiver_user_id,
  151. $subject,
  152. $content
  153. ) {
  154. //TODO: verificar data de envio. Esta divergindo de data!
  155. if ($this->verifyUserPass($username, $password) == "valid") {
  156. $group_id = intval(0);
  157. $parent_id = intval(0);
  158. $edit_message_id = intval(0);
  159. $sent_email = false;
  160. $user_sender_id = UserManager::get_user_id_from_username($username);
  161. $subject = htmlentities($subject);
  162. $content = htmlentities($content);
  163. $table_message = Database::get_main_table(TABLE_MESSAGE);
  164. $query = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
  165. " VALUES ('$user_sender_id', '$receiver_user_id', '1', '".api_get_utc_datetime()."','$subject','$content','$group_id','$parent_id', '".api_get_utc_datetime()."')";
  166. Database::query($query);
  167. $query = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) ".
  168. " VALUES ('$user_sender_id', '$receiver_user_id', '4', '".api_get_utc_datetime()."','$subject','$content','$group_id','$parent_id', '".api_get_utc_datetime()."')";
  169. Database::query($query);
  170. $inbox_last_id = Database::insert_id();
  171. return $inbox_last_id;
  172. } else {
  173. return get_lang('Login failed - incorrect login or password.');
  174. }
  175. }
  176. protected function set_message_as_read($user_id, $message_id)
  177. {
  178. $table_message = Database::get_main_table(TABLE_MESSAGE);
  179. $query = "UPDATE $table_message SET msg_status = '".MESSAGE_STATUS_NEW."'
  180. WHERE user_receiver_id=".$user_id." AND id='".$message_id."';";
  181. Database::query($query);
  182. }
  183. }
  184. /*
  185. echo "aqui: ";
  186. $aqui = new WSCMInbox();
  187. //print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
  188. print_r($aqui->message_send("aluno", "356a192b7913b04c54574d18c28d46e6395428ab", "1", "Título da mensagem", "Conteúdo da mensagem com ç ã"));
  189. */