mod-choice MDL-19805 Added PAGE->set_url calls and removed deprecated functions
[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
c13a5e71 17 if (!$course = $DB->get_record('course', array('id'=>$chatuser->course))) {
2f52a088 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
c13a5e71 28 $PAGE->set_course($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>
810944af 89 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
516121bd 90 <script type="text/javascript">
0cdf34fb 91 //<![CDATA[
f298a625 92 function safari_refresh() {
93 self.location.href= '<?php echo $refreshurl;?>';
94 }
95 var issafari = false;
96 if(window.devicePixelRatio){
97 issafari = true;
98 setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room*1000;?>);
99 }
5537bb04 100 if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
2debdf04 101 parent.msg.document.close();
102 parent.msg.document.open("text/html","replace");
aa5c32fd 103 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 104 parent.msg.document.write("<html><head>");
810944af 105 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
b9795750 106 parent.msg.document.write("<base target=\"_blank\" />");
4702be4e 107 parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
0cdf34fb 108 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 109 }
516121bd 110 <?php
111 $beep = false;
112 $refreshusers = false;
113 $us = array ();
aa5c32fd 114 if (($chat_lasttime != $chat_newlasttime) and $messages) {
516121bd 115
516121bd 116 foreach ($messages as $message) {
aa5c32fd 117 $chat_lastrow = ($chat_lastrow + 1) % 2;
a6d1886f 118 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
516121bd 119 if ($formatmessage->beep) {
120 $beep = true;
121 }
122 if ($formatmessage->refreshusers) {
123 $refreshusers = true;
124 }
125 $us[$message->userid] = $timenow - $message->timestamp;
5537bb04 126 echo "if(parent.msg)";
4702be4e 127 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
1515a89e 128 }
516121bd 129 }
130
131 $chatuser->lastping = time();
d3bf6f92 132 $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
516121bd 133
134 if ($refreshusers) {
5537bb04 135 ?>
136 var link = parent.users.document.getElementById('refreshLink');
137 if (link != null) {
138 parent.users.location.href = link.href;
139 }
140 <?php
516121bd 141 } else {
142 foreach($us as $uid=>$lastping) {
143 $min = (int) ($lastping/60);
144 $sec = $lastping - ($min*60);
145 $min = $min < 10 ? '0'.$min : $min;
146 $sec = $sec < 10 ? '0'.$sec : $sec;
147 $idle = $min.':'.$sec;
5537bb04 148 echo "if (parent.users && parent.users.document.getElementById('uidle{$uid}') != null) {".
3dfd307f 149 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
516121bd 150 }
151 }
152 ?>
f836189a 153 if(parent.input){
154 var autoscroll = parent.input.document.getElementById('auto');
155 if(parent.msg && autoscroll && autoscroll.checked){
156 parent.msg.scroll(1,5000000);
157 }
4ee00a30 158 }
0cdf34fb 159 //]]>
516121bd 160 </script>
161 </head>
162 <body>
163 <?php
164 if ($beep) {
165 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
166 }
167 ?>
0cdf34fb 168 <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
516121bd 169 </body>
170</html>
dfd1d9e2 171<?php
172
173// support HTTP Keep-Alive
174header("Content-Length: " . ob_get_length() );
5537bb04 175ob_end_flush();
dfd1d9e2 176exit;
177
178
179?>