Skip to content

Commit

Permalink
"MDL-14651, more feature for new chat added"
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng committed Nov 19, 2008
1 parent 2f64da2 commit 1686b89
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 71 deletions.
19 changes: 18 additions & 1 deletion mod/chat/gui_ajax/common.php
@@ -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'){
Expand Down Expand Up @@ -38,4 +56,3 @@ public function set($name, $content){
$this->write($name, serialize($content));
}
}
?>
9 changes: 3 additions & 6 deletions mod/chat/gui_ajax/index.php
Expand Up @@ -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
Expand Down Expand Up @@ -49,6 +45,7 @@ function microtime_float(){
$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');
}
Expand All @@ -63,7 +60,7 @@ function microtime_float(){
<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>
Expand All @@ -80,7 +77,7 @@ function microtime_float(){
</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" />
Expand Down
72 changes: 49 additions & 23 deletions 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() {
Expand All @@ -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){
Expand All @@ -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);
}
Expand All @@ -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){
Expand Down Expand Up @@ -125,6 +140,7 @@ var init_cb = {
}
}
}

function in_array(f, t){
var a = false;
for( var i = 0; i<t.length; i++){
Expand All @@ -135,3 +151,13 @@ function in_array(f, t){
}
return a;
}

// debug code
if(!console){
var console = {
info: function(){
},
log: function(){
}
}
}
75 changes: 34 additions & 41 deletions mod/chat/gui_ajax/update.php
@@ -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))) {
Expand All @@ -38,8 +26,10 @@ function format_user_list(&$data, $course) {
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');
}

Expand All @@ -56,29 +46,28 @@ function format_user_list(&$data, $course) {
$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!!!
Expand All @@ -90,8 +79,8 @@ function format_user_list(&$data, $course) {
$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);
Expand All @@ -103,8 +92,9 @@ function format_user_list(&$data, $course) {
"timestamp ASC");
if ($messages) {
$num = count($messages);
if($CFG->chat_ajax_debug)
if($CFG->chat_ajax_debug) {
$response['count'] = $num;
}
} else {
$num = 0;
}
Expand All @@ -120,18 +110,18 @@ function format_user_list(&$data, $course) {
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);
if($CFG->chat_use_cache){
$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) {
Expand All @@ -142,11 +132,14 @@ function format_user_list(&$data, $course) {
}

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){
Expand All @@ -157,13 +150,13 @@ function format_user_list(&$data, $course) {
$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);
?>

0 comments on commit 1686b89

Please sign in to comment.