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); |
b5de723d |
7 | } |
b5012f3e |
8 | if (!isset($CFG->chat_refresh_userlist)) { |
9 | set_config("chat_refresh_userlist", 10); |
b5de723d |
10 | } |
b5012f3e |
11 | if (!isset($CFG->chat_old_ping)) { |
12 | set_config("chat_old_ping", 30); |
b5de723d |
13 | } |
d434b9ae |
14 | if (!isset($CFG->chat_method)) { |
15 | set_config("chat_method", "header_js"); |
b5de723d |
16 | } |
d434b9ae |
17 | if (!isset($CFG->chat_serverhost)) { |
18 | set_config("chat_serverhost", $_SERVER['HTTP_HOST']); |
b5de723d |
19 | } |
d434b9ae |
20 | if (!isset($CFG->chat_serverip)) { |
21 | set_config("chat_serverip", $_SERVER['SERVER_ADDR']); |
b5de723d |
22 | } |
d434b9ae |
23 | if (!isset($CFG->chat_serverport)) { |
24 | set_config("chat_serverport", 9111); |
b5de723d |
25 | } |
d434b9ae |
26 | if (!isset($CFG->chat_servermax)) { |
27 | set_config("chat_servermax", 100); |
b5de723d |
28 | } |
1515a89e |
29 | |
30 | define("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 | |
49 | function chat_add_instance($chat) { |
b5de723d |
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 |
1515a89e |
53 | /// of the new instance. |
54 | |
55 | $chat->timemodified = time(); |
56 | |
b5de723d |
57 | $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday, |
fcd3a1ee |
58 | $chat->chathour, $chat->chatminute); |
b5de723d |
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 | |
81 | function chat_update_instance($chat) { |
b5de723d |
82 | /// Given an object containing all the necessary data, |
83 | /// (defined by the form in mod.html) this function |
1515a89e |
84 | /// will update an existing instance with new data. |
85 | |
86 | $chat->timemodified = time(); |
87 | $chat->id = $chat->instance; |
88 | |
b5de723d |
89 | $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday, |
fcd3a1ee |
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 | |
110 | function chat_delete_instance($id) { |
b5de723d |
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. |
1515a89e |
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 | |
134 | function chat_user_outline($course, $user, $mod, $chat) { |
b5de723d |
135 | /// Return a small object with summary information about what a |
1515a89e |
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 | |
144 | function chat_user_complete($course, $user, $mod, $chat) { |
b5de723d |
145 | /// Print a detailed representation of what a user has done with |
1515a89e |
146 | /// a given particular instance of this module, for user activity reports. |
147 | |
148 | return true; |
149 | } |
150 | |
b7602a11 |
151 | function 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 |
b5de723d |
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 " <font size=1>- $fullname</font><br />"; |
b7602a11 |
195 | } |
48548144 |
196 | echo "<br />"; |
b7602a11 |
197 | |
198 | return true; |
1515a89e |
199 | } |
200 | |
201 | function chat_cron () { |
202 | /// Function to be run periodically according to the moodle cron |
b5de723d |
203 | /// This function searches for things that need to be done, such |
204 | /// as sending out mail, toggling flags etc ... |
1515a89e |
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 |
225 | function 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 |
248 | function 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); |
b5de723d |
284 | |
8496c4af |
285 | add_event($event); |
286 | } |
287 | } |
288 | return true; |
289 | } |
290 | |
1515a89e |
291 | ////////////////////////////////////////////////////////////////////// |
292 | /// Functions that require some SQL |
293 | |
84a2fdd7 |
294 | function 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 | } |
b5de723d |
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 |
312 | function 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() |
b5de723d |
315 | /// because the LIMIT command causes problems with |
5a8625e4 |
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 * |
b5de723d |
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 |
342 | function 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 |
360 | function 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(); |
b5de723d |
376 | |
7d792369 |
377 | if (!insert_record("chat_messages", $message)) { |
378 | error("Could not insert a chat message!"); |
379 | } |
380 | } |
381 | } |
382 | } |
1515a89e |
383 | |
22a4491a |
384 | |
fcd3a1ee |
385 | function 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 |
425 | function 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 | } |
b5de723d |
535 | |
1515a89e |
536 | $return["name"] = $BName; |
537 | $return["version"] = $BVersion; |
538 | $return["platform"] = $BPlatform; |
539 | return $return; |
540 | } |
541 | |
542 | function 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 | |
b5de723d |
603 | function chat_format_message_manually($message, $courseid, $sender, $currentuserid, $language = NULL) { |
604 | global $CFG; |
1515a89e |
605 | |
b5de723d |
606 | $output = New stdClass; |
607 | $output->beep = false; // by default |
7d792369 |
608 | |
b5de723d |
609 | if(empty($language)) { |
610 | $language = current_language(); |
1515a89e |
611 | } |
612 | |
b5de723d |
613 | // Override the highest-ranking language variable from current_language() here |
614 | // And save it so we can restore it again afterwards |
615 | |
616 | $oldcfglang = empty($CFG->courselang) ? NULL : $CFG->courselang; |
617 | $CFG->courselang = $language; |
618 | |
619 | // Get some additional info now that the language has been correctly set |
620 | |
621 | // But before that :-) let's override get_user_timezone() for this call... messy stuff... |
622 | $tz = ($sender->timezone == 99) ? $CFG->timezone : $sender->timezone; |
623 | $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz); |
624 | |
625 | $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false); |
582de679 |
626 | if ($courseid) { |
b5de723d |
627 | $message->picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&course=$courseid\">$picture</a>"; |
582de679 |
628 | } |
1515a89e |
629 | |
b5de723d |
630 | // Start processing the message |
1515a89e |
631 | |
b5de723d |
632 | if(!empty($message->system)) { |
633 | // System event |
634 | $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender)); |
635 | $output->html = '<table><tr><td style="vertical-align: top;">'.$message->picture.'</td><td>'; |
636 | $output->html .= '<font size=2 color="#CCAAAA">'.$output->text.'</font></td></tr></table>'; |
7d792369 |
637 | |
b5de723d |
638 | // Don't forget to reset the language before returning!!! |
639 | if(!empty($oldcfglang)) { |
640 | $CFG->courselang = $oldcfglang; |
641 | } |
1515a89e |
642 | return $output; |
643 | } |
644 | |
b5de723d |
645 | // It's not a system event, so format it nicely |
646 | |
647 | $text = $message->message; |
7d792369 |
648 | convert_urls_into_links($text); |
649 | replace_smilies($text); |
62a575d1 |
650 | $text = filter_text($text, $courseid); |
7d792369 |
651 | |
b5de723d |
652 | // And now check for special cases |
653 | |
654 | if (substr($text, 0, 5) == 'beep ') { |
655 | /// It's a beep! |
7d792369 |
656 | $beepwho = trim(substr($text, 5)); |
9f85bed4 |
657 | |
b5de723d |
658 | if ($beepwho == 'all') { // everyone |
659 | $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender)); |
660 | $outmain = ''; |
661 | $output->beep = true; // (eventually this should be set to |
7d792369 |
662 | // to a filename uploaded by the user) |
663 | |
b5de723d |
664 | } else if ($beepwho == $currentuserid) { // current user |
665 | $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender)); |
666 | $outmain = ''; |
7d792369 |
667 | $output->beep = true; |
668 | |
669 | } else { |
670 | return false; |
671 | } |
b5de723d |
672 | } else if (substr($text, 0, 1) == ':') { /// It's an MOO emote |
673 | $outinfo = $message->strtime; |
674 | $outmain = $sender->firstname.' '.substr($text, 1); |
7d792369 |
675 | |
b5de723d |
676 | } else if (substr($text, 0, 1) == '/') { /// It's a user command |
1515a89e |
677 | |
7d792369 |
678 | if (substr($text, 0, 4) == "/me ") { |
b5de723d |
679 | $outinfo = $message->strtime; |
680 | $outmain = $sender->firstname.' '.substr($text, 4); |
9f85bed4 |
681 | } else { |
b5de723d |
682 | $outinfo = $message->strtime; |
7d792369 |
683 | $outmain = $text; |
1515a89e |
684 | } |
9f85bed4 |
685 | |
686 | } else { /// It's a normal message |
b5de723d |
687 | $outinfo = $message->strtime.' '.$sender->firstname; |
7d792369 |
688 | $outmain = $text; |
1515a89e |
689 | } |
690 | |
9f85bed4 |
691 | /// Format the message as a small table |
1515a89e |
692 | |
b5de723d |
693 | $output->text = strip_tags($outinfo.': '.$outmain); |
7d792369 |
694 | |
b5de723d |
695 | $output->html = "<table><tr><td valign=top>$message->picture</td><td><font size=2>"; |
7d792369 |
696 | $output->html .= "<font color=\"#888888\">$outinfo</font>"; |
697 | if ($outmain) { |
698 | $output->html .= ": $outmain"; |
699 | } |
700 | $output->html .= "</font></td></tr></table>"; |
1515a89e |
701 | |
b5de723d |
702 | // Don't forget to reset the language before returning!!! |
703 | if(!empty($oldcfglang)) { |
704 | $CFG->courselang = $oldcfglang; |
705 | } |
706 | |
7d792369 |
707 | return $output; |
b5de723d |
708 | } |
709 | |
710 | function chat_format_message($message, $courseid=0) { |
711 | /// Given a message object full of information, this function |
712 | /// formats it appropriately into text and html, then |
713 | /// returns the formatted data. |
714 | |
715 | global $CFG, $USER; |
716 | |
717 | if (!$user = get_record("user", "id", $message->userid)) { |
718 | return "Error finding user id = $message->userid"; |
719 | } |
720 | |
721 | return chat_format_message_manually($message, $courseid, $user, $USER->id); |
1515a89e |
722 | |
723 | } |
724 | |
1515a89e |
725 | ?> |