MDL-37801 mod_glossary - encode / decode links to 'showentry' pages during backup...
[moodle.git] / mod / chat / chat_ajax.php
blobd9e6b374829d75b64af3d8c46e15c24cf234d51c
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_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
20 require_once(dirname(__FILE__) . '/lib.php');
22 $action = optional_param('action', '', PARAM_ALPHANUM);
23 $beep_id = optional_param('beep', '', PARAM_RAW);
24 $chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
25 $theme = required_param('theme', PARAM_ALPHANUM);
26 $chat_message = optional_param('chat_message', '', PARAM_RAW);
27 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
28 $chat_lastrow = 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'=>$chat_sid))) {
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 // setup $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'=>$chat_sid));
55 ob_start();
56 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
57 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
58 header('Cache-Control: no-cache, must-revalidate');
59 header('Pragma: no-cache');
60 header('Content-Type: text/html; charset=utf-8');
62 switch ($action) {
63 case 'init':
64 $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
65 $users = chat_format_userlist($users, $course);
66 $response['users'] = $users;
67 echo json_encode($response);
68 break;
70 case 'chat':
71 session_get_instance()->write_close();
72 chat_delete_old_users();
73 $chat_message = clean_text($chat_message, FORMAT_MOODLE);
75 if (!empty($beep_id)) {
76 $chat_message = 'beep '.$beep_id;
79 if (!empty($chat_message)) {
80 $message = new stdClass();
81 $message->chatid = $chatuser->chatid;
82 $message->userid = $chatuser->userid;
83 $message->groupid = $chatuser->groupid;
84 $message->message = $chat_message;
85 $message->timestamp = time();
87 $chatuser->lastmessageping = time() - 2;
88 $DB->update_record('chat_users', $chatuser);
90 $DB->insert_record('chat_messages', $message);
91 $DB->insert_record('chat_messages_current', $message);
92 // response ok message
93 echo json_encode(true);
94 add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id);
96 ob_end_flush();
98 break;
100 case 'update':
101 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
102 chat_delete_old_users();
105 if ($latest_message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
106 $chat_newlasttime = $latest_message->timestamp;
107 } else {
108 $chat_newlasttime = 0;
111 if ($chat_lasttime == 0) {
112 $chat_lasttime = time() - $CFG->chat_old_ping;
115 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
117 $groupselect = $chatuser->groupid ? " AND (groupid=".$chatuser->groupid." OR groupid=0) " : "";
119 $messages = $DB->get_records_select('chat_messages_current',
120 'chatid = :chatid AND timestamp > :lasttime '.$groupselect, $params,
121 'timestamp ASC');
123 if (!empty($messages)) {
124 $num = count($messages);
125 } else {
126 $num = 0;
128 $chat_newrow = ($chat_lastrow + $num) % 2;
129 $send_user_list = false;
130 if ($messages && ($chat_lasttime != $chat_newlasttime)) {
131 foreach ($messages as $n => &$message) {
132 $tmp = new stdClass();
133 // when somebody enter room, user list will be updated
134 if (!empty($message->system)){
135 $send_user_list = true;
136 $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
138 if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {
139 $message->mymessage = ($USER->id == $message->userid);
140 $message->message = $html->html;
141 if (!empty($html->type)) {
142 $message->type = $html->type;
144 } else {
145 unset($messages[$n]);
150 if(!empty($users) && $send_user_list){
151 // return users when system message coming
152 $response['users'] = $users;
155 $DB->set_field('chat_users', 'lastping', time(), array('id'=>$chatuser->id));
157 $response['lasttime'] = $chat_newlasttime;
158 $response['lastrow'] = $chat_newrow;
159 if($messages){
160 $response['msgs'] = $messages;
163 echo json_encode($response);
164 header('Content-Length: ' . ob_get_length() );
166 ob_end_flush();
167 break;
169 default:
170 break;