MDL-8323 finished full conversion to proper $COURSE global - no more $CFG->coursethem...
[moodle.git] / mod / chat / gui_header_js / jsupdate.php
CommitLineData
516121bd 1<?php // $Id$
1515a89e 2
516121bd 3 $nomoodlecookie = true; // Session not needed!
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
516121bd 12 if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) {
13 error('Not logged in!');
14 }
f7e648da 15
9152fc99 16 //Get the minimal course
17 if (!$course = get_record('course','id',$chatuser->course,'','','','','id,theme,lang')) {
18 error('incorrect course id');
aa5c32fd 19 }
a6d1886f 20
21 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
9152fc99 22 if (!$USER = get_record('user','id',$chatuser->userid,'','','','','id, lang, theme, username, timezone')) {
a6d1886f 23 error('User does not exist!');
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
48 $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : "";
49
aa5c32fd 50 $messages = get_records_select("chat_messages",
51 "chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime' $groupselect",
52 "timestamp ASC");
53
54 if ($messages) {
55 $num = count($messages);
56 } else {
57 $num = 0;
58 }
59
60 $chat_newrow = ($chat_lastrow + $num) % 2;
61
dfd1d9e2 62 // no &amp; in url, does not work in header!
63 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&chat_lastrow=$chat_newrow";
0cdf34fb 64 $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 65
516121bd 66 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
67 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
68 header('Cache-Control: no-cache, must-revalidate');
69 header('Pragma: no-cache');
810944af 70 header('Content-Type: text/html; charset=utf-8');
516121bd 71 header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
1515a89e 72
68eb9f32 73 /// required stylesheets
74 $stylesheetshtml = '';
75 foreach ($CFG->stylesheets as $stylesheet) {
4702be4e 76 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
68eb9f32 77 }
78
dfd1d9e2 79 // use ob to be able to send Content-Length headers
80 // needed for Keep-Alive to work
81 ob_start();
82
1515a89e 83?>
aa5c32fd 84<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
516121bd 85<html>
86 <head>
0cdf34fb 87 <title> </title>
810944af 88 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
516121bd 89 <script type="text/javascript">
0cdf34fb 90 //<![CDATA[
2debdf04 91 if (parent.msg.document.getElementById("msgStarted") == null) {
92 parent.msg.document.close();
93 parent.msg.document.open("text/html","replace");
aa5c32fd 94 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 95 parent.msg.document.write("<html><head>");
810944af 96 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
b9795750 97 parent.msg.document.write("<base target=\"_blank\" />");
4702be4e 98 parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
0cdf34fb 99 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 100 }
516121bd 101 <?php
102 $beep = false;
103 $refreshusers = false;
104 $us = array ();
aa5c32fd 105 if (($chat_lasttime != $chat_newlasttime) and $messages) {
516121bd 106
516121bd 107 foreach ($messages as $message) {
aa5c32fd 108 $chat_lastrow = ($chat_lastrow + 1) % 2;
a6d1886f 109 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
516121bd 110 if ($formatmessage->beep) {
111 $beep = true;
112 }
113 if ($formatmessage->refreshusers) {
114 $refreshusers = true;
115 }
116 $us[$message->userid] = $timenow - $message->timestamp;
4702be4e 117 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
1515a89e 118 }
516121bd 119 }
120
121 $chatuser->lastping = time();
46c85976 122 set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id );
516121bd 123
124 if ($refreshusers) {
3dfd307f 125 echo "if (parent.users.document.anchors[0] != null) {" .
126 "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
516121bd 127 } else {
128 foreach($us as $uid=>$lastping) {
129 $min = (int) ($lastping/60);
130 $sec = $lastping - ($min*60);
131 $min = $min < 10 ? '0'.$min : $min;
132 $sec = $sec < 10 ? '0'.$sec : $sec;
133 $idle = $min.':'.$sec;
3dfd307f 134 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
135 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
516121bd 136 }
137 }
138 ?>
139 parent.msg.scroll(1,5000000);
0cdf34fb 140 //]]>
516121bd 141 </script>
142 </head>
143 <body>
144 <?php
145 if ($beep) {
146 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
147 }
148 ?>
0cdf34fb 149 <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
516121bd 150 </body>
151</html>
dfd1d9e2 152<?php
153
154// support HTTP Keep-Alive
155header("Content-Length: " . ob_get_length() );
156ob_end_flush();
157exit;
158
159
160?>