MDL-12783 IMS idon't unenroll students who were enrolled by other plug-ins
[moodle.git] / mod / chat / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
1515a89e 2
3/// Library of functions and constants for module chat
c4d588cc 4require_once($CFG->libdir.'/pagelib.php');
5
1515a89e 6
1515a89e 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";
9
10// The HTML head for the message window to start with (with js scrolling)
32f0b38a 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";
1515a89e 12
13// The HTML code for standard empty pages (e.g. if a user was kicked out)
89f688b6 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>";
1515a89e 15
16// The HTML head for the message input page
89f688b6 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\">";
1515a89e 18
19// The HTML code for the message input page, with JavaScript
32f0b38a 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();\">";
1515a89e 21
fbabbd23 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";
1515a89e 24
25function chat_add_instance($chat) {
c18269c7 26 global $DB;
b5de723d 27/// Given an object containing all the necessary data,
7cac0c4b 28/// (defined by the form in mod_form.php) this function
b5de723d 29/// will create a new instance and return the id number
1515a89e 30/// of the new instance.
31
32 $chat->timemodified = time();
33
c18269c7 34 if ($returnid = $DB->insert_record("chat", $chat)) {
8496c4af 35
36 $event = NULL;
37 $event->name = $chat->name;
38 $event->description = $chat->intro;
39 $event->courseid = $chat->course;
40 $event->groupid = 0;
41 $event->userid = 0;
42 $event->modulename = 'chat';
43 $event->instance = $returnid;
44 $event->eventtype = $chat->schedule;
45 $event->timestart = $chat->chattime;
46 $event->timeduration = 0;
47
48 add_event($event);
49 }
50
51 return $returnid;
1515a89e 52}
53
54
55function chat_update_instance($chat) {
c18269c7 56 global $DB;
b5de723d 57/// Given an object containing all the necessary data,
7cac0c4b 58/// (defined by the form in mod_form.php) this function
1515a89e 59/// will update an existing instance with new data.
60
61 $chat->timemodified = time();
62 $chat->id = $chat->instance;
63
1515a89e 64
c18269c7 65 if ($returnid = $DB->update_record("chat", $chat)) {
8496c4af 66
264867fd 67 $event = new object();
8496c4af 68
c18269c7 69 if ($event->id = $DB->get_field('event', 'id', array('modulename'=>'chat', 'instance'=>$chat->id))) {
8496c4af 70
71 $event->name = $chat->name;
72 $event->description = $chat->intro;
73 $event->timestart = $chat->chattime;
74
75 update_event($event);
76 }
77 }
78
79 return $returnid;
1515a89e 80}
81
82
83function chat_delete_instance($id) {
c18269c7 84 global $DB;
b5de723d 85/// Given an ID of an instance of this module,
86/// this function will permanently delete the instance
87/// and any data that depends on it.
1515a89e 88
c18269c7 89 if (! $chat = $DB->get_record('chat', array('id'=>$id))) {
1515a89e 90 return false;
91 }
92
93 $result = true;
94
95 # Delete any dependent records here #
96
c18269c7 97 if (! $DB->delete_records('chat', array('id'=>$chat->id))) {
a71efae3 98 $result = false;
99 }
c18269c7 100 if (! $DB->delete_records('chat_messages', array('chatid'=>$chat->id))) {
a71efae3 101 $result = false;
102 }
c18269c7 103 if (! $DB->delete_records('chat_users', array('chatid'=>$chat->id))) {
1515a89e 104 $result = false;
105 }
106
880d8675 107 $pagetypes = page_import_types('mod/chat/');
108 foreach($pagetypes as $pagetype) {
c18269c7 109 if (!$DB->delete_records('block_instance', array('pageid'=>$chat->id, 'pagetype'=>$pagetype))) {
880d8675 110 $result = false;
111 }
112 }
113
c18269c7 114 if (! $DB->delete_records('event', array('modulename'=>'chat', 'instance'=>$chat->id))) {
36eb856f 115 $result = false;
116 }
117
1515a89e 118 return $result;
119}
120
121function chat_user_outline($course, $user, $mod, $chat) {
b5de723d 122/// Return a small object with summary information about what a
1515a89e 123/// user has done with a given particular instance of this module
124/// Used for user activity reports.
125/// $return->time = the time they did it
126/// $return->info = a short text description
127
5c05d23b 128 $return = NULL;
1515a89e 129 return $return;
130}
131
132function chat_user_complete($course, $user, $mod, $chat) {
b5de723d 133/// Print a detailed representation of what a user has done with
1515a89e 134/// a given particular instance of this module, for user activity reports.
135
136 return true;
137}
138
dd97c328 139function chat_print_recent_activity($course, $viewfullnames, $timestart) {
140/// Given a course and a date, prints a summary of all chat rooms past and present
1515a89e 141/// This function is called from course/lib.php: print_recent_activity()
142
dd97c328 143 global $CFG, $USER;
144
145 // this is approximate only, but it is really fast ;-)
146 $timeout = $CFG->chat_old_ping * 10;
147
fd4d41c3 148 if (!$mcms = get_records_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
149 FROM {$CFG->prefix}course_modules cm
150 JOIN {$CFG->prefix}modules md ON md.id = cm.module
151 JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance
152 JOIN {$CFG->prefix}chat_messages chm ON chm.chatid = ch.id
153 WHERE chm.timestamp > $timestart AND ch.course = {$course->id} AND md.name = 'chat'
154 GROUP BY cm.id
155 ORDER BY lasttime ASC")) {
dd97c328 156 return false;
157 }
158
159 $past = array();
160 $current = array();
161 $modinfo =& get_fast_modinfo($course); // reference needed because we might load the groups
162
fd4d41c3 163 foreach ($mcms as $cmid=>$mcm) {
164 if (!array_key_exists($cmid, $modinfo->cms)) {
dd97c328 165 continue;
166 }
fd4d41c3 167 $cm = $modinfo->cms[$cmid];
168 $cm->lasttime = $mcm->lasttime;
dd97c328 169 if (!$modinfo->cms[$cm->id]->uservisible) {
170 continue;
171 }
b7602a11 172
dd97c328 173 if (groups_get_activity_groupmode($cm) != SEPARATEGROUPS
174 or has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id))) {
175 if ($timeout > time() - $cm->lasttime) {
176 $current[] = $cm;
177 } else {
178 $past[] = $cm;
179 }
180
181 continue;
182 }
183
184 if (is_null($modinfo->groups)) {
185 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
186 }
c5a05b95 187
dd97c328 188 // verify groups in separate mode
189 if (!$mygroupids = $modinfo->groups[$cm->groupingid]) {
190 continue;
191 }
0469cccf 192
dd97c328 193 // ok, last post was not for my group - we have to query db to get last message from one of my groups
194 // only minor problem is that the order will not be correct
195 $mygroupids = implode(',', $mygroupids);
196 $cm->mygroupids = $mygroupids;
197
fd4d41c3 198 if (!$mcm = get_record_sql("SELECT cm.id, MAX(chm.timestamp) AS lasttime
199 FROM {$CFG->prefix}course_modules cm
200 JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance
201 JOIN {$CFG->prefix}chat_messages chm ON chm.chatid = ch.id
202 WHERE chm.timestamp > $timestart AND cm.id = {$cm->id} AND
203 (chm.groupid IN ($mygroupids) OR chm.groupid = 0)
204 GROUP BY cm.id")) {
dd97c328 205 continue;
206 }
fd4d41c3 207
208 $cm->lasttime = $mcm->lasttime;
dd97c328 209 if ($timeout > time() - $cm->lasttime) {
210 $current[] = $cm;
211 } else {
212 $past[] = $cm;
213 }
214 }
215
216 if (!$past and !$current) {
b7602a11 217 return false;
218 }
1515a89e 219
dd97c328 220 $strftimerecent = get_string('strftimerecent');
221
222 if ($past) {
223 print_headline(get_string('pastchats', 'chat').':');
224
225 foreach ($past as $cm) {
226 $link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id;
227 $date = userdate($cm->lasttime, $strftimerecent);
228 echo '<div class="head"><div class="date">'.$date.'</div></div>';
229 echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name,true).'</a></div>';
b7602a11 230 }
8f7dc7f1 231 }
232
233 if ($current) {
dd97c328 234 print_headline(get_string('currentchats', 'chat').':');
235
236 $oldest = floor((time()-$CFG->chat_old_ping)/10)*10; // better db caching
237
238 $timeold = time() - $CFG->chat_old_ping;
239 $timeold = floor($timeold/10)*10; // better db caching
240 $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts
241 $timeoldext = floor($timeoldext/10)*10; // better db caching
242
fd4d41c3 243 $timeout = "AND (chu.version<>'basic' AND chu.lastping>$timeold) OR (chu.version='basic' AND chu.lastping>$timeoldext)";
dd97c328 244
245 foreach ($current as $cm) {
246 //count users first
247 if (isset($cm->mygroupids)) {
248 $groupselect = "AND (chu.groupid IN ({$cm->mygroupids}) OR chu.groupid = 0)";
249 } else {
250 $groupselect = "";
251 }
fd4d41c3 252
dd97c328 253 if (!$users = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.email, u.picture
254 FROM {$CFG->prefix}course_modules cm
255 JOIN {$CFG->prefix}chat ch ON ch.id = cm.instance
256 JOIN {$CFG->prefix}chat_users chu ON chu.chatid = ch.id
257 JOIN {$CFG->prefix}user u ON u.id = chu.userid
258 WHERE cm.id = {$cm->id} $timeout $groupselect
fd4d41c3 259 GROUP BY u.id, u.firstname, u.lastname, u.email, u.picture")) {
dd97c328 260 }
261
262 $link = $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id;
263 $date = userdate($cm->lasttime, $strftimerecent);
264
265 echo '<div class="head"><div class="date">'.$date.'</div></div>';
266 echo '<div class="info"><a href="'.$link.'">'.format_string($cm->name,true).'</a></div>';
267 echo '<div class="userlist">';
268 if ($users) {
269 echo '<ul>';
270 foreach ($users as $user) {
271 echo '<li>'.fullname($user, $viewfullnames).'</li>';
272 }
273 echo '</ul>';
274 }
275 echo '</div>';
276 }
b7602a11 277 }
278
279 return true;
1515a89e 280}
281
8f7dc7f1 282
1515a89e 283function chat_cron () {
284/// Function to be run periodically according to the moodle cron
b5de723d 285/// This function searches for things that need to be done, such
286/// as sending out mail, toggling flags etc ...
1515a89e 287
288 global $CFG;
289
fcd3a1ee 290 chat_update_chat_times();
291
7d792369 292 chat_delete_old_users();
293
319038c3 294 /// Delete old messages with a
295 /// single SQL query.
296 $subselect = "SELECT c.keepdays
297 FROM {$CFG->prefix}chat c
298 WHERE c.id = {$CFG->prefix}chat_messages.chatid";
4388027c 299
319038c3 300 $sql = "DELETE
4388027c 301 FROM {$CFG->prefix}chat_messages
817fb4cb 302 WHERE ($subselect) > 0 AND timestamp < ( ".time()." -($subselect) * 24 * 3600)";
4388027c 303
304 execute_sql($sql, false);
22a4491a 305
1515a89e 306 return true;
307}
308
84a2fdd7 309function chat_get_participants($chatid, $groupid=0) {
05855091 310//Returns the users with data in one chat
311//(users with records in chat_messages, students)
312
313 global $CFG;
314
84a2fdd7 315 if ($groupid) {
316 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
317 } else {
318 $groupselect = "";
319 }
320
05855091 321 //Get students
95e72c12 322 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 323 FROM {$CFG->prefix}user u,
324 {$CFG->prefix}chat_messages c
84a2fdd7 325 WHERE c.chatid = '$chatid' $groupselect
326 AND u.id = c.userid");
1515a89e 327
05855091 328 //Return students array (it contains an array of unique users)
329 return ($students);
330}
1515a89e 331
8496c4af 332function chat_refresh_events($courseid = 0) {
333// This standard function will check all instances of this module
334// and make sure there are up-to-date events created for each of them.
335// If courseid = 0, then every chat event in the site is checked, else
336// only chat events belonging to the course specified are checked.
337// This function is used, in its new format, by restore_refresh_events()
338
339 if ($courseid) {
340 if (! $chats = get_records("chat", "course", $courseid)) {
341 return true;
342 }
343 } else {
344 if (! $chats = get_records("chat")) {
345 return true;
346 }
347 }
dcd338ff 348 $moduleid = get_field('modules', 'id', 'name', 'chat');
8496c4af 349
350 foreach ($chats as $chat) {
351 $event = NULL;
352 $event->name = addslashes($chat->name);
353 $event->description = addslashes($chat->intro);
354 $event->timestart = $chat->chattime;
355
356 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
357 update_event($event);
358
359 } else {
360 $event->courseid = $chat->course;
361 $event->groupid = 0;
362 $event->userid = 0;
363 $event->modulename = 'chat';
364 $event->instance = $chat->id;
365 $event->eventtype = $chat->schedule;
366 $event->timeduration = 0;
dcd338ff 367 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
b5de723d 368
8496c4af 369 add_event($event);
370 }
371 }
372 return true;
373}
374
516121bd 375
1515a89e 376//////////////////////////////////////////////////////////////////////
377/// Functions that require some SQL
378
a12e11c1 379function chat_get_users($chatid, $groupid=0, $groupingid=0) {
1515a89e 380
381 global $CFG;
84a2fdd7 382
383 if ($groupid) {
384 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
385 } else {
386 $groupselect = "";
387 }
a12e11c1 388
389 if (!empty($CFG->enablegroupings) && !(empty($groupingid))) {
390 $groupingjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
391 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
392
393 } else {
394 $groupingjoin = '';
395 }
b5de723d 396
65bcf17b 397 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping, u.imagealt
a12e11c1 398 FROM {$CFG->prefix}chat_users c
399 INNER JOIN {$CFG->prefix}user u ON u.id = c.userid
400 $groupingjoin
1515a89e 401 WHERE c.chatid = '$chatid'
a12e11c1 402 $groupselect
1515a89e 403 ORDER BY c.firstping ASC");
404}
405
84a2fdd7 406function chat_get_latest_message($chatid, $groupid=0) {
f33e1ed4 407 global $DB;
1515a89e 408
f33e1ed4 409 $params = array();
1515a89e 410
84a2fdd7 411 if ($groupid) {
f33e1ed4 412 $groupselect = " AND (groupid=? OR groupid=0)";
413 $params[] = $groupid;
84a2fdd7 414 } else {
415 $groupselect = "";
416 }
417
f33e1ed4 418 $sql = "SELECT *
419 FROM {chat_messages}
420 WHERE chatid = ?
421 $groupselect
422 ORDER BY timestamp DESC";
423 $params[] = $chatid;
03cedd62 424
f33e1ed4 425 return $DB->get_record_sql($sql, $params, true);
1515a89e 426}
427
5a8625e4 428
1515a89e 429//////////////////////////////////////////////////////////////////////
516121bd 430// login if not already logged in
1515a89e 431
a32c7772 432function chat_login_user($chatid, $version, $groupid, $course) {
1515a89e 433 global $USER;
e7fbd0b3 434 if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) {
516121bd 435 $chatuser->version = $version;
d96466d2 436 $chatuser->ip = $USER->lastip;
516121bd 437 $chatuser->lastping = time();
438 $chatuser->lang = current_language();
1515a89e 439
d96466d2 440 // Sometimes $USER->lastip is not setup properly
d13ef2fb 441 // during login. Update with current value if possible
f83edcb1 442 // or provide a dummy value for the db
d13ef2fb 443 if (empty($chatuser->ip)) {
444 $chatuser->ip = getremoteaddr();
445 if (empty($chatuser->ip)) {
f83edcb1 446 $chatuser->ip = '';
d13ef2fb 447 }
448 }
449
516121bd 450 if (($chatuser->course != $course->id)
451 or ($chatuser->userid != $USER->id)) {
452 return false;
453 }
454 if (!update_record('chat_users', $chatuser)) {
455 return false;
456 }
516121bd 457 } else {
6ee78cee 458 $chatuser = new object();
516121bd 459 $chatuser->chatid = $chatid;
460 $chatuser->userid = $USER->id;
461 $chatuser->groupid = $groupid;
462 $chatuser->version = $version;
d96466d2 463 $chatuser->ip = $USER->lastip;
516121bd 464 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
465 $chatuser->sid = random_string(32);
3dfd307f 466 $chatuser->course = $course->id; //caching - needed for current_language too
467 $chatuser->lang = current_language(); //caching - to resource intensive to find out later
516121bd 468
d96466d2 469 // Sometimes $USER->lastip is not setup properly
274f0091 470 // during login. Update with current value if possible
471 // or provide a dummy value for the db
472 if (empty($chatuser->ip)) {
473 $chatuser->ip = getremoteaddr();
474 if (empty($chatuser->ip)) {
475 $chatuser->ip = '';
476 }
477 }
478
479
516121bd 480 if (!insert_record('chat_users', $chatuser)) {
481 return false;
482 }
483
a32c7772 484 if ($version == 'sockets') {
485 // do not send 'enter' message, chatd will do it
486 } else {
6ee78cee 487 $message = new object();
2ac0d13b 488 $message->chatid = $chatuser->chatid;
489 $message->userid = $chatuser->userid;
490 $message->groupid = $groupid;
491 $message->message = 'enter';
492 $message->system = 1;
493 $message->timestamp = time();
494
495 if (!insert_record('chat_messages', $message)) {
2f52a088 496 print_error('cantinsert', 'chat');
2ac0d13b 497 }
516121bd 498 }
1515a89e 499 }
500
501 return $chatuser->sid;
502}
503
7d792369 504function chat_delete_old_users() {
505// Delete the old and in the way
506
b5012f3e 507 global $CFG;
508
e7fbd0b3 509 $timeold = time() - $CFG->chat_old_ping;
953eb6f3 510 $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts
a32c7772 511
6ee78cee 512 $query = "(version<>'basic' AND lastping<'$timeold') OR (version='basic' AND lastping<'$timeoldext')";
7d792369 513
516121bd 514 if ($oldusers = get_records_select('chat_users', $query) ) {
515 delete_records_select('chat_users', $query);
7d792369 516 foreach ($oldusers as $olduser) {
6ee78cee 517 $message = new object();
516121bd 518 $message->chatid = $olduser->chatid;
519 $message->userid = $olduser->userid;
520 $message->groupid = $olduser->groupid;
521 $message->message = 'exit';
522 $message->system = 1;
7d792369 523 $message->timestamp = time();
b5de723d 524
516121bd 525 if (!insert_record('chat_messages', $message)) {
2f52a088 526 print_error('cantinsert', 'chat');
7d792369 527 }
528 }
529 }
530}
1515a89e 531
22a4491a 532
fcd3a1ee 533function chat_update_chat_times($chatid=0) {
534/// Updates chat records so that the next chat time is correct
535
536 $timenow = time();
537 if ($chatid) {
538 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
539 return;
540 }
541 } else {
542 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
543 return;
544 }
545 }
546
547 foreach ($chats as $chat) {
7626969c 548 unset($chat->name);
549 unset($chat->intro);
fcd3a1ee 550 switch ($chat->schedule) {
551 case 1: // Single event - turn off schedule and disable
552 $chat->chattime = 0;
553 $chat->schedule = 0;
554 break;
555 case 2: // Repeat daily
f0d3bb9e 556 while ($chat->chattime <= $timenow) {
557 $chat->chattime += 24 * 3600;
558 }
fcd3a1ee 559 break;
560 case 3: // Repeat weekly
f0d3bb9e 561 while ($chat->chattime <= $timenow) {
562 $chat->chattime += 7 * 24 * 3600;
563 }
fcd3a1ee 564 break;
565 }
566 update_record("chat", $chat);
8496c4af 567
568 $event = NULL; // Update calendar too
b78f4cbd 569 $cond = "modulename='chat' AND instance = {$chat->id}
570 AND timestart != {$chat->chattime}";
571 if ($event->id = get_field_select('event', 'id', $cond)) {
8496c4af 572 $event->timestart = $chat->chattime;
573 update_event($event);
574 }
fcd3a1ee 575 }
576}
577
578
aa5c32fd 579function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chat_lastrow=NULL) {
72989350 580 global $CFG, $USER;
1515a89e 581
6ee78cee 582 $output = new object();
516121bd 583 $output->beep = false; // by default
584 $output->refreshusers = false; // by default
7d792369 585
72989350 586 // Use get_user_timezone() to find the correct timezone for displaying this message:
587 // It's either the current user's timezone or else decided by some Moodle config setting
970f144e 588 // First, "reset" $USER->timezone (which could have been set by a previous call to here)
589 // because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone
590 $USER->timezone = 99;
72989350 591 $tz = get_user_timezone($currentuser->timezone);
b5de723d 592
72989350 593 // Before formatting the message time string, set $USER->timezone to the above.
594 // This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
595 // message times appear off because DST is not taken into account when it should be.
596 $USER->timezone = $tz;
b5de723d 597 $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
598
599 $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
582de679 600 if ($courseid) {
d3981e38 601 $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 602 }
1515a89e 603
aa5c32fd 604 //Calculate the row class
605 if ($chat_lastrow !== NULL) {
606 $rowclass = ' class="r'.$chat_lastrow.'" ';
607 } else {
608 $rowclass = '';
609 }
610
b5de723d 611 // Start processing the message
1515a89e 612
b5de723d 613 if(!empty($message->system)) {
614 // System event
615 $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
aa5c32fd 616 $output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td><td class="text">';
617 $output->html .= '<span class="event">'.$output->text.'</span></td></tr></table>';
953eb6f3 618 $output->basic = '<dl><dt class="event">'.$message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender)).'</dt></dl>';
7d792369 619
516121bd 620 if($message->message == 'exit' or $message->message == 'enter') {
621 $output->refreshusers = true; //force user panel refresh ASAP
622 }
1515a89e 623 return $output;
624 }
625
82a524ef 626 // It's not a system event
b5de723d 627
628 $text = $message->message;
82a524ef 629
630 /// Parse the text to clean and filter it
631
6ee78cee 632 $options = new object();
82a524ef 633 $options->para = false;
634 $text = format_text($text, FORMAT_MOODLE, $options, $courseid);
927a7808 635
b5de723d 636 // And now check for special cases
927a7808 637 $special = false;
638
b5de723d 639 if (substr($text, 0, 5) == 'beep ') {
927a7808 640 /// It's a beep!
641 $special = true;
7d792369 642 $beepwho = trim(substr($text, 5));
9f85bed4 643
b5de723d 644 if ($beepwho == 'all') { // everyone
645 $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender));
646 $outmain = '';
647 $output->beep = true; // (eventually this should be set to
7d792369 648 // to a filename uploaded by the user)
649
82a524ef 650 } else if ($beepwho == $currentuser->id) { // current user
b5de723d 651 $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender));
652 $outmain = '';
7d792369 653 $output->beep = true;
264867fd 654
0eda0a46 655 } else { //something is not caught?
7d792369 656 return false;
657 }
b5de723d 658 } else if (substr($text, 0, 1) == '/') { /// It's a user command
927a7808 659 if (trim(substr($text, 0, 4)) == '/me') {
660 $special = true;
b5de723d 661 $outinfo = $message->strtime;
662 $outmain = $sender->firstname.' '.substr($text, 4);
1515a89e 663 }
927a7808 664 }
9f85bed4 665
927a7808 666 if(!$special) {
b5de723d 667 $outinfo = $message->strtime.' '.$sender->firstname;
7d792369 668 $outmain = $text;
1515a89e 669 }
264867fd 670
9f85bed4 671 /// Format the message as a small table
1515a89e 672
b5de723d 673 $output->text = strip_tags($outinfo.': '.$outmain);
7d792369 674
5379d249 675 $output->html = "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\" valign=\"top\">$message->picture</td><td class=\"text\">";
aa5c32fd 676 $output->html .= "<span class=\"title\">$outinfo</span>";
7d792369 677 if ($outmain) {
678 $output->html .= ": $outmain";
953eb6f3 679 $output->basic = '<dl><dt class="title">'.$outinfo.':</dt><dd class="text">'.$outmain.'</dd></dl>';
6ee78cee 680 } else {
953eb6f3 681 $output->basic = '<dl><dt class="title">'.$outinfo.'</dt></dl>';
7d792369 682 }
aa5c32fd 683 $output->html .= "</td></tr></table>";
7d792369 684 return $output;
b5de723d 685}
686
aa5c32fd 687function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NULL) {
b5de723d 688/// Given a message object full of information, this function
689/// formats it appropriately into text and html, then
690/// returns the formatted data.
691
78c98892 692 static $users; // Cache user lookups
693
694 if (isset($users[$message->userid])) {
695 $user = $users[$message->userid];
696 } else if ($user = get_record('user', 'id', $message->userid, '','','','','id,picture,firstname,lastname')) {
697 $users[$message->userid] = $user;
698 } else {
699 return NULL;
b5de723d 700 }
aa5c32fd 701 return chat_format_message_manually($message, $courseid, $user, $currentuser, $chat_lastrow);
1515a89e 702}
703
f3221af9 704function chat_get_view_actions() {
705 return array('view','view all','report');
706}
707
708function chat_get_post_actions() {
709 return array('talk');
710}
711
9ca0187e 712function chat_print_overview($courses, &$htmlarray) {
713 global $USER, $CFG;
714
715 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
716 return array();
717 }
718
719 if (!$chats = get_all_instances_in_courses('chat',$courses)) {
720 return;
721 }
722
723 $strchat = get_string('modulename', 'chat');
724 $strnextsession = get_string('nextsession', 'chat');
9ca0187e 725
726 foreach ($chats as $chat) {
9ca0187e 727 if ($chat->chattime and $chat->schedule) { // A chat is scheduled
a2a37336 728 $str = '<div class="chat overview"><div class="name">'.
729 $strchat.': <a '.($chat->visible?'':' class="dimmed"').
730 ' href="'.$CFG->wwwroot.'/mod/chat/view.php?id='.$chat->coursemodule.'">'.
731 $chat->name.'</a></div>';
732 $str .= '<div class="info">'.$strnextsession.': '.userdate($chat->chattime).'</div></div>';
733
734 if (empty($htmlarray[$chat->course]['chat'])) {
735 $htmlarray[$chat->course]['chat'] = $str;
736 } else {
737 $htmlarray[$chat->course]['chat'] .= $str;
738 }
9ca0187e 739 }
9ca0187e 740 }
741}
742
0b5a80a1 743
744/**
745 * Implementation of the function for printing the form elements that control
746 * whether the course reset functionality affects the chat.
747 * @param $mform form passed by reference
748 */
749function chat_reset_course_form_definition(&$mform) {
750 $mform->addElement('header', 'chatheader', get_string('modulenameplural', 'chat'));
751 $mform->addElement('advcheckbox', 'reset_chat', get_string('removemessages','chat'));
752}
753
754/**
755 * Course reset form defaults.
756 */
757function chat_reset_course_form_defaults($course) {
758 return array('reset_chat'=>1);
759}
760
761/**
762 * Actual implementation of the rest coures functionality, delete all the
763 * chat messages for course $data->courseid.
764 * @param $data the data submitted from the reset course.
765 * @return array status array
766 */
767function chat_reset_userdata($data) {
768 global $CFG;
769
770 $componentstr = get_string('modulenameplural', 'chat');
771 $status = array();
772
773 if (!empty($data->reset_chat)) {
774 $chatessql = "SELECT ch.id
775 FROM {$CFG->prefix}chat ch
776 WHERE ch.course={$data->courseid}";
777
778 delete_records_select('chat_messages', "chatid IN ($chatessql)");
779 delete_records_select('chat_users', "chatid IN ($chatessql)");
780 $status[] = array('component'=>$componentstr, 'item'=>get_string('removemessages', 'chat'), 'error'=>false);
781 }
782
783 /// updating dates - shift may be negative too
784 if ($data->timeshift) {
785 shift_course_mod_dates('chat', array('chattime'), $data->timeshift, $data->courseid);
786 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
787 }
788
789 return $status;
790}
791
1515a89e 792?>