[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/chat/gui_header_js/ -> jsupdated.php (source)

   1  <?php  // $Id: jsupdated.php,v 1.8.2.2 2008/10/14 08:38:26 dongsheng Exp $
   2  
   3  /** jsupdated.php - notes by Martin Langhoff <martin@catalyst.net.nz>
   4   ** 
   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"
   8   ** updates.
   9   ** 
  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. 
  12   ** 
  13   ** This buys us all the benefits that chatd has, minus the setup,
  14   ** as we are using apache to do the daemon handling.
  15   **
  16   **/
  17  
  18  
  19      define('CHAT_MAX_CLIENT_UPDATES', 1000);
  20      $nomoodlecookie = true;     // Session not needed!
  21  
  22      require('../../../config.php');
  23      require ('../lib.php');
  24  
  25      // we are going to run for a long time
  26      // avoid being terminated by php
  27      @set_time_limit(0);
  28  
  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);
  33  
  34      if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) {
  35          error('Not logged in!');
  36      }
  37  
  38      //Get the minimal course
  39      if (!$course = get_record('course','id',$chatuser->course,'','','','','id,theme,lang')) {
  40          error('incorrect course id');
  41      }
  42  
  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)) { // no optimisation here, it would break again in future!
  46          error('User does not exist!');
  47      }
  48      $USER->description = '';
  49  
  50      //Setup course, lang and theme
  51      course_setup($course);
  52  
  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();
  57      }
  58  
  59      //
  60      // Time to send headers, and lay out the basic JS updater page
  61      //
  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');
  67  
  68      /// required stylesheets
  69      $stylesheetshtml = '';
  70      foreach ($CFG->stylesheets as $stylesheet) {
  71          $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
  72      }
  73  
  74  ?>
  75  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  76  <html>
  77      <head>
  78          <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  79          <script type="text/javascript">
  80          //<![CDATA[
  81          function safari_refresh() {
  82              self.location.href= '<?php echo $refreshurl;?>';
  83          }
  84          var issafari = false;
  85          if(window.devicePixelRatio){
  86              issafari = true;
  87              setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room*1000;?>);
  88          }
  89          if (parent.msg.document.getElementById("msgStarted") == null) {
  90              parent.msg.document.close();
  91              parent.msg.document.open("text/html","replace");
  92              parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
  93              parent.msg.document.write("<html><head>");
  94              parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
  95              parent.msg.document.write("<base target=\"_blank\" />");
  96              parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
  97              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>");
  98          }
  99          //]]>
 100          </script>
 101     </head>
 102     <body>
 103  
 104  <?php
 105  
 106      // Ensure the HTML head makes it out there
 107      echo $CHAT_DUMMY_DATA;
 108      @ob_end_flush();   
 109  
 110      for ($n=0; $n <= CHAT_MAX_CLIENT_UPDATES; $n++) { 
 111          
 112          // ping first so we can later shortcut as needed.
 113          $chatuser->lastping = time();
 114          set_field('chat_users', 'lastping', $chatuser->lastping, 'id', $chatuser->id  );
 115  
 116          if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
 117              $chat_newlasttime = $message->timestamp;
 118              $chat_newlastid   = $message->id;
 119          } else {
 120              $chat_newlasttime = 0;
 121              $chat_newlastid   = 0;
 122              print " \n";
 123              print $CHAT_DUMMY_DATA;
 124              @ob_end_flush();
 125              sleep($CFG->chat_refresh_room);
 126              continue;
 127          }
 128  
 129          $timenow    = time();
 130                  
 131                  
 132          $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : "";
 133  
 134          $newcriteria = '';
 135          if ($chat_lastid > 0) {
 136              $newcriteria = "id > $chat_lastid";
 137          } else {
 138              if ($chat_lasttime == 0) { //display some previous messages
 139                  $chat_lasttime = $timenow - $CFG->chat_old_ping; //TO DO - any better value??
 140              }
 141              $newcriteria = "timestamp > $chat_lasttime";
 142          }
 143          
 144          $messages = get_records_select("chat_messages",
 145                                         "chatid = '$chatuser->chatid' AND $newcriteria $groupselect",
 146                                         "timestamp ASC");
 147          
 148          if ($messages) {
 149              $num = count($messages);
 150          } else {
 151              print " \n";
 152              print $CHAT_DUMMY_DATA;
 153              @ob_end_flush();
 154              sleep($CFG->chat_refresh_room);
 155              continue;
 156              $num = 0;
 157          }
 158  
 159          print '<script type="text/javascript">' . "\n";
 160          print "//<![CDATA[\n\n"; 
 161  
 162          $chat_newrow = ($chat_lastrow + $num) % 2;
 163  
 164          $refreshusers = false;
 165          $us = array ();
 166          if (($chat_lasttime != $chat_newlasttime) and $messages) {
 167  
 168              $beep         = false;
 169              $refreshusers = false; 
 170              foreach ($messages as $message) {
 171                  $chat_lastrow = ($chat_lastrow + 1) % 2;
 172                  $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
 173                  if ($formatmessage->beep) {
 174                      $beep = true;
 175                  }
 176                  if ($formatmessage->refreshusers) {
 177                      $refreshusers = true;
 178                  }
 179                  $us[$message->userid] = $timenow - $message->timestamp;
 180                  echo "parent.msg.document.write('".addslashes_js($formatmessage->html )."\\n');\n";
 181  
 182              }
 183              // from the last message printed...
 184              // a strange case where lack of closures is useful!
 185              $chat_lasttime = $message->timestamp;
 186              $chat_lastid   = $message->id;
 187          }
 188                  
 189          if ($refreshusers) {
 190              echo "if (parent.users.document.anchors[0] != null) {" .
 191                  "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
 192          } else {
 193              foreach($us as $uid=>$lastping) {
 194                  $min = (int) ($lastping/60);
 195                  $sec = $lastping - ($min*60);
 196                  $min = $min < 10 ? '0'.$min : $min;
 197                  $sec = $sec < 10 ? '0'.$sec : $sec;
 198                  $idle = $min.':'.$sec;
 199                  echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
 200                          "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
 201              }
 202          }
 203  
 204          print <<<EOD
 205          if(parent.input){
 206              var autoscroll = parent.input.document.getElementById('auto');
 207              if(parent.msg && autoscroll && autoscroll.checked){
 208                  parent.msg.scroll(1,5000000);
 209              }
 210          }
 211  EOD;
 212          print "//]]>\n";
 213          print '</script>' . "\n\n";
 214          if ($beep) {
 215              print '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
 216          }
 217          print $CHAT_DUMMY_DATA;
 218          @ob_end_flush();
 219          sleep($CFG->chat_refresh_room);
 220      } // here ends the for() loop 
 221  
 222      // here & should be written & :-D
 223      $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"; 
 224      print '<script type="text/javascript">' . "\n";
 225      print "//<![CDATA[ \n\n"; 
 226      print "location.href = '$refreshurl';\n";
 227      print "//]]>\n";
 228      print '</script>' . "\n\n";
 229  
 230  ?>
 231  
 232      </body>
 233  </html>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7