mod/chat: make http-chat more database friendly and change msg insertion to use POST
[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);
aa5c32fd 10 $chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT);
1515a89e 11
516121bd 12 if (!$chatuser = get_record('chat_users', 'sid', $chat_sid)) {
13 error('Not logged in!');
14 }
f7e648da 15
aa5c32fd 16 //Get the course theme
17 $course = get_record('course','id',$chatuser->course,'','','','','id,theme');
18 //Set the course theme if necessary
19 if (!empty($course->theme)) {
20 if (!empty($CFG->allowcoursethemes)) {
21 $CFG->coursetheme = $course->theme;
22 }
23 }
24 //Get the user theme
25 $USER = get_record('user','id',$chatuser->userid,'','','','','id, theme');
26
27 //Adjust the prefered theme (main, course, user)
28 theme_setup();
29
516121bd 30 chat_force_language($chatuser->lang);
f7e648da 31
516121bd 32 // force deleting of timed out users if there is a silence in room or just entering
33 if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
34 // must be done before chat_get_latest_message!!!
35 chat_delete_old_users();
36 }
1515a89e 37
516121bd 38 if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
39 $chat_newlasttime = $message->timestamp;
40 } else {
41 $chat_newlasttime = 0;
42 }
1515a89e 43
516121bd 44 if ($chat_lasttime == 0) { //display some previous messages
45 $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
46 }
1515a89e 47
516121bd 48 $timenow = time();
49
50 $groupselect = $chatuser->groupid ? " AND (groupid='".$chatuser->groupid."' OR groupid='0') " : "";
51
aa5c32fd 52 $messages = get_records_select("chat_messages",
53 "chatid = '$chatuser->chatid' AND timestamp > '$chat_lasttime' $groupselect",
54 "timestamp ASC");
55
56 if ($messages) {
57 $num = count($messages);
58 } else {
59 $num = 0;
60 }
61
62 $chat_newrow = ($chat_lastrow + $num) % 2;
63
64 $refreshurl = "jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&chat_lastrow=$chat_newrow"; // no &amp; in url, does not work in header!
65
516121bd 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');
16ad40e1 70 header('Content-Type: text/html; charset='.current_charset());
516121bd 71 header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
1515a89e 72
68eb9f32 73 /// required stylesheets
74 $stylesheetshtml = '';
75 foreach ($CFG->stylesheets as $stylesheet) {
76 $stylesheetshtml .= '<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"'.$stylesheet.'\\" />';
77 }
78
1515a89e 79?>
aa5c32fd 80<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
516121bd 81<html>
82 <head>
b49f9e89 83 <meta http-equiv="content-type" content="text/html; charset=<?php echo current_charset(); ?>" />
516121bd 84 <script type="text/javascript">
85 <!--
2debdf04 86 if (parent.msg.document.getElementById("msgStarted") == null) {
87 parent.msg.document.close();
88 parent.msg.document.open("text/html","replace");
aa5c32fd 89 parent.msg.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
2debdf04 90 parent.msg.document.write("<html><head>");
b49f9e89 91 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=<?php echo current_charset(); ?>\" />");
b9795750 92 parent.msg.document.write("<base target=\"_blank\" />");
68eb9f32 93 parent.msg.document.write("<?php echo $stylesheetshtml ?>");
aa5c32fd 94 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>");
2debdf04 95 }
516121bd 96 <?php
97 $beep = false;
98 $refreshusers = false;
99 $us = array ();
aa5c32fd 100 if (($chat_lasttime != $chat_newlasttime) and $messages) {
516121bd 101
4d5525c6 102 // $currentuser is only used in chat_format_message() which passes it along to
103 // chat_format_message_manually() -- and only id and timezone are used.
104 if (!$currentuser = get_record('user', 'id', $chatuser->userid, '','', '','', 'id, username, timezone')) {
516121bd 105 error('User does not exist!');
106 }
107 $currentuser->description = '';
108
109 foreach ($messages as $message) {
aa5c32fd 110 $chat_lastrow = ($chat_lastrow + 1) % 2;
111 $formatmessage = chat_format_message($message, $chatuser->course, $currentuser, $chat_lastrow);
516121bd 112 if ($formatmessage->beep) {
113 $beep = true;
114 }
115 if ($formatmessage->refreshusers) {
116 $refreshusers = true;
117 }
118 $us[$message->userid] = $timenow - $message->timestamp;
119 echo "parent.msg.document.write('".addslashes($formatmessage->html)."\\n');\n";
1515a89e 120 }
516121bd 121 }
122
123 $chatuser->lastping = time();
4d5525c6 124 set_field('chat_users', 'lastping', $chatuser->lastping, 'sid', $chatuser->sid );
516121bd 125
126 if ($refreshusers) {
3dfd307f 127 echo "if (parent.users.document.anchors[0] != null) {" .
128 "parent.users.location.href = parent.users.document.anchors[0].href;}\n";
516121bd 129 } else {
130 foreach($us as $uid=>$lastping) {
131 $min = (int) ($lastping/60);
132 $sec = $lastping - ($min*60);
133 $min = $min < 10 ? '0'.$min : $min;
134 $sec = $sec < 10 ? '0'.$sec : $sec;
135 $idle = $min.':'.$sec;
3dfd307f 136 echo "if (parent.users.document.getElementById('uidle{$uid}') != null) {".
137 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
516121bd 138 }
139 }
140 ?>
141 parent.msg.scroll(1,5000000);
142 // -->
143 </script>
144 </head>
145 <body>
146 <?php
147 if ($beep) {
148 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
149 }
150 ?>
107e7612 151 <a href="<?php echo $refreshurl ?>" name="refreshLink">Refresh link</a>
516121bd 152 </body>
153</html>