MDL-16810 - fix typo on function name. thanks to Jason Hardin.
[moodle.git] / mod / chat / gui_header_js / jsupdate.php
CommitLineData
516121bd 1<?php // $Id$
1515a89e 2
f836189a 3 define('NO_MOODLE_COOKIES', true); // session not used here
1515a89e 4
516121bd 5 require('../../../config.php');
6 require('../lib.php');
84a2fdd7 7
516121bd 8 $chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
9 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
aa5c32fd 10 $chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT);
1515a89e 11
2f52a088 12 if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
13 print_error('notlogged', 'chat');
516121bd 14 }
f7e648da 15
9152fc99 16 //Get the minimal course
2f52a088 17 if (!$course = $DB->get_record('course', array('id'=>$chatuser->course), 'id,theme,lang')) {
18 print_error('invalidcourseid');
aa5c32fd 19 }
a6d1886f 20
21 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
2f52a088 22 if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) { // no optimisation here, it would break again in future!
23 print_error('invaliduser');
a6d1886f 24 }
25 $USER->description = '';
aa5c32fd 26
9152fc99 27 //Setup course, lang and theme
28 course_setup($course);
f7e648da 29
516121bd 30 // force deleting of timed out users if there is a silence in room or just entering
31 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
32 // must be done before chat_get_latest_message!!!
33 chat_delete_old_users();
34 }
1515a89e 35
516121bd 36 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
37 $chat_newlasttime = $message->timestamp;
38 } else {
39 $chat_newlasttime = 0;
40 }
1515a89e 41
516121bd 42 if ($chat_lasttime == 0) { //display some previous messages
43 $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
44 }
1515a89e 45
516121bd 46 $timenow = time();
47
d3bf6f92 48 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
49
50 $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
516121bd 51
6e5f40ea 52 $messages = $DB->get_records_select("chat_messages_current",
d3bf6f92 53 "chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
aa5c32fd 54 "timestamp ASC");
55
56 if ($messages) {
57 $num = count($messages);
58 } else {
59 $num = 0;
60 }
61
62 $chat_newrow = ($chat_lastrow + $num) % 2;
63
dfd1d9e2 64 // no &amp; in url, does not work in header!
5537bb04 65 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&chat_lastrow=$chat_newrow";
66 $refreshurlamp = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&amp;chat_lasttime=$chat_newlasttime&amp;chat_lastrow=$chat_newrow";
aa5c32fd 67
516121bd 68 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
69 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
70 header('Cache-Control: no-cache, must-revalidate');
71 header('Pragma: no-cache');
810944af 72 header('Content-Type: text/html; charset=utf-8');
516121bd 73 header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
1515a89e 74
68eb9f32 75 /// required stylesheets
76 $stylesheetshtml = '';
77 foreach ($CFG->stylesheets as $stylesheet) {
4702be4e 78 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
68eb9f32 79 }
80
dfd1d9e2 81 // use ob to be able to send Content-Length headers
82 // needed for Keep-Alive to work
83 ob_start();
84
1515a89e 85?>
aa5c32fd 86<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
516121bd 87<html>
88 <head>
0cdf34fb 89 <title> </title>
810944af 90 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
516121bd 91 <script type="text/javascript">
0cdf34fb 92 //<![CDATA[
5537bb04 93 if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
2debdf04 94 parent.msg.document.close();
95 parent.msg.document.open("text/html","replace");
aa5c32fd 96 parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
2debdf04 97 parent.msg.document.write("<html><head>");
810944af 98 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
b9795750 99 parent.msg.document.write("<base target=\"_blank\" />");
4702be4e 100 parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
0cdf34fb 101 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\">&nbsp;<\/div>");
2debdf04 102 }
516121bd 103 <?php
104 $beep = false;
105 $refreshusers = false;
106 $us = array ();
aa5c32fd 107 if (($chat_lasttime != $chat_newlasttime) and $messages) {
516121bd 108
516121bd 109 foreach ($messages as $message) {
aa5c32fd 110 $chat_lastrow = ($chat_lastrow + 1) % 2;
a6d1886f 111 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
516121bd 112 if ($formatmessage->beep) {
113 $beep = true;
114 }
115 if ($formatmessage->refreshusers) {
116 $refreshusers = true;
117 }
118 $us[$message->userid] = $timenow - $message->timestamp;
5537bb04 119 echo "if(parent.msg)";
4702be4e 120 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
1515a89e 121 }
516121bd 122 }
123
124 $chatuser->lastping = time();
d3bf6f92 125 $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
516121bd 126
127 if ($refreshusers) {
5537bb04 128 ?>
129 var link = parent.users.document.getElementById('refreshLink');
130 if (link != null) {
131 parent.users.location.href = link.href;
132 }
133 <?php
516121bd 134 } else {
135 foreach($us as $uid=>$lastping) {
136 $min = (int) ($lastping/60);
137 $sec = $lastping - ($min*60);
138 $min = $min < 10 ? '0'.$min : $min;
139 $sec = $sec < 10 ? '0'.$sec : $sec;
140 $idle = $min.':'.$sec;
5537bb04 141 echo "if (parent.users && parent.users.document.getElementById('uidle{$uid}') != null) {".
3dfd307f 142 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
516121bd 143 }
144 }
145 ?>
f836189a 146 if(parent.input){
147 var autoscroll = parent.input.document.getElementById('auto');
148 if(parent.msg && autoscroll && autoscroll.checked){
149 parent.msg.scroll(1,5000000);
150 }
4ee00a30 151 }
0cdf34fb 152 //]]>
516121bd 153 </script>
154 </head>
155 <body>
156 <?php
157 if ($beep) {
158 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
159 }
160 ?>
0cdf34fb 161 <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
516121bd 162 </body>
163</html>
dfd1d9e2 164<?php
165
166// support HTTP Keep-Alive
167header("Content-Length: " . ob_get_length() );
5537bb04 168ob_end_flush();
dfd1d9e2 169exit;
170
171
172?>