3 /** jsupdated.php - notes by Martin Langhoff <martin@catalyst.net.nz>
5 ** This is an alternative version of jsupdate.php that acts
6 ** as a long-running daemon. It will feed/stall/feed JS updates
7 ** to the client. From the module configuration select "Stream"
10 ** The client connection is not forever though. Once we reach
11 ** CHAT_MAX_CLIENT_UPDATES, it will force the client to re-fetch it.
13 ** This buys us all the benefits that chatd has, minus the setup,
14 ** as we are using apache to do the daemon handling.
19 define('CHAT_MAX_CLIENT_UPDATES', 1000);
20 $nomoodlecookie = true; // Session not needed!
22 require('../../../config.php');
23 require('../lib.php');
25 // we are going to run for a long time
26 // avoid being terminated by php
29 $chat_sid = required_param('chat_sid', PARAM_ALPHANUM
);
30 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT
);
31 $chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT
);
32 $chat_lastid = optional_param('chat_lastid', 0, PARAM_INT
);
34 if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) {
35 error('Not logged in!');
38 //Get the minimal course
39 if (!$course = get_record('course','id',$chatuser->course
,'','','','','id,theme,lang')) {
40 error('incorrect course id');
43 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
44 // chat_format_message_manually() -- and only id and timezone are used.
45 if (!$USER = get_record('user','id',$chatuser->userid
,'','','','','id, lang, theme, username, timezone')) {
46 error('User does not exist!');
48 $USER->description
= '';
50 //Setup course, lang and theme
51 course_setup($course);
53 // force deleting of timed out users if there is a silence in room or just entering
54 if ((time() - $chat_lasttime) > $CFG->chat_old_ping
) {
55 // must be done before chat_get_latest_message!!!
56 chat_delete_old_users();
60 // Time to send headers, and lay out the basic JS updater page
62 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
63 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
64 header('Cache-Control: no-cache, must-revalidate');
65 header('Pragma: no-cache');
66 header('Content-Type: text/html; charset=utf-8');
68 /// required stylesheets
69 $stylesheetshtml = '';
70 foreach ($CFG->stylesheets
as $stylesheet) {
71 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
75 <!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
78 <meta http
-equiv
="content-type" content
="text/html; charset=utf-8" />
79 <script type
="text/javascript">
81 if (parent
.msg
.document
.getElementById("msgStarted") == null) {
82 parent
.msg
.document
.close();
83 parent
.msg
.document
.open("text/html","replace");
84 parent
.msg
.document
.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
85 parent
.msg
.document
.write("<html><head>");
86 parent
.msg
.document
.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
87 parent
.msg
.document
.write("<base target=\"_blank\" />");
88 parent
.msg
.document
.write("<?php echo addslashes_js($stylesheetshtml) ?>");
89 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>");
98 // Ensure the HTML head makes it out there
99 echo $CHAT_DUMMY_DATA;
102 for ($n=0; $n <= CHAT_MAX_CLIENT_UPDATES
; $n++
) {
104 // ping first so we can later shortcut as needed.
105 $chatuser->lastping
= time();
106 set_field('chat_users', 'lastping', $chatuser->lastping
, 'id', $chatuser->id
);
108 if ($message = chat_get_latest_message($chatuser->chatid
, $chatuser->groupid
)) {
109 $chat_newlasttime = $message->timestamp
;
110 $chat_newlastid = $message->id
;
112 $chat_newlasttime = 0;
115 print $CHAT_DUMMY_DATA;
117 sleep($CFG->chat_refresh_room
);
124 $groupselect = $chatuser->groupid ?
" AND (groupid='".$chatuser->groupid
."' OR groupid='0') " : "";
127 if ($chat_lastid > 0) {
128 $newcriteria = "id > $chat_lastid";
130 if ($chat_lasttime == 0) { //display some previous messages
131 $chat_lasttime = $timenow - $CFG->chat_old_ping
; //TO DO - any better value??
133 $newcriteria = "timestamp > $chat_lasttime";
136 $messages = get_records_select("chat_messages",
137 "chatid = '$chatuser->chatid' AND $newcriteria $groupselect",
141 $num = count($messages);
144 print $CHAT_DUMMY_DATA;
146 sleep($CFG->chat_refresh_room
);
151 print '<script type="text/javascript">' . "\n";
152 print "//<![CDATA[\n\n";
154 $chat_newrow = ($chat_lastrow +
$num) %
2;
156 $refreshusers = false;
158 if (($chat_lasttime != $chat_newlasttime) and $messages) {
161 $refreshusers = false;
162 foreach ($messages as $message) {
163 $chat_lastrow = ($chat_lastrow +
1) %
2;
164 $formatmessage = chat_format_message($message, $chatuser->course
, $USER, $chat_lastrow);
165 if ($formatmessage->beep
) {
168 if ($formatmessage->refreshusers
) {
169 $refreshusers = true;
171 $us[$message->userid
] = $timenow - $message->timestamp
;
172 echo "parent.msg.document.write('".addslashes_js($formatmessage->html
)."\\n');\n";
175 // from the last message printed...
176 // a strange case where lack of closures is useful!
177 $chat_lasttime = $message->timestamp
;
178 $chat_lastid = $message->id
;
182 echo "if (parent.users.document.anchors[0] != null) {" .
183 "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
185 foreach($us as $uid=>$lastping) {
186 $min = (int) ($lastping/60);
187 $sec = $lastping - ($min*60);
188 $min = $min < 10 ?
'0'.$min : $min;
189 $sec = $sec < 10 ?
'0'.$sec : $sec;
190 $idle = $min.':'.$sec;
191 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
192 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
196 print 'parent.msg.scroll(1,5000000);' . "\n\n";
198 print '</script>' . "\n\n";
200 print '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
202 print $CHAT_DUMMY_DATA;
204 sleep($CFG->chat_refresh_room
);
205 } // here ends the for() loop
207 // here & should be written & :-D
208 $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";
209 print '<script type="text/javascript">' . "\n";
210 print "//<![CDATA[ \n\n";
211 print "location.href = '$refreshurl';\n";
213 print '</script>' . "\n\n";