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