PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/chat/chat_ajax.php

http://github.com/moodle/moodle
PHP | 162 lines | 116 code | 28 blank | 18 comment | 26 complexity | 5ff203ab60db1cb575c1301e40dbd0f6 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. define('AJAX_SCRIPT', true);
  17. require(__DIR__.'/../../config.php');
  18. require_once(__DIR__ . '/lib.php');
  19. $action = optional_param('action', '', PARAM_ALPHANUM);
  20. $beepid = optional_param('beep', '', PARAM_RAW);
  21. $chatsid = required_param('chat_sid', PARAM_ALPHANUM);
  22. $theme = required_param('theme', PARAM_ALPHANUMEXT);
  23. $chatmessage = optional_param('chat_message', '', PARAM_RAW);
  24. $chatlasttime = optional_param('chat_lasttime', 0, PARAM_INT);
  25. $chatlastrow = optional_param('chat_lastrow', 1, PARAM_INT);
  26. if (!confirm_sesskey()) {
  27. throw new moodle_exception('invalidsesskey', 'error');
  28. }
  29. if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) {
  30. throw new moodle_exception('notlogged', 'chat');
  31. }
  32. if (!$chat = $DB->get_record('chat', array('id' => $chatuser->chatid))) {
  33. throw new moodle_exception('invaliduserid', 'error');
  34. }
  35. if (!$course = $DB->get_record('course', array('id' => $chat->course))) {
  36. throw new moodle_exception('invalidcourseid', 'error');
  37. }
  38. if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
  39. throw new moodle_exception('invalidcoursemodule', 'error');
  40. }
  41. if (!isloggedin()) {
  42. throw new moodle_exception('notlogged', 'chat');
  43. }
  44. // Set up $PAGE so that format_text will work properly.
  45. $PAGE->set_cm($cm, $course, $chat);
  46. $PAGE->set_url('/mod/chat/chat_ajax.php', array('chat_sid' => $chatsid));
  47. require_login($course, false, $cm);
  48. $context = context_module::instance($cm->id);
  49. require_capability('mod/chat:chat', $context);
  50. ob_start();
  51. header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
  52. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  53. header('Cache-Control: no-cache, must-revalidate');
  54. header('Pragma: no-cache');
  55. header('Content-Type: text/html; charset=utf-8');
  56. switch ($action) {
  57. case 'init':
  58. $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
  59. $users = chat_format_userlist($users, $course);
  60. $response['users'] = $users;
  61. echo json_encode($response);
  62. break;
  63. case 'chat':
  64. \core\session\manager::write_close();
  65. chat_delete_old_users();
  66. $chatmessage = clean_text($chatmessage, FORMAT_MOODLE);
  67. if (!empty($beepid)) {
  68. $chatmessage = 'beep '.$beepid;
  69. }
  70. if (!empty($chatmessage)) {
  71. chat_send_chatmessage($chatuser, $chatmessage, 0, $cm);
  72. $chatuser->lastmessageping = time() - 2;
  73. $DB->update_record('chat_users', $chatuser);
  74. // Response OK message.
  75. echo json_encode(true);
  76. ob_end_flush();
  77. }
  78. break;
  79. case 'update':
  80. if ((time() - $chatlasttime) > $CFG->chat_old_ping) {
  81. chat_delete_old_users();
  82. }
  83. if ($latestmessage = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
  84. $chatnewlasttime = $latestmessage->timestamp;
  85. } else {
  86. $chatnewlasttime = 0;
  87. }
  88. if ($chatlasttime == 0) {
  89. $chatlasttime = time() - $CFG->chat_old_ping;
  90. }
  91. $messages = chat_get_latest_messages($chatuser, $chatlasttime);
  92. if (!empty($messages)) {
  93. $num = count($messages);
  94. } else {
  95. $num = 0;
  96. }
  97. $chatnewrow = ($chatlastrow + $num) % 2;
  98. $senduserlist = false;
  99. if ($messages && ($chatlasttime != $chatnewlasttime)) {
  100. foreach ($messages as $n => &$message) {
  101. $tmp = new stdClass();
  102. // When somebody enter room, user list will be updated.
  103. if (!empty($message->issystem)) {
  104. $senduserlist = true;
  105. }
  106. if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {
  107. $message->mymessage = ($USER->id == $message->userid);
  108. $message->message = $html->html;
  109. if (!empty($html->type)) {
  110. $message->type = $html->type;
  111. }
  112. } else {
  113. unset($messages[$n]);
  114. }
  115. }
  116. }
  117. if ($senduserlist) {
  118. // Return users when system message arrives.
  119. $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
  120. $response['users'] = $users;
  121. }
  122. $DB->set_field('chat_users', 'lastping', time(), array('id' => $chatuser->id));
  123. $response['lasttime'] = $chatnewlasttime;
  124. $response['lastrow'] = $chatnewrow;
  125. if ($messages) {
  126. $response['msgs'] = $messages;
  127. }
  128. echo json_encode($response);
  129. header('Content-Length: ' . ob_get_length());
  130. ob_end_flush();
  131. break;
  132. default:
  133. break;
  134. }