chat_functions.lib.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.chat
  5. */
  6. use Michelf\MarkdownExtra;
  7. /**
  8. * @author isaac flores paz
  9. * @param integer $user_id
  10. *
  11. * @return boolean
  12. * @todo this function need more parameters seems not to be use anymore
  13. * @deprecated fix this function or create another
  14. */
  15. function user_connected_in_chat($user_id)
  16. {
  17. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  18. $user_id = intval($user_id);
  19. $session_id = api_get_session_id();
  20. $group_id = api_get_group_id();
  21. $course_id = api_get_course_int_id();
  22. if (!empty($group_id)) {
  23. $extra_condition = " AND to_group_id = '$group_id'";
  24. } else {
  25. $extra_condition = api_get_session_condition($session_id);
  26. }
  27. $sql = 'SELECT COUNT(*) AS count
  28. FROM '.$tbl_chat_connected .' c
  29. WHERE
  30. c_id = '.$course_id.' AND
  31. user_id='.$user_id.$extra_condition;
  32. $result = Database::query($sql);
  33. $count = Database::fetch_array($result,'ASSOC');
  34. return $count['count'] == 1;
  35. }
  36. /**
  37. * @param integer
  38. * @return void
  39. */
  40. function exit_of_chat($user_id)
  41. {
  42. $user_id = intval($user_id);
  43. $list_course = CourseManager::get_courses_list_by_user_id($user_id);
  44. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  45. foreach ($list_course as $course) {
  46. $response = user_connected_in_chat($user_id);
  47. $sql = 'DELETE FROM '.$tbl_chat_connected.'
  48. WHERE c_id = '.$course['real_id'].' AND user_id = '.$user_id;
  49. Database::query($sql);
  50. }
  51. }
  52. /**
  53. * @return void
  54. */
  55. function disconnect_user_of_chat()
  56. {
  57. $list_info_user_in_chat = users_list_in_chat();
  58. $course_id = api_get_course_int_id();
  59. $groupId = api_get_group_id();
  60. $now = time();
  61. $cd_date = date('Y-m-d', $now);
  62. $cdate_h = date('H', $now);
  63. $cdate_m = date('i', $now);
  64. $cdate_s = date('s', $now);
  65. $cd_count_time_seconds = $cdate_h*3600 + $cdate_m*60 + $cdate_s;
  66. if (is_array($list_info_user_in_chat) && count($list_info_user_in_chat) > 0) {
  67. foreach ($list_info_user_in_chat as $list_info_user) {
  68. $date_db_date = date('Y-m-d', api_strtotime($list_info_user['last_connection'], 'UTC'));
  69. $date_db_h = date('H', api_strtotime($list_info_user['last_connection'], 'UTC'));
  70. $date_db_m = date('i', api_strtotime($list_info_user['last_connection'], 'UTC'));
  71. $date_db_s = date('s', api_strtotime($list_info_user['last_connection'], 'UTC'));
  72. $date_count_time_seconds = $date_db_h * 3600 + $date_db_m * 60 + $date_db_s;
  73. if ($cd_date == $date_db_date) {
  74. if (($cd_count_time_seconds - $date_count_time_seconds) > 5) {
  75. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  76. $sql = 'DELETE FROM '.$tbl_chat_connected.'
  77. WHERE
  78. c_id = '.$course_id.' AND
  79. user_id = '.$list_info_user['user_id'].' AND
  80. to_group_id = '.$groupId.'
  81. ';
  82. Database::query($sql);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. /**
  89. * @return array user list in chat
  90. */
  91. function users_list_in_chat()
  92. {
  93. $list_users_in_chat = array();
  94. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  95. $course_id = api_get_course_int_id();
  96. $session_id = api_get_session_id();
  97. $group_id = api_get_group_id();
  98. if (!empty($group_id)) {
  99. $extra_condition = " WHERE to_group_id = '$group_id'";
  100. } else{
  101. $extra_condition = api_get_session_condition($session_id, false);
  102. }
  103. $extra_condition.= " AND c_id = $course_id ";
  104. $sql = 'SELECT user_id, last_connection FROM '.$tbl_chat_connected.$extra_condition;
  105. $result = Database::query($sql);
  106. while ($row = Database::fetch_array($result, 'ASSOC')) {
  107. $list_users_in_chat[] = $row;
  108. }
  109. return $list_users_in_chat;
  110. }
  111. /**
  112. * @param string $message
  113. * @param array $_course
  114. * @param int $group_id
  115. * @param int $session_id
  116. * @param bool $preview
  117. */
  118. function saveMessage($message, $userId, $_course, $session_id, $group_id, $preview = true)
  119. {
  120. $userInfo = api_get_user_info($userId);
  121. $fullName = $userInfo['complete_name'];
  122. $isMaster = (bool)api_is_course_admin();
  123. $document_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  124. if (!empty($group_id)) {
  125. $group_info = GroupManager :: get_group_properties($group_id);
  126. $basepath_chat = $group_info['directory'].'/chat_files';
  127. } else {
  128. $basepath_chat = '/chat_files';
  129. }
  130. $chat_path = $document_path.$basepath_chat.'/';
  131. if (!is_dir($chat_path)) {
  132. if (is_file($chat_path)) {
  133. @unlink($chat_path);
  134. }
  135. }
  136. $date_now = date('Y-m-d');
  137. $message = trim($message);
  138. $timeNow = date('d/m/y H:i:s');
  139. if (!empty($group_id)) {
  140. $basename_chat = 'messages-'.$date_now.'_gid-'.$group_id;
  141. } elseif (!empty($session_id)) {
  142. $basename_chat = 'messages-'.$date_now.'_sid-'.$session_id;
  143. } else {
  144. $basename_chat = 'messages-'.$date_now;
  145. }
  146. if (!api_is_anonymous()) {
  147. if (!empty($message)) {
  148. Emojione\Emojione::$imagePathPNG = api_get_path(WEB_LIBRARY_PATH).'javascript/emojione/png/';
  149. Emojione\Emojione::$ascii = true;
  150. // Parsing emojis
  151. $message = Emojione\Emojione::toImage($message);
  152. // Parsing text to understand markdown (code highlight)
  153. $message = MarkdownExtra::defaultTransform($message);
  154. // Security XSS
  155. $message = Security::remove_XSS($message);
  156. if ($preview == true) {
  157. return $message;
  158. }
  159. if (!file_exists($chat_path.$basename_chat.'.log.html')) {
  160. $doc_id = add_document(
  161. $_course,
  162. $basepath_chat . '/' . $basename_chat . '.log.html',
  163. 'file',
  164. 0,
  165. $basename_chat . '.log.html'
  166. );
  167. api_item_property_update(
  168. $_course,
  169. TOOL_DOCUMENT,
  170. $doc_id,
  171. 'DocumentAdded',
  172. $userId,
  173. $group_id,
  174. null,
  175. null,
  176. null,
  177. $session_id
  178. );
  179. api_item_property_update(
  180. $_course,
  181. TOOL_DOCUMENT,
  182. $doc_id,
  183. 'invisible',
  184. $userId,
  185. $group_id,
  186. null,
  187. null,
  188. null,
  189. $session_id
  190. );
  191. item_property_update_on_folder(
  192. $_course,
  193. $basepath_chat,
  194. $userId
  195. );
  196. } else {
  197. $doc_id = DocumentManager::get_document_id(
  198. $_course,
  199. $basepath_chat.'/'.$basename_chat.'.log.html'
  200. );
  201. }
  202. $fp = fopen($chat_path.$basename_chat.'.log.html', 'a');
  203. $userPhoto = UserManager::getUserPicture($userId, USER_IMAGE_SIZE_MEDIUM);
  204. $filePhoto = '<img class="chat-image" src="'.$userPhoto.'"/>';
  205. if ($isMaster) {
  206. fputs($fp, '<div class="message-teacher"><div class="content-message"><div class="chat-message-block-name">'.$fullName.'</div><div class="chat-message-block-content">'.$message.'</div><div class="message-date">'.$timeNow.'</div></div><div class="icon-message"></div>'.$filePhoto.'</div>'."\n");
  207. } else {
  208. fputs($fp, '<div class="message-student">'.$filePhoto.'<div class="icon-message"></div><div class="content-message"><div class="chat-message-block-name">'.$fullName.'</div><div class="chat-message-block-content">'.$message.'</div><div class="message-date">'.$timeNow.'</div></div></div>'."\n");
  209. }
  210. fclose($fp);
  211. $chat_size = filesize($chat_path.$basename_chat.'.log.html');
  212. update_existing_document($_course, $doc_id, $chat_size);
  213. item_property_update_on_folder($_course, $basepath_chat, $userId);
  214. }
  215. }
  216. }