Don't put HTML and text files through clean_text.
[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}
d434b9ae 14if (!isset($CFG->chat_method)) {
15 set_config("chat_method", "header_js");
16}
17if (!isset($CFG->chat_serverhost)) {
18 set_config("chat_serverhost", $_SERVER['HTTP_HOST']);
19}
20if (!isset($CFG->chat_serverip)) {
21 set_config("chat_serverip", $_SERVER['SERVER_ADDR']);
22}
23if (!isset($CFG->chat_serverport)) {
24 set_config("chat_serverport", 9111);
25}
26if (!isset($CFG->chat_servermax)) {
27 set_config("chat_servermax", 100);
28}
1515a89e 29
30define("CHAT_DRAWBOARD", false); // Look into this later
31
32
33// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
34$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";
35
36// The HTML head for the message window to start with (with js scrolling)
37$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";
38
39// The HTML code for standard empty pages (e.g. if a user was kicked out)
40$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>";
41
42// The HTML head for the message input page
43$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\">";
44
45// The HTML code for the message input page, with JavaScript
46$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();\">";
47
48
49function chat_add_instance($chat) {
50/// Given an object containing all the necessary data,
51/// (defined by the form in mod.html) this function
52/// will create a new instance and return the id number
53/// of the new instance.
54
55 $chat->timemodified = time();
56
fcd3a1ee 57 $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday,
58 $chat->chathour, $chat->chatminute);
1515a89e 59
8496c4af 60 if ($returnid = insert_record("chat", $chat)) {
61
62 $event = NULL;
63 $event->name = $chat->name;
64 $event->description = $chat->intro;
65 $event->courseid = $chat->course;
66 $event->groupid = 0;
67 $event->userid = 0;
68 $event->modulename = 'chat';
69 $event->instance = $returnid;
70 $event->eventtype = $chat->schedule;
71 $event->timestart = $chat->chattime;
72 $event->timeduration = 0;
73
74 add_event($event);
75 }
76
77 return $returnid;
1515a89e 78}
79
80
81function chat_update_instance($chat) {
82/// Given an object containing all the necessary data,
83/// (defined by the form in mod.html) this function
84/// will update an existing instance with new data.
85
86 $chat->timemodified = time();
87 $chat->id = $chat->instance;
88
fcd3a1ee 89 $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday,
90 $chat->chathour, $chat->chatminute);
1515a89e 91
8496c4af 92 if ($returnid = update_record("chat", $chat)) {
93
94 $event = NULL;
95
96 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
97
98 $event->name = $chat->name;
99 $event->description = $chat->intro;
100 $event->timestart = $chat->chattime;
101
102 update_event($event);
103 }
104 }
105
106 return $returnid;
1515a89e 107}
108
109
110function chat_delete_instance($id) {
111/// Given an ID of an instance of this module,
112/// this function will permanently delete the instance
113/// and any data that depends on it.
114
115 if (! $chat = get_record("chat", "id", "$id")) {
116 return false;
117 }
118
119 $result = true;
120
121 # Delete any dependent records here #
122
123 if (! delete_records("chat", "id", "$chat->id")) {
124 $result = false;
125 }
126
36eb856f 127 if (! delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) {
128 $result = false;
129 }
130
1515a89e 131 return $result;
132}
133
134function chat_user_outline($course, $user, $mod, $chat) {
135/// Return a small object with summary information about what a
136/// user has done with a given particular instance of this module
137/// Used for user activity reports.
138/// $return->time = the time they did it
139/// $return->info = a short text description
140
141 return $return;
142}
143
144function chat_user_complete($course, $user, $mod, $chat) {
145/// Print a detailed representation of what a user has done with
146/// a given particular instance of this module, for user activity reports.
147
148 return true;
149}
150
b7602a11 151function chat_print_recent_activity($course, $isteacher, $timestart) {
152/// Given a course and a date, prints a summary of all chat rooms
153/// that currently have people in them.
1515a89e 154/// This function is called from course/lib.php: print_recent_activity()
155
b7602a11 156 global $CFG;
157
c5a05b95 158 $timeold = time() - $CFG->chat_old_ping;
159
132911c8 160 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
b7602a11 161 FROM {$CFG->prefix}chat_users as cu,
cea57965 162 {$CFG->prefix}chat as ch,
b7602a11 163 {$CFG->prefix}user as u
164 WHERE cu.userid = u.id
cea57965 165 AND cu.chatid = ch.id
c5a05b95 166 AND cu.lastping > '$timeold'
cea57965 167 AND ch.course = '$course->id'
b7602a11 168 ORDER BY cu.chatid ASC") ) {
169 return false;
170 }
1515a89e 171
3120f6f2 172 $isteacher = isteacher($course->id);
b7602a11 173
3120f6f2 174 $outputstarted = false;
b7602a11 175 $current = 0;
176 foreach ($chatusers as $chatuser) {
177 if ($current != $chatuser->chatid) {
178 if ($current) {
179 echo "</p>";
180 }
181 if ($chat = get_record("chat", "id", $chatuser->chatid)) {
3120f6f2 182 if (!($isteacher or instance_is_visible('chat', $chat))) { // Chat hidden to students
183 continue;
184 }
185 if (!$outputstarted) {
186 print_headline(get_string("currentchats", "chat").":");
187 $outputstarted = true;
188 }
b7602a11 189 echo "<p><font size=1><a href=\"$CFG->wwwroot/mod/chat/view.php?c=$chat->id\">$chat->name</a></font><br />";
190 }
191 $current = $chatuser->chatid;
192 }
8f6ca087 193 $fullname = fullname($chatuser);
194 echo "&nbsp;&nbsp;&nbsp;<font size=1>- $fullname</font><br />";
b7602a11 195 }
48548144 196 echo "<br />";
b7602a11 197
198 return true;
1515a89e 199}
200
201function chat_cron () {
202/// Function to be run periodically according to the moodle cron
203/// This function searches for things that need to be done, such
204/// as sending out mail, toggling flags etc ...
205
206 global $CFG;
207
fcd3a1ee 208 chat_update_chat_times();
209
7d792369 210 chat_delete_old_users();
211
22a4491a 212 /// Delete old messages
213 if ($chats = get_records("chat")) {
214 foreach ($chats as $chat) {
215 if ($chat->keepdays) {
216 $timeold = time() - ($chat->keepdays * 24 * 3600);
217 delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'");
218 }
219 }
220 }
221
1515a89e 222 return true;
223}
224
84a2fdd7 225function chat_get_participants($chatid, $groupid=0) {
05855091 226//Returns the users with data in one chat
227//(users with records in chat_messages, students)
228
229 global $CFG;
230
84a2fdd7 231 if ($groupid) {
232 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
233 } else {
234 $groupselect = "";
235 }
236
05855091 237 //Get students
238 $students = get_records_sql("SELECT DISTINCT u.*
239 FROM {$CFG->prefix}user u,
240 {$CFG->prefix}chat_messages c
84a2fdd7 241 WHERE c.chatid = '$chatid' $groupselect
242 AND u.id = c.userid");
1515a89e 243
05855091 244 //Return students array (it contains an array of unique users)
245 return ($students);
246}
1515a89e 247
8496c4af 248function chat_refresh_events($courseid = 0) {
249// This standard function will check all instances of this module
250// and make sure there are up-to-date events created for each of them.
251// If courseid = 0, then every chat event in the site is checked, else
252// only chat events belonging to the course specified are checked.
253// This function is used, in its new format, by restore_refresh_events()
254
255 if ($courseid) {
256 if (! $chats = get_records("chat", "course", $courseid)) {
257 return true;
258 }
259 } else {
260 if (! $chats = get_records("chat")) {
261 return true;
262 }
263 }
dcd338ff 264 $moduleid = get_field('modules', 'id', 'name', 'chat');
8496c4af 265
266 foreach ($chats as $chat) {
267 $event = NULL;
268 $event->name = addslashes($chat->name);
269 $event->description = addslashes($chat->intro);
270 $event->timestart = $chat->chattime;
271
272 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
273 update_event($event);
274
275 } else {
276 $event->courseid = $chat->course;
277 $event->groupid = 0;
278 $event->userid = 0;
279 $event->modulename = 'chat';
280 $event->instance = $chat->id;
281 $event->eventtype = $chat->schedule;
282 $event->timeduration = 0;
dcd338ff 283 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
284
8496c4af 285 add_event($event);
286 }
287 }
288 return true;
289}
290
1515a89e 291//////////////////////////////////////////////////////////////////////
292/// Functions that require some SQL
293
84a2fdd7 294function chat_get_users($chatid, $groupid=0) {
1515a89e 295
296 global $CFG;
84a2fdd7 297
298 if ($groupid) {
299 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
300 } else {
301 $groupselect = "";
302 }
1515a89e 303
426c0437 304 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
1515a89e 305 FROM {$CFG->prefix}chat_users c,
306 {$CFG->prefix}user u
307 WHERE c.chatid = '$chatid'
84a2fdd7 308 AND u.id = c.userid $groupselect
1515a89e 309 ORDER BY c.firstping ASC");
310}
311
84a2fdd7 312function chat_get_latest_message($chatid, $groupid=0) {
5a8625e4 313/// Efficient way to extract just the latest message
314/// Uses ADOdb directly instead of get_record_sql()
315/// because the LIMIT command causes problems with
316/// the developer debugging in there.
1515a89e 317
5a8625e4 318 global $db, $CFG;
1515a89e 319
84a2fdd7 320 if ($groupid) {
321 $groupselect = " AND (groupid='$groupid' OR groupid='0')";
322 } else {
323 $groupselect = "";
324 }
325
5a8625e4 326 if (!$rs = $db->Execute("SELECT *
327 FROM {$CFG->prefix}chat_messages
84a2fdd7 328 WHERE chatid = '$chatid' $groupselect
5a8625e4 329 ORDER BY timestamp DESC LIMIT 1")) {
330 return false;
331 }
332 if ($rs->RecordCount() == 1) {
333 return (object)$rs->fields;
334 } else {
335 return false; // Found no records
336 }
1515a89e 337}
338
5a8625e4 339
1515a89e 340//////////////////////////////////////////////////////////////////////
341
fdff1f63 342function chat_login_user($chatid, $version="header_js", $groupid=0) {
1515a89e 343 global $USER;
344
345 $chatuser->chatid = $chatid;
346 $chatuser->userid = $USER->id;
fdff1f63 347 $chatuser->groupid = $groupid;
1515a89e 348 $chatuser->version = $version;
349 $chatuser->ip = $USER->lastIP;
f7e648da 350 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
1515a89e 351 $chatuser->sid = random_string(32);
352
353 if (!insert_record("chat_users", $chatuser)) {
354 return false;
355 }
356
357 return $chatuser->sid;
358}
359
7d792369 360function chat_delete_old_users() {
361// Delete the old and in the way
362
b5012f3e 363 global $CFG;
364
365 $timeold = time() - $CFG->chat_old_ping;
7d792369 366
367 if ($oldusers = get_records_select("chat_users", "lastping < '$timeold'") ) {
368 delete_records_select("chat_users", "lastping < '$timeold'");
369 foreach ($oldusers as $olduser) {
370 $message->chatid = $olduser->chatid;
371 $message->userid = $olduser->userid;
84a2fdd7 372 $message->groupid = $olduser->groupid;
7d792369 373 $message->message = "exit";
374 $message->system = 1;
375 $message->timestamp = time();
376
377 if (!insert_record("chat_messages", $message)) {
378 error("Could not insert a chat message!");
379 }
380 }
381 }
382}
1515a89e 383
22a4491a 384
fcd3a1ee 385function chat_update_chat_times($chatid=0) {
386/// Updates chat records so that the next chat time is correct
387
388 $timenow = time();
389 if ($chatid) {
390 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
391 return;
392 }
393 } else {
394 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
395 return;
396 }
397 }
398
399 foreach ($chats as $chat) {
7626969c 400 unset($chat->name);
401 unset($chat->intro);
fcd3a1ee 402 switch ($chat->schedule) {
403 case 1: // Single event - turn off schedule and disable
404 $chat->chattime = 0;
405 $chat->schedule = 0;
406 break;
407 case 2: // Repeat daily
408 $chat->chattime += 24 * 3600;
409 break;
410 case 3: // Repeat weekly
411 $chat->chattime += 7 * 24 * 3600;
412 break;
413 }
414 update_record("chat", $chat);
8496c4af 415
416 $event = NULL; // Update calendar too
417 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
418 $event->timestart = $chat->chattime;
419 update_event($event);
420 }
fcd3a1ee 421 }
422}
423
424
1515a89e 425function chat_browser_detect($HTTP_USER_AGENT) {
426
427 if(eregi("(opera) ([0-9]{1,2}.[0-9]{1,3}){0,1}", $HTTP_USER_AGENT, $match)
428 || eregi("(opera/)([0-9]{1,2}.[0-9]{1,3}){0,1}", $HTTP_USER_AGENT, $match))
429 {
430 $BName = "Opera"; $BVersion=$match[2];
431 }
432 elseif( eregi("(konqueror)/([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
433 {
434 $BName = "Konqueror"; $BVersion=$match[2];
435 }
436 elseif( eregi("(lynx)/([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})", $HTTP_USER_AGENT, $match) )
437 {
438 $BName = "Lynx"; $BVersion=$match[2];
439 }
440 elseif( eregi("(links) \(([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
441 {
442 $BName = "Links"; $BVersion=$match[2];
443 }
444 elseif( eregi("(msie) ([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
445 {
446 $BName = "MSIE"; $BVersion=$match[2];
447 }
448 elseif( eregi("(netscape6)/(6.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
449 {
450 $BName = "Netscape"; $BVersion=$match[2];
451 }
452 elseif( eregi("mozilla/5", $HTTP_USER_AGENT) )
453 {
454 $BName = "Netscape"; $BVersion="Unknown";
455 }
456 elseif( eregi("(mozilla)/([0-9]{1,2}.[0-9]{1,3})", $HTTP_USER_AGENT, $match) )
457 {
458 $BName = "Netscape"; $BVersion=$match[2];
459 }
460 elseif( eregi("w3m", $HTTP_USER_AGENT) )
461 {
462 $BName = "w3m"; $BVersion="Unknown";
463 }
464 else
465 {
466 $BName = "Unknown"; $BVersion="Unknown";
467 }
468
469 if(eregi("linux", $HTTP_USER_AGENT))
470 {
471 $BPlatform = "Linux";
472 }
473 elseif( eregi("win32", $HTTP_USER_AGENT) )
474 {
475 $BPlatform = "Windows";
476 }
477 elseif( (eregi("(win)([0-9]{2})", $HTTP_USER_AGENT, $match) )
478 || (eregi("(windows) ([0-9]{2})", $HTTP_USER_AGENT, $match) ))
479 {
480 $BPlatform = "Windows $match[2]";
481 }
482 elseif( eregi("(winnt)([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
483 {
484 $BPlatform = "Windows NT $match[2]";
485 }
486 elseif( eregi("(windows nt)( ){0,1}([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
487 {
488 $BPlatform = "Windows NT $match[3]";
489 }
490 elseif( eregi("mac", $HTTP_USER_AGENT) )
491 {
492 $BPlatform = "Macintosh";
493 }
494 elseif( eregi("(sunos) ([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
495 {
496 $BPlatform = "SunOS $match[2]";
497 }
498 elseif( eregi("(beos) r([0-9]{1,2}.[0-9]{1,2}){0,1}", $HTTP_USER_AGENT, $match) )
499 {
500 $BPlatform = "BeOS $match[2]";
501 }
502 elseif( eregi("freebsd", $HTTP_USER_AGENT) )
503 {
504 $BPlatform = "FreeBSD";
505 }
506 elseif( eregi("openbsd", $HTTP_USER_AGENT) )
507 {
508 $BPlatform = "OpenBSD";
509 }
510 elseif( eregi("irix", $HTTP_USER_AGENT) )
511 {
512 $BPlatform = "IRIX";
513 }
514 elseif( eregi("os/2", $HTTP_USER_AGENT) )
515 {
516 $BPlatform = "OS/2";
517 }
518 elseif( eregi("plan9", $HTTP_USER_AGENT) )
519 {
520 $BPlatform = "Plan9";
521 }
522 elseif( eregi("unix", $HTTP_USER_AGENT)
523 || eregi("hp-ux", $HTTP_USER_AGENT) )
524 {
525 $BPlatform = "Unix";
526 }
527 elseif( eregi("osf", $HTTP_USER_AGENT) )
528 {
529 $BPlatform = "OSF";
530 }
531 else
532 {
533 $BPlatform = "Unknown";
534 }
535
536 $return["name"] = $BName;
537 $return["version"] = $BVersion;
538 $return["platform"] = $BPlatform;
539 return $return;
540}
541
542function chat_display_version($version, $browser)
543{
544 GLOBAL $CFG;
545
546 $checked = "";
547 if (($version == "sockets") OR ($version == "push_js"))
548 {
549 $checked = "checked";
550 }
551 if (($version == "sockets" OR $version == "push_js")
552 AND
553 ($browser["name"] == "Lynx"
554 OR
555 $browser["name"] == "Links"
556 OR
557 $browser["name"] == "w3m"
558 OR
559 $browser["name"] == "Konqueror"
560 OR
561 ($browser["name"] == "Netscape" AND substr($browser["version"], 0, 1) == "2")))
562 {
563 $checked = "";
564 }
565 if (($version == "text")
566 AND
567 ($browser["name"] == "Lynx"
568 OR
569 $browser["name"] == "Links"
570 OR
571 $browser["name"] == "w3m"))
572 {
573 $checked = "checked";
574 }
575 if (($version == "header")
576 AND
577 ($browser["name"] == "Konqueror"))
578 {
579 $checked = "checked";
580 }
581 if (($version == "header_js")
582 AND
583 ($browser["name"] == "Netscape" AND substr($browser["version"], 0, 1) == "2"))
584 {
585 $checked = "checked";
586 }
587 ?>
588 <tr>
589 <td valign="top">
590 <input type="radio" name="chat_chatversion" value="<?php echo $version; ?>"<?php echo $checked; ?>>
591 </td>
592 <td valign="top" align="left">
593 <font face="Arial" size="2">
594 <?php echo $chat_lang["gui_".$version]; ?>
595 </font>
596 </td>
597 </tr>
598 <?php
599
600}
601
602
582de679 603function chat_format_message($message, $courseid=0) {
7d792369 604/// Given a message object full of information, this function
605/// formats it appropriately into text and html, then
606/// returns the formatted data.
1515a89e 607
608 global $CFG, $USER;
609
7d792369 610 $output = new object;
611
612 if (!$user = get_record("user", "id", $message->userid)) {
613 return "Error finding user id = $message->userid";
1515a89e 614 }
615
616 $picture = print_user_picture($user->id, 0, $user->picture, false, true, false);
582de679 617 if ($courseid) {
618 $picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$courseid\">$picture</a>";
619 }
1515a89e 620
7d792369 621 $strtime = userdate($message->timestamp, get_string("strftimemessage", "chat"));
622
623 $output->beep = false; // by default
1515a89e 624
7d792369 625 $text = $message->message;
626
627 if (!empty($message->system)) { /// It's a system message
8f6ca087 628 $output->text = get_string("message$text", "chat", fullname($user));
7d792369 629 $output->text = "$strtime: $output->text";
630 $output->html = "<table><tr><td valign=top>$picture</td><td>";
631 $output->html .= "<font size=2 color=\"#CCAAAA\">$output->text</font>";
632 $output->html .= "</td></tr></table>";
1515a89e 633 return $output;
634 }
635
7d792369 636 convert_urls_into_links($text);
637 replace_smilies($text);
62a575d1 638 $text = filter_text($text, $courseid);
7d792369 639
640 if (substr($text, 0, 5) == "beep ") { /// It's a beep!
641 $beepwho = trim(substr($text, 5));
9f85bed4 642
7d792369 643 if ($beepwho == "all") { // everyone
8f6ca087 644 $outinfo = "$strtime: ". get_string("messagebeepseveryone", "chat", fullname($user));
7d792369 645 $outmain = "";
646 $output->beep = true; // (eventually this should be set to
647 // to a filename uploaded by the user)
648
649 } else if ($beepwho == $USER->id) { // current user
8f6ca087 650 $outinfo = "$strtime: ". get_string("messagebeepsyou", "chat", fullname($user));
7d792369 651 $outmain = "";
652 $output->beep = true;
653
654 } else {
655 return false;
656 }
657
658 } else if (substr($text, 0, 1) == ":") { /// It's an MOO emote
9f85bed4 659 $outinfo = $strtime;
7d792369 660 $outmain = "$user->firstname ".substr($text, 1);
9f85bed4 661
7d792369 662 } else if (substr($text, 0, 1) == "/") { /// It's a user command
1515a89e 663
7d792369 664 if (substr($text, 0, 4) == "/me ") {
9f85bed4 665 $outinfo = $strtime;
7d792369 666 $outmain = "$user->firstname ".substr($text, 4);
9f85bed4 667 } else {
668 $outinfo = $strtime;
7d792369 669 $outmain = $text;
1515a89e 670 }
9f85bed4 671
672 } else { /// It's a normal message
673 $outinfo = "$strtime $user->firstname";
7d792369 674 $outmain = $text;
1515a89e 675 }
676
9f85bed4 677 /// Format the message as a small table
1515a89e 678
7d792369 679 $output->text = strip_tags("$outinfo: $outmain");
680
681 $output->html = "<table><tr><td valign=top>$picture</td><td><font size=2>";
682 $output->html .= "<font color=\"#888888\">$outinfo</font>";
683 if ($outmain) {
684 $output->html .= ": $outmain";
685 }
686 $output->html .= "</font></td></tr></table>";
1515a89e 687
7d792369 688 return $output;
1515a89e 689
690}
691
1515a89e 692?>