Possible fix to MDL-12093 : give window without heading and navigation special css...
[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}
930413b1 19if (!isset($CFG->chat_normal_updatemode)) {
20 set_config("chat_normal_updatemode", 'jsupdate');
21}
d434b9ae 22if (!isset($CFG->chat_serverhost)) {
23 set_config("chat_serverhost", $_SERVER['HTTP_HOST']);
b5de723d 24}
d434b9ae 25if (!isset($CFG->chat_serverip)) {
e2a1a0fa 26 set_config("chat_serverip", '127.0.0.1');
b5de723d 27}
d434b9ae 28if (!isset($CFG->chat_serverport)) {
29 set_config("chat_serverport", 9111);
b5de723d 30}
d434b9ae 31if (!isset($CFG->chat_servermax)) {
32 set_config("chat_servermax", 100);
b5de723d 33}
1515a89e 34
1515a89e 35// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
36$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";
37
38// The HTML head for the message window to start with (with js scrolling)
32f0b38a 39$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 type=\"text/javascript\">\n//<![CDATA[\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";
1515a89e 40
41// The HTML code for standard empty pages (e.g. if a user was kicked out)
89f688b6 42$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 43
44// The HTML head for the message input page
89f688b6 45$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 46
47// The HTML code for the message input page, with JavaScript
32f0b38a 48$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 type=\"text/javascript\">\n//<![CDATA[\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 49
fbabbd23 50// Dummy data that gets output to the browser as needed, in order to make it show output
51$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 52
53function chat_add_instance($chat) {
b5de723d 54/// Given an object containing all the necessary data,
55/// (defined by the form in mod.html) this function
56/// will create a new instance and return the id number
1515a89e 57/// of the new instance.
58
59 $chat->timemodified = time();
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
1515a89e 90
8496c4af 91 if ($returnid = update_record("chat", $chat)) {
92
264867fd 93 $event = new object();
8496c4af 94
95 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
96
97 $event->name = $chat->name;
98 $event->description = $chat->intro;
99 $event->timestart = $chat->chattime;
100
101 update_event($event);
102 }
103 }
104
105 return $returnid;
1515a89e 106}
107
108
109function chat_delete_instance($id) {
b5de723d 110/// Given an ID of an instance of this module,
111/// this function will permanently delete the instance
112/// and any data that depends on it.
1515a89e 113
a71efae3 114 if (! $chat = get_record('chat', 'id', $id)) {
1515a89e 115 return false;
116 }
117
118 $result = true;
119
120 # Delete any dependent records here #
121
a71efae3 122 if (! delete_records('chat', 'id', $chat->id)) {
123 $result = false;
124 }
125 if (! delete_records('chat_messages', 'chatid', $chat->id)) {
126 $result = false;
127 }
128 if (! delete_records('chat_users', 'chatid', $chat->id)) {
1515a89e 129 $result = false;
130 }
131
880d8675 132 $pagetypes = page_import_types('mod/chat/');
133 foreach($pagetypes as $pagetype) {
134 if(!delete_records('block_instance', 'pageid', $chat->id, 'pagetype', $pagetype)) {
135 $result = false;
136 }
137 }
138
36eb856f 139 if (! delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) {
140 $result = false;
141 }
142
1515a89e 143 return $result;
144}
145
146function chat_user_outline($course, $user, $mod, $chat) {
b5de723d 147/// Return a small object with summary information about what a
1515a89e 148/// user has done with a given particular instance of this module
149/// Used for user activity reports.
150/// $return->time = the time they did it
151/// $return->info = a short text description
152
5c05d23b 153 $return = NULL;
1515a89e 154 return $return;
155}
156
157function chat_user_complete($course, $user, $mod, $chat) {
b5de723d 158/// Print a detailed representation of what a user has done with
1515a89e 159/// a given particular instance of this module, for user activity reports.
160
161 return true;
162}
163
b7602a11 164function chat_print_recent_activity($course, $isteacher, $timestart) {
165/// Given a course and a date, prints a summary of all chat rooms
166/// that currently have people in them.
1515a89e 167/// This function is called from course/lib.php: print_recent_activity()
168
b7602a11 169 global $CFG;
170
c5a05b95 171 $timeold = time() - $CFG->chat_old_ping;
172
8f7dc7f1 173 $lastpingsearch = ($CFG->chat_method == 'sockets') ? '': 'AND cu.lastping > \''.$timeold.'\'';
0469cccf 174
132911c8 175 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
3d81c41e 176 FROM {$CFG->prefix}chat_users cu,
177 {$CFG->prefix}chat ch,
178 {$CFG->prefix}user u
b5de723d 179 WHERE cu.userid = u.id
0469cccf 180 AND cu.chatid = ch.id $lastpingsearch
cea57965 181 AND ch.course = '$course->id'
b7602a11 182 ORDER BY cu.chatid ASC") ) {
183 return false;
184 }
1515a89e 185
3120f6f2 186 $outputstarted = false;
b7602a11 187 $current = 0;
188 foreach ($chatusers as $chatuser) {
189 if ($current != $chatuser->chatid) {
190 if ($current) {
8f7dc7f1 191 echo '</ul></div>'; // room
192 $current = 0;
b7602a11 193 }
8f7dc7f1 194 if ($chat = get_record('chat', 'id', $chatuser->chatid)) {
264867fd 195
dabfd0ed 196 // we find the course module id
80aede31 197 $cm = get_coursemodule_from_instance('chat', $chat->id, $course->id);
dabfd0ed 198 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
264867fd 199
dabfd0ed 200 // needs to be fixed
0468976c 201 if (!(has_capability('mod/chat:readlog', $context) or instance_is_visible('chat', $chat))) { // Chat hidden to students
3120f6f2 202 continue;
203 }
204 if (!$outputstarted) {
8f7dc7f1 205 print_headline(get_string('currentchats', 'chat').':');
3120f6f2 206 $outputstarted = true;
207 }
200371e9 208 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 209 }
210 $current = $chatuser->chatid;
211 }
80aede31 212 $fullname = fullname($chatuser, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
8f7dc7f1 213 echo '<li class="info name">'.$fullname.'</li>';
214 }
215
216 if ($current) {
217 echo '</ul></div>'; // room
b7602a11 218 }
219
220 return true;
1515a89e 221}
222
8f7dc7f1 223
1515a89e 224function chat_cron () {
225/// Function to be run periodically according to the moodle cron
b5de723d 226/// This function searches for things that need to be done, such
227/// as sending out mail, toggling flags etc ...
1515a89e 228
229 global $CFG;
230
fcd3a1ee 231 chat_update_chat_times();
232
7d792369 233 chat_delete_old_users();
234
22a4491a 235 /// Delete old messages
4388027c 236 $keepdays = "SELECT c.keepdays
237 FROM {$CFG->prefix}chat c
238 WHERE c.id = {$CFG->prefix}chat_messages.chatid AND c.keepdays > 0";
239
240 $sql = "DELETE
241 FROM {$CFG->prefix}chat_messages
242 WHERE timestamp < ( ".time()." - COALESCE(($keepdays), 999999) * 24 * 3600)";
243
244 execute_sql($sql, false);
22a4491a 245
1515a89e 246 return true;
247}
248
84a2fdd7 249function chat_get_participants($chatid, $groupid=0) {
05855091 250//Returns the users with data in one chat
251//(users with records in chat_messages, students)
252
253 global $CFG;
254
84a2fdd7 255 if ($groupid) {
256 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
257 } else {
258 $groupselect = "";
259 }
260
05855091 261 //Get students
95e72c12 262 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 263 FROM {$CFG->prefix}user u,
264 {$CFG->prefix}chat_messages c
84a2fdd7 265 WHERE c.chatid = '$chatid' $groupselect
266 AND u.id = c.userid");
1515a89e 267
05855091 268 //Return students array (it contains an array of unique users)
269 return ($students);
270}
1515a89e 271
8496c4af 272function chat_refresh_events($courseid = 0) {
273// This standard function will check all instances of this module
274// and make sure there are up-to-date events created for each of them.
275// If courseid = 0, then every chat event in the site is checked, else
276// only chat events belonging to the course specified are checked.
277// This function is used, in its new format, by restore_refresh_events()
278
279 if ($courseid) {
280 if (! $chats = get_records("chat", "course", $courseid)) {
281 return true;
282 }
283 } else {
284 if (! $chats = get_records("chat")) {
285 return true;
286 }
287 }
dcd338ff 288 $moduleid = get_field('modules', 'id', 'name', 'chat');
8496c4af 289
290 foreach ($chats as $chat) {
291 $event = NULL;
292 $event->name = addslashes($chat->name);
293 $event->description = addslashes($chat->intro);
294 $event->timestart = $chat->chattime;
295
296 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
297 update_event($event);
298
299 } else {
300 $event->courseid = $chat->course;
301 $event->groupid = 0;
302 $event->userid = 0;
303 $event->modulename = 'chat';
304 $event->instance = $chat->id;
305 $event->eventtype = $chat->schedule;
306 $event->timeduration = 0;
dcd338ff 307 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
b5de723d 308
8496c4af 309 add_event($event);
310 }
311 }
312 return true;
313}
314
516121bd 315
1515a89e 316//////////////////////////////////////////////////////////////////////
317/// Functions that require some SQL
318
a12e11c1 319function chat_get_users($chatid, $groupid=0, $groupingid=0) {
1515a89e 320
321 global $CFG;
84a2fdd7 322
323 if ($groupid) {
324 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
325 } else {
326 $groupselect = "";
327 }
a12e11c1 328
329 if (!empty($CFG->enablegroupings) && !(empty($groupingid))) {
330 $groupingjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
331 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
332
333 } else {
334 $groupingjoin = '';
335 }
b5de723d 336
426c0437 337 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
a12e11c1 338 FROM {$CFG->prefix}chat_users c
339 INNER JOIN {$CFG->prefix}user u ON u.id = c.userid
340 $groupingjoin
1515a89e 341 WHERE c.chatid = '$chatid'
a12e11c1 342 $groupselect
1515a89e 343 ORDER BY c.firstping ASC");
344}
345
84a2fdd7 346function chat_get_latest_message($chatid, $groupid=0) {
5a8625e4 347/// Efficient way to extract just the latest message
348/// Uses ADOdb directly instead of get_record_sql()
b5de723d 349/// because the LIMIT command causes problems with
5a8625e4 350/// the developer debugging in there.
1515a89e 351
5a8625e4 352 global $db, $CFG;
1515a89e 353
84a2fdd7 354 if ($groupid) {
355 $groupselect = " AND (groupid='$groupid' OR groupid='0')";
356 } else {
357 $groupselect = "";
358 }
359
0217757b 360 if (!$rs = $db->SelectLimit("SELECT *
361 FROM {$CFG->prefix}chat_messages
362 WHERE chatid = '$chatid' $groupselect
363 ORDER BY timestamp DESC", 1)) {
5a8625e4 364 return false;
365 }
03cedd62 366
367 $result = rs_fetch_record($rs);
82f66bed 368
369 rs_close($rs);
03cedd62 370
03cedd62 371 return $result;
1515a89e 372}
373
5a8625e4 374
1515a89e 375//////////////////////////////////////////////////////////////////////
516121bd 376// login if not already logged in
1515a89e 377
a32c7772 378function chat_login_user($chatid, $version, $groupid, $course) {
1515a89e 379 global $USER;
e7fbd0b3 380 if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) {
516121bd 381 $chatuser->version = $version;
d96466d2 382 $chatuser->ip = $USER->lastip;
516121bd 383 $chatuser->lastping = time();
384 $chatuser->lang = current_language();
1515a89e 385
d96466d2 386 // Sometimes $USER->lastip is not setup properly
d13ef2fb 387 // during login. Update with current value if possible
f83edcb1 388 // or provide a dummy value for the db
d13ef2fb 389 if (empty($chatuser->ip)) {
390 $chatuser->ip = getremoteaddr();
391 if (empty($chatuser->ip)) {
f83edcb1 392 $chatuser->ip = '';
d13ef2fb 393 }
394 }
395
516121bd 396 if (($chatuser->course != $course->id)
397 or ($chatuser->userid != $USER->id)) {
398 return false;
399 }
400 if (!update_record('chat_users', $chatuser)) {
401 return false;
402 }
516121bd 403 } else {
6ee78cee 404 $chatuser = new object();
516121bd 405 $chatuser->chatid = $chatid;
406 $chatuser->userid = $USER->id;
407 $chatuser->groupid = $groupid;
408 $chatuser->version = $version;
d96466d2 409 $chatuser->ip = $USER->lastip;
516121bd 410 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
411 $chatuser->sid = random_string(32);
3dfd307f 412 $chatuser->course = $course->id; //caching - needed for current_language too
413 $chatuser->lang = current_language(); //caching - to resource intensive to find out later
516121bd 414
d96466d2 415 // Sometimes $USER->lastip is not setup properly
274f0091 416 // during login. Update with current value if possible
417 // or provide a dummy value for the db
418 if (empty($chatuser->ip)) {
419 $chatuser->ip = getremoteaddr();
420 if (empty($chatuser->ip)) {
421 $chatuser->ip = '';
422 }
423 }
424
425
516121bd 426 if (!insert_record('chat_users', $chatuser)) {
427 return false;
428 }
429
a32c7772 430 if ($version == 'sockets') {
431 // do not send 'enter' message, chatd will do it
432 } else {
6ee78cee 433 $message = new object();
2ac0d13b 434 $message->chatid = $chatuser->chatid;
435 $message->userid = $chatuser->userid;
436 $message->groupid = $groupid;
437 $message->message = 'enter';
438 $message->system = 1;
439 $message->timestamp = time();
440
441 if (!insert_record('chat_messages', $message)) {
442 error('Could not insert a chat message!');
443 }
516121bd 444 }
1515a89e 445 }
446
447 return $chatuser->sid;
448}
449
7d792369 450function chat_delete_old_users() {
451// Delete the old and in the way
452
b5012f3e 453 global $CFG;
454
e7fbd0b3 455 $timeold = time() - $CFG->chat_old_ping;
953eb6f3 456 $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts
a32c7772 457
6ee78cee 458 $query = "(version<>'basic' AND lastping<'$timeold') OR (version='basic' AND lastping<'$timeoldext')";
7d792369 459
516121bd 460 if ($oldusers = get_records_select('chat_users', $query) ) {
461 delete_records_select('chat_users', $query);
7d792369 462 foreach ($oldusers as $olduser) {
6ee78cee 463 $message = new object();
516121bd 464 $message->chatid = $olduser->chatid;
465 $message->userid = $olduser->userid;
466 $message->groupid = $olduser->groupid;
467 $message->message = 'exit';
468 $message->system = 1;
7d792369 469 $message->timestamp = time();
b5de723d 470
516121bd 471 if (!insert_record('chat_messages', $message)) {
472 error('Could not insert a chat message!');
7d792369 473 }
474 }
475 }
476}
1515a89e 477
22a4491a 478
fcd3a1ee 479function chat_update_chat_times($chatid=0) {
480/// Updates chat records so that the next chat time is correct
481
482 $timenow = time();
483 if ($chatid) {
484 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
485 return;
486 }
487 } else {
488 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
489 return;
490 }
491 }
492
493 foreach ($chats as $chat) {
7626969c 494 unset($chat->name);
495 unset($chat->intro);
fcd3a1ee 496 switch ($chat->schedule) {
497 case 1: // Single event - turn off schedule and disable
498 $chat->chattime = 0;
499 $chat->schedule = 0;
500 break;
501 case 2: // Repeat daily
f0d3bb9e 502 while ($chat->chattime <= $timenow) {
503 $chat->chattime += 24 * 3600;
504 }
fcd3a1ee 505 break;
506 case 3: // Repeat weekly
f0d3bb9e 507 while ($chat->chattime <= $timenow) {
508 $chat->chattime += 7 * 24 * 3600;
509 }
fcd3a1ee 510 break;
511 }
512 update_record("chat", $chat);
8496c4af 513
514 $event = NULL; // Update calendar too
b78f4cbd 515 $cond = "modulename='chat' AND instance = {$chat->id}
516 AND timestart != {$chat->chattime}";
517 if ($event->id = get_field_select('event', 'id', $cond)) {
8496c4af 518 $event->timestart = $chat->chattime;
519 update_event($event);
520 }
fcd3a1ee 521 }
522}
523
524
aa5c32fd 525function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chat_lastrow=NULL) {
72989350 526 global $CFG, $USER;
1515a89e 527
6ee78cee 528 $output = new object();
516121bd 529 $output->beep = false; // by default
530 $output->refreshusers = false; // by default
7d792369 531
72989350 532 // Use get_user_timezone() to find the correct timezone for displaying this message:
533 // It's either the current user's timezone or else decided by some Moodle config setting
970f144e 534 // First, "reset" $USER->timezone (which could have been set by a previous call to here)
535 // because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone
536 $USER->timezone = 99;
72989350 537 $tz = get_user_timezone($currentuser->timezone);
b5de723d 538
72989350 539 // Before formatting the message time string, set $USER->timezone to the above.
540 // This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
541 // message times appear off because DST is not taken into account when it should be.
542 $USER->timezone = $tz;
b5de723d 543 $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
544
545 $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
582de679 546 if ($courseid) {
d3981e38 547 $message->picture = "<a onclick=\"window.open('$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid')\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid\">$message->picture</a>";
582de679 548 }
1515a89e 549
aa5c32fd 550 //Calculate the row class
551 if ($chat_lastrow !== NULL) {
552 $rowclass = ' class="r'.$chat_lastrow.'" ';
553 } else {
554 $rowclass = '';
555 }
556
b5de723d 557 // Start processing the message
1515a89e 558
b5de723d 559 if(!empty($message->system)) {
560 // System event
561 $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
aa5c32fd 562 $output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td><td class="text">';
563 $output->html .= '<span class="event">'.$output->text.'</span></td></tr></table>';
953eb6f3 564 $output->basic = '<dl><dt class="event">'.$message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender)).'</dt></dl>';
7d792369 565
516121bd 566 if($message->message == 'exit' or $message->message == 'enter') {
567 $output->refreshusers = true; //force user panel refresh ASAP
568 }
1515a89e 569 return $output;
570 }
571
82a524ef 572 // It's not a system event
b5de723d 573
574 $text = $message->message;
82a524ef 575
576 /// Parse the text to clean and filter it
577
6ee78cee 578 $options = new object();
82a524ef 579 $options->para = false;
580 $text = format_text($text, FORMAT_MOODLE, $options, $courseid);
927a7808 581
b5de723d 582 // And now check for special cases
927a7808 583 $special = false;
584
b5de723d 585 if (substr($text, 0, 5) == 'beep ') {
927a7808 586 /// It's a beep!
587 $special = true;
7d792369 588 $beepwho = trim(substr($text, 5));
9f85bed4 589
b5de723d 590 if ($beepwho == 'all') { // everyone
591 $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender));
592 $outmain = '';
593 $output->beep = true; // (eventually this should be set to
7d792369 594 // to a filename uploaded by the user)
595
82a524ef 596 } else if ($beepwho == $currentuser->id) { // current user
b5de723d 597 $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender));
598 $outmain = '';
7d792369 599 $output->beep = true;
264867fd 600
0eda0a46 601 } else { //something is not caught?
7d792369 602 return false;
603 }
b5de723d 604 } else if (substr($text, 0, 1) == '/') { /// It's a user command
927a7808 605 if (trim(substr($text, 0, 4)) == '/me') {
606 $special = true;
b5de723d 607 $outinfo = $message->strtime;
608 $outmain = $sender->firstname.' '.substr($text, 4);
1515a89e 609 }
927a7808 610 }
9f85bed4 611
927a7808 612 if(!$special) {
b5de723d 613 $outinfo = $message->strtime.' '.$sender->firstname;
7d792369 614 $outmain = $text;
1515a89e 615 }
264867fd 616
9f85bed4 617 /// Format the message as a small table
1515a89e 618
b5de723d 619 $output->text = strip_tags($outinfo.': '.$outmain);
7d792369 620
5379d249 621 $output->html = "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\" valign=\"top\">$message->picture</td><td class=\"text\">";
aa5c32fd 622 $output->html .= "<span class=\"title\">$outinfo</span>";
7d792369 623 if ($outmain) {
624 $output->html .= ": $outmain";
953eb6f3 625 $output->basic = '<dl><dt class="title">'.$outinfo.':</dt><dd class="text">'.$outmain.'</dd></dl>';
6ee78cee 626 } else {
953eb6f3 627 $output->basic = '<dl><dt class="title">'.$outinfo.'</dt></dl>';
7d792369 628 }
aa5c32fd 629 $output->html .= "</td></tr></table>";
7d792369 630 return $output;
b5de723d 631}
632
aa5c32fd 633function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NULL) {
b5de723d 634/// Given a message object full of information, this function
635/// formats it appropriately into text and html, then
636/// returns the formatted data.
637
78c98892 638 static $users; // Cache user lookups
639
640 if (isset($users[$message->userid])) {
641 $user = $users[$message->userid];
642 } else if ($user = get_record('user', 'id', $message->userid, '','','','','id,picture,firstname,lastname')) {
643 $users[$message->userid] = $user;
644 } else {
645 return NULL;
b5de723d 646 }
aa5c32fd 647 return chat_format_message_manually($message, $courseid, $user, $currentuser, $chat_lastrow);
1515a89e 648}
649
f3221af9 650function chat_get_view_actions() {
651 return array('view','view all','report');
652}
653
654function chat_get_post_actions() {
655 return array('talk');
656}
657
9ca0187e 658function chat_print_overview($courses, &$htmlarray) {
659 global $USER, $CFG;
660
661 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
662 return array();
663 }
664
665 if (!$chats = get_all_instances_in_courses('chat',$courses)) {
666 return;
667 }
668
669 $strchat = get_string('modulename', 'chat');
670 $strnextsession = get_string('nextsession', 'chat');
9ca0187e 671
672 foreach ($chats as $chat) {
9ca0187e 673 if ($chat->chattime and $chat->schedule) { // A chat is scheduled
a2a37336 674 $str = '<div class="chat overview"><div class="name">'.
675 $strchat.': <a '.($chat->visible?'':' class="dimmed"').
676 ' href="'.$CFG->wwwroot.'/mod/chat/view.php?id='.$chat->coursemodule.'">'.
677 $chat->name.'</a></div>';
678 $str .= '<div class="info">'.$strnextsession.': '.userdate($chat->chattime).'</div></div>';
679
680 if (empty($htmlarray[$chat->course]['chat'])) {
681 $htmlarray[$chat->course]['chat'] = $str;
682 } else {
683 $htmlarray[$chat->course]['chat'] .= $str;
684 }
9ca0187e 685 }
9ca0187e 686 }
687}
688
1515a89e 689?>