Merge branch 'wip-MDL-34012-master' of git://github.com/marinaglancy/moodle
[moodle.git] / mod / chat / chat_ajax.php
CommitLineData
83064f9c 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
529817c8
DC
17define('AJAX_SCRIPT', true);
18
83064f9c 19require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
20require_once(dirname(__FILE__) . '/lib.php');
21
a8e3b008 22$action = optional_param('action', '', PARAM_ALPHANUM);
83064f9c 23$beep_id = optional_param('beep', '', PARAM_RAW);
24$chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
b661e76c 25$theme = required_param('theme', PARAM_ALPHANUMEXT);
83064f9c 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);
29
467902f2
DC
30if (!confirm_sesskey()) {
31 throw new moodle_exception('invalidsesskey', 'error');
32}
83064f9c 33
34if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
a8e3b008 35 throw new moodle_exception('notlogged', 'chat');
83064f9c 36}
37if (!$chat = $DB->get_record('chat', array('id'=>$chatuser->chatid))) {
a8e3b008 38 throw new moodle_exception('invaliduserid', 'error');
83064f9c 39}
40if (!$course = $DB->get_record('course', array('id'=>$chat->course))) {
a8e3b008 41 throw new moodle_exception('invalidcourseid', 'error');
83064f9c 42}
43if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
a8e3b008 44 throw new moodle_exception('invalidcoursemodule', 'error');
83064f9c 45}
a8e3b008 46
2412f8b8 47if (!isloggedin()) {
a8e3b008 48 throw new moodle_exception('notlogged', 'chat');
83064f9c 49}
50
51// setup $PAGE so that format_text will work properly
52$PAGE->set_cm($cm, $course, $chat);
a6855934 53$PAGE->set_url('/mod/chat/chat_ajax.php', array('chat_sid'=>$chat_sid));
83064f9c 54
5c45ea0c
AD
55require_login($course, false, $cm);
56
57$context = context_module::instance($cm->id);
58require_capability('mod/chat:chat', $context);
59
83064f9c 60ob_start();
61header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
62header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
63header('Cache-Control: no-cache, must-revalidate');
64header('Pragma: no-cache');
65header('Content-Type: text/html; charset=utf-8');
83064f9c 66
67switch ($action) {
68case 'init':
ff0e58c8 69 $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
83064f9c 70 $users = chat_format_userlist($users, $course);
71 $response['users'] = $users;
72 echo json_encode($response);
73 break;
a8e3b008 74
83064f9c 75case 'chat':
d79d5ac2 76 \core\session\manager::write_close();
83064f9c 77 chat_delete_old_users();
78 $chat_message = clean_text($chat_message, FORMAT_MOODLE);
79
80 if (!empty($beep_id)) {
81 $chat_message = 'beep '.$beep_id;
82 }
83
84 if (!empty($chat_message)) {
f49d814a
FM
85
86 chat_send_chatmessage($chatuser, $chat_message, 0, $cm);
83064f9c 87
88 $chatuser->lastmessageping = time() - 2;
89 $DB->update_record('chat_users', $chatuser);
90
f49d814a 91 // Response OK message.
529817c8 92 echo json_encode(true);
83064f9c 93 ob_end_flush();
94 }
95 break;
a8e3b008 96
83064f9c 97case 'update':
98 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
99 chat_delete_old_users();
100 }
101
102 if ($latest_message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
103 $chat_newlasttime = $latest_message->timestamp;
104 } else {
105 $chat_newlasttime = 0;
106 }
107
108 if ($chat_lasttime == 0) {
109 $chat_lasttime = time() - $CFG->chat_old_ping;
110 }
111
112 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
113
114 $groupselect = $chatuser->groupid ? " AND (groupid=".$chatuser->groupid." OR groupid=0) " : "";
115
116 $messages = $DB->get_records_select('chat_messages_current',
117 'chatid = :chatid AND timestamp > :lasttime '.$groupselect, $params,
118 'timestamp ASC');
119
120 if (!empty($messages)) {
121 $num = count($messages);
122 } else {
123 $num = 0;
124 }
125 $chat_newrow = ($chat_lastrow + $num) % 2;
126 $send_user_list = false;
127 if ($messages && ($chat_lasttime != $chat_newlasttime)) {
128 foreach ($messages as $n => &$message) {
6bdfef5d 129 $tmp = new stdClass();
83064f9c 130 // when somebody enter room, user list will be updated
75d07096 131 if (!empty($message->system)){
83064f9c 132 $send_user_list = true;
83064f9c 133 }
d0157d82 134 if ($html = chat_format_message_theme($message, $chatuser, $USER, $cm->groupingid, $theme)) {
75d07096 135 $message->mymessage = ($USER->id == $message->userid);
136 $message->message = $html->html;
e8a4928d
DC
137 if (!empty($html->type)) {
138 $message->type = $html->type;
139 }
83064f9c 140 } else {
75d07096 141 unset($messages[$n]);
83064f9c 142 }
143 }
144 }
145
9dbcfd93 146 if($send_user_list){
83064f9c 147 // return users when system message coming
9dbcfd93 148 $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
83064f9c 149 $response['users'] = $users;
150 }
151
152 $DB->set_field('chat_users', 'lastping', time(), array('id'=>$chatuser->id));
153
154 $response['lasttime'] = $chat_newlasttime;
155 $response['lastrow'] = $chat_newrow;
156 if($messages){
157 $response['msgs'] = $messages;
158 }
159
160 echo json_encode($response);
161 header('Content-Length: ' . ob_get_length() );
162
163 ob_end_flush();
164 break;
a8e3b008 165
83064f9c 166default:
167 break;
168}