MDL-41999 fix setting of USER in chat
[moodle.git] / mod / chat / gui_header_js / jsupdate.php
CommitLineData
eb588b22 1<?php
2
3define('NO_MOODLE_COOKIES', true); // session not used here
4
5require('../../../config.php');
6require('../lib.php');
7
8$chat_sid = required_param('chat_sid', PARAM_ALPHANUM);
9$chat_lasttime = optional_param('chat_lasttime', 0, PARAM_INT);
10$chat_lastrow = optional_param('chat_lastrow', 1, PARAM_INT);
11
a6855934 12$url = new moodle_url('/mod/chat/gui_header_js/jsupdate.php', array('chat_sid'=>$chat_sid));
eb588b22 13if ($chat_lasttime !== 0) {
14 $url->param('chat_lasttime', $chat_lasttime);
15}
16if ($chat_lastrow !== 1) {
17 $url->param('chat_lastrow', $chat_lastrow);
18}
19$PAGE->set_url($url);
20
21
22if (!$chatuser = $DB->get_record('chat_users', array('sid'=>$chat_sid))) {
23 print_error('notlogged', 'chat');
24}
25
26//Get the minimal course
27if (!$course = $DB->get_record('course', array('id'=>$chatuser->course))) {
28 print_error('invalidcourseid');
29}
30
31//Get the user theme and enough info to be used in chat_format_message() which passes it along to
92911ef1 32if (!$user = $DB->get_record('user', array('id'=>$chatuser->userid, 'deleted'=>0, 'suspended'=>0))) { // no optimisation here, it would break again in future!
eb588b22 33 print_error('invaliduser');
34}
92911ef1 35\core\session\manager::set_user($user);
eb588b22 36
37//Setup course, lang and theme
38$PAGE->set_course($course);
39
40// force deleting of timed out users if there is a silence in room or just entering
41if ((time() - $chat_lasttime) > $CFG->chat_old_ping) {
42 // must be done before chat_get_latest_message!!!
43 chat_delete_old_users();
44}
45
46if ($message = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) {
47 $chat_newlasttime = $message->timestamp;
48} else {
49 $chat_newlasttime = 0;
50}
51
52if ($chat_lasttime == 0) { //display some previous messages
53 $chat_lasttime = time() - $CFG->chat_old_ping; //TO DO - any better value??
54}
55
56$timenow = time();
57
e7521559 58$params = array('groupid'=>$chatuser->groupid, 'chatid'=>$chatuser->chatid, 'lasttime'=>$chat_lasttime);
eb588b22 59
60$groupselect = $chatuser->groupid ? " AND (groupid=:groupid OR groupid=0) " : "";
61
62$messages = $DB->get_records_select("chat_messages_current",
63 "chatid = :chatid AND timestamp > :lasttime $groupselect", $params,
64 "timestamp ASC");
65
66if ($messages) {
67 $num = count($messages);
68} else {
69 $num = 0;
70}
71
72$chat_newrow = ($chat_lastrow + $num) % 2;
73
74// no &amp; in url, does not work in header!
75$refreshurl = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&chat_lasttime=$chat_newlasttime&chat_lastrow=$chat_newrow";
76$refreshurlamp = "{$CFG->wwwroot}/mod/chat/gui_header_js/jsupdate.php?chat_sid=$chat_sid&amp;chat_lasttime=$chat_newlasttime&amp;chat_lastrow=$chat_newrow";
77
78header('Expires: Sun, 28 Dec 1997 09:32:45 GMT');
79header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
80header('Cache-Control: no-cache, must-revalidate');
81header('Pragma: no-cache');
82header('Content-Type: text/html; charset=utf-8');
83header("Refresh: $CFG->chat_refresh_room; url=$refreshurl");
84
85/// required stylesheets
86$stylesheetshtml = '';
78946b9b
PS
87/*foreach ($CFG->stylesheets as $stylesheet) {
88 //TODO: MDL-21120
eb588b22 89 $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />';
78946b9b 90}*/
eb588b22 91
92// use ob to be able to send Content-Length headers
93// needed for Keep-Alive to work
94ob_start();
dfd1d9e2 95
1515a89e 96?>
aa5c32fd 97<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
516121bd 98<html>
99 <head>
810944af 100 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
516121bd 101 <script type="text/javascript">
0cdf34fb 102 //<![CDATA[
f298a625 103 function safari_refresh() {
104 self.location.href= '<?php echo $refreshurl;?>';
105 }
106 var issafari = false;
107 if(window.devicePixelRatio){
108 issafari = true;
109 setTimeout('safari_refresh()', <?php echo $CFG->chat_refresh_room*1000;?>);
110 }
5537bb04 111 if (parent.msg && parent.msg.document.getElementById("msgStarted") == null) {
2debdf04 112 parent.msg.document.close();
113 parent.msg.document.open("text/html","replace");
aa5c32fd 114 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 115 parent.msg.document.write("<html><head>");
810944af 116 parent.msg.document.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />");
b9795750 117 parent.msg.document.write("<base target=\"_blank\" />");
4702be4e 118 parent.msg.document.write("<?php echo addslashes_js($stylesheetshtml) ?>");
0cdf34fb 119 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 120 }
516121bd 121 <?php
122 $beep = false;
123 $refreshusers = false;
124 $us = array ();
aa5c32fd 125 if (($chat_lasttime != $chat_newlasttime) and $messages) {
516121bd 126
516121bd 127 foreach ($messages as $message) {
aa5c32fd 128 $chat_lastrow = ($chat_lastrow + 1) % 2;
a6d1886f 129 $formatmessage = chat_format_message($message, $chatuser->course, $USER, $chat_lastrow);
516121bd 130 if ($formatmessage->beep) {
131 $beep = true;
132 }
133 if ($formatmessage->refreshusers) {
134 $refreshusers = true;
135 }
136 $us[$message->userid] = $timenow - $message->timestamp;
5537bb04 137 echo "if(parent.msg)";
4702be4e 138 echo "parent.msg.document.write('".addslashes_js($formatmessage->html)."\\n');\n";
1515a89e 139 }
516121bd 140 }
141
142 $chatuser->lastping = time();
d3bf6f92 143 $DB->set_field('chat_users', 'lastping', $chatuser->lastping, array('id'=>$chatuser->id));
516121bd 144
145 if ($refreshusers) {
5537bb04 146 ?>
147 var link = parent.users.document.getElementById('refreshLink');
148 if (link != null) {
149 parent.users.location.href = link.href;
150 }
151 <?php
516121bd 152 } else {
153 foreach($us as $uid=>$lastping) {
154 $min = (int) ($lastping/60);
155 $sec = $lastping - ($min*60);
156 $min = $min < 10 ? '0'.$min : $min;
157 $sec = $sec < 10 ? '0'.$sec : $sec;
158 $idle = $min.':'.$sec;
5537bb04 159 echo "if (parent.users && parent.users.document.getElementById('uidle{$uid}') != null) {".
3dfd307f 160 "parent.users.document.getElementById('uidle{$uid}').innerHTML = '$idle';}\n";
516121bd 161 }
162 }
163 ?>
f836189a 164 if(parent.input){
165 var autoscroll = parent.input.document.getElementById('auto');
166 if(parent.msg && autoscroll && autoscroll.checked){
167 parent.msg.scroll(1,5000000);
168 }
4ee00a30 169 }
0cdf34fb 170 //]]>
516121bd 171 </script>
172 </head>
173 <body>
174 <?php
175 if ($beep) {
176 echo '<embed src="../beep.wav" autostart="true" hidden="true" name="beep" />';
177 }
178 ?>
0cdf34fb 179 <a href="<?php echo $refreshurlamp ?>" name="refreshLink">Refresh link</a>
516121bd 180 </body>
181</html>
dfd1d9e2 182<?php
183
184// support HTTP Keep-Alive
185header("Content-Length: " . ob_get_length() );
5537bb04 186ob_end_flush();
dfd1d9e2 187exit;
188
189
190?>