- Not Categorized is now the default view for category tab.
[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
132911c8 109 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
b7602a11 110 FROM {$CFG->prefix}chat_users as cu,
cea57965 111 {$CFG->prefix}chat as ch,
b7602a11 112 {$CFG->prefix}user as u
113 WHERE cu.userid = u.id
cea57965 114 AND cu.chatid = ch.id
115 AND ch.course = '$course->id'
b7602a11 116 ORDER BY cu.chatid ASC") ) {
117 return false;
118 }
1515a89e 119
b7602a11 120 print_headline(get_string("currentchats", "chat").":");
121
122 $current = 0;
123 foreach ($chatusers as $chatuser) {
124 if ($current != $chatuser->chatid) {
125 if ($current) {
126 echo "</p>";
127 }
128 if ($chat = get_record("chat", "id", $chatuser->chatid)) {
129 echo "<p><font size=1><a href=\"$CFG->wwwroot/mod/chat/view.php?c=$chat->id\">$chat->name</a></font><br />";
130 }
131 $current = $chatuser->chatid;
132 }
133 echo "&nbsp;&nbsp;&nbsp;<font size=1>- $chatuser->firstname $chatuser->lastname</font><br />";
134 }
48548144 135 echo "<br />";
b7602a11 136
137 return true;
1515a89e 138}
139
140function chat_cron () {
141/// Function to be run periodically according to the moodle cron
142/// This function searches for things that need to be done, such
143/// as sending out mail, toggling flags etc ...
144
145 global $CFG;
146
fcd3a1ee 147 chat_update_chat_times();
148
7d792369 149 chat_delete_old_users();
150
22a4491a 151 /// Delete old messages
152 if ($chats = get_records("chat")) {
153 foreach ($chats as $chat) {
154 if ($chat->keepdays) {
155 $timeold = time() - ($chat->keepdays * 24 * 3600);
156 delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'");
157 }
158 }
159 }
160
1515a89e 161 return true;
162}
163
1515a89e 164
165
166//////////////////////////////////////////////////////////////////////
167/// Functions that require some SQL
168
169function chat_get_users($chatid) {
170
171 global $CFG;
172
f7e648da 173 return get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping
1515a89e 174 FROM {$CFG->prefix}chat_users c,
175 {$CFG->prefix}user u
176 WHERE c.chatid = '$chatid'
177 AND u.id = c.userid
178 GROUP BY u.id
179 ORDER BY c.firstping ASC");
180}
181
182function chat_get_latest_message($chatid) {
5a8625e4 183/// Efficient way to extract just the latest message
184/// Uses ADOdb directly instead of get_record_sql()
185/// because the LIMIT command causes problems with
186/// the developer debugging in there.
1515a89e 187
5a8625e4 188 global $db, $CFG;
1515a89e 189
5a8625e4 190 if (!$rs = $db->Execute("SELECT *
191 FROM {$CFG->prefix}chat_messages
192 WHERE chatid = '$chatid'
193 ORDER BY timestamp DESC LIMIT 1")) {
194 return false;
195 }
196 if ($rs->RecordCount() == 1) {
197 return (object)$rs->fields;
198 } else {
199 return false; // Found no records
200 }
1515a89e 201}
202
5a8625e4 203
1515a89e 204//////////////////////////////////////////////////////////////////////
205
206function chat_login_user($chatid, $version="header_js") {
207 global $USER;
208
209 $chatuser->chatid = $chatid;
210 $chatuser->userid = $USER->id;
211 $chatuser->version = $version;
212 $chatuser->ip = $USER->lastIP;
f7e648da 213 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
1515a89e 214 $chatuser->sid = random_string(32);
215
216 if (!insert_record("chat_users", $chatuser)) {
217 return false;
218 }
219
220 return $chatuser->sid;
221}
222
7d792369 223function chat_delete_old_users() {
224// Delete the old and in the way
225
b5012f3e 226 global $CFG;
227
228 $timeold = time() - $CFG->chat_old_ping;
7d792369 229
230 if ($oldusers = get_records_select("chat_users", "lastping < '$timeold'") ) {
231 delete_records_select("chat_users", "lastping < '$timeold'");
232 foreach ($oldusers as $olduser) {
233 $message->chatid = $olduser->chatid;
234 $message->userid = $olduser->userid;
235 $message->message = "exit";
236 $message->system = 1;
237 $message->timestamp = time();
238
239 if (!insert_record("chat_messages", $message)) {
240 error("Could not insert a chat message!");
241 }
242 }
243 }
244}
1515a89e 245
22a4491a 246
fcd3a1ee 247function chat_update_chat_times($chatid=0) {
248/// Updates chat records so that the next chat time is correct
249
250 $timenow = time();
251 if ($chatid) {
252 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
253 return;
254 }
255 } else {
256 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
257 return;
258 }
259 }
260
261 foreach ($chats as $chat) {
262 switch ($chat->schedule) {
263 case 1: // Single event - turn off schedule and disable
264 $chat->chattime = 0;
265 $chat->schedule = 0;
266 break;
267 case 2: // Repeat daily
268 $chat->chattime += 24 * 3600;
269 break;
270 case 3: // Repeat weekly
271 $chat->chattime += 7 * 24 * 3600;
272 break;
273 }
274 update_record("chat", $chat);
275 }
276}
277
278
1515a89e 279function chat_browser_detect($HTTP_USER_AGENT) {
280
281 if(eregi("(opera) ([0-9]{1,2}.[0-9]{1,3}){0,1}", $HTTP_USER_AGENT, $match)
282 || eregi("(opera/)([0-9]{1,2}.[0-9]{1,3}){0,1}", $HTTP_USER_AGENT, $match))
283 {
284 $BName = "Opera"; $BVersion=$match[2];
285 }
286 elseif( eregi("(konqueror)/([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
287 {
288 $BName = "Konqueror"; $BVersion=$match[2];
289 }
290 elseif( eregi("(lynx)/([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})", $HTTP_USER_AGENT, $match) )
291 {
292 $BName = "Lynx"; $BVersion=$match[2];
293 }
294 elseif( eregi("(links) \(([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
295 {
296 $BName = "Links"; $BVersion=$match[2];
297 }
298 elseif( eregi("(msie) ([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
299 {
300 $BName = "MSIE"; $BVersion=$match[2];
301 }
302 elseif( eregi("(netscape6)/(6.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
303 {
304 $BName = "Netscape"; $BVersion=$match[2];
305 }
306 elseif( eregi("mozilla/5", $HTTP_USER_AGENT) )
307 {
308 $BName = "Netscape"; $BVersion="Unknown";
309 }
310 elseif( eregi("(mozilla)/([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
311 {
312 $BName = "Netscape"; $BVersion=$match[2];
313 }
314 elseif( eregi("w3m", $HTTP_USER_AGENT) )
315 {
316 $BName = "w3m"; $BVersion="Unknown";
317 }
318 else
319 {
320 $BName = "Unknown"; $BVersion="Unknown";
321 }
322
323 if(eregi("linux", $HTTP_USER_AGENT))
324 {
325 $BPlatform = "Linux";
326 }
327 elseif( eregi("win32", $HTTP_USER_AGENT) )
328 {
329 $BPlatform = "Windows";
330 }
331 elseif( (eregi("(win)([0-9]{2})", $HTTP_USER_AGENT, $match) )
332 || (eregi("(windows) ([0-9]{2})", $HTTP_USER_AGENT, $match) ))
333 {
334 $BPlatform = "Windows $match[2]";
335 }
336 elseif( eregi("(winnt)([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
337 {
338 $BPlatform = "Windows NT $match[2]";
339 }
340 elseif( eregi("(windows nt)( ){0,1}([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
341 {
342 $BPlatform = "Windows NT $match[3]";
343 }
344 elseif( eregi("mac", $HTTP_USER_AGENT) )
345 {
346 $BPlatform = "Macintosh";
347 }
348 elseif( eregi("(sunos) ([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
349 {
350 $BPlatform = "SunOS $match[2]";
351 }
352 elseif( eregi("(beos) r([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
353 {
354 $BPlatform = "BeOS $match[2]";
355 }
356 elseif( eregi("freebsd", $HTTP_USER_AGENT) )
357 {
358 $BPlatform = "FreeBSD";
359 }
360 elseif( eregi("openbsd", $HTTP_USER_AGENT) )
361 {
362 $BPlatform = "OpenBSD";
363 }
364 elseif( eregi("irix", $HTTP_USER_AGENT) )
365 {
366 $BPlatform = "IRIX";
367 }
368 elseif( eregi("os/2", $HTTP_USER_AGENT) )
369 {
370 $BPlatform = "OS/2";
371 }
372 elseif( eregi("plan9", $HTTP_USER_AGENT) )
373 {
374 $BPlatform = "Plan9";
375 }
376 elseif( eregi("unix", $HTTP_USER_AGENT)
377 || eregi("hp-ux", $HTTP_USER_AGENT) )
378 {
379 $BPlatform = "Unix";
380 }
381 elseif( eregi("osf", $HTTP_USER_AGENT) )
382 {
383 $BPlatform = "OSF";
384 }
385 else
386 {
387 $BPlatform = "Unknown";
388 }
389
390 $return["name"] = $BName;
391 $return["version"] = $BVersion;
392 $return["platform"] = $BPlatform;
393 return $return;
394}
395
396function chat_display_version($version, $browser)
397{
398 GLOBAL $CFG;
399
400 $checked = "";
401 if (($version == "sockets") OR ($version == "push_js"))
402 {
403 $checked = "checked";
404 }
405 if (($version == "sockets" OR $version == "push_js")
406 AND
407 ($browser["name"] == "Lynx"
408 OR
409 $browser["name"] == "Links"
410 OR
411 $browser["name"] == "w3m"
412 OR
413 $browser["name"] == "Konqueror"
414 OR
415 ($browser["name"] == "Netscape" AND substr($browser["version"], 0, 1) == "2")))
416 {
417 $checked = "";
418 }
419 if (($version == "text")
420 AND
421 ($browser["name"] == "Lynx"
422 OR
423 $browser["name"] == "Links"
424 OR
425 $browser["name"] == "w3m"))
426 {
427 $checked = "checked";
428 }
429 if (($version == "header")
430 AND
431 ($browser["name"] == "Konqueror"))
432 {
433 $checked = "checked";
434 }
435 if (($version == "header_js")
436 AND
437 ($browser["name"] == "Netscape" AND substr($browser["version"], 0, 1) == "2"))
438 {
439 $checked = "checked";
440 }
441 ?>
442 <tr>
443 <td valign="top">
444 <input type="radio" name="chat_chatversion" value="<?php echo $version; ?>"<?php echo $checked; ?>>
445 </td>
446 <td valign="top" align="left">
447 <font face="Arial" size="2">
448 <?php echo $chat_lang["gui_".$version]; ?>
449 </font>
450 </td>
451 </tr>
452 <?php
453
454}
455
456
582de679 457function chat_format_message($message, $courseid=0) {
7d792369 458/// Given a message object full of information, this function
459/// formats it appropriately into text and html, then
460/// returns the formatted data.
1515a89e 461
462 global $CFG, $USER;
463
7d792369 464 $output = new object;
465
466 if (!$user = get_record("user", "id", $message->userid)) {
467 return "Error finding user id = $message->userid";
1515a89e 468 }
469
470 $picture = print_user_picture($user->id, 0, $user->picture, false, true, false);
582de679 471 if ($courseid) {
472 $picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$courseid\">$picture</a>";
473 }
1515a89e 474
7d792369 475 $strtime = userdate($message->timestamp, get_string("strftimemessage", "chat"));
476
477 $output->beep = false; // by default
1515a89e 478
7d792369 479 $text = $message->message;
480
481 if (!empty($message->system)) { /// It's a system message
482 $output->text = get_string("message$text", "chat",
483 "$user->firstname $user->lastname");
484 $output->text = "$strtime: $output->text";
485 $output->html = "<table><tr><td valign=top>$picture</td><td>";
486 $output->html .= "<font size=2 color=\"#CCAAAA\">$output->text</font>";
487 $output->html .= "</td></tr></table>";
1515a89e 488 return $output;
489 }
490
7d792369 491 convert_urls_into_links($text);
492 replace_smilies($text);
493
494 if (substr($text, 0, 5) == "beep ") { /// It's a beep!
495 $beepwho = trim(substr($text, 5));
9f85bed4 496
7d792369 497 if ($beepwho == "all") { // everyone
498 $outinfo = "$strtime: ". get_string("messagebeepseveryone", "chat",
499 "$user->firstname $user->lastname");
500 $outmain = "";
501 $output->beep = true; // (eventually this should be set to
502 // to a filename uploaded by the user)
503
504 } else if ($beepwho == $USER->id) { // current user
505 $outinfo = "$strtime: ". get_string("messagebeepsyou", "chat",
506 "$user->firstname $user->lastname");
507 $outmain = "";
508 $output->beep = true;
509
510 } else {
511 return false;
512 }
513
514 } else if (substr($text, 0, 1) == ":") { /// It's an MOO emote
9f85bed4 515 $outinfo = $strtime;
7d792369 516 $outmain = "$user->firstname ".substr($text, 1);
9f85bed4 517
7d792369 518 } else if (substr($text, 0, 1) == "/") { /// It's a user command
1515a89e 519
7d792369 520 if (substr($text, 0, 4) == "/me ") {
9f85bed4 521 $outinfo = $strtime;
7d792369 522 $outmain = "$user->firstname ".substr($text, 4);
9f85bed4 523 } else {
524 $outinfo = $strtime;
7d792369 525 $outmain = $text;
1515a89e 526 }
9f85bed4 527
528 } else { /// It's a normal message
529 $outinfo = "$strtime $user->firstname";
7d792369 530 $outmain = $text;
1515a89e 531 }
532
9f85bed4 533 /// Format the message as a small table
1515a89e 534
7d792369 535 $output->text = strip_tags("$outinfo: $outmain");
536
537 $output->html = "<table><tr><td valign=top>$picture</td><td><font size=2>";
538 $output->html .= "<font color=\"#888888\">$outinfo</font>";
539 if ($outmain) {
540 $output->html .= ": $outmain";
541 }
542 $output->html .= "</font></td></tr></table>";
1515a89e 543
7d792369 544 return $output;
1515a89e 545
546}
547
c8c9cb7b 548function chat_get_participants($chatid) {
549//Returns the users with data in one chat
550//(users with records in chat_messages, students)
551
552 global $CFG;
553
554 //Get students
555 $students = get_records_sql("SELECT DISTINCT u.*
556 FROM {$CFG->prefix}user u,
557 {$CFG->prefix}chat_messages c
558 WHERE c.chatid = '$chatid' and
559 u.id = c.userid");
560
561 //Return students array (it contains an array of unique users)
562 return ($students);
563}
564
1515a89e 565?>