cm_webservice_inbox.php 7.9 KB

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