chat_chat.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(
  117. $_course,
  118. TOOL_DOCUMENT,
  119. $doc_id,
  120. 'DocumentAdded',
  121. $userId,
  122. $group_id,
  123. null,
  124. null,
  125. null,
  126. $session_id
  127. );
  128. api_item_property_update(
  129. $_course,
  130. TOOL_DOCUMENT,
  131. $doc_id,
  132. 'invisible',
  133. $userId,
  134. $group_id,
  135. null,
  136. null,
  137. null,
  138. $session_id
  139. );
  140. item_property_update_on_folder($_course, $basepath_chat, $userId);
  141. $doc_id = DocumentManager::get_document_id(
  142. $_course,
  143. $basepath_chat.'/'.$basename_chat.'.log.html'
  144. );
  145. update_existing_document($_course, $doc_id, 0);
  146. }
  147. $remove = 0;
  148. $content = array();
  149. if (file_exists($chat_path.$basename_chat.'.log.html')) {
  150. $content = file($chat_path.$basename_chat.'.log.html');
  151. $nbr_lines = sizeof($content);
  152. $remove = $nbr_lines - 100;
  153. }
  154. if ($remove < 0) {
  155. $remove = 0;
  156. }
  157. array_splice($content, 0, $remove);
  158. require 'header_frame.inc.php';
  159. if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonline') {
  160. //the caller
  161. $content[0] = get_lang('CallSent').'<br />'.$content[0];
  162. }
  163. if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonlinejoin') {
  164. //the joiner (we have to delete the chat request to him when he joins the chat)
  165. $track_user_table = Database::get_main_table(TABLE_MAIN_USER);
  166. $sql = "UPDATE $track_user_table SET
  167. chatcall_user_id = '',
  168. chatcall_date = '',
  169. chatcall_text=''
  170. WHERE (user_id = ".$userId.")";
  171. $result = Database::query($sql);
  172. }
  173. echo '<div id="content-chat markdown-body">';
  174. foreach ($content as & $this_line) {
  175. echo $this_line;
  176. }
  177. echo '</div>';
  178. echo '<a name="bottom" style="text-decoration:none;">&nbsp;</a>';
  179. if ($isMaster || $is_courseCoach) {
  180. $rand = mt_rand(1, 1000);
  181. echo '<div id="clear-chat">';
  182. 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;">'.
  183. get_lang('ClearList').
  184. '</a>';
  185. echo '</div>';
  186. }
  187. } else {
  188. echo '</div>';
  189. require 'header_frame.inc.php';
  190. $message = get_lang('CloseOtherSession');
  191. Display :: display_error_message($message);
  192. }
  193. require 'footer_frame.inc.php';