polished.
[moodle.git] / mod / chat / gui_header_js / jsupdate.php
CommitLineData
516121bd 1<?php // $Id$
1515a89e 2
516121bd 3 $nomoodlecookie = true; // Session not needed!
1515a89e 4
516121bd 5 require('../../../config.php');
6 require('../lib.php');
84a2fdd7 7
516121bd 8 $chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
9 $chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
1515a89e 10
516121bd 11 if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) {
12 error('Not logged in!');
13 }
f7e648da 14
516121bd 15 chat_force_language($chatuser->lang);
f7e648da 16
516121bd 17 // force deleting of timed out users if there is a silence in room or just entering
18 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
19 // must be done before chat_get_latest_message!!!
20 chat_delete_old_users();
21 }
1515a89e 22
516121bd 23 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
24 $chat_newlasttime = $message->timestamp;
25 } else {
26 $chat_newlasttime = 0;
27 }
1515a89e 28
516121bd 29 if ($chat_lasttime == 0) { //display some previous messages
30 $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
31 }
1515a89e 32
516121bd 33 $refreshurl = "jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime"; // no &amp; in url, does not work in header!
34 $timenow = time();
35
36 $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : "";
37
38 header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
39 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
40 header('Cache-Control: no-cache, must-revalidate');
41 header('Pragma: no-cache');
42 header('Content-Type: text/html');
43 header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
1515a89e 44
68eb9f32 45 /// required stylesheets
46 $stylesheetshtml = '';
47 foreach ($CFG->stylesheets as $stylesheet) {
48 $stylesheetshtml .= '<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"'.$stylesheet.'\\" />';
49 }
50
1515a89e 51?>
516121bd 52<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
53<html>
54 <head>
55 <meta http-equiv="content-type" content="text/html; charset=<?php echo get_string('thischarset'); ?>" />
56 <script type="text/javascript">
57 <!--
2debdf04 58 if (parent.msg.document.getElementById("msgStarted") == null) {
59 parent.msg.document.close();
60 parent.msg.document.open("text/html","replace");
61 parent.msg.document.write("<html><head>");
62 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=<?php echo get_string('thischarset'); ?>\" />");
b9795750 63 parent.msg.document.write("<base target=\"_blank\" />");
68eb9f32 64 parent.msg.document.write("<?php echo $stylesheetshtml ?>");
2debdf04 65 parent.msg.document.write("</head><body><div style=\"display: none\" id=\"msgStarted\">&nbsp;</div>");
66 }
516121bd 67 <?php
68 $beep = false;
69 $refreshusers = false;
70 $us = array ();
71 if (($chat_lasttime != $chat_newlasttime)
72 and $messages = get_records_select("chat_messages",
73 "chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime' $groupselect",
74 "timestamp ASC")) {
75
76 if (!$currentuser = get_record('user', 'id', $chatuser->userid)) {
77 error('User does not exist!');
78 }
79 $currentuser->description = '';
80
81 foreach ($messages as $message) {
82 $formatmessage = chat_format_message($message, $chatuser->course, $currentuser);
83 if ($formatmessage->beep) {
84 $beep = true;
85 }
86 if ($formatmessage->refreshusers) {
87 $refreshusers = true;
88 }
89 $us[$message->userid] = $timenow - $message->timestamp;
90 echo "parent.msg.document.write('".addslashes($formatmessage->html)."\\n');\n";
1515a89e 91 }
516121bd 92 }
93
94 $chatuser->lastping = time();
95 update_record('chat_users', $chatuser);
96
97 if ($refreshusers) {
3dfd307f 98 echo "if (parent.users.document.anchors[0] != null) {" .
99 "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
516121bd 100 } else {
101 foreach($us as $uid=>$lastping) {
102 $min = (int) ($lastping/60);
103 $sec = $lastping - ($min*60);
104 $min = $min < 10 ? '0'.$min : $min;
105 $sec = $sec < 10 ? '0'.$sec : $sec;
106 $idle = $min.':'.$sec;
3dfd307f 107 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
108 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
516121bd 109 }
110 }
111 ?>
112 parent.msg.scroll(1,5000000);
113 // -->
114 </script>
115 </head>
116 <body>
117 <?php
118 if ($beep) {
119 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
120 }
121 ?>
3dfd307f 122 <a href="<? echo $refreshurl ?>" name="refreshLink">Refresh link</a>
516121bd 123 </body>
124</html>