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