From: dongsheng Date: Wed, 19 Nov 2008 06:29:27 +0000 (+0000) Subject: CHAT_MOD/MDL-14651, clean up update.php script. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=547ac6644d7292fb3707de214b047262bef39e0f;p=moodle.git CHAT_MOD/MDL-14651, clean up update.php script. --- diff --git a/mod/chat/gui_ajax/script.js b/mod/chat/gui_ajax/script.js index 279f4f1c2e..d74b2c9a6d 100644 --- a/mod/chat/gui_ajax/script.js +++ b/mod/chat/gui_ajax/script.js @@ -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 += '
' + users[i].picture + '' html += users[i].name+'
Beep '; diff --git a/mod/chat/gui_ajax/update.php b/mod/chat/gui_ajax/update.php index 5947488dda..c60b44f2ab 100755 --- a/mod/chat/gui_ajax/update.php +++ b/mod/chat/gui_ajax/update.php @@ -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; diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 8cb39174de..5e1bdadf28 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -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'); } } diff --git a/mod/chat/mod_form.php b/mod/chat/mod_form.php index be54108786..a4b2c74eb5 100644 --- a/mod/chat/mod_form.php +++ b/mod/chat/mod_form.php @@ -58,8 +58,5 @@ class mod_chat_mod_form extends moodleform_mod { $this->add_action_buttons(); } - - - } ?> diff --git a/mod/chat/view.php b/mod/chat/view.php index b6e9577c06..deb8449e31 100644 --- a/mod/chat/view.php +++ b/mod/chat/view.php @@ -142,17 +142,17 @@ $chattarget = "/mod/chat/gui_$CFG->chat_method/index.php?id=$chat->id$groupparam"; } - echo '

'; - link_to_popup_window ($chattarget, - "chat$course->id$chat->id$groupparam", "$strenterchat", 500, 700, get_string('modulename', 'chat')); - echo '

'; - if ($CFG->chat_enable_ajax) { echo '

'; - 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 '

'; } + + echo '

'; + 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 '

'; // 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