chat_chat.php 6.2 KB

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