2008-11-19 02:18:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Produce update data (json format)
|
|
|
|
* @version $Id$
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*/
|
|
|
|
|
2009-01-03 13:03:05 +00:00
|
|
|
define('NO_MOODLE_COOKIES', true); // session not used here
|
|
|
|
|
2008-11-19 02:18:56 +00:00
|
|
|
require_once('../../../config.php');
|
|
|
|
require_once('../lib.php');
|
2008-11-18 05:21:57 +00:00
|
|
|
require_once('common.php');
|
|
|
|
|
2008-11-19 07:58:48 +00:00
|
|
|
ob_start();
|
2008-11-19 06:29:27 +00:00
|
|
|
header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
|
|
|
|
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
|
|
|
|
header('Cache-Control: no-cache, must-revalidate');
|
|
|
|
header('Pragma: no-cache');
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
2008-11-19 07:58:48 +00:00
|
|
|
header('X-Powered-By: MOODLE-Chat-V2');
|
|
|
|
|
2008-11-19 06:29:27 +00:00
|
|
|
|
2008-11-18 05:21:57 +00:00
|
|
|
$time_start = microtime_float();
|
|
|
|
|
|
|
|
$chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
|
|
|
|
$chat_init = optional_param('chat_init', 0, PARAM_INT);
|
2008-11-19 06:29:27 +00:00
|
|
|
$chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
|
2008-11-18 05:21:57 +00:00
|
|
|
$chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT);
|
2008-11-19 02:18:56 +00:00
|
|
|
|
2008-11-18 05:21:57 +00:00
|
|
|
$response = array();
|
|
|
|
|
|
|
|
if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
|
2008-11-19 07:58:48 +00:00
|
|
|
chat_print_error('ERROR', get_string('notlogged','chat'));
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Get the minimal course
|
|
|
|
if (!$course = $DB->get_record('course', array('id'=>$chatuser->course), 'id,theme,lang')) {
|
2008-11-19 07:58:48 +00:00
|
|
|
chat_print_error('ERROR', get_string('invalidcourseid', 'error'));
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
2008-11-19 02:18:56 +00:00
|
|
|
|
2008-11-18 05:21:57 +00:00
|
|
|
//Get the user theme and enough info to be used in chat_format_message() which passes it along to
|
2008-11-19 02:18:56 +00:00
|
|
|
if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) {
|
|
|
|
// no optimisation here, it would break again in future!
|
2008-11-19 07:58:48 +00:00
|
|
|
chat_print_error('ERROR', get_string('invaliduserid', 'error'));
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 05:24:24 +00:00
|
|
|
if (!$chat = $DB->get_record('chat', array('id'=>$chatuser->chatid))) {
|
|
|
|
chat_print_error('ERROR', get_string('invalidcoursemodule', 'error'));
|
|
|
|
}
|
|
|
|
|
2008-11-18 05:21:57 +00:00
|
|
|
if (!$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $course->id)) {
|
2008-11-19 07:58:48 +00:00
|
|
|
chat_print_error('ERROR', get_string('invalidcoursemodule', 'error'));
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
2009-06-02 05:24:24 +00:00
|
|
|
// setup $PAGE so that format_text will work properly
|
|
|
|
$PAGE->set_cm($cm, $course, $chat);
|
2008-11-18 05:21:57 +00:00
|
|
|
|
|
|
|
if($CFG->chat_use_cache){
|
|
|
|
$cache = new file_cache();
|
|
|
|
$users = $cache->get('user');
|
|
|
|
if(empty($users)) {
|
|
|
|
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
|
|
|
|
$cache->set('user', $users);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
|
|
|
|
}
|
|
|
|
|
2008-11-19 02:18:56 +00:00
|
|
|
$users = format_user_list($users, $course);
|
2008-11-18 05:21:57 +00:00
|
|
|
|
|
|
|
if(!empty($chat_init)) {
|
|
|
|
$response['users'] = $users;
|
|
|
|
echo json_encode($response);
|
2008-11-19 02:18:56 +00:00
|
|
|
exit;
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// force deleting of timed out users if there is a silence in room or just entering
|
|
|
|
if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
|
|
|
|
// must be done before chat_get_latest_message!!!
|
|
|
|
chat_delete_old_users();
|
|
|
|
}
|
2008-11-19 06:29:27 +00:00
|
|
|
|
|
|
|
if ($latest_message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
|
|
|
|
$chat_newlasttime = $latest_message->timestamp;
|
2008-11-18 05:21:57 +00:00
|
|
|
} else {
|
|
|
|
$chat_newlasttime = 0;
|
|
|
|
}
|
|
|
|
|
2008-11-19 02:18:56 +00:00
|
|
|
if ($chat_lasttime == 0) {
|
|
|
|
$chat_lasttime = time() - $CFG->chat_old_ping;
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
|
|
|
|
|
|
|
|
$groupselect = $chatuser->groupid ? " AND (groupid=".$chatuser->groupid." OR groupid=0) " : "";
|
|
|
|
|
2008-11-19 06:29:27 +00:00
|
|
|
$messages = $DB->get_records_select('chat_messages_current',
|
|
|
|
'chatid = :chatid AND timestamp > :lasttime '.$groupselect, $params,
|
|
|
|
'timestamp ASC');
|
|
|
|
|
|
|
|
if (!empty($messages)) {
|
2008-11-18 05:21:57 +00:00
|
|
|
$num = count($messages);
|
|
|
|
} else {
|
|
|
|
$num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$chat_newrow = ($chat_lastrow + $num) % 2;
|
|
|
|
|
2008-11-19 06:29:27 +00:00
|
|
|
$send_user_list = false;
|
2008-11-18 05:21:57 +00:00
|
|
|
if ($messages && ($chat_lasttime != $chat_newlasttime)) {
|
|
|
|
foreach ($messages as $n => &$message) {
|
2008-11-19 04:22:10 +00:00
|
|
|
$tmp = new stdclass;
|
2008-11-19 02:18:56 +00:00
|
|
|
// when somebody enter room, user list will be updated
|
2008-11-18 05:21:57 +00:00
|
|
|
if($message->system == 1){
|
2008-11-19 06:29:27 +00:00
|
|
|
$send_user_list = true;
|
|
|
|
$tmp->type = 'system';
|
|
|
|
$users = format_user_list(
|
|
|
|
chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
2008-11-19 04:22:10 +00:00
|
|
|
if ($html = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow)) {
|
|
|
|
if ($html->beep) {
|
|
|
|
$tmp->type = 'beep';
|
2008-11-19 06:29:27 +00:00
|
|
|
} elseif (empty($tmp->type)) {
|
|
|
|
$tmp->type = 'user';
|
2008-11-19 04:22:10 +00:00
|
|
|
}
|
|
|
|
$tmp->msg = $html->html;
|
|
|
|
$message = $tmp;
|
|
|
|
} else {
|
|
|
|
unset($message);
|
2008-11-18 05:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 06:29:27 +00:00
|
|
|
if(!empty($users) && $send_user_list){
|
2008-11-19 02:18:56 +00:00
|
|
|
// return users when system message coming
|
2008-11-18 05:21:57 +00:00
|
|
|
$response['users'] = $users;
|
|
|
|
}
|
2008-11-19 02:18:56 +00:00
|
|
|
|
2008-11-19 06:29:27 +00:00
|
|
|
$DB->set_field('chat_users', 'lastping', time(), array('id'=>$chatuser->id));
|
|
|
|
|
2008-11-18 05:21:57 +00:00
|
|
|
$response['lasttime'] = $chat_newlasttime;
|
|
|
|
$response['lastrow'] = $chat_newrow;
|
|
|
|
if($messages){
|
|
|
|
$response['msgs'] = $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
$time_end = microtime_float();
|
2008-11-19 02:18:56 +00:00
|
|
|
$time = $time_end - $time_start;
|
2008-11-19 06:29:27 +00:00
|
|
|
if(!empty($CFG->chat_ajax_debug)) {
|
|
|
|
$response['time'] = $time;
|
2008-11-19 02:18:56 +00:00
|
|
|
}
|
2008-11-18 05:21:57 +00:00
|
|
|
|
|
|
|
echo json_encode($response);
|
2008-11-19 06:29:27 +00:00
|
|
|
|
|
|
|
header('Content-Length: ' . ob_get_length() );
|
|
|
|
|
|
|
|
ob_end_flush();
|
|
|
|
exit;
|