Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/chat/gui_header_js/jsupdated.php
Go to the documentation of this file.
00001 <?php
00002 
00019 define('CHAT_MAX_CLIENT_UPDATES', 1000);
00020 define('NO_MOODLE_COOKIES', true); // session not used here
00021 define('NO_OUTPUT_BUFFERING', true);
00022 
00023 require('../../../config.php');
00024 require('../lib.php');
00025 
00026 // we are going to run for a long time
00027 // avoid being terminated by php
00028 @set_time_limit(0);
00029 
00030 $chat_sid      = required_param('chat_sid',          PARAM_ALPHANUM);
00031 $chat_lasttime = optional_param('chat_lasttime',  0, PARAM_INT);
00032 $chat_lastrow  = optional_param('chat_lastrow',   1, PARAM_INT);
00033 $chat_lastid   = optional_param('chat_lastid',    0, PARAM_INT);
00034 
00035 $url = new moodle_url('/mod/chat/gui_header_js/jsupdated.php', array('chat_sid'=>$chat_sid));
00036 if ($chat_lasttime !== 0) {
00037     $url->param('chat_lasttime', $chat_lasttime);
00038 }
00039 if ($chat_lastrow !== 1) {
00040     $url->param('chat_lastrow', $chat_lastrow);
00041 }
00042 if ($chat_lastid !== 1) {
00043     $url->param('chat_lastid', $chat_lastid);
00044 }
00045 $PAGE->set_url($url);
00046 
00047 if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
00048     print_error('notlogged', 'chat');
00049 }
00050 
00051 //Get the minimal course
00052 if (!$course = $DB->get_record('course', array('id'=>$chatuser->course))) {
00053     print_error('invalidcourseid');
00054 }
00055 
00056 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
00057 // chat_format_message_manually() -- and only id and timezone are used.
00058 if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) { // no optimisation here, it would break again in future!
00059     print_error('invaliduser');
00060 }
00061 $USER->description = '';
00062 
00063 //Setup course, lang and theme
00064 $PAGE->set_course($course);
00065 
00066 // force deleting of timed out users if there is a silence in room or just entering
00067 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
00068     // must be done before chat_get_latest_message!!!
00069     chat_delete_old_users();
00070 }
00071 
00072 //
00073 // Time to send headers, and lay out the basic JS updater page
00074 //
00075 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
00076 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
00077 header('Cache-Control: no-cache, must-revalidate');
00078 header('Pragma: no-cache');
00079 header('Content-Type: text/html; charset=utf-8');
00080 
00082 $stylesheetshtml = '';
00083 /*foreach ($CFG->stylesheets as $stylesheet) {
00084     //TODO: MDL-21120
00085     $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
00086 }*/
00087 
00088 $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";
00089 ?>
00090 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00091 <html>
00092     <head>
00093         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
00094         <script type="text/javascript">
00095         //<![CDATA[
00096         function safari_refresh() {
00097             self.location.href= '<?php echo $refreshurl;?>';
00098         }
00099         var issafari = false;
00100         if(window.devicePixelRatio){
00101             issafari = true;
00102             setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room*1000;?>);
00103         }
00104         if (parent.msg.document.getElementById("msgStarted") == null) {
00105             parent.msg.document.close();
00106             parent.msg.document.open("text/html","replace");
00107             parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
00108             parent.msg.document.write("<html><head>");
00109             parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
00110             parent.msg.document.write("<base target=\"_blank\" />");
00111             parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
00112             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>");
00113         }
00114         //]]>
00115         </script>
00116     </head>
00117     <body>
00118 
00119 <?php
00120 
00121     // Ensure the HTML head makes it out there
00122     echo $CHAT_DUMMY_DATA;
00123 
00124     for ($n=0; $n <= CHAT_MAX_CLIENT_UPDATES; $n++) {
00125 
00126         // ping first so we can later shortcut as needed.
00127         $chatuser->lastping = time();
00128         $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
00129 
00130         if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
00131             $chat_newlasttime = $message->timestamp;
00132             $chat_newlastid   = $message->id;
00133         } else {
00134             $chat_newlasttime = 0;
00135             $chat_newlastid   = 0;
00136             print " \n";
00137             print $CHAT_DUMMY_DATA;
00138             sleep($CFG->chat_refresh_room);
00139             continue;
00140         }
00141 
00142         $timenow    = time();
00143 
00144         $params = array('groupid'=>$chatuser->groupid, 'lastid'=>$chat_lastid, 'lasttime'=>$chat_lasttime, 'chatid'=>$chatuser->chatid);
00145         $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
00146 
00147         $newcriteria = '';
00148         if ($chat_lastid > 0) {
00149             $newcriteria = "id > :lastid";
00150         } else {
00151             if ($chat_lasttime == 0) { //display some previous messages
00152                 $chat_lasttime = $timenow - $CFG->chat_old_ping; //TO DO - any better value??
00153             }
00154             $newcriteria = "timestamp > :lasttime";
00155         }
00156 
00157         $messages = $DB->get_records_select("chat_messages_current",
00158                                        "chatid = :chatid AND $newcriteria $groupselect", $params,
00159                                        "timestamp ASC");
00160 
00161         if ($messages) {
00162             $num = count($messages);
00163         } else {
00164             print " \n";
00165             print $CHAT_DUMMY_DATA;
00166             sleep($CFG->chat_refresh_room);
00167             continue;
00168             $num = 0;
00169         }
00170 
00171         print '<script type="text/javascript">' . "\n";
00172         print "//<![CDATA[\n\n";
00173 
00174         $chat_newrow = ($chat_lastrow + $num) % 2;
00175 
00176         $refreshusers = false;
00177         $us = array ();
00178         if (($chat_lasttime != $chat_newlasttime) and $messages) {
00179 
00180             $beep         = false;
00181             $refreshusers = false;
00182             foreach ($messages as $message) {
00183                 $chat_lastrow = ($chat_lastrow + 1) % 2;
00184                 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
00185                 if ($formatmessage->beep) {
00186                     $beep = true;
00187                 }
00188                 if ($formatmessage->refreshusers) {
00189                     $refreshusers = true;
00190                 }
00191                 $us[$message->userid] = $timenow - $message->timestamp;
00192                 echo "parent.msg.document.write('".addslashes_js($formatmessage->html )."\\n');\n";
00193 
00194             }
00195             // from the last message printed...
00196             // a strange case where lack of closures is useful!
00197             $chat_lasttime = $message->timestamp;
00198             $chat_lastid   = $message->id;
00199         }
00200 
00201         if ($refreshusers) {
00202             echo "if (parent.users.document.anchors[0] != null) {" .
00203                 "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
00204         } else {
00205             foreach($us as $uid=>$lastping) {
00206                 $min = (int) ($lastping/60);
00207                 $sec = $lastping - ($min*60);
00208                 $min = $min < 10 ? '0'.$min : $min;
00209                 $sec = $sec < 10 ? '0'.$sec : $sec;
00210                 $idle = $min.':'.$sec;
00211                 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
00212                         "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
00213             }
00214         }
00215 
00216         print <<<EOD
00217         if(parent.input){
00218             var autoscroll = parent.input.document.getElementById('auto');
00219             if(parent.msg && autoscroll && autoscroll.checked){
00220                 parent.msg.scroll(1,5000000);
00221             }
00222         }
00223 EOD;
00224         print "//]]>\n";
00225         print '</script>' . "\n\n";
00226         if ($beep) {
00227             print '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
00228         }
00229         print $CHAT_DUMMY_DATA;
00230         sleep($CFG->chat_refresh_room);
00231     } // here ends the for() loop
00232 
00233     // here & should be written & :-D
00234     $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";
00235     print '<script type="text/javascript">' . "\n";
00236     print "//<![CDATA[ \n\n";
00237     print "location.href = '$refreshurl';\n";
00238     print "//]]>\n";
00239     print '</script>' . "\n\n";
00240 
00241 ?>
00242 
00243     </body>
00244 </html>
 All Data Structures Namespaces Files Functions Variables Enumerations