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