Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/chat/gui_header_js/jsupdate.php
Go to the documentation of this file.
00001 <?php
00002 
00003 define('NO_MOODLE_COOKIES', true); // session not used here
00004 
00005 require('../../../config.php');
00006 require('../lib.php');
00007 
00008 $chat_sid      = required_param('chat_sid', PARAM_ALPHANUM);
00009 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
00010 $chat_lastrow  = optional_param('chat_lastrow', 1, PARAM_INT);
00011 
00012 $url = new moodle_url('/mod/chat/gui_header_js/jsupdate.php', array('chat_sid'=>$chat_sid));
00013 if ($chat_lasttime !== 0) {
00014     $url->param('chat_lasttime', $chat_lasttime);
00015 }
00016 if ($chat_lastrow !== 1) {
00017     $url->param('chat_lastrow', $chat_lastrow);
00018 }
00019 $PAGE->set_url($url);
00020 
00021 
00022 if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
00023     print_error('notlogged', 'chat');
00024 }
00025 
00026 //Get the minimal course
00027 if (!$course = $DB->get_record('course', array('id'=>$chatuser->course))) {
00028     print_error('invalidcourseid');
00029 }
00030 
00031 //Get the user theme and enough info to be used in chat_format_message() which passes it along to
00032 if (!$USER = $DB->get_record('user', array('id'=>$chatuser->userid))) { // no optimisation here, it would break again in future!
00033     print_error('invaliduser');
00034 }
00035 $USER->description = '';
00036 
00037 //Setup course, lang and theme
00038 $PAGE->set_course($course);
00039 
00040 // force deleting of timed out users if there is a silence in room or just entering
00041 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
00042     // must be done before chat_get_latest_message!!!
00043     chat_delete_old_users();
00044 }
00045 
00046 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
00047     $chat_newlasttime = $message->timestamp;
00048 } else {
00049     $chat_newlasttime = 0;
00050 }
00051 
00052 if ($chat_lasttime == 0) { //display some previous messages
00053     $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
00054 }
00055 
00056 $timenow    = time();
00057 
00058 $params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
00059 
00060 $groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
00061 
00062 $messages = $DB->get_records_select("chat_messages_current",
00063                     "chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
00064                     "timestamp ASC");
00065 
00066 if ($messages) {
00067     $num = count($messages);
00068 } else {
00069     $num = 0;
00070 }
00071 
00072 $chat_newrow = ($chat_lastrow + $num) % 2;
00073 
00074 // no &amp; in url, does not work in header!
00075 $refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&chat_lastrow=$chat_newrow";
00076 $refreshurlamp = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&amp;chat_lasttime=$chat_newlasttime&amp;chat_lastrow=$chat_newrow";
00077 
00078 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
00079 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
00080 header('Cache-Control: no-cache, must-revalidate');
00081 header('Pragma: no-cache');
00082 header('Content-Type: text/html; charset=utf-8');
00083 header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
00084 
00086 $stylesheetshtml = '';
00087 /*foreach ($CFG->stylesheets as $stylesheet) {
00088     //TODO: MDL-21120
00089     $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
00090 }*/
00091 
00092 // use ob to be able to send Content-Length headers
00093 // needed for Keep-Alive to work
00094 ob_start();
00095 
00096 ?>
00097 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
00098 <html>
00099     <head>
00100         <meta http-equiv="content-type" content="text/html; charset=utf-8" />
00101         <script type="text/javascript">
00102         //<![CDATA[
00103         function safari_refresh() {
00104             self.location.href= '<?php echo $refreshurl;?>';
00105         }
00106         var issafari = false;
00107         if(window.devicePixelRatio){
00108             issafari = true;
00109             setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room*1000;?>);
00110         }
00111         if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
00112             parent.msg.document.close();
00113             parent.msg.document.open("text/html","replace");
00114             parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
00115             parent.msg.document.write("<html><head>");
00116             parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
00117             parent.msg.document.write("<base target=\"_blank\" />");
00118             parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
00119             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>");
00120         }
00121         <?php
00122         $beep = false;
00123         $refreshusers = false;
00124         $us = array ();
00125         if (($chat_lasttime != $chat_newlasttime) and $messages) {
00126 
00127             foreach ($messages as $message) {
00128                 $chat_lastrow = ($chat_lastrow + 1) % 2;
00129                 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
00130                 if ($formatmessage->beep) {
00131                      $beep = true;
00132                 }
00133                 if ($formatmessage->refreshusers) {
00134                      $refreshusers = true;
00135                 }
00136                 $us[$message->userid] = $timenow - $message->timestamp;
00137                 echo "if(parent.msg)";
00138                 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
00139              }
00140         }
00141 
00142         $chatuser->lastping = time();
00143         $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
00144 
00145         if ($refreshusers) {
00146         ?>
00147         var link = parent.users.document.getElementById('refreshLink');
00148         if (link != null) {
00149             parent.users.location.href = link.href;
00150         }
00151         <?php
00152         } else {
00153             foreach($us as $uid=>$lastping) {
00154                 $min = (int) ($lastping/60);
00155                 $sec = $lastping - ($min*60);
00156                 $min = $min < 10 ? '0'.$min : $min;
00157                 $sec = $sec < 10 ? '0'.$sec : $sec;
00158                 $idle = $min.':'.$sec;
00159                 echo "if (parent.users && parent.users.document.getElementById('uidle{$uid}') != null) {".
00160                         "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
00161             }
00162         }
00163         ?>
00164         if(parent.input){
00165             var autoscroll = parent.input.document.getElementById('auto');
00166             if(parent.msg && autoscroll && autoscroll.checked){
00167                 parent.msg.scroll(1,5000000);
00168             }
00169         }
00170         //]]>
00171         </script>
00172     </head>
00173     <body>
00174        <?php
00175             if ($beep) {
00176                 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
00177             }
00178         ?>
00179        <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
00180     </body>
00181 </html>
00182 <?php
00183 
00184 // support HTTP Keep-Alive
00185 header("Content-Length: " . ob_get_length() );
00186 ob_end_flush();
00187 exit;
00188 
00189 
00190 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations