cm_webservice_inbox.php 7.7 KB

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