CHAT_MOD/MDL-14651, clean up update.php script.

This commit is contained in:
dongsheng 2008-11-19 06:29:27 +00:00
parent 80bff20b74
commit 547ac6644d
5 changed files with 54 additions and 67 deletions

View File

@ -69,7 +69,7 @@ function update_users(users) {
}
var list = document.getElementById('listing');
var html = '';
for(i in users){
for(var i in users){
var el = document.createElement('li');
html += '<table><tr><td>' + users[i].picture + '</td><td>'
html += users[i].name+'<br/><a href="###" onclick="send_beep('+users[i].id+')"> Beep </a>';

View File

@ -9,11 +9,17 @@ require_once('../../../config.php');
require_once('../lib.php');
require_once('common.php');
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');
$time_start = microtime_float();
$chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
$chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
$chat_init = optional_param('chat_init', 0, PARAM_INT);
$chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
$chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT);
$response = array();
@ -37,8 +43,6 @@ if (!$cm = get_coursemodule_from_instance('chat', $chatuser->chatid, $course->id
$response['error'] = get_string('invalidcoursemodule', 'error');
}
$users = new stdclass;
if($CFG->chat_use_cache){
$cache = new file_cache();
$users = $cache->get('user');
@ -46,17 +50,11 @@ if($CFG->chat_use_cache){
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
$cache->set('user', $users);
}
if($CFG->chat_ajax_debug) {
$response['cache'] = true;
}
} else {
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
if($CFG->chat_ajax_debug) {
$response['cache'] = false;
}
}
if (!$users) {
if (empty($users)) {
$response['error'] = get_string('nousers', 'error');
}
@ -73,8 +71,9 @@ if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
// must be done before chat_get_latest_message!!!
chat_delete_old_users();
}
if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
$chat_newlasttime = $message->timestamp;
if ($latest_message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
$chat_newlasttime = $latest_message->timestamp;
} else {
$chat_newlasttime = 0;
}
@ -87,45 +86,36 @@ $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'las
$groupselect = $chatuser->groupid ? " AND (groupid=".$chatuser->groupid." OR groupid=0) " : "";
$messages = $DB->get_records_select("chat_messages_current",
"chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
"timestamp ASC");
if ($messages) {
$messages = $DB->get_records_select('chat_messages_current',
'chatid = :chatid AND timestamp > :lasttime '.$groupselect, $params,
'timestamp ASC');
if (!empty($messages)) {
$num = count($messages);
if($CFG->chat_ajax_debug) {
$response['count'] = $num;
}
} else {
$num = 0;
}
$chat_newrow = ($chat_lastrow + $num) % 2;
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');
ob_start();
$sendlist = false;
$send_user_list = false;
if ($messages && ($chat_lasttime != $chat_newlasttime)) {
foreach ($messages as $n => &$message) {
$tmp = new stdclass;
// when somebody enter room, user list will be updated
if($message->system == 1){
$sendlist = true;
$users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid);
if($CFG->chat_use_cache){
$cache = new file_cache();
$cache->set('user', $users);
}
$users = format_user_list($users, $course);
$send_user_list = true;
$tmp->type = 'system';
$users = format_user_list(
chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid), $course);
}
if ($html = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow)) {
if ($html->beep) {
$tmp->type = 'beep';
} elseif (empty($tmp->type)) {
$tmp->type = 'user';
}
$tmp->msg = $html->html;
$message = $tmp;
@ -135,28 +125,29 @@ if ($messages && ($chat_lasttime != $chat_newlasttime)) {
}
}
if($users && $sendlist){
if(!empty($users) && $send_user_list){
// return users when system message coming
$response['users'] = $users;
}
$DB->set_field('chat_users', 'lastping', time(), array('id'=>$chatuser->id));
$response['lasttime'] = $chat_newlasttime;
$response['lastrow'] = $chat_newrow;
if($messages){
$response['msgs'] = $messages;
}
// set user's last active time
$chatuser->lastping = time();
$DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
header("Content-Length: " . ob_get_length() );
header("X-Powered-By: MOODLE-Chat-V2");
ob_end_flush();
$time_end = microtime_float();
$time = $time_end - $time_start;
if($CFG->chat_ajax_debug) {
$response['time']=$time;
if(!empty($CFG->chat_ajax_debug)) {
$response['time'] = $time;
}
echo json_encode($response);
header('X-Powered-By: MOODLE-Chat-V2');
header('Content-Length: ' . ob_get_length() );
ob_end_flush();
exit;

View File

@ -446,13 +446,11 @@ function chat_get_users($chatid, $groupid=0, $groupingid=0) {
$groupingjoin = '';
}
return $DB->get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt
FROM {chat_users} c
JOIN {user} u ON u.id = c.userid
$groupingjoin
WHERE c.chatid = :chatid
$groupselect
ORDER BY c.firstping ASC", $params);
return $DB->get_records_sql("SELECT
DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt
FROM {chat_users} c JOIN {user} u ON u.id = c.userid $groupingjoin
WHERE c.chatid = :chatid $groupselect
ORDER BY c.firstping ASC", $params);
}
function chat_get_latest_message($chatid, $groupid=0) {
@ -466,12 +464,11 @@ function chat_get_latest_message($chatid, $groupid=0) {
$groupselect = "";
}
$sql = "SELECT *
FROM {chat_messages_current}
WHERE chatid = :chatid
$groupselect
ORDER BY timestamp DESC";
$sql = "SELECT *
FROM {chat_messages_current} WHERE chatid = :chatid $groupselect
ORDER BY timestamp DESC";
// return the lastest one message
return $DB->get_record_sql($sql, $params, true);
}
@ -573,7 +570,9 @@ function chat_delete_old_users() {
$message->system = 1;
$message->timestamp = time();
if (!$DB->insert_record('chat_messages', $message) || !$DB->insert_record('chat_messages_current', $message) ) {
if (!$DB->insert_record('chat_messages', $message)
|| !$DB->insert_record('chat_messages_current', $message) )
{
print_error('cantinsert', 'chat');
}
}

View File

@ -58,8 +58,5 @@ class mod_chat_mod_form extends moodleform_mod {
$this->add_action_buttons();
}
}
?>

View File

@ -142,17 +142,17 @@
$chattarget = "/mod/chat/gui_$CFG->chat_method/index.php?id=$chat->id$groupparam";
}
echo '<p>';
link_to_popup_window ($chattarget,
"chat$course->id$chat->id$groupparam", "$strenterchat", 500, 700, get_string('modulename', 'chat'));
echo '</p>';
if ($CFG->chat_enable_ajax) {
echo '<p>';
link_to_popup_window ("/mod/chat/gui_ajax/index.php?id=$chat->id$groupparam",
"chat$course->id$chat->id$groupparam", get_string('ajax_gui', 'message'), 500, 700, get_string('modulename', 'chat'));
link_to_popup_window ($chattarget,
"chat$course->id$chat->id$groupparam", "$strenterchat", 500, 700, get_string('modulename', 'chat'));
echo '</p>';
}
echo '<p>';
link_to_popup_window ("/mod/chat/gui_ajax/index.php?id=$chat->id$groupparam",
"chat$course->id$chat->id$groupparam", get_string('ajax_gui', 'message'), 500, 700, get_string('modulename', 'chat'));
echo '</p>';
// if user is using screen reader, then there is no need to display this link again
if ($CFG->chat_method == 'header_js' && empty($USER->screenreader)) {
// show frame/js-less alternative