]> git.mjollnir.org Git - moodle.git/commitdiff
CHAT_MOD/MDL-14651, clean up update.php script.
authordongsheng <dongsheng>
Wed, 19 Nov 2008 06:29:27 +0000 (06:29 +0000)
committerdongsheng <dongsheng>
Wed, 19 Nov 2008 06:29:27 +0000 (06:29 +0000)
mod/chat/gui_ajax/script.js
mod/chat/gui_ajax/update.php
mod/chat/lib.php
mod/chat/mod_form.php
mod/chat/view.php

index 279f4f1c2e8a6f09e25e17adbf6a77895344c01d..d74b2c9a6d9e120c13cdde88e351649eadd37f0f 100644 (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>';
index 5947488dda0273d85dfd5f682ba82e0b494e9b0d..c60b44f2ab9ad25257ffa3d86551090720891dd0 100755 (executable)
@@ -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;
index 8cb39174de7f4d03512a5c9b6e64fbbf187276a1..5e1bdadf28053c59b8f60650a6d9469e65221193 100644 (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');
             }
         }
index be54108786ec4dbc97597b7298e4a64ee84229d0..a4b2c74eb53274c90ee1ecfaec2d48ea5d45b8c9 100644 (file)
@@ -58,8 +58,5 @@ class mod_chat_mod_form extends moodleform_mod {
 
         $this->add_action_buttons();
     }
-
-
-
 }
 ?>
index b6e9577c06dfdf799e6a86a1a7ad91ffe5cf1ba6..deb8449e3120bf86e96629e056e8ed47a71557ed 100644 (file)
                     $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