2009-09-23 07:08:43 +00:00
< ? php
2014-07-23 09:58:29 +08:00
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
define ( 'NO_MOODLE_COOKIES' , true ); // Session not used here.
2009-09-23 07:08:43 +00:00
require ( '../../../config.php' );
2020-12-04 10:43:46 -05:00
require_once ( '../lib.php' );
2009-09-23 07:08:43 +00:00
2014-07-23 09:58:29 +08:00
$chatsid = required_param ( 'chat_sid' , PARAM_ALPHANUM );
$chatlasttime = optional_param ( 'chat_lasttime' , 0 , PARAM_INT );
$chatlastrow = optional_param ( 'chat_lastrow' , 1 , PARAM_INT );
2009-09-23 07:08:43 +00:00
2014-07-23 09:58:29 +08:00
$url = new moodle_url ( '/mod/chat/gui_header_js/jsupdate.php' , array ( 'chat_sid' => $chatsid ));
if ( $chatlasttime !== 0 ) {
$url -> param ( 'chat_lasttime' , $chatlasttime );
2009-09-23 07:08:43 +00:00
}
2014-07-23 09:58:29 +08:00
if ( $chatlastrow !== 1 ) {
$url -> param ( 'chat_lastrow' , $chatlastrow );
2009-09-23 07:08:43 +00:00
}
$PAGE -> set_url ( $url );
2014-07-23 09:58:29 +08:00
if ( ! $chatuser = $DB -> get_record ( 'chat_users' , array ( 'sid' => $chatsid ))) {
2009-09-23 07:08:43 +00:00
print_error ( 'notlogged' , 'chat' );
}
2014-07-23 09:58:29 +08:00
// Get the minimal course.
if ( ! $course = $DB -> get_record ( 'course' , array ( 'id' => $chatuser -> course ))) {
2009-09-23 07:08:43 +00:00
print_error ( 'invalidcourseid' );
}
2014-07-23 09:58:29 +08:00
// Get the user theme and enough info to be used in chat_format_message() which passes it along to.
// No optimisation here, it would break again in future!
if ( ! $user = $DB -> get_record ( 'user' , array ( 'id' => $chatuser -> userid , 'deleted' => 0 , 'suspended' => 0 ))) {
2009-09-23 07:08:43 +00:00
print_error ( 'invaliduser' );
}
2013-10-01 09:23:42 +02:00
\core\session\manager :: set_user ( $user );
2009-09-23 07:08:43 +00:00
2014-07-23 09:58:29 +08:00
// Setup course, lang and theme.
2009-09-23 07:08:43 +00:00
$PAGE -> set_course ( $course );
2014-07-23 09:58:29 +08:00
// Force deleting of timed out users if there is a silence in room or just entering.
if (( time () - $chatlasttime ) > $CFG -> chat_old_ping ) {
// Must be done before chat_get_latest_message!
2009-09-23 07:08:43 +00:00
chat_delete_old_users ();
}
if ( $message = chat_get_latest_message ( $chatuser -> chatid , $chatuser -> groupid )) {
2014-07-23 09:58:29 +08:00
$chatnewlasttime = $message -> timestamp ;
2009-09-23 07:08:43 +00:00
} else {
2014-07-23 09:58:29 +08:00
$chatnewlasttime = 0 ;
2009-09-23 07:08:43 +00:00
}
2014-07-23 09:58:29 +08:00
if ( $chatlasttime == 0 ) { // Display some previous messages.
$chatlasttime = time () - $CFG -> chat_old_ping ; // TO DO - any better value?
2009-09-23 07:08:43 +00:00
}
$timenow = time ();
2014-07-23 09:58:29 +08:00
$params = array ( 'groupid' => $chatuser -> groupid , 'chatid' => $chatuser -> chatid , 'lasttime' => $chatlasttime );
2009-09-23 07:08:43 +00:00
$groupselect = $chatuser -> groupid ? " AND (groupid=:groupid OR groupid=0) " : " " ;
$messages = $DB -> get_records_select ( " chat_messages_current " ,
" chatid = :chatid AND timestamp > :lasttime $groupselect " , $params ,
" timestamp ASC " );
if ( $messages ) {
$num = count ( $messages );
} else {
$num = 0 ;
}
2014-07-23 09:58:29 +08:00
$chatnewrow = ( $chatlastrow + $num ) % 2 ;
2009-09-23 07:08:43 +00:00
2014-07-23 09:58:29 +08:00
// No & in url, does not work in header!
$baseurl = " { $CFG -> wwwroot } /mod/chat/gui_header_js/jsupdate.php? " ;
$refreshurl = $baseurl . " chat_sid= $chatsid &chat_lasttime= $chatnewlasttime &chat_lastrow= $chatnewrow " ;
$refreshurlamp = $baseurl . " chat_sid= $chatsid &chat_lasttime= $chatnewlasttime &chat_lastrow= $chatnewrow " ;
2009-09-23 07:08:43 +00:00
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' );
header ( " Refresh: $CFG->chat_refresh_room ; url= $refreshurl " );
2014-07-23 09:58:29 +08:00
// Use ob to be able to send Content-Length headers.
// Needed for Keep-Alive to work.
2009-09-23 07:08:43 +00:00
ob_start ();
2006-04-17 21:04:19 +00:00
2003-07-07 06:44:16 +00:00
?>
2005-09-10 22:57:21 +00:00
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
2005-02-03 20:39:27 +00:00
< html >
< head >
2006-11-11 17:23:20 +00:00
< meta http - equiv = " content-type " content = " text/html; charset=utf-8 " />
2005-02-03 20:39:27 +00:00
< script type = " text/javascript " >
2006-12-22 05:42:36 +00:00
//<![CDATA[
2008-06-18 03:18:22 +00:00
if ( parent . msg && parent . msg . document . getElementById ( " msgStarted " ) == null ) {
2005-02-10 19:25:42 +00:00
parent . msg . document . close ();
parent . msg . document . open ( " text/html " , " replace " );
2005-09-10 22:57:21 +00:00
parent . msg . document . write ( " <!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN \" \" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd \" > " );
2005-02-10 19:25:42 +00:00
parent . msg . document . write ( " <html><head> " );
2006-11-11 17:23:20 +00:00
parent . msg . document . write ( " <meta http-equiv= \" content-type \" content= \" text/html; charset=utf-8 \" /> " );
2005-02-25 12:35:20 +00:00
parent . msg . document . write ( " <base target= \" _blank \" /> " );
2006-12-22 05:42:36 +00:00
parent . msg . document . write ( " < \ /head><body class= \" mod-chat-gui_header_js course-<?php echo $chatuser->course ?> \" id= \" mod-chat-gui_header_js-jsupdate \" ><div style= \" display: none \" id= \" msgStarted \" > < \ /div> " );
2005-02-10 19:25:42 +00:00
}
2014-07-23 09:58:29 +08:00
< ? php
$beep = false ;
$refreshusers = false ;
$us = array ();
if (( $chatlasttime != $chatnewlasttime ) and $messages ) {
foreach ( $messages as $message ) {
$chatlastrow = ( $chatlastrow + 1 ) % 2 ;
$formatmessage = chat_format_message ( $message , $chatuser -> course , $USER , $chatlastrow );
if ( $formatmessage -> beep ) {
$beep = true ;
}
if ( $formatmessage -> refreshusers ) {
$refreshusers = true ;
2005-02-03 20:39:27 +00:00
}
2014-07-23 09:58:29 +08:00
$us [ $message -> userid ] = $timenow - $message -> timestamp ;
echo " if(parent.msg) " ;
echo " parent.msg.document.write(' " . addslashes_js ( $formatmessage -> html ) . " \\ n'); \n " ;
}
}
2005-02-03 20:39:27 +00:00
2014-07-23 09:58:29 +08:00
$chatuser -> lastping = time ();
$DB -> set_field ( 'chat_users' , 'lastping' , $chatuser -> lastping , array ( 'id' => $chatuser -> id ));
2005-02-03 20:39:27 +00:00
2014-07-23 09:58:29 +08:00
if ( $refreshusers ) {
?>
2008-06-18 03:18:22 +00:00
var link = parent . users . document . getElementById ( 'refreshLink' );
if ( link != null ) {
parent . users . location . href = link . href ;
}
2014-07-23 09:58:29 +08:00
< ? php
} else {
foreach ( $us as $uid => $lastping ) {
$min = ( int ) ( $lastping / 60 );
$sec = $lastping - ( $min * 60 );
$min = $min < 10 ? '0' . $min : $min ;
$sec = $sec < 10 ? '0' . $sec : $sec ;
$idle = $min . ':' . $sec ;
echo " if (parent.users && parent.users.document.getElementById('uidle { $uid } ') != null) { " .
" parent.users.document.getElementById('uidle { $uid } ').innerHTML = ' $idle ';} \n " ;
}
}
?>
if ( parent . input ) {
2008-06-25 02:18:55 +00:00
var autoscroll = parent . input . document . getElementById ( 'auto' );
2014-07-23 09:58:29 +08:00
if ( parent . msg && autoscroll && autoscroll . checked ) {
2008-06-25 02:18:55 +00:00
parent . msg . scroll ( 1 , 5000000 );
}
2008-06-18 04:06:11 +00:00
}
2006-12-22 05:42:36 +00:00
//]]>
2005-02-03 20:39:27 +00:00
</ script >
</ head >
< body >
2014-07-23 09:58:29 +08:00
< ? php
if ( $beep ) {
2017-11-06 15:08:55 +08:00
echo '<script> (function() {' ;
echo 'var audioElement = document.createElement("audio");' ;
echo 'audioElement.setAttribute("src", "../beep.mp3");' ;
echo 'audioElement.play(); })();' ;
echo '</script>' ;
2014-07-23 09:58:29 +08:00
}
?>
2006-12-22 05:42:36 +00:00
< a href = " <?php echo $refreshurlamp ?> " name = " refreshLink " > Refresh link </ a >
2005-02-03 20:39:27 +00:00
</ body >
</ html >
2006-04-17 21:04:19 +00:00
< ? php
2014-07-23 09:58:29 +08:00
// Support HTTP Keep-Alive.
2006-04-17 21:04:19 +00:00
header ( " Content-Length: " . ob_get_length () );
2008-06-18 03:18:22 +00:00
ob_end_flush ();
2006-04-17 21:04:19 +00:00
exit ;