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