chat_chat.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chat frame that shows the message list
  5. *
  6. * @author Olivier Brouckaert
  7. * @package chamilo.chat
  8. */
  9. define('FRAME', 'chat');
  10. //require_once '../inc/global.inc.php';
  11. $session_id = api_get_session_id();
  12. $group_id = api_get_group_id();
  13. $userId = api_get_user_id();
  14. $_course = api_get_course_info();
  15. $time = api_get_utc_datetime();
  16. // if we have the session set up
  17. if (!empty($_course)) {
  18. $reset = isset($_GET['reset']) ? (bool)$_GET['reset'] : null;
  19. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  20. $query = "SELECT username FROM $tbl_user WHERE user_id='".$userId."'";
  21. $result = Database::query($query);
  22. list($pseudo_user) = Database::fetch_row($result);
  23. $isAllowed = !(empty($pseudo_user));
  24. $isMaster = (bool)api_is_course_admin();
  25. $date_now = date('Y-m-d');
  26. $basepath_chat = '';
  27. $document_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  28. if (!empty($group_id)) {
  29. $group_info = GroupManager :: get_group_properties($group_id);
  30. $basepath_chat = $group_info['directory'].'/chat_files';
  31. } else {
  32. $basepath_chat = '/chat_files';
  33. }
  34. $chat_path = $document_path.$basepath_chat.'/';
  35. $TABLEITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  36. $course_id = api_get_course_int_id();
  37. if (!is_dir($chat_path)) {
  38. if (is_file($chat_path)) {
  39. @unlink($chat_path);
  40. }
  41. if (!api_is_anonymous()) {
  42. @mkdir($chat_path, api_get_permissions_for_new_directories());
  43. // Save chat files document for group into item property
  44. if (!empty($group_id)) {
  45. $doc_id = add_document($_course, $basepath_chat, 'folder', 0, 'chat_files');
  46. api_item_property_update(
  47. $_course,
  48. TOOL_DOCUMENT,
  49. $doc_id,
  50. 'FolderCreated',
  51. null,
  52. $group_id,
  53. null,
  54. null,
  55. null
  56. );
  57. }
  58. }
  59. }
  60. $filename_chat = '';
  61. if (!empty($group_id)) {
  62. $filename_chat = 'messages-'.$date_now.'_gid-'.$group_id.'.log.html';
  63. } else if (!empty($session_id)) {
  64. $filename_chat = 'messages-'.$date_now.'_sid-'.$session_id.'.log.html';
  65. } else {
  66. $filename_chat = 'messages-'.$date_now.'.log.html';
  67. }
  68. if (!file_exists($chat_path.$filename_chat)) {
  69. @fclose(fopen($chat_path.$filename_chat, 'w'));
  70. if (!api_is_anonymous()) {
  71. $doc_id = add_document($_course, $basepath_chat.'/'.$filename_chat, 'file', 0, $filename_chat);
  72. api_item_property_update(
  73. $_course,
  74. TOOL_DOCUMENT,
  75. $doc_id,
  76. 'DocumentAdded',
  77. $userId,
  78. $group_id,
  79. null,
  80. null,
  81. null,
  82. $session_id
  83. );
  84. api_item_property_update(
  85. $_course,
  86. TOOL_DOCUMENT,
  87. $doc_id,
  88. 'invisible',
  89. $userId,
  90. $group_id,
  91. null,
  92. null,
  93. null,
  94. $session_id
  95. );
  96. item_property_update_on_folder($_course, $basepath_chat, $userId);
  97. }
  98. }
  99. $basename_chat = '';
  100. if (!empty($group_id)) {
  101. $basename_chat = 'messages-'.$date_now.'_gid-'.$group_id;
  102. } else if (!empty($session_id)) {
  103. $basename_chat = 'messages-'.$date_now.'_sid-'.$session_id;
  104. } else {
  105. $basename_chat = 'messages-'.$date_now;
  106. }
  107. if ($reset && $isMaster) {
  108. $i = 1;
  109. while (file_exists($chat_path.$basename_chat.'-'.$i.'.log.html')) {
  110. $i++;
  111. }
  112. @rename($chat_path.$basename_chat.'.log.html', $chat_path.$basename_chat.'-'.$i.'.log.html');
  113. @fclose(fopen($chat_path.$basename_chat.'.log.html', 'w'));
  114. $doc_id = add_document($_course, $basepath_chat.'/'.$basename_chat.'-'.$i.'.log.html', 'file', filesize($chat_path.$basename_chat.'-'.$i.'.log.html'), $basename_chat.'-'.$i.'.log.html');
  115. api_item_property_update(
  116. $_course,
  117. TOOL_DOCUMENT,
  118. $doc_id,
  119. 'DocumentAdded',
  120. $userId,
  121. $group_id,
  122. null,
  123. null,
  124. null,
  125. $session_id
  126. );
  127. api_item_property_update(
  128. $_course,
  129. TOOL_DOCUMENT,
  130. $doc_id,
  131. 'invisible',
  132. $userId,
  133. $group_id,
  134. null,
  135. null,
  136. null,
  137. $session_id
  138. );
  139. item_property_update_on_folder($_course, $basepath_chat, $userId);
  140. $doc_id = DocumentManager::get_document_id(
  141. $_course,
  142. $basepath_chat.'/'.$basename_chat.'.log.html'
  143. );
  144. update_existing_document($_course, $doc_id, 0);
  145. }
  146. $remove = 0;
  147. $content = array();
  148. if (file_exists($chat_path.$basename_chat.'.log.html')) {
  149. $content = file($chat_path.$basename_chat.'.log.html');
  150. $nbr_lines = sizeof($content);
  151. $remove = $nbr_lines - 100;
  152. }
  153. if ($remove < 0) {
  154. $remove = 0;
  155. }
  156. array_splice($content, 0, $remove);
  157. require 'header_frame.inc.php';
  158. if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonline') {
  159. //the caller
  160. $content[0] = get_lang('CallSent').'<br />'.$content[0];
  161. }
  162. if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonlinejoin') {
  163. //the joiner (we have to delete the chat request to him when he joins the chat)
  164. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  165. $sql = "UPDATE $track_user_table SET
  166. chatcall_user_id = '',
  167. chatcall_date = '',
  168. chatcall_text=''
  169. WHERE (user_id = ".$userId.")";
  170. $result = Database::query($sql);
  171. }
  172. echo '<div id="content-chat"';
  173. foreach ($content as & $this_line) {
  174. echo $this_line;
  175. }
  176. echo '</div>';
  177. echo '<a name="bottom" style="text-decoration:none;">&nbsp;</a>';
  178. if ($isMaster || $is_courseCoach) {
  179. $rand = mt_rand(1, 1000);
  180. echo '<div id="clear-chat">';
  181. echo '<a class="btn btn-danger btn-small " href="'.api_get_self().'?rand='.$rand.'&reset=1&'.api_get_cidreq().'#bottom" onclick="javascript: if(!confirm(\''.addslashes(api_htmlentities(get_lang('ConfirmReset'), ENT_QUOTES)).'\')) return false;">'.
  182. get_lang('ClearList').
  183. '</a>';
  184. echo '</div>';
  185. }
  186. } else {
  187. echo '</div>';
  188. require 'header_frame.inc.php';
  189. $message = get_lang('CloseOtherSession');
  190. Display :: display_error_message($message);
  191. }
  192. require 'footer_frame.inc.php';