Merge branch 'MDL-79186-401' of https://github.com/paulholden/moodle into MOODLE_401_...
[moodle.git] / mod / chat / chat_ajax.php
blobb925808d6571c637efc734a2b257d3e4591a845f
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.
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/>.
17 define('AJAX_SCRIPT', true);
19 require(__DIR__.'/../../config.php');
20 require_once(__DIR__ . '/lib.php');
22 $action = optional_param('action', '', PARAM_ALPHANUM);
23 $beepid = optional_param('beep', '', PARAM_RAW);
24 $chatsid = required_param('chat_sid', PARAM_ALPHANUM);
25 $theme = required_param('chat_theme', PARAM_ALPHANUMEXT);
26 $chatmessage = optional_param('chat_message', '', PARAM_RAW);
27 $chatlasttime = optional_param('chat_lasttime', 0, PARAM_INT);
28 $chatlastrow = optional_param('chat_lastrow', 1, PARAM_INT);
30 if (!confirm_sesskey()) {
31 throw new moodle_exception('invalidsesskey', 'error');
34 if (!$chatuser = $DB->get_record('chat_users', array('sid' => $chatsid))) {
35 throw new moodle_exception('notlogged', 'chat');
37 if (!$chat = $DB->get_record('chat', array('id' => $chatuser->chatid))) {
38 throw new moodle_exception('invaliduserid', 'error');
40 if (!$course = $DB->get_record('course', array('id' => $chat->course))) {
41 throw new moodle_exception('invalidcourseid', 'error');
43 if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
44 throw new moodle_exception('invalidcoursemodule', 'error');
47 if (!isloggedin()) {
48 throw new moodle_exception('notlogged', 'chat');
51 // Set up $PAGE so that format_text will work properly.
52 $PAGE->set_cm($cm, $course, $chat);
53 $PAGE->set_url('/mod/chat/chat_ajax.php', array('chat_sid' => $chatsid));
55 require_login($course, false, $cm);
57 $context = context_module::instance($cm->id);
58 require_capability('mod/chat:chat', $context);
60 ob_start();
61 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
62 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
63 header('Cache-Control: no-cache, must-revalidate');
64 header('Pragma: no-cache');
65 header('Content-Type: text/html; charset=utf-8');
67 switch ($action) {
68 case 'init':
69 $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
70 $users = chat_format_userlist($users, $course);
71 $response['users'] = $users;
72 echo json_encode($response);
73 break;
75 case 'chat':
76 \core\session\manager::write_close();
77 chat_delete_old_users();
78 $chatmessage = clean_text($chatmessage, FORMAT_MOODLE);
80 if (!empty($beepid)) {
81 $chatmessage = 'beep '.$beepid;
84 if (!empty($chatmessage)) {
86 chat_send_chatmessage($chatuser, $chatmessage, 0, $cm);
88 $chatuser->lastmessageping = time() - 2;
89 $DB->update_record('chat_users', $chatuser);
91 // Response OK message.
92 echo json_encode(true);
93 ob_end_flush();
95 break;
97 case 'update':
98 if ((time() - $chatlasttime) > $CFG->chat_old_ping) {
99 chat_delete_old_users();
102 if ($latestmessage = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
103 $chatnewlasttime = $latestmessage->timestamp;
104 } else {
105 $chatnewlasttime = 0;
108 if ($chatlasttime == 0) {
109 $chatlasttime = time() - $CFG->chat_old_ping;
112 $messages = chat_get_latest_messages($chatuser, $chatlasttime);
114 if (!empty($messages)) {
115 $num = count($messages);
116 } else {
117 $num = 0;
119 $chatnewrow = ($chatlastrow + $num) % 2;
120 $senduserlist = false;
121 if ($messages && ($chatlasttime != $chatnewlasttime)) {
122 foreach ($messages as $n => &$message) {
123 $tmp = new stdClass();
124 // When somebody enter room, user list will be updated.
125 if (!empty($message->issystem)) {
126 $senduserlist = true;
128 if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {
129 $message->mymessage = ($USER->id == $message->userid);
130 $message->message = $html->html;
131 if (!empty($html->type)) {
132 $message->type = $html->type;
134 } else {
135 unset($messages[$n]);
140 if ($senduserlist) {
141 // Return users when system message arrives.
142 $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
143 $response['users'] = $users;
146 $DB->set_field('chat_users', 'lastping', time(), array('id' => $chatuser->id));
148 $response['lasttime'] = $chatnewlasttime;
149 $response['lastrow'] = $chatnewrow;
150 if ($messages) {
151 $response['msgs'] = $messages;
154 echo json_encode($response);
155 header('Content-Length: ' . ob_get_length());
157 ob_end_flush();
158 break;
160 default:
161 break;