Bump
[moodle.git] / mod / chat / lib.php
CommitLineData
1515a89e 1<?PHP // $Id$
2
3/// Library of functions and constants for module chat
4
b5012f3e 5if (!isset($CFG->chat_refresh_room)) {
6 set_config("chat_refresh_room", 5);
7}
8if (!isset($CFG->chat_refresh_userlist)) {
9 set_config("chat_refresh_userlist", 10);
10}
11if (!isset($CFG->chat_old_ping)) {
12 set_config("chat_old_ping", 30);
13}
1515a89e 14
1515a89e 15
16define("CHAT_DRAWBOARD", false); // Look into this later
17
18
19// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
20$CHAT_HTMLHEAD = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head></head>\n<body bgcolor=\"#FFFFFF\">\n\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n";
21
22// The HTML head for the message window to start with (with js scrolling)
23$CHAT_HTMLHEAD_JS = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><script language=\"JavaScript\">\n<!--\nfunction move()\n{\nif (scroll_active) window.scroll(1,400000);\nwindow.setTimeout(\"move()\",100);\n}\nscroll_active = true;\nmove();\n//-->\n</script>\n</head>\n<body bgcolor=\"#FFFFFF\" onBlur=\"scroll_active = true\" onFocus=\"scroll_active = false\">\n\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n";
24
25// The HTML code for standard empty pages (e.g. if a user was kicked out)
26$CHAT_HTMLHEAD_OUT = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>You are out!</title></head><body bgcolor=\"$THEME->body\"></body></html>";
27
28// The HTML head for the message input page
29$CHAT_HTMLHEAD_MSGINPUT = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>Message Input</title></head><body bgcolor=\"$THEME->body\">";
30
31// The HTML code for the message input page, with JavaScript
32$CHAT_HTMLHEAD_MSGINPUT_JS = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>Message Input</title>\n<script language=\"Javascript\">\n<!--\nscroll_active = true;\nfunction empty_field_and_submit()\n{\ndocument.fdummy.arsc_message.value=document.f.arsc_message.value;\ndocument.fdummy.submit();\ndocument.f.arsc_message.focus();\ndocument.f.arsc_message.select();\nreturn false;\n}\n// -->\n</script>\n</head><body bgcolor=\"$THEME->body\" OnLoad=\"document.f.arsc_message.focus();document.f.arsc_message.select();\">";
33
34
35function chat_add_instance($chat) {
36/// Given an object containing all the necessary data,
37/// (defined by the form in mod.html) this function
38/// will create a new instance and return the id number
39/// of the new instance.
40
41 $chat->timemodified = time();
42
fcd3a1ee 43 $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday,
44 $chat->chathour, $chat->chatminute);
1515a89e 45
46 return insert_record("chat", $chat);
47}
48
49
50function chat_update_instance($chat) {
51/// Given an object containing all the necessary data,
52/// (defined by the form in mod.html) this function
53/// will update an existing instance with new data.
54
55 $chat->timemodified = time();
56 $chat->id = $chat->instance;
57
fcd3a1ee 58 $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday,
59 $chat->chathour, $chat->chatminute);
1515a89e 60
61 return update_record("chat", $chat);
62}
63
64
65function chat_delete_instance($id) {
66/// Given an ID of an instance of this module,
67/// this function will permanently delete the instance
68/// and any data that depends on it.
69
70 if (! $chat = get_record("chat", "id", "$id")) {
71 return false;
72 }
73
74 $result = true;
75
76 # Delete any dependent records here #
77
78 if (! delete_records("chat", "id", "$chat->id")) {
79 $result = false;
80 }
81
82 return $result;
83}
84
85function chat_user_outline($course, $user, $mod, $chat) {
86/// Return a small object with summary information about what a
87/// user has done with a given particular instance of this module
88/// Used for user activity reports.
89/// $return->time = the time they did it
90/// $return->info = a short text description
91
92 return $return;
93}
94
95function chat_user_complete($course, $user, $mod, $chat) {
96/// Print a detailed representation of what a user has done with
97/// a given particular instance of this module, for user activity reports.
98
99 return true;
100}
101
b7602a11 102function chat_print_recent_activity($course, $isteacher, $timestart) {
103/// Given a course and a date, prints a summary of all chat rooms
104/// that currently have people in them.
1515a89e 105/// This function is called from course/lib.php: print_recent_activity()
106
b7602a11 107 global $CFG;
108
c5a05b95 109 $timeold = time() - $CFG->chat_old_ping;
110
132911c8 111 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
b7602a11 112 FROM {$CFG->prefix}chat_users as cu,
cea57965 113 {$CFG->prefix}chat as ch,
b7602a11 114 {$CFG->prefix}user as u
115 WHERE cu.userid = u.id
cea57965 116 AND cu.chatid = ch.id
c5a05b95 117 AND cu.lastping > '$timeold'
cea57965 118 AND ch.course = '$course->id'
b7602a11 119 ORDER BY cu.chatid ASC") ) {
120 return false;
121 }
1515a89e 122
b7602a11 123 print_headline(get_string("currentchats", "chat").":");
124
125 $current = 0;
126 foreach ($chatusers as $chatuser) {
127 if ($current != $chatuser->chatid) {
128 if ($current) {
129 echo "</p>";
130 }
131 if ($chat = get_record("chat", "id", $chatuser->chatid)) {
132 echo "<p><font size=1><a href=\"$CFG->wwwroot/mod/chat/view.php?c=$chat->id\">$chat->name</a></font><br />";
133 }
134 $current = $chatuser->chatid;
135 }
8f6ca087 136 $fullname = fullname($chatuser);
137 echo "&nbsp;&nbsp;&nbsp;<font size=1>- $fullname</font><br />";
b7602a11 138 }
48548144 139 echo "<br />";
b7602a11 140
141 return true;
1515a89e 142}
143
144function chat_cron () {
145/// Function to be run periodically according to the moodle cron
146/// This function searches for things that need to be done, such
147/// as sending out mail, toggling flags etc ...
148
149 global $CFG;
150
fcd3a1ee 151 chat_update_chat_times();
152
7d792369 153 chat_delete_old_users();
154
22a4491a 155 /// Delete old messages
156 if ($chats = get_records("chat")) {
157 foreach ($chats as $chat) {
158 if ($chat->keepdays) {
159 $timeold = time() - ($chat->keepdays * 24 * 3600);
160 delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'");
161 }
162 }
163 }
164
1515a89e 165 return true;
166}
167
05855091 168function chat_get_participants($chatid) {
169//Returns the users with data in one chat
170//(users with records in chat_messages, students)
171
172 global $CFG;
173
174 //Get students
175 $students = get_records_sql("SELECT DISTINCT u.*
176 FROM {$CFG->prefix}user u,
177 {$CFG->prefix}chat_messages c
178 WHERE c.chatid = '$chatid' and
179 u.id = c.userid");
1515a89e 180
05855091 181 //Return students array (it contains an array of unique users)
182 return ($students);
183}
1515a89e 184
185//////////////////////////////////////////////////////////////////////
186/// Functions that require some SQL
187
188function chat_get_users($chatid) {
189
190 global $CFG;
191
f7e648da 192 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping
1515a89e 193 FROM {$CFG->prefix}chat_users c,
194 {$CFG->prefix}user u
195 WHERE c.chatid = '$chatid'
196 AND u.id = c.userid
197 GROUP BY u.id
198 ORDER BY c.firstping ASC");
199}
200
201function chat_get_latest_message($chatid) {
5a8625e4 202/// Efficient way to extract just the latest message
203/// Uses ADOdb directly instead of get_record_sql()
204/// because the LIMIT command causes problems with
205/// the developer debugging in there.
1515a89e 206
5a8625e4 207 global $db, $CFG;
1515a89e 208
5a8625e4 209 if (!$rs = $db->Execute("SELECT *
210 FROM {$CFG->prefix}chat_messages
211 WHERE chatid = '$chatid'
212 ORDER BY timestamp DESC LIMIT 1")) {
213 return false;
214 }
215 if ($rs->RecordCount() == 1) {
216 return (object)$rs->fields;
217 } else {
218 return false; // Found no records
219 }
1515a89e 220}
221
5a8625e4 222
1515a89e 223//////////////////////////////////////////////////////////////////////
224
225function chat_login_user($chatid, $version="header_js") {
226 global $USER;
227
228 $chatuser->chatid = $chatid;
229 $chatuser->userid = $USER->id;
230 $chatuser->version = $version;
231 $chatuser->ip = $USER->lastIP;
f7e648da 232 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
1515a89e 233 $chatuser->sid = random_string(32);
234
235 if (!insert_record("chat_users", $chatuser)) {
236 return false;
237 }
238
239 return $chatuser->sid;
240}
241
7d792369 242function chat_delete_old_users() {
243// Delete the old and in the way
244
b5012f3e 245 global $CFG;
246
247 $timeold = time() - $CFG->chat_old_ping;
7d792369 248
249 if ($oldusers = get_records_select("chat_users", "lastping < '$timeold'") ) {
250 delete_records_select("chat_users", "lastping < '$timeold'");
251 foreach ($oldusers as $olduser) {
252 $message->chatid = $olduser->chatid;
253 $message->userid = $olduser->userid;
254 $message->message = "exit";
255 $message->system = 1;
256 $message->timestamp = time();
257
258 if (!insert_record("chat_messages", $message)) {
259 error("Could not insert a chat message!");
260 }
261 }
262 }
263}
1515a89e 264
22a4491a 265
fcd3a1ee 266function chat_update_chat_times($chatid=0) {
267/// Updates chat records so that the next chat time is correct
268
269 $timenow = time();
270 if ($chatid) {
271 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
272 return;
273 }
274 } else {
275 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
276 return;
277 }
278 }
279
280 foreach ($chats as $chat) {
281 switch ($chat->schedule) {
282 case 1: // Single event - turn off schedule and disable
283 $chat->chattime = 0;
284 $chat->schedule = 0;
285 break;
286 case 2: // Repeat daily
287 $chat->chattime += 24 * 3600;
288 break;
289 case 3: // Repeat weekly
290 $chat->chattime += 7 * 24 * 3600;
291 break;
292 }
293 update_record("chat", $chat);
294 }
295}
296
297
1515a89e 298function chat_browser_detect($HTTP_USER_AGENT) {
299
300 if(eregi("(opera) ([0-9]{1,2}.[0-9]{1,3}){0,1}", $HTTP_USER_AGENT, $match)
301 || eregi("(opera/)([0-9]{1,2}.[0-9]{1,3}){0,1}", $HTTP_USER_AGENT, $match))
302 {
303 $BName = "Opera"; $BVersion=$match[2];
304 }
305 elseif( eregi("(konqueror)/([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
306 {
307 $BName = "Konqueror"; $BVersion=$match[2];
308 }
309 elseif( eregi("(lynx)/([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})", $HTTP_USER_AGENT, $match) )
310 {
311 $BName = "Lynx"; $BVersion=$match[2];
312 }
313 elseif( eregi("(links) \(([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
314 {
315 $BName = "Links"; $BVersion=$match[2];
316 }
317 elseif( eregi("(msie) ([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
318 {
319 $BName = "MSIE"; $BVersion=$match[2];
320 }
321 elseif( eregi("(netscape6)/(6.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
322 {
323 $BName = "Netscape"; $BVersion=$match[2];
324 }
325 elseif( eregi("mozilla/5", $HTTP_USER_AGENT) )
326 {
327 $BName = "Netscape"; $BVersion="Unknown";
328 }
329 elseif( eregi("(mozilla)/([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
330 {
331 $BName = "Netscape"; $BVersion=$match[2];
332 }
333 elseif( eregi("w3m", $HTTP_USER_AGENT) )
334 {
335 $BName = "w3m"; $BVersion="Unknown";
336 }
337 else
338 {
339 $BName = "Unknown"; $BVersion="Unknown";
340 }
341
342 if(eregi("linux", $HTTP_USER_AGENT))
343 {
344 $BPlatform = "Linux";
345 }
346 elseif( eregi("win32", $HTTP_USER_AGENT) )
347 {
348 $BPlatform = "Windows";
349 }
350 elseif( (eregi("(win)([0-9]{2})", $HTTP_USER_AGENT, $match) )
351 || (eregi("(windows) ([0-9]{2})", $HTTP_USER_AGENT, $match) ))
352 {
353 $BPlatform = "Windows $match[2]";
354 }
355 elseif( eregi("(winnt)([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
356 {
357 $BPlatform = "Windows NT $match[2]";
358 }
359 elseif( eregi("(windows nt)( ){0,1}([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
360 {
361 $BPlatform = "Windows NT $match[3]";
362 }
363 elseif( eregi("mac", $HTTP_USER_AGENT) )
364 {
365 $BPlatform = "Macintosh";
366 }
367 elseif( eregi("(sunos) ([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
368 {
369 $BPlatform = "SunOS $match[2]";
370 }
371 elseif( eregi("(beos) r([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
372 {
373 $BPlatform = "BeOS $match[2]";
374 }
375 elseif( eregi("freebsd", $HTTP_USER_AGENT) )
376 {
377 $BPlatform = "FreeBSD";
378 }
379 elseif( eregi("openbsd", $HTTP_USER_AGENT) )
380 {
381 $BPlatform = "OpenBSD";
382 }
383 elseif( eregi("irix", $HTTP_USER_AGENT) )
384 {
385 $BPlatform = "IRIX";
386 }
387 elseif( eregi("os/2", $HTTP_USER_AGENT) )
388 {
389 $BPlatform = "OS/2";
390 }
391 elseif( eregi("plan9", $HTTP_USER_AGENT) )
392 {
393 $BPlatform = "Plan9";
394 }
395 elseif( eregi("unix", $HTTP_USER_AGENT)
396 || eregi("hp-ux", $HTTP_USER_AGENT) )
397 {
398 $BPlatform = "Unix";
399 }
400 elseif( eregi("osf", $HTTP_USER_AGENT) )
401 {
402 $BPlatform = "OSF";
403 }
404 else
405 {
406 $BPlatform = "Unknown";
407 }
408
409 $return["name"] = $BName;
410 $return["version"] = $BVersion;
411 $return["platform"] = $BPlatform;
412 return $return;
413}
414
415function chat_display_version($version, $browser)
416{
417 GLOBAL $CFG;
418
419 $checked = "";
420 if (($version == "sockets") OR ($version == "push_js"))
421 {
422 $checked = "checked";
423 }
424 if (($version == "sockets" OR $version == "push_js")
425 AND
426 ($browser["name"] == "Lynx"
427 OR
428 $browser["name"] == "Links"
429 OR
430 $browser["name"] == "w3m"
431 OR
432 $browser["name"] == "Konqueror"
433 OR
434 ($browser["name"] == "Netscape" AND substr($browser["version"], 0, 1) == "2")))
435 {
436 $checked = "";
437 }
438 if (($version == "text")
439 AND
440 ($browser["name"] == "Lynx"
441 OR
442 $browser["name"] == "Links"
443 OR
444 $browser["name"] == "w3m"))
445 {
446 $checked = "checked";
447 }
448 if (($version == "header")
449 AND
450 ($browser["name"] == "Konqueror"))
451 {
452 $checked = "checked";
453 }
454 if (($version == "header_js")
455 AND
456 ($browser["name"] == "Netscape" AND substr($browser["version"], 0, 1) == "2"))
457 {
458 $checked = "checked";
459 }
460 ?>
461 <tr>
462 <td valign="top">
463 <input type="radio" name="chat_chatversion" value="<?php echo $version; ?>"<?php echo $checked; ?>>
464 </td>
465 <td valign="top" align="left">
466 <font face="Arial" size="2">
467 <?php echo $chat_lang["gui_".$version]; ?>
468 </font>
469 </td>
470 </tr>
471 <?php
472
473}
474
475
582de679 476function chat_format_message($message, $courseid=0) {
7d792369 477/// Given a message object full of information, this function
478/// formats it appropriately into text and html, then
479/// returns the formatted data.
1515a89e 480
481 global $CFG, $USER;
482
7d792369 483 $output = new object;
484
485 if (!$user = get_record("user", "id", $message->userid)) {
486 return "Error finding user id = $message->userid";
1515a89e 487 }
488
489 $picture = print_user_picture($user->id, 0, $user->picture, false, true, false);
582de679 490 if ($courseid) {
491 $picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$courseid\">$picture</a>";
492 }
1515a89e 493
7d792369 494 $strtime = userdate($message->timestamp, get_string("strftimemessage", "chat"));
495
496 $output->beep = false; // by default
1515a89e 497
7d792369 498 $text = $message->message;
499
500 if (!empty($message->system)) { /// It's a system message
8f6ca087 501 $output->text = get_string("message$text", "chat", fullname($user));
7d792369 502 $output->text = "$strtime: $output->text";
503 $output->html = "<table><tr><td valign=top>$picture</td><td>";
504 $output->html .= "<font size=2 color=\"#CCAAAA\">$output->text</font>";
505 $output->html .= "</td></tr></table>";
1515a89e 506 return $output;
507 }
508
7d792369 509 convert_urls_into_links($text);
510 replace_smilies($text);
62a575d1 511 $text = filter_text($text, $courseid);
7d792369 512
513 if (substr($text, 0, 5) == "beep ") { /// It's a beep!
514 $beepwho = trim(substr($text, 5));
9f85bed4 515
7d792369 516 if ($beepwho == "all") { // everyone
8f6ca087 517 $outinfo = "$strtime: ". get_string("messagebeepseveryone", "chat", fullname($user));
7d792369 518 $outmain = "";
519 $output->beep = true; // (eventually this should be set to
520 // to a filename uploaded by the user)
521
522 } else if ($beepwho == $USER->id) { // current user
8f6ca087 523 $outinfo = "$strtime: ". get_string("messagebeepsyou", "chat", fullname($user));
7d792369 524 $outmain = "";
525 $output->beep = true;
526
527 } else {
528 return false;
529 }
530
531 } else if (substr($text, 0, 1) == ":") { /// It's an MOO emote
9f85bed4 532 $outinfo = $strtime;
7d792369 533 $outmain = "$user->firstname ".substr($text, 1);
9f85bed4 534
7d792369 535 } else if (substr($text, 0, 1) == "/") { /// It's a user command
1515a89e 536
7d792369 537 if (substr($text, 0, 4) == "/me ") {
9f85bed4 538 $outinfo = $strtime;
7d792369 539 $outmain = "$user->firstname ".substr($text, 4);
9f85bed4 540 } else {
541 $outinfo = $strtime;
7d792369 542 $outmain = $text;
1515a89e 543 }
9f85bed4 544
545 } else { /// It's a normal message
546 $outinfo = "$strtime $user->firstname";
7d792369 547 $outmain = $text;
1515a89e 548 }
549
9f85bed4 550 /// Format the message as a small table
1515a89e 551
7d792369 552 $output->text = strip_tags("$outinfo: $outmain");
553
554 $output->html = "<table><tr><td valign=top>$picture</td><td><font size=2>";
555 $output->html .= "<font color=\"#888888\">$outinfo</font>";
556 if ($outmain) {
557 $output->html .= ": $outmain";
558 }
559 $output->html .= "</font></td></tr></table>";
1515a89e 560
7d792369 561 return $output;
1515a89e 562
563}
564
1515a89e 565?>