MDL-25069, chat, check if user loggedin
[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
a9637e7d
PS
17//TODO: use standard CLI_SCRIPT support here (skodak)
18
529817c8
DC
19define('AJAX_SCRIPT', true);
20
83064f9c 21require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
22require_once(dirname(__FILE__) . '/lib.php');
23
24$action = optional_param('action', '', PARAM_ALPHANUM);
25$beep_id = optional_param('beep', '', PARAM_RAW);
26$chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
75d07096 27$theme = required_param('theme', PARAM_ALPHANUM);
83064f9c 28$chat_message = optional_param('chat_message', '', PARAM_RAW);
29$chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
30$chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT);
31
32
33if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
34 chat_print_error('ERROR', get_string('notlogged','chat'));
35}
36if (!$chat = $DB->get_record('chat', array('id'=>$chatuser->chatid))) {
37 chat_print_error('ERROR', get_string('invalidcoursemodule', 'error'));
38}
39if (!$course = $DB->get_record('course', array('id'=>$chat->course))) {
40 chat_print_error('ERROR', get_string('invaliduserid', 'error'));
41}
42if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
43 chat_print_error('ERROR', get_string('invalidcoursemodule', 'error'));
44}
2412f8b8 45if (!isloggedin()) {
83064f9c 46 chat_print_error('ERROR', get_string('notlogged','chat'));
47}
48
49// setup $PAGE so that format_text will work properly
50$PAGE->set_cm($cm, $course, $chat);
a6855934 51$PAGE->set_url('/mod/chat/chat_ajax.php', array('chat_sid'=>$chat_sid));
83064f9c 52
53ob_start();
54header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
55header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
56header('Cache-Control: no-cache, must-revalidate');
57header('Pragma: no-cache');
58header('Content-Type: text/html; charset=utf-8');
59header('X-Powered-By: MOODLE-Chat-V2');
60
61switch ($action) {
62case 'init':
ff0e58c8 63 $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
83064f9c 64 $users = chat_format_userlist($users, $course);
65 $response['users'] = $users;
66 echo json_encode($response);
67 break;
68case 'chat':
69 session_get_instance()->write_close();
70 chat_delete_old_users();
71 $chat_message = clean_text($chat_message, FORMAT_MOODLE);
72
73 if (!empty($beep_id)) {
74 $chat_message = 'beep '.$beep_id;
75 }
76
77 if (!empty($chat_message)) {
39790bd8 78 $message = new stdClass();
83064f9c 79 $message->chatid = $chatuser->chatid;
80 $message->userid = $chatuser->userid;
81 $message->groupid = $chatuser->groupid;
82 $message->message = $chat_message;
83 $message->timestamp = time();
84
85 $chatuser->lastmessageping = time() - 2;
86 $DB->update_record('chat_users', $chatuser);
87
a9637e7d
PS
88 $DB->insert_record('chat_messages', $message);
89 $DB->insert_record('chat_messages_current', $message);
529817c8
DC
90 // response ok message
91 echo json_encode(true);
83064f9c 92 add_to_log($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id);
93
94 ob_end_flush();
95 }
96 break;
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 $users = chat_format_userlist(chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
134 }
75d07096 135 if ($html = chat_format_message_theme($message, $chatuser->course, $USER, $theme)) {
136 $message->mymessage = ($USER->id == $message->userid);
137 $message->message = $html->html;
e8a4928d
DC
138 if (!empty($html->type)) {
139 $message->type = $html->type;
140 }
83064f9c 141 } else {
75d07096 142 unset($messages[$n]);
83064f9c 143 }
144 }
145 }
146
147 if(!empty($users) && $send_user_list){
148 // return users when system message coming
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;
165default:
166 break;
167}