** ** This is an alternative version of jsupdate.php that acts ** as a long-running daemon. It will feed/stall/feed JS updates ** to the client. From the module configuration select "Stream" ** updates. ** ** The client connection is not forever though. Once we reach ** CHAT_MAX_CLIENT_UPDATES, it will force the client to re-fetch it. ** ** This buys us all the benefits that chatd has, minus the setup, ** as we are using apache to do the daemon handling. ** **/ define('CHAT_MAX_CLIENT_UPDATES', 1000); $nomoodlecookie = true; // Session not needed! require('../../../config.php'); require('../lib.php'); // we are going to run for a long time // avoid being terminated by php @set_time_limit(0); $chat_sid = required_param('chat_sid', PARAM_ALPHANUM); $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT); $chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT); $chat_lastid = optional_param('chat_lastid', 0, PARAM_INT); if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) { error('Not logged in!'); } //Get the minimal course if (!$course = get_record('course','id',$chatuser->course,'','','','','id,theme,lang')) { error('incorrect course id'); } //Get the user theme and enough info to be used in chat_format_message() which passes it along to // chat_format_message_manually() -- and only id and timezone are used. if (!$USER = get_record('user','id',$chatuser->userid)) { // no optimisation here, it would break again in future! error('User does not exist!'); } $USER->description = ''; //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_delete_old_users(); } // // Time to send headers, and lay out the basic JS updater page // 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'); /// required stylesheets $stylesheetshtml = ''; foreach ($CFG->stylesheets as $stylesheet) { $stylesheetshtml .= ''; } ?> lastping = time(); set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id ); if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { $chat_newlasttime = $message->timestamp; $chat_newlastid = $message->id; } else { $chat_newlasttime = 0; $chat_newlastid = 0; print " \n"; print $CHAT_DUMMY_DATA; @ob_end_flush(); sleep($CFG->chat_refresh_room); continue; } $timenow = time(); $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : ""; $newcriteria = ''; if ($chat_lastid > 0) { $newcriteria = "id > $chat_lastid"; } else { if ($chat_lasttime == 0) { //display some previous messages $chat_lasttime = $timenow - $CFG->chat_old_ping; //TO DO - any better value?? } $newcriteria = "timestamp > $chat_lasttime"; } $messages = get_records_select("chat_messages", "chatid = '$chatuser->chatid' AND $newcriteria $groupselect", "timestamp ASC"); if ($messages) { $num = count($messages); } else { print " \n"; print $CHAT_DUMMY_DATA; @ob_end_flush(); sleep($CFG->chat_refresh_room); continue; $num = 0; } print '' . "\n\n"; if ($beep) { print ''; } print $CHAT_DUMMY_DATA; @ob_end_flush(); sleep($CFG->chat_refresh_room); } // here ends the for() loop // here & should be written & :-D $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdated.php?chat_sid=$chat_sid&chat_lasttime=$chat_lasttime&chat_lastrow=$chat_newrow&chat_lastid=$chat_lastid"; print '' . "\n\n"; ?>