Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / chat / lib.php
blob99b3e10d4affea7b6bb0ce77e7125cf56eae6721
1 <?php // $Id$
3 /// Library of functions and constants for module chat
4 require_once($CFG->libdir.'/pagelib.php');
7 // The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
8 $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";
10 // The HTML head for the message window to start with (with js scrolling)
11 $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";
13 // The HTML code for standard empty pages (e.g. if a user was kicked out)
14 $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>";
16 // The HTML head for the message input page
17 $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\">";
19 // The HTML code for the message input page, with JavaScript
20 $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();\">";
22 // Dummy data that gets output to the browser as needed, in order to make it show output
23 $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";
25 function chat_add_instance($chat) {
26 /// Given an object containing all the necessary data,
27 /// (defined by the form in mod.html) this function
28 /// will create a new instance and return the id number
29 /// of the new instance.
31 $chat->timemodified = time();
33 if ($returnid = insert_record("chat", $chat)) {
35 $event = NULL;
36 $event->name = $chat->name;
37 $event->description = $chat->intro;
38 $event->courseid = $chat->course;
39 $event->groupid = 0;
40 $event->userid = 0;
41 $event->modulename = 'chat';
42 $event->instance = $returnid;
43 $event->eventtype = $chat->schedule;
44 $event->timestart = $chat->chattime;
45 $event->timeduration = 0;
47 add_event($event);
50 return $returnid;
54 function chat_update_instance($chat) {
55 /// Given an object containing all the necessary data,
56 /// (defined by the form in mod.html) this function
57 /// will update an existing instance with new data.
59 $chat->timemodified = time();
60 $chat->id = $chat->instance;
63 if ($returnid = update_record("chat", $chat)) {
65 $event = new object();
67 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
69 $event->name = $chat->name;
70 $event->description = $chat->intro;
71 $event->timestart = $chat->chattime;
73 update_event($event);
77 return $returnid;
81 function chat_delete_instance($id) {
82 /// Given an ID of an instance of this module,
83 /// this function will permanently delete the instance
84 /// and any data that depends on it.
86 if (! $chat = get_record('chat', 'id', $id)) {
87 return false;
90 $result = true;
92 # Delete any dependent records here #
94 if (! delete_records('chat', 'id', $chat->id)) {
95 $result = false;
97 if (! delete_records('chat_messages', 'chatid', $chat->id)) {
98 $result = false;
100 if (! delete_records('chat_users', 'chatid', $chat->id)) {
101 $result = false;
104 $pagetypes = page_import_types('mod/chat/');
105 foreach($pagetypes as $pagetype) {
106 if(!delete_records('block_instance', 'pageid', $chat->id, 'pagetype', $pagetype)) {
107 $result = false;
111 if (! delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) {
112 $result = false;
115 return $result;
118 function chat_user_outline($course, $user, $mod, $chat) {
119 /// Return a small object with summary information about what a
120 /// user has done with a given particular instance of this module
121 /// Used for user activity reports.
122 /// $return->time = the time they did it
123 /// $return->info = a short text description
125 $return = NULL;
126 return $return;
129 function chat_user_complete($course, $user, $mod, $chat) {
130 /// Print a detailed representation of what a user has done with
131 /// a given particular instance of this module, for user activity reports.
133 return true;
136 function chat_print_recent_activity($course, $viewfullnames, $timestart) {
137 /// Given a course and a date, prints a summary of all chat rooms past and present
138 /// This function is called from course/lib.php: print_recent_activity()
140 global $CFG, $USER;
142 // this is approximate only, but it is really fast ;-)
143 $timeout = $CFG->chat_old_ping * 10;
145 if (!$mcms = get_records_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
146 FROM {$CFG->prefix}course_modules cm
147 JOIN {$CFG->prefix}modules md ON md.id = cm.module
148 JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance
149 JOIN {$CFG->prefix}chat_messages chm ON chm.chatid = ch.id
150 WHERE chm.timestamp > $timestart AND ch.course = {$course->id} AND md.name = 'chat'
151 GROUP BY cm.id
152 ORDER BY lasttime ASC")) {
153 return false;
156 $past = array();
157 $current = array();
158 $modinfo =& get_fast_modinfo($course); // reference needed because we might load the groups
160 foreach ($mcms as $cmid=>$mcm) {
161 if (!array_key_exists($cmid, $modinfo->cms)) {
162 continue;
164 $cm = $modinfo->cms[$cmid];
165 $cm->lasttime = $mcm->lasttime;
166 if (!$modinfo->cms[$cm->id]->uservisible) {
167 continue;
170 if (groups_get_activity_groupmode($cm) != SEPARATEGROUPS
171 or has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id))) {
172 if ($timeout > time() - $cm->lasttime) {
173 $current[] = $cm;
174 } else {
175 $past[] = $cm;
178 continue;
181 if (is_null($modinfo->groups)) {
182 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
185 // verify groups in separate mode
186 if (!$mygroupids = $modinfo->groups[$cm->groupingid]) {
187 continue;
190 // ok, last post was not for my group - we have to query db to get last message from one of my groups
191 // only minor problem is that the order will not be correct
192 $mygroupids = implode(',', $mygroupids);
193 $cm->mygroupids = $mygroupids;
195 if (!$mcm = get_record_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
196 FROM {$CFG->prefix}course_modules cm
197 JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance
198 JOIN {$CFG->prefix}chat_messages chm ON chm.chatid = ch.id
199 WHERE chm.timestamp > $timestart AND cm.id = {$cm->id} AND
200 (chm.groupid IN ($mygroupids) OR chm.groupid = 0)
201 GROUP BY cm.id")) {
202 continue;
205 $cm->lasttime = $mcm->lasttime;
206 if ($timeout > time() - $cm->lasttime) {
207 $current[] = $cm;
208 } else {
209 $past[] = $cm;
213 if (!$past and !$current) {
214 return false;
217 $strftimerecent = get_string('strftimerecent');
219 if ($past) {
220 print_headline(get_string('pastchats', 'chat').':');
222 foreach ($past as $cm) {
223 $link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id;
224 $date = userdate($cm->lasttime, $strftimerecent);
225 echo '<div class="head"><div class="date">'.$date.'</div></div>';
226 echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name,true).'</a></div>';
230 if ($current) {
231 print_headline(get_string('currentchats', 'chat').':');
233 $oldest = floor((time()-$CFG->chat_old_ping)/10)*10; // better db caching
235 $timeold = time() - $CFG->chat_old_ping;
236 $timeold = floor($timeold/10)*10; // better db caching
237 $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts
238 $timeoldext = floor($timeoldext/10)*10; // better db caching
240 $timeout = "AND (chu.version<>'basic' AND chu.lastping>$timeold) OR (chu.version='basic' AND chu.lastping>$timeoldext)";
242 foreach ($current as $cm) {
243 //count users first
244 if (isset($cm->mygroupids)) {
245 $groupselect = "AND (chu.groupid IN ({$cm->mygroupids}) OR chu.groupid = 0)";
246 } else {
247 $groupselect = "";
250 if (!$users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email, u.picture
251 FROM {$CFG->prefix}course_modules cm
252 JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance
253 JOIN {$CFG->prefix}chat_users chu ON chu.chatid = ch.id
254 JOIN {$CFG->prefix}user u ON u.id = chu.userid
255 WHERE cm.id = {$cm->id} $timeout $groupselect
256 GROUP BY u.id, u.firstname, u.lastname, u.email, u.picture")) {
259 $link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id;
260 $date = userdate($cm->lasttime, $strftimerecent);
262 echo '<div class="head"><div class="date">'.$date.'</div></div>';
263 echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name,true).'</a></div>';
264 echo '<div class="userlist">';
265 if ($users) {
266 echo '<ul>';
267 foreach ($users as $user) {
268 echo '<li>'.fullname($user, $viewfullnames).'</li>';
270 echo '</ul>';
272 echo '</div>';
276 return true;
280 function chat_cron () {
281 /// Function to be run periodically according to the moodle cron
282 /// This function searches for things that need to be done, such
283 /// as sending out mail, toggling flags etc ...
285 global $CFG;
287 chat_update_chat_times();
289 chat_delete_old_users();
291 /// Delete old messages with a
292 /// single SQL query.
293 $subselect = "SELECT c.keepdays
294 FROM {$CFG->prefix}chat c
295 WHERE c.id = {$CFG->prefix}chat_messages.chatid";
297 $sql = "DELETE
298 FROM {$CFG->prefix}chat_messages
299 WHERE ($subselect) > 0 AND timestamp < ( ".time()." -($subselect) * 24 * 3600)";
301 execute_sql($sql, false);
303 return true;
306 function chat_get_participants($chatid, $groupid=0) {
307 //Returns the users with data in one chat
308 //(users with records in chat_messages, students)
310 global $CFG;
312 if ($groupid) {
313 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
314 } else {
315 $groupselect = "";
318 //Get students
319 $students = get_records_sql("SELECT DISTINCT u.id, u.id
320 FROM {$CFG->prefix}user u,
321 {$CFG->prefix}chat_messages c
322 WHERE c.chatid = '$chatid' $groupselect
323 AND u.id = c.userid");
325 //Return students array (it contains an array of unique users)
326 return ($students);
329 function chat_refresh_events($courseid = 0) {
330 // This standard function will check all instances of this module
331 // and make sure there are up-to-date events created for each of them.
332 // If courseid = 0, then every chat event in the site is checked, else
333 // only chat events belonging to the course specified are checked.
334 // This function is used, in its new format, by restore_refresh_events()
336 if ($courseid) {
337 if (! $chats = get_records("chat", "course", $courseid)) {
338 return true;
340 } else {
341 if (! $chats = get_records("chat")) {
342 return true;
345 $moduleid = get_field('modules', 'id', 'name', 'chat');
347 foreach ($chats as $chat) {
348 $event = NULL;
349 $event->name = addslashes($chat->name);
350 $event->description = addslashes($chat->intro);
351 $event->timestart = $chat->chattime;
353 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
354 update_event($event);
356 } else {
357 $event->courseid = $chat->course;
358 $event->groupid = 0;
359 $event->userid = 0;
360 $event->modulename = 'chat';
361 $event->instance = $chat->id;
362 $event->eventtype = $chat->schedule;
363 $event->timeduration = 0;
364 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
366 add_event($event);
369 return true;
373 //////////////////////////////////////////////////////////////////////
374 /// Functions that require some SQL
376 function chat_get_users($chatid, $groupid=0, $groupingid=0) {
378 global $CFG;
380 if ($groupid) {
381 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
382 } else {
383 $groupselect = "";
386 if (!empty($CFG->enablegroupings) && !(empty($groupingid))) {
387 $groupingjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
388 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
390 } else {
391 $groupingjoin = '';
394 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt
395 FROM {$CFG->prefix}chat_users c
396 INNER JOIN {$CFG->prefix}user u ON u.id = c.userid
397 $groupingjoin
398 WHERE c.chatid = '$chatid'
399 $groupselect
400 ORDER BY c.firstping ASC");
403 function chat_get_latest_message($chatid, $groupid=0) {
404 /// Efficient way to extract just the latest message
405 /// Uses ADOdb directly instead of get_record_sql()
406 /// because the LIMIT command causes problems with
407 /// the developer debugging in there.
409 global $db, $CFG;
411 if ($groupid) {
412 $groupselect = " AND (groupid='$groupid' OR groupid='0')";
413 } else {
414 $groupselect = "";
417 if (!$rs = $db->SelectLimit("SELECT *
418 FROM {$CFG->prefix}chat_messages
419 WHERE chatid = '$chatid' $groupselect
420 ORDER BY timestamp DESC", 1)) {
421 return false;
424 $result = rs_fetch_record($rs);
426 rs_close($rs);
428 return $result;
432 //////////////////////////////////////////////////////////////////////
433 // login if not already logged in
435 function chat_login_user($chatid, $version, $groupid, $course) {
436 global $USER;
437 if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) {
438 $chatuser->version = $version;
439 $chatuser->ip = $USER->lastip;
440 $chatuser->lastping = time();
441 $chatuser->lang = current_language();
443 // Sometimes $USER->lastip is not setup properly
444 // during login. Update with current value if possible
445 // or provide a dummy value for the db
446 if (empty($chatuser->ip)) {
447 $chatuser->ip = getremoteaddr();
448 if (empty($chatuser->ip)) {
449 $chatuser->ip = '';
453 if (($chatuser->course != $course->id)
454 or ($chatuser->userid != $USER->id)) {
455 return false;
457 if (!update_record('chat_users', $chatuser)) {
458 return false;
460 } else {
461 $chatuser = new object();
462 $chatuser->chatid = $chatid;
463 $chatuser->userid = $USER->id;
464 $chatuser->groupid = $groupid;
465 $chatuser->version = $version;
466 $chatuser->ip = $USER->lastip;
467 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
468 $chatuser->sid = random_string(32);
469 $chatuser->course = $course->id; //caching - needed for current_language too
470 $chatuser->lang = current_language(); //caching - to resource intensive to find out later
472 // Sometimes $USER->lastip is not setup properly
473 // during login. Update with current value if possible
474 // or provide a dummy value for the db
475 if (empty($chatuser->ip)) {
476 $chatuser->ip = getremoteaddr();
477 if (empty($chatuser->ip)) {
478 $chatuser->ip = '';
483 if (!insert_record('chat_users', $chatuser)) {
484 return false;
487 if ($version == 'sockets') {
488 // do not send 'enter' message, chatd will do it
489 } else {
490 $message = new object();
491 $message->chatid = $chatuser->chatid;
492 $message->userid = $chatuser->userid;
493 $message->groupid = $groupid;
494 $message->message = 'enter';
495 $message->system = 1;
496 $message->timestamp = time();
498 if (!insert_record('chat_messages', $message)) {
499 error('Could not insert a chat message!');
504 return $chatuser->sid;
507 function chat_delete_old_users() {
508 // Delete the old and in the way
510 global $CFG;
512 $timeold = time() - $CFG->chat_old_ping;
513 $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts
515 $query = "(version<>'basic' AND lastping<'$timeold') OR (version='basic' AND lastping<'$timeoldext')";
517 if ($oldusers = get_records_select('chat_users', $query) ) {
518 delete_records_select('chat_users', $query);
519 foreach ($oldusers as $olduser) {
520 $message = new object();
521 $message->chatid = $olduser->chatid;
522 $message->userid = $olduser->userid;
523 $message->groupid = $olduser->groupid;
524 $message->message = 'exit';
525 $message->system = 1;
526 $message->timestamp = time();
528 if (!insert_record('chat_messages', $message)) {
529 error('Could not insert a chat message!');
536 function chat_update_chat_times($chatid=0) {
537 /// Updates chat records so that the next chat time is correct
539 $timenow = time();
540 if ($chatid) {
541 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
542 return;
544 } else {
545 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
546 return;
550 foreach ($chats as $chat) {
551 unset($chat->name);
552 unset($chat->intro);
553 switch ($chat->schedule) {
554 case 1: // Single event - turn off schedule and disable
555 $chat->chattime = 0;
556 $chat->schedule = 0;
557 break;
558 case 2: // Repeat daily
559 while ($chat->chattime <= $timenow) {
560 $chat->chattime += 24 * 3600;
562 break;
563 case 3: // Repeat weekly
564 while ($chat->chattime <= $timenow) {
565 $chat->chattime += 7 * 24 * 3600;
567 break;
569 update_record("chat", $chat);
571 $event = NULL; // Update calendar too
572 $cond = "modulename='chat' AND instance = {$chat->id}
573 AND timestart != {$chat->chattime}";
574 if ($event->id = get_field_select('event', 'id', $cond)) {
575 $event->timestart = $chat->chattime;
576 update_event($event);
582 function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chat_lastrow=NULL) {
583 global $CFG, $USER;
585 $output = new object();
586 $output->beep = false; // by default
587 $output->refreshusers = false; // by default
589 // Use get_user_timezone() to find the correct timezone for displaying this message:
590 // It's either the current user's timezone or else decided by some Moodle config setting
591 // First, "reset" $USER->timezone (which could have been set by a previous call to here)
592 // because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone
593 $USER->timezone = 99;
594 $tz = get_user_timezone($currentuser->timezone);
596 // Before formatting the message time string, set $USER->timezone to the above.
597 // This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
598 // message times appear off because DST is not taken into account when it should be.
599 $USER->timezone = $tz;
600 $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
602 $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
603 if ($courseid) {
604 $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>";
607 //Calculate the row class
608 if ($chat_lastrow !== NULL) {
609 $rowclass = ' class="r'.$chat_lastrow.'" ';
610 } else {
611 $rowclass = '';
614 // Start processing the message
616 if(!empty($message->system)) {
617 // System event
618 $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
619 $output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td><td class="text">';
620 $output->html .= '<span class="event">'.$output->text.'</span></td></tr></table>';
621 $output->basic = '<dl><dt class="event">'.$message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender)).'</dt></dl>';
623 if($message->message == 'exit' or $message->message == 'enter') {
624 $output->refreshusers = true; //force user panel refresh ASAP
626 return $output;
629 // It's not a system event
631 $text = $message->message;
633 /// Parse the text to clean and filter it
635 $options = new object();
636 $options->para = false;
637 $text = format_text($text, FORMAT_MOODLE, $options, $courseid);
639 // And now check for special cases
640 $special = false;
642 if (substr($text, 0, 5) == 'beep ') {
643 /// It's a beep!
644 $special = true;
645 $beepwho = trim(substr($text, 5));
647 if ($beepwho == 'all') { // everyone
648 $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender));
649 $outmain = '';
650 $output->beep = true; // (eventually this should be set to
651 // to a filename uploaded by the user)
653 } else if ($beepwho == $currentuser->id) { // current user
654 $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender));
655 $outmain = '';
656 $output->beep = true;
658 } else { //something is not caught?
659 return false;
661 } else if (substr($text, 0, 1) == '/') { /// It's a user command
662 if (trim(substr($text, 0, 4)) == '/me') {
663 $special = true;
664 $outinfo = $message->strtime;
665 $outmain = $sender->firstname.' '.substr($text, 4);
669 if(!$special) {
670 $outinfo = $message->strtime.' '.$sender->firstname;
671 $outmain = $text;
674 /// Format the message as a small table
676 $output->text = strip_tags($outinfo.': '.$outmain);
678 $output->html = "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\" valign=\"top\">$message->picture</td><td class=\"text\">";
679 $output->html .= "<span class=\"title\">$outinfo</span>";
680 if ($outmain) {
681 $output->html .= ": $outmain";
682 $output->basic = '<dl><dt class="title">'.$outinfo.':</dt><dd class="text">'.$outmain.'</dd></dl>';
683 } else {
684 $output->basic = '<dl><dt class="title">'.$outinfo.'</dt></dl>';
686 $output->html .= "</td></tr></table>";
687 return $output;
690 function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NULL) {
691 /// Given a message object full of information, this function
692 /// formats it appropriately into text and html, then
693 /// returns the formatted data.
695 static $users; // Cache user lookups
697 if (isset($users[$message->userid])) {
698 $user = $users[$message->userid];
699 } else if ($user = get_record('user', 'id', $message->userid, '','','','','id,picture,firstname,lastname')) {
700 $users[$message->userid] = $user;
701 } else {
702 return NULL;
704 return chat_format_message_manually($message, $courseid, $user, $currentuser, $chat_lastrow);
707 function chat_get_view_actions() {
708 return array('view','view all','report');
711 function chat_get_post_actions() {
712 return array('talk');
715 function chat_print_overview($courses, &$htmlarray) {
716 global $USER, $CFG;
718 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
719 return array();
722 if (!$chats = get_all_instances_in_courses('chat',$courses)) {
723 return;
726 $strchat = get_string('modulename', 'chat');
727 $strnextsession = get_string('nextsession', 'chat');
729 foreach ($chats as $chat) {
730 if ($chat->chattime and $chat->schedule) { // A chat is scheduled
731 $str = '<div class="chat overview"><div class="name">'.
732 $strchat.': <a '.($chat->visible?'':' class="dimmed"').
733 ' href="'.$CFG->wwwroot.'/mod/chat/view.php?id='.$chat->coursemodule.'">'.
734 $chat->name.'</a></div>';
735 $str .= '<div class="info">'.$strnextsession.': '.userdate($chat->chattime).'</div></div>';
737 if (empty($htmlarray[$chat->course]['chat'])) {
738 $htmlarray[$chat->course]['chat'] = $str;
739 } else {
740 $htmlarray[$chat->course]['chat'] .= $str;
748 * Implementation of the function for printing the form elements that control
749 * whether the course reset functionality affects the chat.
750 * @param $mform form passed by reference
752 function chat_reset_course_form_definition(&$mform) {
753 $mform->addElement('header', 'chatheader', get_string('modulenameplural', 'chat'));
754 $mform->addElement('advcheckbox', 'reset_chat', get_string('removemessages','chat'));
758 * Course reset form defaults.
760 function chat_reset_course_form_defaults($course) {
761 return array('reset_chat'=>1);
765 * Actual implementation of the rest coures functionality, delete all the
766 * chat messages for course $data->courseid.
767 * @param $data the data submitted from the reset course.
768 * @return array status array
770 function chat_reset_userdata($data) {
771 global $CFG;
773 $componentstr = get_string('modulenameplural', 'chat');
774 $status = array();
776 if (!empty($data->reset_chat)) {
777 $chatessql = "SELECT ch.id
778 FROM {$CFG->prefix}chat ch
779 WHERE ch.course={$data->courseid}";
781 delete_records_select('chat_messages', "chatid IN ($chatessql)");
782 delete_records_select('chat_users', "chatid IN ($chatessql)");
783 $status[] = array('component'=>$componentstr, 'item'=>get_string('removemessages', 'chat'), 'error'=>false);
786 /// updating dates - shift may be negative too
787 if ($data->timeshift) {
788 shift_course_mod_dates('chat', array('chattime'), $data->timeshift, $data->courseid);
789 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
792 return $status;
796 * Returns all other caps used in module
798 function chat_get_extra_capabilities() {
799 return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames');