More styles for My Moodle
[moodle.git] / mod / chat / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
1515a89e 2
3/// Library of functions and constants for module chat
4
c4d588cc 5require_once($CFG->libdir.'/pagelib.php');
6
b5012f3e 7if (!isset($CFG->chat_refresh_room)) {
8 set_config("chat_refresh_room", 5);
b5de723d 9}
b5012f3e 10if (!isset($CFG->chat_refresh_userlist)) {
11 set_config("chat_refresh_userlist", 10);
b5de723d 12}
b5012f3e 13if (!isset($CFG->chat_old_ping)) {
34308732 14 set_config("chat_old_ping", 35);
b5de723d 15}
d434b9ae 16if (!isset($CFG->chat_method)) {
17 set_config("chat_method", "header_js");
b5de723d 18}
d434b9ae 19if (!isset($CFG->chat_serverhost)) {
20 set_config("chat_serverhost", $_SERVER['HTTP_HOST']);
b5de723d 21}
d434b9ae 22if (!isset($CFG->chat_serverip)) {
e2a1a0fa 23 set_config("chat_serverip", '127.0.0.1');
b5de723d 24}
d434b9ae 25if (!isset($CFG->chat_serverport)) {
26 set_config("chat_serverport", 9111);
b5de723d 27}
d434b9ae 28if (!isset($CFG->chat_servermax)) {
29 set_config("chat_servermax", 100);
b5de723d 30}
1515a89e 31
1515a89e 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)
89f688b6 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=\"#FFFFFF\"></body></html>";
1515a89e 40
41// The HTML head for the message input page
89f688b6 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=\"#FFFFFF\">";
1515a89e 43
44// The HTML code for the message input page, with JavaScript
89f688b6 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=\"#FFFFFF\" OnLoad=\"document.f.arsc_message.focus();document.f.arsc_message.select();\">";
1515a89e 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
a71efae3 116 if (! $chat = get_record('chat', 'id', $id)) {
1515a89e 117 return false;
118 }
119
120 $result = true;
121
122 # Delete any dependent records here #
123
a71efae3 124 if (! delete_records('chat', 'id', $chat->id)) {
125 $result = false;
126 }
127 if (! delete_records('chat_messages', 'chatid', $chat->id)) {
128 $result = false;
129 }
130 if (! delete_records('chat_users', 'chatid', $chat->id)) {
1515a89e 131 $result = false;
132 }
133
880d8675 134 $pagetypes = page_import_types('mod/chat/');
135 foreach($pagetypes as $pagetype) {
136 if(!delete_records('block_instance', 'pageid', $chat->id, 'pagetype', $pagetype)) {
137 $result = false;
138 }
139 }
140
36eb856f 141 if (! delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) {
142 $result = false;
143 }
144
1515a89e 145 return $result;
146}
147
148function chat_user_outline($course, $user, $mod, $chat) {
b5de723d 149/// Return a small object with summary information about what a
1515a89e 150/// user has done with a given particular instance of this module
151/// Used for user activity reports.
152/// $return->time = the time they did it
153/// $return->info = a short text description
154
5c05d23b 155 $return = NULL;
1515a89e 156 return $return;
157}
158
159function chat_user_complete($course, $user, $mod, $chat) {
b5de723d 160/// Print a detailed representation of what a user has done with
1515a89e 161/// a given particular instance of this module, for user activity reports.
162
163 return true;
164}
165
b7602a11 166function chat_print_recent_activity($course, $isteacher, $timestart) {
167/// Given a course and a date, prints a summary of all chat rooms
168/// that currently have people in them.
1515a89e 169/// This function is called from course/lib.php: print_recent_activity()
170
b7602a11 171 global $CFG;
172
c5a05b95 173 $timeold = time() - $CFG->chat_old_ping;
174
8f7dc7f1 175 $lastpingsearch = ($CFG->chat_method == 'sockets') ? '': 'AND cu.lastping > \''.$timeold.'\'';
0469cccf 176
132911c8 177 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
b7602a11 178 FROM {$CFG->prefix}chat_users as cu,
cea57965 179 {$CFG->prefix}chat as ch,
b7602a11 180 {$CFG->prefix}user as u
b5de723d 181 WHERE cu.userid = u.id
0469cccf 182 AND cu.chatid = ch.id $lastpingsearch
cea57965 183 AND ch.course = '$course->id'
b7602a11 184 ORDER BY cu.chatid ASC") ) {
185 return false;
186 }
1515a89e 187
3120f6f2 188 $isteacher = isteacher($course->id);
b7602a11 189
3120f6f2 190 $outputstarted = false;
b7602a11 191 $current = 0;
192 foreach ($chatusers as $chatuser) {
193 if ($current != $chatuser->chatid) {
194 if ($current) {
8f7dc7f1 195 echo '</ul></div>'; // room
196 $current = 0;
b7602a11 197 }
8f7dc7f1 198 if ($chat = get_record('chat', 'id', $chatuser->chatid)) {
3120f6f2 199 if (!($isteacher or instance_is_visible('chat', $chat))) { // Chat hidden to students
200 continue;
201 }
202 if (!$outputstarted) {
8f7dc7f1 203 print_headline(get_string('currentchats', 'chat').':');
3120f6f2 204 $outputstarted = true;
205 }
200371e9 206 echo '<div class="room"><p class="head"><a href="'.$CFG->wwwroot.'/mod/chat/view.php?c='.$chat->id.'">'.format_string($chat->name,true).'</a></p><ul>';
b7602a11 207 }
208 $current = $chatuser->chatid;
209 }
8f7dc7f1 210 $fullname = fullname($chatuser, $isteacher);
211 echo '<li class="info name">'.$fullname.'</li>';
212 }
213
214 if ($current) {
215 echo '</ul></div>'; // room
b7602a11 216 }
217
218 return true;
1515a89e 219}
220
8f7dc7f1 221
1515a89e 222function chat_cron () {
223/// Function to be run periodically according to the moodle cron
b5de723d 224/// This function searches for things that need to be done, such
225/// as sending out mail, toggling flags etc ...
1515a89e 226
227 global $CFG;
228
fcd3a1ee 229 chat_update_chat_times();
230
7d792369 231 chat_delete_old_users();
232
22a4491a 233 /// Delete old messages
234 if ($chats = get_records("chat")) {
235 foreach ($chats as $chat) {
236 if ($chat->keepdays) {
237 $timeold = time() - ($chat->keepdays * 24 * 3600);
238 delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'");
239 }
240 }
241 }
242
1515a89e 243 return true;
244}
245
84a2fdd7 246function chat_get_participants($chatid, $groupid=0) {
05855091 247//Returns the users with data in one chat
248//(users with records in chat_messages, students)
249
250 global $CFG;
251
84a2fdd7 252 if ($groupid) {
253 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
254 } else {
255 $groupselect = "";
256 }
257
05855091 258 //Get students
95e72c12 259 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 260 FROM {$CFG->prefix}user u,
261 {$CFG->prefix}chat_messages c
84a2fdd7 262 WHERE c.chatid = '$chatid' $groupselect
263 AND u.id = c.userid");
1515a89e 264
05855091 265 //Return students array (it contains an array of unique users)
266 return ($students);
267}
1515a89e 268
8496c4af 269function chat_refresh_events($courseid = 0) {
270// This standard function will check all instances of this module
271// and make sure there are up-to-date events created for each of them.
272// If courseid = 0, then every chat event in the site is checked, else
273// only chat events belonging to the course specified are checked.
274// This function is used, in its new format, by restore_refresh_events()
275
276 if ($courseid) {
277 if (! $chats = get_records("chat", "course", $courseid)) {
278 return true;
279 }
280 } else {
281 if (! $chats = get_records("chat")) {
282 return true;
283 }
284 }
dcd338ff 285 $moduleid = get_field('modules', 'id', 'name', 'chat');
8496c4af 286
287 foreach ($chats as $chat) {
288 $event = NULL;
289 $event->name = addslashes($chat->name);
290 $event->description = addslashes($chat->intro);
291 $event->timestart = $chat->chattime;
292
293 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
294 update_event($event);
295
296 } else {
297 $event->courseid = $chat->course;
298 $event->groupid = 0;
299 $event->userid = 0;
300 $event->modulename = 'chat';
301 $event->instance = $chat->id;
302 $event->eventtype = $chat->schedule;
303 $event->timeduration = 0;
dcd338ff 304 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
b5de723d 305
8496c4af 306 add_event($event);
307 }
308 }
309 return true;
310}
311
516121bd 312function chat_force_language($lang) {
313/// This function prepares moodle to operate in given language
314/// usable when $nomoodlecookie = true;
315/// BEWARE: there must be no $course, $USER or $SESSION
a776c371 316 global $CFG;
317
516121bd 318 if(!empty($CFG->courselang)) {
a776c371 319 unset($CFG->courselang);
516121bd 320 }
321 if(!empty($CFG->locale)) {
a776c371 322 unset($CFG->locale);
516121bd 323 }
e7fbd0b3 324 $CFG->lang = $lang;
516121bd 325 moodle_setlocale();
326}
327
1515a89e 328//////////////////////////////////////////////////////////////////////
329/// Functions that require some SQL
330
84a2fdd7 331function chat_get_users($chatid, $groupid=0) {
1515a89e 332
333 global $CFG;
84a2fdd7 334
335 if ($groupid) {
336 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
337 } else {
338 $groupselect = "";
339 }
b5de723d 340
426c0437 341 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
1515a89e 342 FROM {$CFG->prefix}chat_users c,
343 {$CFG->prefix}user u
344 WHERE c.chatid = '$chatid'
84a2fdd7 345 AND u.id = c.userid $groupselect
1515a89e 346 ORDER BY c.firstping ASC");
347}
348
84a2fdd7 349function chat_get_latest_message($chatid, $groupid=0) {
5a8625e4 350/// Efficient way to extract just the latest message
351/// Uses ADOdb directly instead of get_record_sql()
b5de723d 352/// because the LIMIT command causes problems with
5a8625e4 353/// the developer debugging in there.
1515a89e 354
5a8625e4 355 global $db, $CFG;
1515a89e 356
84a2fdd7 357 if ($groupid) {
358 $groupselect = " AND (groupid='$groupid' OR groupid='0')";
359 } else {
360 $groupselect = "";
361 }
362
5a8625e4 363 if (!$rs = $db->Execute("SELECT *
b5de723d 364 FROM {$CFG->prefix}chat_messages
84a2fdd7 365 WHERE chatid = '$chatid' $groupselect
5a8625e4 366 ORDER BY timestamp DESC LIMIT 1")) {
367 return false;
368 }
369 if ($rs->RecordCount() == 1) {
370 return (object)$rs->fields;
371 } else {
372 return false; // Found no records
373 }
1515a89e 374}
375
5a8625e4 376
1515a89e 377//////////////////////////////////////////////////////////////////////
516121bd 378// login if not already logged in
1515a89e 379
a32c7772 380function chat_login_user($chatid, $version, $groupid, $course) {
1515a89e 381 global $USER;
e7fbd0b3 382 if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) {
516121bd 383 $chatuser->version = $version;
0f4e83c1 384 $chatuser->ip = $USER->lastip;
516121bd 385 $chatuser->lastping = time();
386 $chatuser->lang = current_language();
1515a89e 387
516121bd 388 if (($chatuser->course != $course->id)
389 or ($chatuser->userid != $USER->id)) {
390 return false;
391 }
392 if (!update_record('chat_users', $chatuser)) {
393 return false;
394 }
516121bd 395 } else {
396 $chatuser->chatid = $chatid;
397 $chatuser->userid = $USER->id;
398 $chatuser->groupid = $groupid;
399 $chatuser->version = $version;
7b5aa1b7 400 $chatuser->ip = $USER->lastIP;
516121bd 401 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
402 $chatuser->sid = random_string(32);
3dfd307f 403 $chatuser->course = $course->id; //caching - needed for current_language too
404 $chatuser->lang = current_language(); //caching - to resource intensive to find out later
516121bd 405
406 if (!insert_record('chat_users', $chatuser)) {
407 return false;
408 }
409
a32c7772 410 if ($version == 'sockets') {
411 // do not send 'enter' message, chatd will do it
412 } else {
2ac0d13b 413 $message->chatid = $chatuser->chatid;
414 $message->userid = $chatuser->userid;
415 $message->groupid = $groupid;
416 $message->message = 'enter';
417 $message->system = 1;
418 $message->timestamp = time();
419
420 if (!insert_record('chat_messages', $message)) {
421 error('Could not insert a chat message!');
422 }
516121bd 423 }
1515a89e 424 }
425
426 return $chatuser->sid;
427}
428
7d792369 429function chat_delete_old_users() {
430// Delete the old and in the way
431
b5012f3e 432 global $CFG;
433
e7fbd0b3 434 $timeold = time() - $CFG->chat_old_ping;
a32c7772 435
c7756c73 436 $query = "lastping < '$timeold'";
7d792369 437
516121bd 438 if ($oldusers = get_records_select('chat_users', $query) ) {
439 delete_records_select('chat_users', $query);
7d792369 440 foreach ($oldusers as $olduser) {
516121bd 441 $message->chatid = $olduser->chatid;
442 $message->userid = $olduser->userid;
443 $message->groupid = $olduser->groupid;
444 $message->message = 'exit';
445 $message->system = 1;
7d792369 446 $message->timestamp = time();
b5de723d 447
516121bd 448 if (!insert_record('chat_messages', $message)) {
449 error('Could not insert a chat message!');
7d792369 450 }
451 }
452 }
453}
1515a89e 454
22a4491a 455
fcd3a1ee 456function chat_update_chat_times($chatid=0) {
457/// Updates chat records so that the next chat time is correct
458
459 $timenow = time();
460 if ($chatid) {
461 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
462 return;
463 }
464 } else {
465 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
466 return;
467 }
468 }
469
470 foreach ($chats as $chat) {
7626969c 471 unset($chat->name);
472 unset($chat->intro);
fcd3a1ee 473 switch ($chat->schedule) {
474 case 1: // Single event - turn off schedule and disable
475 $chat->chattime = 0;
476 $chat->schedule = 0;
477 break;
478 case 2: // Repeat daily
479 $chat->chattime += 24 * 3600;
480 break;
481 case 3: // Repeat weekly
482 $chat->chattime += 7 * 24 * 3600;
483 break;
484 }
485 update_record("chat", $chat);
8496c4af 486
487 $event = NULL; // Update calendar too
488 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
489 $event->timestart = $chat->chattime;
490 update_event($event);
491 }
fcd3a1ee 492 }
493}
494
495
aa5c32fd 496function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chat_lastrow=NULL) {
72989350 497 global $CFG, $USER;
1515a89e 498
b5de723d 499 $output = New stdClass;
516121bd 500 $output->beep = false; // by default
501 $output->refreshusers = false; // by default
7d792369 502
72989350 503 // Use get_user_timezone() to find the correct timezone for displaying this message:
504 // It's either the current user's timezone or else decided by some Moodle config setting
970f144e 505 // First, "reset" $USER->timezone (which could have been set by a previous call to here)
506 // because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone
507 $USER->timezone = 99;
72989350 508 $tz = get_user_timezone($currentuser->timezone);
b5de723d 509
72989350 510 // Before formatting the message time string, set $USER->timezone to the above.
511 // This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
512 // message times appear off because DST is not taken into account when it should be.
513 $USER->timezone = $tz;
b5de723d 514 $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
515
516 $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
582de679 517 if ($courseid) {
839f2456 518 $message->picture = "<a target=\"_new\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid\">$message->picture</a>";
582de679 519 }
1515a89e 520
aa5c32fd 521 //Calculate the row class
522 if ($chat_lastrow !== NULL) {
523 $rowclass = ' class="r'.$chat_lastrow.'" ';
524 } else {
525 $rowclass = '';
526 }
527
b5de723d 528 // Start processing the message
1515a89e 529
b5de723d 530 if(!empty($message->system)) {
531 // System event
532 $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
aa5c32fd 533 $output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td><td class="text">';
534 $output->html .= '<span class="event">'.$output->text.'</span></td></tr></table>';
7d792369 535
516121bd 536 if($message->message == 'exit' or $message->message == 'enter') {
537 $output->refreshusers = true; //force user panel refresh ASAP
538 }
539
1515a89e 540 return $output;
541 }
542
82a524ef 543 // It's not a system event
b5de723d 544
545 $text = $message->message;
82a524ef 546
547 /// Parse the text to clean and filter it
548
549 $options->para = false;
550 $text = format_text($text, FORMAT_MOODLE, $options, $courseid);
7d792369 551
b5de723d 552 // And now check for special cases
553
554 if (substr($text, 0, 5) == 'beep ') {
555 /// It's a beep!
7d792369 556 $beepwho = trim(substr($text, 5));
9f85bed4 557
b5de723d 558 if ($beepwho == 'all') { // everyone
559 $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender));
560 $outmain = '';
561 $output->beep = true; // (eventually this should be set to
7d792369 562 // to a filename uploaded by the user)
563
82a524ef 564 } else if ($beepwho == $currentuser->id) { // current user
b5de723d 565 $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender));
566 $outmain = '';
7d792369 567 $output->beep = true;
568
569 } else {
570 return false;
571 }
b5de723d 572 } else if (substr($text, 0, 1) == ':') { /// It's an MOO emote
573 $outinfo = $message->strtime;
574 $outmain = $sender->firstname.' '.substr($text, 1);
7d792369 575
b5de723d 576 } else if (substr($text, 0, 1) == '/') { /// It's a user command
1515a89e 577
7d792369 578 if (substr($text, 0, 4) == "/me ") {
b5de723d 579 $outinfo = $message->strtime;
580 $outmain = $sender->firstname.' '.substr($text, 4);
9f85bed4 581 } else {
b5de723d 582 $outinfo = $message->strtime;
7d792369 583 $outmain = $text;
1515a89e 584 }
9f85bed4 585
586 } else { /// It's a normal message
b5de723d 587 $outinfo = $message->strtime.' '.$sender->firstname;
7d792369 588 $outmain = $text;
1515a89e 589 }
590
9f85bed4 591 /// Format the message as a small table
1515a89e 592
b5de723d 593 $output->text = strip_tags($outinfo.': '.$outmain);
7d792369 594
aa5c32fd 595 $output->html = "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\">$message->picture</td><td class=\"text\">";
596 $output->html .= "<span class=\"title\">$outinfo</span>";
7d792369 597 if ($outmain) {
598 $output->html .= ": $outmain";
599 }
aa5c32fd 600 $output->html .= "</td></tr></table>";
1515a89e 601
7d792369 602 return $output;
b5de723d 603}
604
aa5c32fd 605function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NULL) {
b5de723d 606/// Given a message object full of information, this function
607/// formats it appropriately into text and html, then
608/// returns the formatted data.
609
b5de723d 610 if (!$user = get_record("user", "id", $message->userid)) {
611 return "Error finding user id = $message->userid";
612 }
613
aa5c32fd 614 return chat_format_message_manually($message, $courseid, $user, $currentuser, $chat_lastrow);
1515a89e 615
616}
617
38be9b31 618if (!function_exists('ob_get_clean')) {
619/// Compatibility function for PHP < 4.3.0
620 function ob_get_clean() {
621 $cont = ob_get_contents();
622 if ($cont !== false) {
623 ob_end_clean();
624 return $cont;
625 } else {
626 return $cont;
627 }
628 }
629}
630
f3221af9 631function chat_get_view_actions() {
632 return array('view','view all','report');
633}
634
635function chat_get_post_actions() {
636 return array('talk');
637}
638
9ca0187e 639function chat_print_overview($courses, &$htmlarray) {
640 global $USER, $CFG;
641
642 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
643 return array();
644 }
645
646 if (!$chats = get_all_instances_in_courses('chat',$courses)) {
647 return;
648 }
649
650 $strchat = get_string('modulename', 'chat');
651 $strnextsession = get_string('nextsession', 'chat');
652 $strnoscheduledsession = get_string('noscheduledsession', 'chat');
653
654 foreach ($chats as $chat) {
655 $str = '<a '.($chat->visible?'':' class="dimmed"').' href="'.$CFG->wwwroot.'/mod/chat/view.php?id='.$chat->coursemodule.'">'
656 .$strchat.': '.$chat->name.'</a><br />';
657 if ($chat->chattime and $chat->schedule) { // A chat is scheduled
658 $str .= "$strnextsession: ".userdate($chat->chattime).' ('.usertimezone($USER->timezone).')<br />';
659 } else {
660 $str .= $strnoscheduledsession.'<br />';
661 }
662 $htmlarray[$chat->course]['chat'] .= $str;
663 }
664}
665
1515a89e 666?>