From 1686b89a54d89f1a7515e7fe22491b05f314225d Mon Sep 17 00:00:00 2001 From: dongsheng Date: Wed, 19 Nov 2008 02:18:56 +0000 Subject: [PATCH] "MDL-14651, more feature for new chat added" --- mod/chat/gui_ajax/common.php | 19 ++++++++- mod/chat/gui_ajax/index.php | 9 ++--- mod/chat/gui_ajax/script.js | 72 +++++++++++++++++++++++----------- mod/chat/gui_ajax/update.php | 75 ++++++++++++++++-------------------- 4 files changed, 104 insertions(+), 71 deletions(-) diff --git a/mod/chat/gui_ajax/common.php b/mod/chat/gui_ajax/common.php index e0f50c9125..5ed757c82d 100755 --- a/mod/chat/gui_ajax/common.php +++ b/mod/chat/gui_ajax/common.php @@ -1,4 +1,22 @@ wwwroot.'/user/view.php?id='.$v->id.'&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)); } } -?> diff --git a/mod/chat/gui_ajax/index.php b/mod/chat/gui_ajax/index.php index cd9d6536e8..530ee23fab 100644 --- a/mod/chat/gui_ajax/index.php +++ b/mod/chat/gui_ajax/index.php @@ -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)) { $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'); ?> @@ -80,7 +77,7 @@ print_js_config(array('send'=>$str_send, 'sending'=>$str_sending), 'chat_lang');
-

Moodle 2.0

+

diff --git a/mod/chat/gui_ajax/script.js b/mod/chat/gui_ajax/script.js index 6601052696..c6607a48dc 100644 --- a/mod/chat/gui_ajax/script.js +++ b/mod/chat/gui_ajax/script.js @@ -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; iwwwroot.'/user/view.php?id='.$v->id.'&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); -?> -- 2.39.5