cm_webservice_inbox.php 7.8 KB

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