Bugfix: don't send HTML digests to users who want simple text mail.
[moodle.git] / mod / chat / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
1515a89e 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);
b5de723d 7}
b5012f3e 8if (!isset($CFG->chat_refresh_userlist)) {
9 set_config("chat_refresh_userlist", 10);
b5de723d 10}
b5012f3e 11if (!isset($CFG->chat_old_ping)) {
34308732 12 set_config("chat_old_ping", 35);
b5de723d 13}
d434b9ae 14if (!isset($CFG->chat_method)) {
15 set_config("chat_method", "header_js");
b5de723d 16}
d434b9ae 17if (!isset($CFG->chat_serverhost)) {
18 set_config("chat_serverhost", $_SERVER['HTTP_HOST']);
b5de723d 19}
d434b9ae 20if (!isset($CFG->chat_serverip)) {
21 set_config("chat_serverip", $_SERVER['SERVER_ADDR']);
b5de723d 22}
d434b9ae 23if (!isset($CFG->chat_serverport)) {
24 set_config("chat_serverport", 9111);
b5de723d 25}
d434b9ae 26if (!isset($CFG->chat_servermax)) {
27 set_config("chat_servermax", 100);
b5de723d 28}
1515a89e 29
ff458eb5 30global $THEME;
1515a89e 31
32// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
33$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";
34
35// The HTML head for the message window to start with (with js scrolling)
36$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";
37
38// The HTML code for standard empty pages (e.g. if a user was kicked out)
39$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>";
40
41// The HTML head for the message input page
42$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\">";
43
44// The HTML code for the message input page, with JavaScript
45$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();\">";
46
fbabbd23 47// Dummy data that gets output to the browser as needed, in order to make it show output
48$CHAT_DUMMY_DATA = "<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n";
1515a89e 49
50function chat_add_instance($chat) {
b5de723d 51/// Given an object containing all the necessary data,
52/// (defined by the form in mod.html) this function
53/// will create a new instance and return the id number
1515a89e 54/// of the new instance.
55
56 $chat->timemodified = time();
57
b5de723d 58 $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday,
fcd3a1ee 59 $chat->chathour, $chat->chatminute);
b5de723d 60
8496c4af 61 if ($returnid = insert_record("chat", $chat)) {
62
63 $event = NULL;
64 $event->name = $chat->name;
65 $event->description = $chat->intro;
66 $event->courseid = $chat->course;
67 $event->groupid = 0;
68 $event->userid = 0;
69 $event->modulename = 'chat';
70 $event->instance = $returnid;
71 $event->eventtype = $chat->schedule;
72 $event->timestart = $chat->chattime;
73 $event->timeduration = 0;
74
75 add_event($event);
76 }
77
78 return $returnid;
1515a89e 79}
80
81
82function chat_update_instance($chat) {
b5de723d 83/// Given an object containing all the necessary data,
84/// (defined by the form in mod.html) this function
1515a89e 85/// will update an existing instance with new data.
86
87 $chat->timemodified = time();
88 $chat->id = $chat->instance;
89
b5de723d 90 $chat->chattime = make_timestamp($chat->chatyear, $chat->chatmonth, $chat->chatday,
fcd3a1ee 91 $chat->chathour, $chat->chatminute);
1515a89e 92
8496c4af 93 if ($returnid = update_record("chat", $chat)) {
94
95 $event = NULL;
96
97 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
98
99 $event->name = $chat->name;
100 $event->description = $chat->intro;
101 $event->timestart = $chat->chattime;
102
103 update_event($event);
104 }
105 }
106
107 return $returnid;
1515a89e 108}
109
110
111function chat_delete_instance($id) {
b5de723d 112/// Given an ID of an instance of this module,
113/// this function will permanently delete the instance
114/// and any data that depends on it.
1515a89e 115
116 if (! $chat = get_record("chat", "id", "$id")) {
117 return false;
118 }
119
120 $result = true;
121
122 # Delete any dependent records here #
123
124 if (! delete_records("chat", "id", "$chat->id")) {
125 $result = false;
126 }
127
36eb856f 128 if (! delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) {
129 $result = false;
130 }
131
1515a89e 132 return $result;
133}
134
135function chat_user_outline($course, $user, $mod, $chat) {
b5de723d 136/// Return a small object with summary information about what a
1515a89e 137/// user has done with a given particular instance of this module
138/// Used for user activity reports.
139/// $return->time = the time they did it
140/// $return->info = a short text description
141
5c05d23b 142 $return = NULL;
1515a89e 143 return $return;
144}
145
146function chat_user_complete($course, $user, $mod, $chat) {
b5de723d 147/// Print a detailed representation of what a user has done with
1515a89e 148/// a given particular instance of this module, for user activity reports.
149
150 return true;
151}
152
b7602a11 153function chat_print_recent_activity($course, $isteacher, $timestart) {
154/// Given a course and a date, prints a summary of all chat rooms
155/// that currently have people in them.
1515a89e 156/// This function is called from course/lib.php: print_recent_activity()
157
b7602a11 158 global $CFG;
159
c5a05b95 160 $timeold = time() - $CFG->chat_old_ping;
161
0469cccf 162 $lastpingsearch = ($CFG->chat_method == 'sockets') ? "": "AND cu.lastping > '$timeold'";
163
132911c8 164 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
b7602a11 165 FROM {$CFG->prefix}chat_users as cu,
cea57965 166 {$CFG->prefix}chat as ch,
b7602a11 167 {$CFG->prefix}user as u
b5de723d 168 WHERE cu.userid = u.id
0469cccf 169 AND cu.chatid = ch.id $lastpingsearch
cea57965 170 AND ch.course = '$course->id'
b7602a11 171 ORDER BY cu.chatid ASC") ) {
172 return false;
173 }
1515a89e 174
3120f6f2 175 $isteacher = isteacher($course->id);
b7602a11 176
3120f6f2 177 $outputstarted = false;
b7602a11 178 $current = 0;
179 foreach ($chatusers as $chatuser) {
180 if ($current != $chatuser->chatid) {
181 if ($current) {
182 echo "</p>";
183 }
184 if ($chat = get_record("chat", "id", $chatuser->chatid)) {
3120f6f2 185 if (!($isteacher or instance_is_visible('chat', $chat))) { // Chat hidden to students
186 continue;
187 }
188 if (!$outputstarted) {
189 print_headline(get_string("currentchats", "chat").":");
190 $outputstarted = true;
191 }
1c61c8d6 192 echo "<p><font size=\"1\"><a href=\"$CFG->wwwroot/mod/chat/view.php?c=$chat->id\">$chat->name</a></font><br />";
b7602a11 193 }
194 $current = $chatuser->chatid;
195 }
8f6ca087 196 $fullname = fullname($chatuser);
1c61c8d6 197 echo "&nbsp;&nbsp;&nbsp;<font size=\"1\">- $fullname</font><br />";
b7602a11 198 }
48548144 199 echo "<br />";
b7602a11 200
201 return true;
1515a89e 202}
203
204function chat_cron () {
205/// Function to be run periodically according to the moodle cron
b5de723d 206/// This function searches for things that need to be done, such
207/// as sending out mail, toggling flags etc ...
1515a89e 208
209 global $CFG;
210
fcd3a1ee 211 chat_update_chat_times();
212
7d792369 213 chat_delete_old_users();
214
22a4491a 215 /// Delete old messages
216 if ($chats = get_records("chat")) {
217 foreach ($chats as $chat) {
218 if ($chat->keepdays) {
219 $timeold = time() - ($chat->keepdays * 24 * 3600);
220 delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'");
221 }
222 }
223 }
224
1515a89e 225 return true;
226}
227
84a2fdd7 228function chat_get_participants($chatid, $groupid=0) {
05855091 229//Returns the users with data in one chat
230//(users with records in chat_messages, students)
231
232 global $CFG;
233
84a2fdd7 234 if ($groupid) {
235 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
236 } else {
237 $groupselect = "";
238 }
239
05855091 240 //Get students
241 $students = get_records_sql("SELECT DISTINCT u.*
242 FROM {$CFG->prefix}user u,
243 {$CFG->prefix}chat_messages c
84a2fdd7 244 WHERE c.chatid = '$chatid' $groupselect
245 AND u.id = c.userid");
1515a89e 246
05855091 247 //Return students array (it contains an array of unique users)
248 return ($students);
249}
1515a89e 250
8496c4af 251function chat_refresh_events($courseid = 0) {
252// This standard function will check all instances of this module
253// and make sure there are up-to-date events created for each of them.
254// If courseid = 0, then every chat event in the site is checked, else
255// only chat events belonging to the course specified are checked.
256// This function is used, in its new format, by restore_refresh_events()
257
258 if ($courseid) {
259 if (! $chats = get_records("chat", "course", $courseid)) {
260 return true;
261 }
262 } else {
263 if (! $chats = get_records("chat")) {
264 return true;
265 }
266 }
dcd338ff 267 $moduleid = get_field('modules', 'id', 'name', 'chat');
8496c4af 268
269 foreach ($chats as $chat) {
270 $event = NULL;
271 $event->name = addslashes($chat->name);
272 $event->description = addslashes($chat->intro);
273 $event->timestart = $chat->chattime;
274
275 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
276 update_event($event);
277
278 } else {
279 $event->courseid = $chat->course;
280 $event->groupid = 0;
281 $event->userid = 0;
282 $event->modulename = 'chat';
283 $event->instance = $chat->id;
284 $event->eventtype = $chat->schedule;
285 $event->timeduration = 0;
dcd338ff 286 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
b5de723d 287
8496c4af 288 add_event($event);
289 }
290 }
291 return true;
292}
293
516121bd 294function chat_force_language($lang) {
295/// This function prepares moodle to operate in given language
296/// usable when $nomoodlecookie = true;
297/// BEWARE: there must be no $course, $USER or $SESSION
298 if(!empty($CFG->courselang)) {
299 uset($CFG->courselang);
300 }
301 if(!empty($CFG->locale)) {
302 uset($CFG->locale);
303 }
304 $CFG->lang = clean_filename($lang);
305 moodle_setlocale();
306}
307
1515a89e 308//////////////////////////////////////////////////////////////////////
309/// Functions that require some SQL
310
84a2fdd7 311function chat_get_users($chatid, $groupid=0) {
1515a89e 312
313 global $CFG;
84a2fdd7 314
315 if ($groupid) {
316 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
317 } else {
318 $groupselect = "";
319 }
b5de723d 320
426c0437 321 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
1515a89e 322 FROM {$CFG->prefix}chat_users c,
323 {$CFG->prefix}user u
324 WHERE c.chatid = '$chatid'
84a2fdd7 325 AND u.id = c.userid $groupselect
1515a89e 326 ORDER BY c.firstping ASC");
327}
328
84a2fdd7 329function chat_get_latest_message($chatid, $groupid=0) {
5a8625e4 330/// Efficient way to extract just the latest message
331/// Uses ADOdb directly instead of get_record_sql()
b5de723d 332/// because the LIMIT command causes problems with
5a8625e4 333/// the developer debugging in there.
1515a89e 334
5a8625e4 335 global $db, $CFG;
1515a89e 336
84a2fdd7 337 if ($groupid) {
338 $groupselect = " AND (groupid='$groupid' OR groupid='0')";
339 } else {
340 $groupselect = "";
341 }
342
5a8625e4 343 if (!$rs = $db->Execute("SELECT *
b5de723d 344 FROM {$CFG->prefix}chat_messages
84a2fdd7 345 WHERE chatid = '$chatid' $groupselect
5a8625e4 346 ORDER BY timestamp DESC LIMIT 1")) {
347 return false;
348 }
349 if ($rs->RecordCount() == 1) {
350 return (object)$rs->fields;
351 } else {
352 return false; // Found no records
353 }
1515a89e 354}
355
5a8625e4 356
1515a89e 357//////////////////////////////////////////////////////////////////////
516121bd 358// login if not already logged in
1515a89e 359
516121bd 360function chat_login_user($chatid, $version, $groupid, $course) {
1515a89e 361 global $USER;
362
516121bd 363 if ($chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid' LIMIT 1")) {
364 $chatuser->version = $version;
365 $chatuser->ip = $USER->lastIP;
366 $chatuser->lastping = time();
367 $chatuser->lang = current_language();
1515a89e 368
516121bd 369 if (($chatuser->course != $course->id)
370 or ($chatuser->userid != $USER->id)) {
371 return false;
372 }
373 if (!update_record('chat_users', $chatuser)) {
374 return false;
375 }
376
377 } else {
378 $chatuser->chatid = $chatid;
379 $chatuser->userid = $USER->id;
380 $chatuser->groupid = $groupid;
381 $chatuser->version = $version;
382 $chatuser->ip = $USER->lastIP;
383 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
384 $chatuser->sid = random_string(32);
385 $chatuser->course = $course->id; //caching only, but needed for current_language() to work properly!
386 $chatuser->lang = current_language();
387
388 if (!insert_record('chat_users', $chatuser)) {
389 return false;
390 }
391
392 $message->chatid = $chatuser->chatid;
393 $message->userid = $chatuser->userid;
394 $message->groupid = $groupid;
395 $message->message = 'enter';
396 $message->system = 1;
397 $message->timestamp = time();
398
399 if (!insert_record('chat_messages', $message)) {
400 error('Could not insert a chat message!');
401 }
1515a89e 402 }
403
404 return $chatuser->sid;
405}
406
7d792369 407function chat_delete_old_users() {
408// Delete the old and in the way
409
b5012f3e 410 global $CFG;
411
412 $timeold = time() - $CFG->chat_old_ping;
c7756c73 413 $query = "lastping < '$timeold'";
7d792369 414
516121bd 415 if ($oldusers = get_records_select('chat_users', $query) ) {
416 delete_records_select('chat_users', $query);
7d792369 417 foreach ($oldusers as $olduser) {
516121bd 418 $message->chatid = $olduser->chatid;
419 $message->userid = $olduser->userid;
420 $message->groupid = $olduser->groupid;
421 $message->message = 'exit';
422 $message->system = 1;
7d792369 423 $message->timestamp = time();
b5de723d 424
516121bd 425 if (!insert_record('chat_messages', $message)) {
426 error('Could not insert a chat message!');
7d792369 427 }
428 }
429 }
430}
1515a89e 431
22a4491a 432
fcd3a1ee 433function chat_update_chat_times($chatid=0) {
434/// Updates chat records so that the next chat time is correct
435
436 $timenow = time();
437 if ($chatid) {
438 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
439 return;
440 }
441 } else {
442 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
443 return;
444 }
445 }
446
447 foreach ($chats as $chat) {
7626969c 448 unset($chat->name);
449 unset($chat->intro);
fcd3a1ee 450 switch ($chat->schedule) {
451 case 1: // Single event - turn off schedule and disable
452 $chat->chattime = 0;
453 $chat->schedule = 0;
454 break;
455 case 2: // Repeat daily
456 $chat->chattime += 24 * 3600;
457 break;
458 case 3: // Repeat weekly
459 $chat->chattime += 7 * 24 * 3600;
460 break;
461 }
462 update_record("chat", $chat);
8496c4af 463
464 $event = NULL; // Update calendar too
465 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
466 $event->timestart = $chat->chattime;
467 update_event($event);
468 }
fcd3a1ee 469 }
470}
471
472
1515a89e 473
82a524ef 474function chat_language_override($language) {
475 // Override the highest-ranking language variable from current_language()
476 // And save it so we can restore it again afterwards
477 global $CFG;
1515a89e 478
82a524ef 479 $oldlang = empty($CFG->courselang) ? NULL : $CFG->courselang;
480 $CFG->courselang = $language;
481
482 return $oldlang;
483}
484
485function chat_language_restore($language = NULL) {
486 // Restore the highest-ranking language variable from current_language()
487 global $CFG;
488
489 if(!empty($language)) {
490 $CFG->courselang = $language;
491 }
492}
493
494function chat_format_message_manually($message, $courseid, $sender, $currentuser, $language = NULL) {
b5de723d 495 global $CFG;
1515a89e 496
b5de723d 497 $output = New stdClass;
516121bd 498 $output->beep = false; // by default
499 $output->refreshusers = false; // by default
7d792369 500
b5de723d 501 if(empty($language)) {
502 $language = current_language();
1515a89e 503 }
504
82a524ef 505 $oldcfglang = chat_language_override($language);
b5de723d 506
507 // Get some additional info now that the language has been correctly set
508
509 // But before that :-) let's override get_user_timezone() for this call... messy stuff...
82a524ef 510 $tz = ($currentuser->timezone == 99) ? $CFG->timezone : $currentuser->timezone;
b5de723d 511 $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
512
513 $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
582de679 514 if ($courseid) {
839f2456 515 $message->picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid\">$message->picture</a>";
582de679 516 }
1515a89e 517
b5de723d 518 // Start processing the message
1515a89e 519
b5de723d 520 if(!empty($message->system)) {
521 // System event
522 $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
523 $output->html = '<table><tr><td style="vertical-align: top;">'.$message->picture.'</td><td>';
2defdcd7 524 $output->html .= '<font size="2" color="#ccaaaa">'.$output->text.'</font></td></tr></table>';
7d792369 525
516121bd 526 if($message->message == 'exit' or $message->message == 'enter') {
527 $output->refreshusers = true; //force user panel refresh ASAP
528 }
529
b5de723d 530 // Don't forget to reset the language before returning!!!
531 if(!empty($oldcfglang)) {
532 $CFG->courselang = $oldcfglang;
533 }
1515a89e 534 return $output;
535 }
536
82a524ef 537 // It's not a system event
b5de723d 538
539 $text = $message->message;
82a524ef 540
541 /// Parse the text to clean and filter it
542
543 $options->para = false;
544 $text = format_text($text, FORMAT_MOODLE, $options, $courseid);
7d792369 545
b5de723d 546 // And now check for special cases
547
548 if (substr($text, 0, 5) == 'beep ') {
549 /// It's a beep!
7d792369 550 $beepwho = trim(substr($text, 5));
9f85bed4 551
b5de723d 552 if ($beepwho == 'all') { // everyone
553 $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender));
554 $outmain = '';
555 $output->beep = true; // (eventually this should be set to
7d792369 556 // to a filename uploaded by the user)
557
82a524ef 558 } else if ($beepwho == $currentuser->id) { // current user
b5de723d 559 $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender));
560 $outmain = '';
7d792369 561 $output->beep = true;
562
563 } else {
564 return false;
565 }
b5de723d 566 } else if (substr($text, 0, 1) == ':') { /// It's an MOO emote
567 $outinfo = $message->strtime;
568 $outmain = $sender->firstname.' '.substr($text, 1);
7d792369 569
b5de723d 570 } else if (substr($text, 0, 1) == '/') { /// It's a user command
1515a89e 571
7d792369 572 if (substr($text, 0, 4) == "/me ") {
b5de723d 573 $outinfo = $message->strtime;
574 $outmain = $sender->firstname.' '.substr($text, 4);
9f85bed4 575 } else {
b5de723d 576 $outinfo = $message->strtime;
7d792369 577 $outmain = $text;
1515a89e 578 }
9f85bed4 579
580 } else { /// It's a normal message
b5de723d 581 $outinfo = $message->strtime.' '.$sender->firstname;
7d792369 582 $outmain = $text;
1515a89e 583 }
584
9f85bed4 585 /// Format the message as a small table
1515a89e 586
b5de723d 587 $output->text = strip_tags($outinfo.': '.$outmain);
7d792369 588
1c61c8d6 589 $output->html = "<table><tr><td valign=\"top\">$message->picture</td><td><font size=\"2\">";
7d792369 590 $output->html .= "<font color=\"#888888\">$outinfo</font>";
591 if ($outmain) {
592 $output->html .= ": $outmain";
593 }
594 $output->html .= "</font></td></tr></table>";
1515a89e 595
b5de723d 596 // Don't forget to reset the language before returning!!!
82a524ef 597 chat_language_restore($oldcfglang);
b5de723d 598
7d792369 599 return $output;
b5de723d 600}
601
516121bd 602function chat_format_message($message, $courseid, $currentuser) {
b5de723d 603/// Given a message object full of information, this function
604/// formats it appropriately into text and html, then
605/// returns the formatted data.
606
b5de723d 607 if (!$user = get_record("user", "id", $message->userid)) {
608 return "Error finding user id = $message->userid";
609 }
610
516121bd 611 return chat_format_message_manually($message, $courseid, $user, $currentuser);
1515a89e 612
613}
614
1515a89e 615?>