"MDL-14651, more feature for new chat added"
authordongsheng <dongsheng>
Wed, 19 Nov 2008 02:18:56 +0000 (02:18 +0000)
committerdongsheng <dongsheng>
Wed, 19 Nov 2008 02:18:56 +0000 (02:18 +0000)
mod/chat/gui_ajax/common.php
mod/chat/gui_ajax/index.php
mod/chat/gui_ajax/script.js
mod/chat/gui_ajax/update.php

index e0f50c91258ca1d37bef13ad2e1793df0ee14e6c..5ed757c82deb0b1968acdc3c739f09cde27a54ab 100755 (executable)
@@ -1,4 +1,22 @@
 <?php
+
+function microtime_float(){
+    list($usec, $sec) = explode(" ", microtime());
+    return ((float)$usec+(float)$sec);
+}
+
+function format_user_list(&$data, $course) {
+    global $CFG, $DB;
+    $users = array();
+    foreach($data as $v){
+        $user['name'] = fullname($v);
+        $user['url'] = $CFG->wwwroot.'/user/view.php?id='.$v->id.'&amp;course='.$course->id;
+        $user['picture'] = print_user_picture($v->id, 0, $v->picture, false, true, false);
+        $users[] = $user;
+    }
+    return $users;
+}
+
 class file_cache{
     private $dir = '';
     public function __construct($dir='/dev/shm'){
@@ -38,4 +56,3 @@ class file_cache{
         $this->write($name, serialize($content));
     }
 }
-?>
index cd9d6536e84588f477acd3242726b01f6822f104..530ee23fabf0f0be161c89ab3af0c8242848cea0 100644 (file)
@@ -3,10 +3,6 @@
 require_once('../../../config.php');
 require_once('../lib.php');
 require_once('common.php');
-function microtime_float(){
-    list($usec, $sec) = explode(" ", microtime());
-    return ((float)$usec+(float)$sec);
-}
 $time_start = microtime_float();
 $id      = required_param('id', PARAM_INT);
 $groupid = optional_param('groupid', 0, PARAM_INT); //only for teachers
@@ -49,6 +45,7 @@ if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_
 $strchat = get_string('modulename', 'chat'); // must be before current_language() in chat_login_user() to force course language!!!
 $str_send    = get_string('send', 'chat');
 $str_sending = get_string('sending', 'chat');
+$str_title   = format_string($course->shortname) . ": ".format_string($chat->name,true).$groupname;
 if (!$chat_sid = chat_login_user($chat->id, 'ajax', $groupid, $course)) {
     print_error('cantlogin', 'chat');
 }
@@ -63,7 +60,7 @@ if (!$chat_sid = chat_login_user($chat->id, 'ajax', $groupid, $course)) {
 <link rel="stylesheet" type="text/css" href="<?php echo $CFG->httpswwwroot;?>/lib/yui/layout/assets/skins/sam/layout.css" />
 <link rel="stylesheet" type="text/css" href="<?php echo $CFG->httpswwwroot;?>/lib/yui/button/assets/skins/sam/button.css" />
 <?php
-print_js_config(array('sid'=>$chat_sid,'timer'=>5000, 'chat_lasttime'=>0,'chat_lastrow'=>null), 'chat_cfg');
+print_js_config(array('sid'=>$chat_sid,'timer'=>5000, 'chat_lasttime'=>0,'chat_lastrow'=>null, 'header_title'=>$strchat), 'chat_cfg');
 print_js_config(array('send'=>$str_send, 'sending'=>$str_sending), 'chat_lang');
 ?>
 <script type="text/javascript" src="<?php echo $CFG->httpswwwroot;?>/lib/yui/yahoo-dom-event/yahoo-dom-event.js"></script>
@@ -80,7 +77,7 @@ print_js_config(array('send'=>$str_send, 'sending'=>$str_sending), 'chat_lang');
 </head>
 <body class=" yui-skin-sam">
 <div id="chat_header">
-<p>Moodle 2.0</p>
+<h1><?php echo $str_title;?></h1>
 </div>
 <div id="chat_input">
     <input type="text" id="input_msgbox" value="" size="48" />
index 66010526966e791a7ba445851bd28ddcb4afa47e..c6607a48dc028ab011ec7e56fa8c42423dfc7fc8 100644 (file)
@@ -1,17 +1,15 @@
 (function() {
-var Dom = YAHOO.util.Dom,
-// record msg IDs
-Event = YAHOO.util.Event;
+var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event;
 // window.onload
 Event.onDOMReady(function() {
     // build layout
     var layout = new YAHOO.widget.Layout({
         units: [
-        { position: 'top', height: 50, body: 'chat_header', header: 'Chat Room', gutter: '5px', collapse: true, resize: false },
+        { position: 'top', height: 60, body: 'chat_header', header: chat_cfg.header_title, gutter: '5px', collapse: true, resize: false },
         { position: 'right', header: 'User List', width: 300, resize: true, gutter: '5px', footer: null, collapse: true, scroll: true, body: 'chat_user_list', animate: false },
-        { position: 'bottom', header: 'Input Area', height: 100, resize: true, body: 'chat_input', gutter: '5px', collapse: true },
-        { position: 'left', header: 'Options', width: 200, resize: true, body: 'chat_options', gutter: '5px', collapse: true, close: true, collapseSize: 50, scroll: true, animate: false },
-        { position: 'center', body: 'chat_panel' }
+        { position: 'bottom', header: 'Input Area', height: 60, resize: true, body: 'chat_input', gutter: '5px', collapse: false },
+        //{ position: 'left', header: 'Options', width: 200, resize: true, body: 'chat_options', gutter: '5px', collapse: true, close: true, collapseSize: 50, scroll: true, animate: false },
+        { position: 'center', body: 'chat_panel', gutter: '5px', scroll: true }
         ]
     });
     layout.on('render', function() {
@@ -21,24 +19,35 @@ Event.onDOMReady(function() {
     });
     layout.render();
     Event.on('btn_send', 'click', function(ev) {
-        var msg = document.getElementById('input_msgbox').value;
-        if (!msg) {
-            alert('null?');
-            return;
-        }
-        var url = 'post.php?chat_sid='+chat_cfg.sid;
         Event.stopEvent(ev);
-        this.value = chat_lang.sending;
-        var a = YAHOO.util.Connect.asyncRequest('POST', url, send_cb, "chat_message="+msg);
+        send_msg();
     });
+    var key_send = new YAHOO.util.KeyListener(document, { keys:13 }, {fn:send_msg, correctScope:true });
+    key_send.enable();
+    document.getElementById('input_msgbox').focus();
+
     // done build layout
     var transaction = YAHOO.util.Connect.asyncRequest('POST', "update.php?chat_sid="+chat_cfg.sid+"&chat_init=1", init_cb, null);
-    var interval = setInterval(function(){
+    interval = setInterval(function(){
         update_info();
     }, chat_cfg.timer);
 });
 })();
+function send_msg() {
+    var msg = document.getElementById('input_msgbox').value;
+    var el_send = document.getElementById('btn_send');
+    if (!msg) {
+        alert('null?');
+        return;
+    }
+    var url = 'post.php?chat_sid='+chat_cfg.sid;
+    el_send.value = chat_lang.sending;
+    var a = YAHOO.util.Connect.asyncRequest('POST', url, send_cb, "chat_message="+msg);
+}
+
+// record msg IDs
 var msgs = [];
+var interval = null;
 
 function update_users(users) {
     if(!users){
@@ -48,7 +57,7 @@ function update_users(users) {
     list.innerHTML = '';
     for(i in users){
         var el = document.createElement('li');
-        el.innerHTML = users[i].firstname;
+        el.innerHTML = users[i].picture+' '+users[i].name;
         console.info(users[i]);
         list.appendChild(el);
     }
@@ -74,12 +83,18 @@ function append_msg(msg) {
 }
 
 var send_cb = {
-success: function(o) {
-             if(o.responseText == 200){
-                 document.getElementById('btn_send').value = chat_lang.send;
-                 document.getElementById('input_msgbox').value = '';
-             }
-         }
+    success: function(o) {
+        if(o.responseText == 200){
+         document.getElementById('btn_send').value = chat_lang.send;
+         document.getElementById('input_msgbox').value = '';
+        }
+        clearInterval(interval)
+        update_info();
+        interval = setInterval(function(){
+            update_info();
+        }, chat_cfg.timer);
+        document.getElementById('input_msgbox').focus();
+    }
 }
 var update_cb = {
 success: function(o){
@@ -125,6 +140,7 @@ var init_cb = {
         }
     }
 }
+
 function in_array(f, t){
     var a = false;
     for( var i = 0; i<t.length; i++){
@@ -135,3 +151,13 @@ function in_array(f, t){
     }
     return a;
 }
+
+// debug code
+if(!console){
+    var console = {
+        info: function(){
+        },
+        log: function(){
+        }
+    }
+}
index 85b2a69f9dda27f22ec9dabcea3cbfa2a7632e23..ae2c8d49e4e6ab3c2b6b1febb9e1a814c6575de9 100755 (executable)
@@ -1,33 +1,21 @@
-<?php  // $Id$
-
-// Produce update information (json)
-require('../../../config.php');
-require('../lib.php');
+<?php
+/**
+ * Produce update data (json format)
+ * @version $Id$
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ */
+
+require_once('../../../config.php');
+require_once('../lib.php');
 require_once('common.php');
 
-
-function microtime_float(){
-    list($usec, $sec) = explode(" ", microtime());
-    return ((float)$usec+(float)$sec);
-}
-function format_user_list(&$data, $course) {
-    global $CFG, $DB;
-    $users = array();
-    foreach($data as $v){
-        $user['name'] = fullname($v);
-        $user['url'] = $CFG->wwwroot.'/user/view.php?id='.$v->id.'&amp;course='.$course->id;
-        $user['picture'] = print_user_picture($v->id, 0, $v->picture, false, true, false);
-        $users[] = $user;
-    }
-    //return json_encode($users);
-}
-
 $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_lastrow  = optional_param('chat_lastrow', 1, PARAM_INT);
+
 $response = array();
 
 if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
@@ -38,8 +26,10 @@ if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
 if (!$course = $DB->get_record('course', array('id'=>$chatuser->course), 'id,theme,lang')) {
     $response['error'] = get_string('invalidcourseid', 'error');
 }
+
 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
-if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) { // no optimisation here, it would break again in future!
+if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) {
+    // no optimisation here, it would break again in future!
     $response['error'] = get_string('invaliduserid', 'error');
 }
 
@@ -56,29 +46,28 @@ 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'] = 'yes';
+    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'] = 'no';
+    if($CFG->chat_ajax_debug) {
+        $response['cache'] = false;
+    }
 }
 
 if (!$users) {
     $response['error'] = get_string('nousers', 'error');
 }
 
-format_user_list($users, $course);
+$users = format_user_list($users, $course);
 
 if(!empty($chat_init)) {
     $response['users'] = $users;
     echo json_encode($response);
-    die;
+    exit;
 }
 
-//Setup course, lang and theme
-course_setup($course);
-
 // 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!!!
@@ -90,8 +79,8 @@ if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
     $chat_newlasttime = 0;
 }
 
-if ($chat_lasttime == 0) { //display some previous messages
-    $chat_lasttime = time() - $CFG->chat_old_ping; //TODO - any better value??
+if ($chat_lasttime == 0) {
+    $chat_lasttime = time() - $CFG->chat_old_ping;
 }
 
 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
@@ -103,8 +92,9 @@ $messages = $DB->get_records_select("chat_messages_current",
                     "timestamp ASC");
 if ($messages) {
     $num = count($messages);
-    if($CFG->chat_ajax_debug)
+    if($CFG->chat_ajax_debug) {
         $response['count'] = $num;
+    }
 } else {
     $num = 0;
 }
@@ -120,10 +110,10 @@ header('Content-Type: text/html; charset=utf-8');
 ob_start();
 
 $beep = false;
-$us = array ();
 $sendlist = false;
 if ($messages && ($chat_lasttime != $chat_newlasttime)) {
     foreach ($messages as $n => &$message) {
+        // 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);
@@ -131,7 +121,7 @@ if ($messages && ($chat_lasttime != $chat_newlasttime)) {
                 $cache = new file_cache();
                 $cache->set('user', $users);
             }
-            format_user_list($users, $course);
+            $users = format_user_list($users, $course);
         }
         $html = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
         if ($html->beep) {
@@ -142,11 +132,14 @@ if ($messages && ($chat_lasttime != $chat_newlasttime)) {
 }
 
 if($users && $sendlist){
+    // return users when system message coming
     $response['users'] = $users;
 }
+
 if ($beep) {
     $response['beep'] = true;
 }
+
 $response['lasttime'] = $chat_newlasttime;
 $response['lastrow']  = $chat_newrow;
 if($messages){
@@ -157,13 +150,13 @@ if($messages){
 $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-MOD");
+header("X-Powered-By: MOODLE-Chat-V2");
 ob_end_flush();
 
 $time_end = microtime_float();
-$time = $time_end-$time_start;
-if($CFG->chat_ajax_debug)
+$time = $time_end - $time_start;
+if($CFG->chat_ajax_debug) {
     $response['time']=$time;
+}
 
 echo json_encode($response);
-?>