<?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.'&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'){
$this->write($name, serialize($content));
}
}
-?>
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
$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');
}
<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>
</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" />
(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() {
});
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){
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);
}
}
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){
}
}
}
+
function in_array(f, t){
var a = false;
for( var i = 0; i<t.length; i++){
}
return a;
}
+
+// debug code
+if(!console){
+ var console = {
+ info: function(){
+ },
+ log: function(){
+ }
+ }
+}
-<?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.'&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))) {
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');
}
$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!!!
$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);
"timestamp ASC");
if ($messages) {
$num = count($messages);
- if($CFG->chat_ajax_debug)
+ if($CFG->chat_ajax_debug) {
$response['count'] = $num;
+ }
} else {
$num = 0;
}
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);
$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) {
}
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){
$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);
-?>