message.lib.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) Julio Montoya <gugli100@gmail.com>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  15. Mail: info@dokeos.com
  16. ==============================================================================
  17. */
  18. include_once(api_get_path(LIBRARY_PATH).'/online.inc.php');
  19. $table_message = Database::get_course_table(TABLE_MESSAGE);
  20. function get_online_user_list($current_user_id)
  21. {
  22. $min=30;
  23. global $_configuration;
  24. $userlist = WhoIsOnline($current_user_id,$_configuration['statistics_database'],$min);
  25. foreach($userlist as $row) {
  26. $receiver_id = $row[0];
  27. $online_user_list[$receiver_id] = GetFullUserName($receiver_id).($current_user_id==$receiver_id?("&nbsp;(".get_lang('Myself').")"):(""));
  28. }
  29. return $online_user_list;
  30. }
  31. /**
  32. * Displays info stating that the message is sent successfully.
  33. */
  34. function display_success_message($uid)
  35. {
  36. $success= get_lang('MessageSentTo').
  37. "&nbsp;<b>".
  38. GetFullUserName($uid).
  39. "</b>".
  40. "<br><a href=\"".
  41. "inbox.php\">".
  42. get_lang('BackToInbox').
  43. "</a>";
  44. Display::display_confirmation_message($success, false);
  45. }
  46. /**
  47. * @todo this function seems no longer user
  48. * but is still mentioned in comments, what can be the use?
  49. */
  50. function validate_text($texto)
  51. {
  52. $MAX_SIZE = 60; // minimun size of chars
  53. $i=0;
  54. $lines = array(); //array with lines
  55. $token = strtok($texto, "\n");
  56. while($token) {
  57. $lines[$i]= $token;
  58. $token = strtok("\n");
  59. $i++;
  60. }
  61. $modificado= "";
  62. for($i=0; $i<count($lines); $i++ ) {
  63. if(strlen($lines[$i])>$MAX_SIZE + 1) {
  64. $modificado2= substr($lines[$i], 0, $MAX_SIZE);
  65. for($j=$MAX_SIZE; $j<strlen($lines[$i]); $j+=$MAX_SIZE) {
  66. $modificado2 = $modificado2."\n".substr($lines[$i], $j, $MAX_SIZE);
  67. }
  68. } else {
  69. $modificado2= $lines[$i];
  70. }
  71. $modificado = $modificado.$modificado2."\n";
  72. }
  73. $modificado = substr($modificado, 0 ,strlen($modificado)-1);
  74. $modificado = str_replace("&", "&#038", $modificado); // �em
  75. $modificado = str_replace("<", "&#60", $modificado); // para evitar que lo convierta en html
  76. $modificado = str_replace(">", "&#62", $modificado); // �em
  77. return $modificado;
  78. }
  79. /**
  80. * Displays the wysiwyg html editor.
  81. */
  82. function display_html_editor_area($name,$resp)
  83. {
  84. api_disp_html_area($name, get_lang('TypeYourMessage'), '', '100%');
  85. }
  86. /**
  87. * Get the new messages for the current user from the database.
  88. */
  89. function get_new_messages()
  90. {
  91. global $table_message;
  92. if (!api_get_user_id()) {
  93. return false;
  94. }
  95. $i=0;
  96. $query = "SELECT * FROM $table_message WHERE user_receiver_id=".api_get_user_id()." AND msg_status=1;";
  97. $result = api_sql_query($query,__FILE__,__LINE__);
  98. $i = Database::num_rows($result);
  99. return $i;
  100. }
  101. /**
  102. * Get the list of user_ids of users who are online.
  103. */
  104. function users_connected_by_id()
  105. {
  106. global $_configuration, $_user;
  107. $minute=30;
  108. $user_connect = WhoIsOnline($_user['user_id'],$_configuration['statistics_database'],$minute);
  109. for ($i=0; $i<count($user_connect); $i++) {
  110. $user_id_list[$i]=$user_connect[$i][0];
  111. }
  112. return $user_id_list;
  113. }
  114. /**
  115. * Gets the total number of messages, used for the inbox sortable table
  116. */
  117. function get_number_of_messages()
  118. {
  119. global $table_message;
  120. $sql_query = "SELECT COUNT(*) as number_messages FROM $table_message WHERE user_receiver_id=".api_get_user_id();
  121. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  122. $result = Database::fetch_array($sql_result);
  123. return $result['number_messages'];
  124. }
  125. /**
  126. * Gets information about some messages, used for the inbox sortable table
  127. * @param int $from
  128. * @param int $number_of_items
  129. * @param string $direction
  130. */
  131. function get_message_data($from, $number_of_items, $column, $direction)
  132. {
  133. global $table_message;
  134. $sql_query = "SELECT id as col0, user_sender_id as col1, title as col2, send_date as col3 FROM $table_message " .
  135. "WHERE user_receiver_id=".api_get_user_id()." " .
  136. "ORDER BY col$column $direction LIMIT $from,$number_of_items";
  137. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  138. $i = 0;
  139. $message_list = array ();
  140. while ($result = Database::fetch_row($sql_result)) {
  141. $message[0] = $result[0];
  142. $message[1] = GetFullUserName($result[1]);
  143. //$message[1] = "<a href=\"view_message.php?id=".$result[0]."\" title=\"$texto\">".GetFullUserName($result[1])."</a>";
  144. $message[2] = '<a href="view_message.php?id='.$result[0].'">'.$result[2].'</a>';
  145. $message[3] = $result[3]; //date stays the same
  146. $message[4] = '<a href="new_message.php?re_id='.$result[0].'">'.Display::return_icon('message_reply.png',get_lang('ReplyToMessage')).'</a>'.
  147. '&nbsp;&nbsp;<a href="inbox.php?action=deleteone&id='.$result[0].'" onclick="javascript:if(!confirm('."'".addslashes(htmlentities(get_lang('ConfirmDeleteMessage')))."'".')) return false;">'.Display::return_icon('message_delete.png',get_lang('DeleteMessage')).'</a>';
  148. $message_list[] = $message;
  149. $i++;
  150. }
  151. return $message_list;
  152. }
  153. /**
  154. * Displays the inbox of a user, listing all messages.
  155. * In the process of moving towards sortable table.
  156. */
  157. function inbox_display()
  158. {
  159. global $table_message;
  160. //delete messages if delete action was chosen
  161. if (isset ($_REQUEST['action']))
  162. {
  163. switch ($_REQUEST['action']) {
  164. case 'delete' :
  165. $number_of_selected_messages = count($_POST['id']);
  166. foreach ($_POST['id'] as $index => $message_id) {
  167. delete_message_by_user_receiver(api_get_user_id(), $message_id);
  168. }
  169. Display::display_normal_message(get_lang('SelectedMessagesDeleted'));
  170. break;
  171. case 'deleteone' :
  172. delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
  173. Display::display_confirmation_message(get_lang('MessageDeleted'));
  174. echo '<br / >';
  175. break;
  176. }
  177. }
  178. // display sortable table with messages of the current user
  179. $table = new SortableTable('messages', 'get_number_of_messages', 'get_message_data', 1);
  180. $table->set_header(0, '', false);
  181. $table->set_header(1, get_lang('From'));
  182. $table->set_header(2, get_lang('Title'));
  183. $table->set_header(3, get_lang('Date'));
  184. $table->set_header(4, get_lang('Actions'), false);
  185. $table->set_form_actions(array ('delete' => get_lang('DeleteSelectedMessages')));
  186. $table->display();
  187. }
  188. function send_message($receiver_user_id, $title, $content)
  189. {
  190. global $table_message;
  191. $query = "INSERT INTO $table_message(user_sender_id, user_receiver_id, msg_status, send_date, title, content ) ".
  192. " VALUES (".
  193. "'".api_get_user_id()."', '".Database::escape_string($receiver_user_id)."', '1', '".date('Y-m-d H:i:s')."','".Database::escape_string($title)."','".Database::escape_string($content)."'".
  194. ");";
  195. $result = api_sql_query($query,__FILE__,__LINE__);
  196. return $result;
  197. }
  198. function delete_message_by_user_receiver($user_receiver_id,$id)
  199. {
  200. global $table_message;
  201. $query = "DELETE FROM $table_message " .
  202. "WHERE user_receiver_id=".Database::escape_string($user_receiver_id)." AND id=".Database::escape_string($id);
  203. $result = api_sql_query($query,__FILE__,__LINE__);
  204. return $result;
  205. }
  206. ?>