MDL-8323 finished full conversion to proper $COURSE global - no more $CFG->coursethem...
[moodle.git] / mod / chat / gui_header_js / jsupdate.php
1 <?php  // $Id$
3     $nomoodlecookie = true;     // Session not needed!
5     require('../../../config.php');
6     require('../lib.php');
8     $chat_sid      = required_param('chat_sid', PARAM_ALPHANUM);
9     $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
10     $chat_lastrow  = optional_param('chat_lastrow', 1, PARAM_INT);
12     if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) {
13         error('Not logged in!');
14     }
16     //Get the minimal course
17     if (!$course = get_record('course','id',$chatuser->course,'','','','','id,theme,lang')) {
18         error('incorrect course id');
19     }
21     //Get the user theme and enough info to be used in chat_format_message() which passes it along to
22     if (!$USER = get_record('user','id',$chatuser->userid,'','','','','id, lang, theme, username, timezone')) {
23         error('User does not exist!');
24     }
25     $USER->description = '';
27     //Setup course, lang and theme
28     course_setup($course);
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     }
36     if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
37         $chat_newlasttime = $message->timestamp;
38     } else {
39         $chat_newlasttime = 0;
40     }
42     if ($chat_lasttime == 0) { //display some previous messages
43         $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
44     }
46     $timenow    = time();
48     $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : "";
50     $messages = get_records_select("chat_messages",
51                         "chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime' $groupselect",
52                         "timestamp ASC");
54     if ($messages) {
55         $num = count($messages);
56     } else {
57         $num = 0;
58     }
60     $chat_newrow = ($chat_lastrow + $num) % 2;
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"; 
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"; 
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');
70     header('Content-Type: text/html; charset=utf-8');
71     header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
73     /// required stylesheets
74     $stylesheetshtml = '';
75     foreach ($CFG->stylesheets as $stylesheet) {
76         $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
77     }
79     // use ob to be able to send Content-Length headers
80     // needed for Keep-Alive to work
81     ob_start();
83 ?>
84 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
85 <html>
86     <head>
87         <title> </title>
88         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
89         <script type="text/javascript">
90         //<![CDATA[
91         if (parent.msg.document.getElementById("msgStarted") == null) {
92             parent.msg.document.close();
93             parent.msg.document.open("text/html","replace");
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\">");
95             parent.msg.document.write("<html><head>");
96             parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
97             parent.msg.document.write("<base target=\"_blank\" />");
98             parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
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>");
100         }
101         <?php
102         $beep = false;
103         $refreshusers = false;
104         $us = array ();
105         if (($chat_lasttime != $chat_newlasttime) and $messages) {
107             foreach ($messages as $message) {
108                 $chat_lastrow = ($chat_lastrow + 1) % 2;
109                 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
110                 if ($formatmessage->beep) {
111                      $beep = true;
112                 }
113                 if ($formatmessage->refreshusers) {
114                      $refreshusers = true;
115                 }
116                 $us[$message->userid] = $timenow - $message->timestamp;
117                 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
118              }
119         }
121         $chatuser->lastping = time();
122         set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id  );
124         if ($refreshusers) {
125             echo "if (parent.users.document.anchors[0] != null) {" .
126                     "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
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;
134                 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
135                         "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
136             }
137         }
138         ?>
139         parent.msg.scroll(1,5000000);
140         //]]>
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         ?>
149        <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
150     </body>
151 </html>
152 <?php
154 // support HTTP Keep-Alive
155 header("Content-Length: " . ob_get_length() );
156 ob_end_flush(); 
157 exit;
160 ?>