172186b8 |
1 | <?php |
2 | /// library functions for messaging |
3 | |
e8e2d7f1 |
4 | |
5 | define ('MESSAGE_SHORTLENGTH', 300); |
fd7006f6 |
6 | define ('MESSAGE_WINDOW', true); // We are in a message window (so don't pop up a new one!) |
7 | |
05753d2b |
8 | if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds |
2a38a6be |
9 | $CFG->message_contacts_refresh = 60; |
fd7006f6 |
10 | } |
5d6b319b |
11 | if (!isset($CFG->message_chat_refresh)) { // Look for new comments every 5 seconds |
12 | $CFG->message_chat_refresh = 5; |
fd7006f6 |
13 | } |
f520438c |
14 | if (!isset($CFG->message_offline_time)) { |
15 | $CFG->message_offline_time = 300; |
16 | } |
172186b8 |
17 | |
18 | |
19 | function message_print_contacts() { |
e8e2d7f1 |
20 | global $USER, $CFG; |
21 | |
22 | $timetoshowusers = 300; //Seconds default |
23 | if (isset($CFG->block_online_users_timetosee)) { |
24 | $timetoshowusers = $CFG->block_online_users_timetosee * 60; |
25 | } |
26 | $timefrom = time()-$timetoshowusers; |
27 | |
531e58f1 |
28 | |
e8e2d7f1 |
29 | /// get lists of contacts and unread messages |
2bd89cdb |
30 | $onlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.blocked |
e8e2d7f1 |
31 | FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc |
531e58f1 |
32 | WHERE mc.userid='$USER->id' AND u.id=mc.contactid AND u.lastaccess>=$timefrom |
33 | AND mc.blocked='0' |
fd7006f6 |
34 | ORDER BY u.firstname ASC"); |
e8e2d7f1 |
35 | |
2bd89cdb |
36 | $offlinecontacts = get_records_sql("SELECT u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.blocked |
e8e2d7f1 |
37 | FROM {$CFG->prefix}user u, {$CFG->prefix}message_contacts mc |
38 | WHERE mc.userid='$USER->id' AND u.id=mc.contactid AND u.lastaccess<$timefrom |
531e58f1 |
39 | AND mc.blocked='0' |
fd7006f6 |
40 | ORDER BY u.firstname ASC"); |
e8e2d7f1 |
41 | |
2bd89cdb |
42 | $unreadmessages = get_records_sql("SELECT m.id, m.useridfrom, u.firstname, u.lastname, u.picture, u.imagealt |
531e58f1 |
43 | FROM {$CFG->prefix}user u, {$CFG->prefix}message m |
e8e2d7f1 |
44 | WHERE m.useridto='$USER->id' AND u.id=m.useridfrom"); |
45 | |
46 | $blockedcontacts = get_records_select('message_contacts', "userid='$USER->id' AND blocked='1'", '', 'contactid, id'); |
47 | |
48 | |
65ef518b |
49 | $countonlinecontacts = (is_array($onlinecontacts)) ? count($onlinecontacts) : 0; |
50 | $countofflinecontacts = (is_array($offlinecontacts)) ? count($offlinecontacts) : 0; |
e8e2d7f1 |
51 | |
52 | /// Cycle through messages and extract those that are from unknown contacts |
53 | /// We can take advantage of the keys for $onlinecontacts and $offlinecontacts |
54 | /// which are set to the userid and therefore we just need to see if the key |
55 | /// exists in either of those arrays |
56 | /// We can also discard any messages from users in our blocked contact list |
57 | $unknownmessages = array(); |
58 | if (!empty($unreadmessages)) { |
59 | /// make sure we have valid arrays to test against - they may be boolean false |
60 | if (empty($onlinecontacts)) $onlinecontacts = array(); |
61 | if (empty($offlinecontacts)) $offlinecontacts = array(); |
62 | if (empty($blockedcontacts)) $blockedcontacts = array(); |
63 | foreach ($unreadmessages as $unreadmessage) { |
531e58f1 |
64 | if (array_key_exists($unreadmessage->useridfrom, $onlinecontacts) or |
e8e2d7f1 |
65 | array_key_exists($unreadmessage->useridfrom, $offlinecontacts) or |
66 | array_key_exists($unreadmessage->useridfrom, $blockedcontacts) ) { |
67 | continue; |
68 | } |
69 | if (!isset($unknownmessages[$unreadmessage->useridfrom])) { |
70 | $message = $unreadmessage; |
71 | $message->count = 1; |
72 | $unknownmessages[$unreadmessage->useridfrom] = $message; |
73 | } else { |
74 | $unknownmessages[$unreadmessage->useridfrom]->count++; |
75 | } |
76 | } |
77 | } |
78 | |
a1157dda |
79 | if ($countonlinecontacts + $countofflinecontacts == 0) { |
669be60c |
80 | echo '<div class="heading">'; |
65ef518b |
81 | print_string('contactlistempty', 'message'); |
a1157dda |
82 | echo '</div>'; |
669be60c |
83 | echo '<div class="note">'; |
65ef518b |
84 | print_string('addsomecontacts', 'message', $CFG->wwwroot.'/message/index.php?tab=search'); |
a1157dda |
85 | echo '</div>'; |
65ef518b |
86 | } |
87 | |
88 | if(!empty($onlinecontacts) || !empty($offlinecontacts) || !empty($unknownmessages)) { |
531e58f1 |
89 | |
65ef518b |
90 | echo '<table id="message_contacts" align="center" cellspacing="2" cellpadding="0" border="0">'; |
531e58f1 |
91 | |
65ef518b |
92 | if(!empty($onlinecontacts)) { |
93 | /// print out list of online contacts |
531e58f1 |
94 | |
669be60c |
95 | echo '<tr><td colspan="3" class="heading">'; |
65ef518b |
96 | echo get_string('onlinecontacts', 'message', $countonlinecontacts); |
97 | echo '</td></tr>'; |
531e58f1 |
98 | |
65ef518b |
99 | if (!empty($onlinecontacts)) { |
100 | foreach ($onlinecontacts as $contact) { |
101 | if ($contact->blocked == 1) continue; |
102 | $fullname = fullname($contact); |
103 | $fullnamelink = $fullname; |
104 | /// are there any unread messages for this contact? |
105 | if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { |
106 | $fullnamelink = '<strong>'.$fullnamelink.' ('.$unread.')</strong>'; |
107 | } |
108 | /// link to remove from contact list |
109 | $strcontact = message_contact_link($contact->id, 'remove', true); |
110 | $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); |
531e58f1 |
111 | |
669be60c |
112 | echo '<tr><td class="pix">'; |
2bd89cdb |
113 | print_user_picture($contact, SITEID, $contact->picture, 20, false, true, 'userwindow'); |
65ef518b |
114 | echo '</td>'; |
669be60c |
115 | echo '<td class="contact">'; |
576ad290 |
116 | |
531e58f1 |
117 | link_to_popup_window("/message/discussion.php?id=$contact->id", "message_$contact->id", |
65ef518b |
118 | $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), |
119 | 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); |
576ad290 |
120 | |
65ef518b |
121 | echo '</td>'; |
669be60c |
122 | echo '<td class="link">'.$strcontact.' '.$strhistory.'</td>'; |
65ef518b |
123 | echo '</tr>'; |
124 | } |
e520509a |
125 | } |
65ef518b |
126 | echo '<tr><td colspan="3"> </td></tr>'; |
127 | } |
531e58f1 |
128 | |
a1157dda |
129 | if (!empty($offlinecontacts)) { |
65ef518b |
130 | /// print out list of offline contacts |
a1157dda |
131 | |
669be60c |
132 | echo '<tr><td colspan="3" class="heading">'; |
65ef518b |
133 | echo get_string('offlinecontacts', 'message', $countofflinecontacts); |
134 | echo '</td></tr>'; |
a1157dda |
135 | |
136 | foreach ($offlinecontacts as $contact) { |
137 | if ($contact->blocked == 1) continue; |
138 | $fullname = fullname($contact); |
139 | $fullnamelink = $fullname; |
65ef518b |
140 | /// are there any unread messages for this contact? |
a1157dda |
141 | if (($unread = message_count_messages($unreadmessages, 'useridfrom', $contact->id)) > 0) { |
142 | $fullnamelink = '<strong>'.$fullnamelink.' ('.$unread.')</strong>'; |
65ef518b |
143 | } |
a1157dda |
144 | /// link to remove from contact list |
145 | $strcontact = message_contact_link($contact->id, 'remove', true); |
146 | $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); |
147 | |
669be60c |
148 | echo '<tr><td class="pix">'; |
2bd89cdb |
149 | print_user_picture($contact, SITEID, $contact->picture, 20, false, true, 'userwindow'); |
a1157dda |
150 | echo '</td>'; |
669be60c |
151 | echo '<td class="contact">'; |
531e58f1 |
152 | link_to_popup_window("/message/discussion.php?id=$contact->id", "message_$contact->id", |
576ad290 |
153 | $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), |
154 | 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); |
155 | |
a1157dda |
156 | echo '</td>'; |
669be60c |
157 | echo '<td class="link">'.$strcontact.' '.$strhistory.'</td>'; |
a1157dda |
158 | echo '</tr>'; |
65ef518b |
159 | } |
160 | echo '<tr><td colspan="3"> </td></tr>'; |
e8e2d7f1 |
161 | } |
e8e2d7f1 |
162 | |
531e58f1 |
163 | |
65ef518b |
164 | /// print out list of incoming contacts |
165 | if (!empty($unknownmessages)) { |
669be60c |
166 | echo '<tr><td colspan="3" class="heading">'; |
65ef518b |
167 | echo get_string('incomingcontacts', 'message', count($unknownmessages)); |
168 | echo '</td></tr>'; |
a1157dda |
169 | |
65ef518b |
170 | foreach ($unknownmessages as $messageuser) { |
2bd89cdb |
171 | |
172 | // set id to be userid so functions expecting a user id have it |
173 | $messageuser->id = $messageuser->useridfrom; |
174 | |
65ef518b |
175 | $fullname = fullname($messageuser); |
176 | $fullnamelink = $fullname; |
177 | if ($messageuser->count) { |
178 | $fullnamelink = '<strong>'.$fullnamelink.' ('.$messageuser->count.')</strong>'; |
179 | } |
180 | /// link to add to contact list |
531e58f1 |
181 | |
65ef518b |
182 | $strcontact = message_contact_link($messageuser->useridfrom, 'add', true); |
183 | $strblock = message_contact_link($messageuser->useridfrom, 'block', true); |
184 | $strhistory = message_history_link($messageuser->useridfrom, 0, true, '', '', 'icon'); |
531e58f1 |
185 | |
669be60c |
186 | echo '<tr><td class="pix">'; |
2bd89cdb |
187 | print_user_picture($messageuser, SITEID, $messageuser->picture, 20, false, true, 'userwindow'); |
65ef518b |
188 | echo '</td>'; |
669be60c |
189 | echo '<td class="contact">'; |
576ad290 |
190 | |
531e58f1 |
191 | link_to_popup_window("/message/discussion.php?id=$messageuser->useridfrom", "message_$messageuser->useridfrom", |
65ef518b |
192 | $fullnamelink, 500, 500, get_string('sendmessageto', 'message', $fullname), |
193 | 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); |
576ad290 |
194 | |
65ef518b |
195 | echo '</td>'; |
669be60c |
196 | echo '<td class="link"> '.$strcontact.' '.$strblock.' '.$strhistory.'</td>'; |
65ef518b |
197 | echo '</tr>'; |
198 | } |
199 | } |
200 | |
201 | echo '</table>'; |
531e58f1 |
202 | |
669be60c |
203 | if (!empty($unknownmessages) && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help |
204 | echo '<div class="note">('; |
205 | print_string('addsomecontactsincoming', 'message'); |
206 | echo ')</div>'; |
207 | } |
531e58f1 |
208 | |
65ef518b |
209 | |
210 | } |
b9ee0638 |
211 | |
531e58f1 |
212 | echo '<br />'; |
a9d9b46a |
213 | |
531e58f1 |
214 | $autorefresh = '<p align="center" class="note">'.get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh).'</p>'; |
4702be4e |
215 | $autorefresh = addslashes_js($autorefresh); // js escaping |
531e58f1 |
216 | |
217 | // gracefully degrade JS autorefresh |
218 | echo '<script type="text/javascript"> |
219 | //<![CDATA[ |
220 | document.write("'.$autorefresh.'") |
221 | //]]> |
222 | </script>'; |
223 | echo '<noscript><div align="center">'; |
6ee78cee |
224 | echo print_single_button('index.php', false, get_string('refresh')); |
531e58f1 |
225 | echo '</div></noscript>'; |
e8e2d7f1 |
226 | } |
227 | |
228 | |
229 | |
230 | |
231 | /// $messagearray is an array of objects |
232 | /// $field is a valid property of object |
233 | /// $value is the value $field should equal to be counted |
234 | /// if $field is empty then return count of the whole array |
235 | /// if $field is non-existent then return 0; |
236 | function message_count_messages($messagearray, $field='', $value='') { |
237 | if (!is_array($messagearray)) return 0; |
238 | if ($field == '' or empty($messagearray)) return count($messagearray); |
531e58f1 |
239 | |
e8e2d7f1 |
240 | $count = 0; |
241 | foreach ($messagearray as $message) { |
242 | $count += ($message->$field == $value) ? 1 : 0; |
243 | } |
244 | return $count; |
172186b8 |
245 | } |
246 | |
e8e2d7f1 |
247 | |
172186b8 |
248 | function message_print_search() { |
9ae3253e |
249 | global $USER; |
531e58f1 |
250 | |
172186b8 |
251 | if ($frm = data_submitted()) { |
531e58f1 |
252 | |
172186b8 |
253 | message_print_search_results($frm); |
531e58f1 |
254 | |
172186b8 |
255 | } else { |
1ac41751 |
256 | /* |
531e58f1 |
257 | /// unfinished buggy code disabled in search.html anyway |
d76a5a7f |
258 | // find all courses this use has readallmessages capabilities in |
259 | if ($teachers = get_user_capability_course('moodle/site:readallmessages')) { |
172186b8 |
260 | $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname'); |
261 | $cs = '<select name="courseselect">'; |
262 | foreach ($teachers as $tcourse) { |
d76a5a7f |
263 | $cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->id]->shortname."</option>\n"; |
172186b8 |
264 | } |
265 | $cs .= '</select>'; |
266 | } |
1ac41751 |
267 | */ |
172186b8 |
268 | include('search.html'); |
269 | } |
270 | } |
271 | |
272 | function message_print_settings() { |
9ae3253e |
273 | global $USER; |
531e58f1 |
274 | |
172186b8 |
275 | if ($frm = data_submitted()) { |
acef58f4 |
276 | |
e8e2d7f1 |
277 | $pref = array(); |
278 | $pref['message_showmessagewindow'] = (isset($frm->showmessagewindow)) ? '1' : '0'; |
279 | $pref['message_beepnewmessage'] = (isset($frm->beepnewmessage)) ? '1' : '0'; |
3f85157b |
280 | $pref['message_blocknoncontacts'] = (isset($frm->blocknoncontacts)) ? '1' : '0'; |
e49617af |
281 | $pref['message_usehtmleditor'] = (isset($frm->usehtmleditor)) ? '1' : '0'; |
531e58f1 |
282 | $pref['message_noframesjs'] = (isset($frm->noframesjs)) ? '1' : '0'; |
e8e2d7f1 |
283 | $pref['message_emailmessages'] = (isset($frm->emailmessages)) ? '1' : '0'; |
acef58f4 |
284 | $pref['message_emailtimenosee'] = ((int)$frm->emailtimenosee > 0) ? (int)$frm->emailtimenosee : '10'; |
e8e2d7f1 |
285 | $pref['message_emailaddress'] = (!empty($frm->emailaddress)) ? $frm->emailaddress : $USER->email; |
286 | $pref['message_emailformat'] = (isset($frm->emailformat)) ? $frm->emailformat : FORMAT_PLAIN; |
e8e2d7f1 |
287 | |
288 | set_user_preferences($pref); |
531e58f1 |
289 | |
9ae3253e |
290 | redirect('index.php', get_string('settingssaved', 'message'), 1); |
e8e2d7f1 |
291 | } |
292 | |
293 | $cbshowmessagewindow = (get_user_preferences('message_showmessagewindow', 1) == '1') ? 'checked="checked"' : ''; |
fd7006f6 |
294 | $cbbeepnewmessage = (get_user_preferences('message_beepnewmessage', 0) == '1') ? 'checked="checked"' : ''; |
3f85157b |
295 | $cbblocknoncontacts = (get_user_preferences('message_blocknoncontacts', 0) == '1') ? 'checked="checked"' : ''; |
9aa959f2 |
296 | $cbusehtmleditor = (get_user_preferences('message_usehtmleditor', 0) == '1') ? 'checked="checked"' : ''; |
531e58f1 |
297 | $cbnoframesjs = (get_user_preferences('message_noframesjs', 0) == '1') ? 'checked="checked"' : ''; |
e8e2d7f1 |
298 | $cbemailmessages = (get_user_preferences('message_emailmessages', 1) == '1') ? 'checked="checked"' : ''; |
299 | $txemailaddress = get_user_preferences('message_emailaddress', $USER->email); |
300 | $txemailtimenosee = get_user_preferences('message_emailtimenosee', 10); |
301 | $format_select = choose_from_menu( array(FORMAT_PLAIN => get_string('formatplain'), |
302 | FORMAT_HTML => get_string('formathtml')), |
303 | 'emailformat', |
304 | get_user_preferences('message_emailformat', FORMAT_PLAIN), |
305 | false, '', '0', true ); |
531e58f1 |
306 | |
e8e2d7f1 |
307 | include('settings.html'); |
308 | } |
309 | |
310 | |
311 | |
312 | function message_add_contact($contactid, $blocked=0) { |
313 | global $USER; |
531e58f1 |
314 | |
e8e2d7f1 |
315 | if (!record_exists('user', 'id', $contactid)) { // invalid userid |
316 | return false; |
317 | } |
531e58f1 |
318 | |
e8e2d7f1 |
319 | if (($contact = get_record('message_contacts', 'userid', $USER->id, 'contactid', $contactid)) !== false) { |
320 | /// record already exists - we may be changing blocking status |
531e58f1 |
321 | |
e8e2d7f1 |
322 | if ($contact->blocked !== $blocked) { |
323 | /// change to blocking status |
324 | $contact->blocked = $blocked; |
325 | return update_record('message_contacts', $contact); |
326 | } else { |
327 | /// no changes to blocking status |
328 | return true; |
329 | } |
531e58f1 |
330 | |
172186b8 |
331 | } else { |
e8e2d7f1 |
332 | /// new contact record |
333 | unset($contact); |
334 | $contact->userid = $USER->id; |
335 | $contact->contactid = $contactid; |
336 | $contact->blocked = $blocked; |
337 | return insert_record('message_contacts', $contact, false); |
172186b8 |
338 | } |
339 | } |
340 | |
e8e2d7f1 |
341 | function message_remove_contact($contactid) { |
342 | global $USER; |
e8e2d7f1 |
343 | return delete_records('message_contacts', 'userid', $USER->id, 'contactid', $contactid); |
344 | } |
345 | |
346 | function message_unblock_contact($contactid) { |
082864f9 |
347 | global $USER; |
348 | return delete_records('message_contacts', 'userid', $USER->id, 'contactid', $contactid); |
e8e2d7f1 |
349 | } |
350 | |
351 | function message_block_contact($contactid) { |
352 | return message_add_contact($contactid, 1); |
353 | } |
354 | |
355 | function message_get_contact($contactid) { |
356 | global $USER; |
357 | return get_record('message_contacts', 'userid', $USER->id, 'contactid', $contactid); |
358 | } |
531e58f1 |
359 | |
e8e2d7f1 |
360 | |
361 | |
172186b8 |
362 | function message_print_search_results($frm) { |
9ae3253e |
363 | global $USER, $CFG; |
e8e2d7f1 |
364 | |
365 | echo '<div align="center">'; |
366 | |
367 | /// search for person |
368 | if (!empty($frm->personsubmit) and !empty($frm->name)) { |
531e58f1 |
369 | |
b71d4dd2 |
370 | if (optional_param('mycourses', 0, PARAM_BOOL)) { |
e8e2d7f1 |
371 | $users = array(); |
372 | $mycourses = get_my_courses($USER->id); |
373 | foreach ($mycourses as $mycourse) { |
374 | if (is_array($susers = message_search_users($mycourse->id, $frm->name))) { |
375 | foreach ($susers as $suser) $users[$suser->id] = $suser; |
376 | } |
377 | } |
378 | } else { |
379 | $users = message_search_users(SITEID, $frm->name); |
380 | } |
381 | |
382 | if (!empty($users)) { |
383 | echo '<strong>'.get_string('userssearchresults', 'message', count($users)).'</strong>'; |
384 | echo '<table class="message_users">'; |
385 | foreach ($users as $user) { |
531e58f1 |
386 | |
e8e2d7f1 |
387 | if (($contact = message_get_contact($user->id)) !== false) { |
388 | if ($contact->blocked == 0) { /// not blocked |
082864f9 |
389 | $strcontact = message_contact_link($user->id, 'remove', true); |
390 | $strblock = message_contact_link($user->id, 'block', true); |
e8e2d7f1 |
391 | } else { // blocked |
082864f9 |
392 | $strcontact = message_contact_link($user->id, 'add', true); |
393 | $strblock = message_contact_link($user->id, 'unblock', true); |
e8e2d7f1 |
394 | } |
e8e2d7f1 |
395 | } else { |
082864f9 |
396 | $strcontact = message_contact_link($user->id, 'add', true); |
397 | $strblock = message_contact_link($user->id, 'block', true); |
e8e2d7f1 |
398 | } |
f520438c |
399 | $strhistory = message_history_link($user->id, 0, true, '', '', 'icon'); |
531e58f1 |
400 | |
669be60c |
401 | echo '<tr><td class="pix">'; |
e520509a |
402 | print_user_picture($user->id, SITEID, $user->picture, 20, false, true, 'userwindow'); |
e8e2d7f1 |
403 | echo '</td>'; |
669be60c |
404 | echo '<td class="contact">'; |
531e58f1 |
405 | link_to_popup_window("/message/discussion.php?id=$user->id", "message_$user->id", fullname($user), |
405f01ee |
406 | 500, 500, get_string('sendmessageto', 'message', fullname($user)), |
407 | 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); |
576ad290 |
408 | |
e8e2d7f1 |
409 | echo '</td>'; |
531e58f1 |
410 | |
669be60c |
411 | echo '<td class="link">'.$strcontact.'</td>'; |
412 | echo '<td class="link">'.$strblock.'</td>'; |
413 | echo '<td class="link">'.$strhistory.'</td>'; |
e8e2d7f1 |
414 | echo '</tr>'; |
415 | } |
416 | echo '</table>'; |
531e58f1 |
417 | |
e8e2d7f1 |
418 | } else { |
419 | notify(get_string('nosearchresults', 'message')); |
420 | } |
531e58f1 |
421 | |
422 | |
e8e2d7f1 |
423 | /// search messages for keywords |
424 | } else if (!empty($frm->keywordssubmit) and !empty($frm->keywords)) { |
38c6a928 |
425 | $keywordstring = clean_text(trim($frm->keywords)); |
426 | $keywords = explode(' ', $keywordstring); |
e8e2d7f1 |
427 | $tome = false; |
428 | $fromme = false; |
429 | $courseid = 'none'; |
430 | |
431 | switch ($frm->keywordsoption) { |
432 | case 'tome': |
433 | $tome = true; |
434 | break; |
435 | case 'fromme': |
436 | $fromme = true; |
437 | break; |
438 | case 'allmine': |
439 | $tome = true; |
440 | $fromme = true; |
441 | break; |
442 | case 'allusers': |
443 | $courseid = SITEID; |
444 | break; |
445 | case 'courseusers': |
446 | $courseid = $frm->courseid; |
447 | break; |
448 | default: |
449 | $tome = true; |
450 | $fromme = true; |
451 | } |
452 | |
453 | if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { |
531e58f1 |
454 | |
e8e2d7f1 |
455 | /// get a list of contacts |
082864f9 |
456 | if (($contacts = get_records('message_contacts', 'userid', $USER->id, '', 'contactid, blocked') ) === false) { |
457 | $contacts = array(); |
458 | } |
e8e2d7f1 |
459 | |
082864f9 |
460 | /// print heading with number of results |
669be60c |
461 | echo '<p class="heading">'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($keywordstring).'")</p>'; |
082864f9 |
462 | |
463 | /// print table headings |
669be60c |
464 | echo '<table class="searchresults" cellspacing="0">'; |
082864f9 |
465 | echo '<tr>'; |
fd7006f6 |
466 | echo '<td><strong>'.get_string('from').'</strong></td>'; |
467 | echo '<td><strong>'.get_string('to').'</strong></td>'; |
468 | echo '<td><strong>'.get_string('message', 'message').'</strong></td>'; |
469 | echo '<td><strong>'.get_string('timesent', 'message').'</strong></td>'; |
082864f9 |
470 | echo "</tr>\n"; |
471 | |
472 | $blockedcount = 0; |
e520509a |
473 | $dateformat = get_string('strftimedatetime'); |
38c6a928 |
474 | $strcontext = get_string('context', 'message'); |
e8e2d7f1 |
475 | foreach ($messages as $message) { |
082864f9 |
476 | |
477 | /// ignore messages to and from blocked users unless $frm->includeblocked is set |
b71d4dd2 |
478 | if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( |
082864f9 |
479 | ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or |
480 | ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) |
481 | ) |
482 | ) { |
483 | $blockedcount ++; |
e8e2d7f1 |
484 | continue; |
485 | } |
531e58f1 |
486 | |
082864f9 |
487 | /// load up user to record |
488 | if ($message->useridto !== $USER->id) { |
489 | $userto = get_record('user', 'id', $message->useridto); |
531e58f1 |
490 | $tocontact = (array_key_exists($message->useridto, $contacts) and |
082864f9 |
491 | ($contacts[$message->useridto]->blocked == 0) ); |
531e58f1 |
492 | $toblocked = (array_key_exists($message->useridto, $contacts) and |
082864f9 |
493 | ($contacts[$message->useridto]->blocked == 1) ); |
494 | } else { |
495 | $userto = false; |
496 | $tocontact = false; |
497 | $toblocked = false; |
498 | } |
531e58f1 |
499 | |
082864f9 |
500 | /// load up user from record |
501 | if ($message->useridfrom !== $USER->id) { |
502 | $userfrom = get_record('user', 'id', $message->useridfrom); |
531e58f1 |
503 | $fromcontact = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 |
504 | ($contacts[$message->useridfrom]->blocked == 0) ); |
531e58f1 |
505 | $fromblocked = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 |
506 | ($contacts[$message->useridfrom]->blocked == 1) ); |
507 | } else { |
508 | $userfrom = false; |
509 | $fromcontact = false; |
510 | $fromblocked = false; |
511 | } |
512 | |
e520509a |
513 | /// find date string for this message |
514 | $date = usergetdate($message->timecreated); |
515 | $datestring = $date['year'].$date['mon'].$date['mday']; |
516 | |
082864f9 |
517 | /// print out message row |
518 | echo '<tr valign="top">'; |
669be60c |
519 | echo '<td class="contact">'; |
b9ee0638 |
520 | message_print_user($userfrom, $fromcontact, $fromblocked); |
e8e2d7f1 |
521 | echo '</td>'; |
669be60c |
522 | echo '<td class="contact">'; |
b9ee0638 |
523 | message_print_user($userto, $tocontact, $toblocked); |
e8e2d7f1 |
524 | echo '</td>'; |
669be60c |
525 | echo '<td class="summary">'.message_get_fragment($message->message, $keywords); |
526 | echo '<br /><div class="link">'; |
531e58f1 |
527 | message_history_link($message->useridto, $message->useridfrom, false, |
2973ec18 |
528 | $keywordstring, 'm'.$message->id, $strcontext); |
f520438c |
529 | echo '</div>'; |
e520509a |
530 | echo '</td>'; |
669be60c |
531 | echo '<td class="date">'.userdate($message->timecreated, $dateformat).'</td>'; |
082864f9 |
532 | echo "</tr>\n"; |
533 | } |
531e58f1 |
534 | |
e8e2d7f1 |
535 | |
e520509a |
536 | if ($blockedcount > 0) { |
537 | echo '<tr><td colspan="4" align="center">'.get_string('blockedmessages', 'message', $blockedcount).'</td></tr>'; |
538 | } |
e8e2d7f1 |
539 | echo '</table>'; |
531e58f1 |
540 | |
e8e2d7f1 |
541 | } else { |
542 | notify(get_string('nosearchresults', 'message')); |
543 | } |
544 | |
545 | |
546 | /// what the ????, probably an empty search string, duh! |
547 | } else { |
548 | notify(get_string('emptysearchstring', 'message')); |
549 | } |
550 | |
082864f9 |
551 | echo '<br />'; |
9ae3253e |
552 | print_single_button('index.php', array( 'tab' => 'search'), get_string('newsearch', 'message') ); |
e8e2d7f1 |
553 | |
554 | echo '</div>'; |
555 | } |
556 | |
557 | |
082864f9 |
558 | function message_print_user ($user=false, $iscontact=false, $isblocked=false) { |
559 | global $USER; |
560 | if ($user === false) { |
e520509a |
561 | print_user_picture($USER->id, SITEID, $USER->picture, 20, false, true, 'userwindow'); |
082864f9 |
562 | } else { |
e520509a |
563 | print_user_picture($user->id, SITEID, $user->picture, 20, false, true, 'userwindow'); |
564 | echo ' '; |
082864f9 |
565 | if ($iscontact) { |
566 | message_contact_link($user->id, 'remove'); |
567 | } else { |
568 | message_contact_link($user->id, 'add'); |
569 | } |
570 | echo ' '; |
571 | if ($isblocked) { |
572 | message_contact_link($user->id, 'unblock'); |
573 | } else { |
574 | message_contact_link($user->id, 'block'); |
575 | } |
e520509a |
576 | echo '<br />'; |
576ad290 |
577 | |
531e58f1 |
578 | link_to_popup_window("/message/discussion.php?id=$user->id", "message_$user->id", |
405f01ee |
579 | fullname($user), 400, 400, get_string('sendmessageto', 'message', fullname($user)), |
580 | 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500'); |
082864f9 |
581 | } |
582 | } |
583 | |
584 | |
585 | /// linktype can be: add, remove, block, unblock |
5d6b319b |
586 | function message_contact_link($userid, $linktype='add', $return=false, $script="index.php?tab=contacts", $text=false) { |
e520509a |
587 | global $USER, $CFG; |
588 | |
589 | static $str; |
590 | |
591 | if (empty($str->blockcontact)) { |
592 | $str->blockcontact = get_string('blockcontact', 'message'); |
593 | $str->unblockcontact = get_string('unblockcontact', 'message'); |
594 | $str->removecontact = get_string('removecontact', 'message'); |
595 | $str->addcontact = get_string('addcontact', 'message'); |
596 | } |
597 | |
5d6b319b |
598 | $command = $linktype.'contact'; |
599 | $string = $str->{$command}; |
d53f1cdb |
600 | $alttext = $text ? '' : $string; |
5d6b319b |
601 | $text = $text ? ' '.$string : ''; |
602 | |
082864f9 |
603 | switch ($linktype) { |
604 | case 'block': |
5d6b319b |
605 | $icon = '/t/go.gif'; |
082864f9 |
606 | break; |
607 | case 'unblock': |
5d6b319b |
608 | $icon = '/t/stop.gif'; |
082864f9 |
609 | break; |
610 | case 'remove': |
5d6b319b |
611 | $icon = '/t/user.gif'; |
082864f9 |
612 | break; |
613 | case 'add': |
614 | default: |
5d6b319b |
615 | $icon = '/t/usernot.gif'; |
082864f9 |
616 | } |
5d6b319b |
617 | |
618 | $output = '<span class="'.$linktype.'">'. |
619 | '<a href="'.$script.'&'.$command.'='.$userid. |
0d3bb48d |
620 | '&sesskey='.sesskey().'" title="'.s($string).'">'. |
0d905d9f |
621 | '<img src="'.$CFG->pixpath.$icon.'" class="iconsmall" alt="'.s($alttext).'" />'. |
5d6b319b |
622 | $text.'</a></span>'; |
623 | |
e520509a |
624 | if ($return) { |
625 | return $output; |
082864f9 |
626 | } else { |
e520509a |
627 | echo $output; |
082864f9 |
628 | return true; |
629 | } |
630 | } |
631 | |
38c6a928 |
632 | function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords='', $position='', $linktext='') { |
fd7006f6 |
633 | global $USER, $CFG; |
62119d65 |
634 | |
830d0af6 |
635 | static $strmessagehistory; |
636 | |
637 | if (empty($strmessagehistory)) { |
638 | $strmessagehistory = get_string('messagehistory', 'message'); |
639 | } |
2514081c |
640 | |
62119d65 |
641 | if (!$userid2) { |
642 | $userid2 = $USER->id; |
643 | } |
e520509a |
644 | if ($position) { |
645 | $position = "#$position"; |
646 | } |
38c6a928 |
647 | if ($keywords) { |
648 | $keywords = "&search=".urlencode($keywords); |
649 | } |
62119d65 |
650 | |
830d0af6 |
651 | if ($linktext == 'icon') { // Icon only |
0d905d9f |
652 | $fulllink = '<img src="'.$CFG->pixpath.'/t/log.gif" class="iconsmall" alt="'.$strmessagehistory.'" />'; |
830d0af6 |
653 | } else if ($linktext == 'both') { // Icon and standard name |
0d905d9f |
654 | $fulllink = '<img src="'.$CFG->pixpath.'/t/log.gif" class="iconsmall" alt="" />'; |
830d0af6 |
655 | $fulllink .= ' '.$strmessagehistory; |
656 | } else if ($linktext) { // Custom name |
657 | $fulllink = $linktext; |
658 | } else { // Standard name only |
659 | $fulllink = $strmessagehistory; |
fd7006f6 |
660 | } |
661 | |
531e58f1 |
662 | $str = link_to_popup_window("/message/history.php?user1=$userid1&user2=$userid2$keywords$position", |
663 | "message_history_$userid1", $fulllink, 500, 500, $strmessagehistory, |
5fb86983 |
664 | 'menubar=0,location=0,status,scrollbars,resizable,width=500,height=500', true); |
62119d65 |
665 | |
5d6b319b |
666 | $str = '<span class="history">'.$str.'</span>'; |
667 | |
62119d65 |
668 | if ($returnstr) { |
669 | return $str; |
670 | } else { |
671 | echo $str; |
672 | return true; |
673 | } |
674 | } |
e8e2d7f1 |
675 | |
676 | |
677 | /** |
678 | * Search through course users |
679 | * |
531e58f1 |
680 | * If $coursid specifies the site course then this function searches |
e8e2d7f1 |
681 | * through all undeleted and confirmed users |
682 | * |
683 | * @uses $CFG |
684 | * @uses SITEID |
685 | * @param int $courseid The course in question. |
686 | * @param string $searchtext ? |
687 | * @param string $sort ? |
531e58f1 |
688 | * @param string $exceptions ? |
e8e2d7f1 |
689 | * @return array An array of {@link $USER} records. |
690 | * @todo Finish documenting this function |
691 | */ |
692 | function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { |
693 | global $CFG; |
694 | |
92a2d92a |
695 | $fullname = sql_fullname(); |
6eb7722f |
696 | $LIKE = sql_ilike(); |
e8e2d7f1 |
697 | |
698 | if (!empty($exceptions)) { |
699 | $except = ' AND u.id NOT IN ('. $exceptions .') '; |
700 | } else { |
701 | $except = ''; |
702 | } |
703 | |
704 | if (!empty($sort)) { |
705 | $order = ' ORDER BY '. $sort; |
706 | } else { |
707 | $order = ''; |
708 | } |
709 | |
710 | $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; |
444f6252 |
711 | $fields = 'u.id, u.firstname, u.lastname, u.picture'; |
e8e2d7f1 |
712 | |
713 | if (!$courseid or $courseid == SITEID) { |
444f6252 |
714 | return get_records_sql("SELECT $fields |
e8e2d7f1 |
715 | FROM {$CFG->prefix}user u |
716 | WHERE $select |
717 | AND ($fullname $LIKE '%$searchtext%') |
718 | $except $order"); |
719 | } else { |
720 | |
d76a5a7f |
721 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
722 | $contextlists = get_related_contexts_string($context); |
531e58f1 |
723 | |
d76a5a7f |
724 | // everyone who has a role assignement in this course or higher |
725 | $users = get_records_sql("SELECT $fields |
726 | FROM {$CFG->prefix}user u, |
727 | {$CFG->prefix}role_assignments ra |
531e58f1 |
728 | WHERE $select |
d76a5a7f |
729 | AND ra.contextid $contextlists |
730 | AND u.id = ra.userid |
731 | AND ($fullname $LIKE '%$searchtext%') |
732 | $except $order"); |
733 | |
734 | return $users; |
e8e2d7f1 |
735 | } |
736 | } |
737 | |
738 | |
739 | |
740 | |
082864f9 |
741 | function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { |
e8e2d7f1 |
742 | /// Returns a list of posts found using an array of search terms |
743 | /// eg word +word -word |
744 | /// |
745 | |
746 | global $CFG, $USER; |
747 | |
082864f9 |
748 | /// If no userid sent then assume current user |
531e58f1 |
749 | if ($userid == 0) $userid = $USER->id; |
082864f9 |
750 | |
6eb7722f |
751 | /// Some differences in SQL syntax |
752 | $LIKE = sql_ilike(); |
753 | $NOTLIKE = 'NOT ' . $LIKE; |
bb48a537 |
754 | if ($CFG->dbfamily == "postgres") { |
e8e2d7f1 |
755 | $REGEXP = "~*"; |
756 | $NOTREGEXP = "!~*"; |
757 | } else { |
e8e2d7f1 |
758 | $REGEXP = "REGEXP"; |
759 | $NOTREGEXP = "NOT REGEXP"; |
760 | } |
761 | |
762 | $messagesearch = ""; |
763 | |
764 | foreach ($searchterms as $searchterm) { |
765 | if (strlen($searchterm) < 2) { |
766 | continue; |
767 | } |
6bb4875f |
768 | /// Under Oracle and MSSQL, trim the + and - operators and perform |
769 | /// simpler LIKE search |
bb48a537 |
770 | if ($CFG->dbfamily == 'oracle' || $CFG->dbfamily == 'mssql') { |
6bb4875f |
771 | $searchterm = trim($searchterm, '+-'); |
772 | } |
773 | |
e8e2d7f1 |
774 | if ($messagesearch) { |
775 | $messagesearch .= " AND "; |
776 | } |
777 | |
778 | if (substr($searchterm,0,1) == "+") { |
779 | $searchterm = substr($searchterm,1); |
780 | $messagesearch .= " m.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
781 | } else if (substr($searchterm,0,1) == "-") { |
782 | $searchterm = substr($searchterm,1); |
783 | $messagesearch .= " m.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' "; |
784 | } else { |
785 | $messagesearch .= " m.message $LIKE '%$searchterm%' "; |
786 | } |
172186b8 |
787 | } |
788 | |
8a51efe9 |
789 | if ($messagesearch == '') { // if only 1 letter words searched |
790 | return false; |
791 | } |
e8e2d7f1 |
792 | |
793 | $messagesearch = "($messagesearch) "; |
794 | |
795 | |
082864f9 |
796 | /// There are several possibilities |
797 | /// 1. courseid = SITEID : The admin is searching messages by all users |
798 | /// 2. courseid = ?? : A teacher is searching messages by users in |
799 | /// one of their courses - currently disabled |
800 | /// 3. courseid = none : User is searching their own messages; |
801 | /// a. Messages from user |
802 | /// b. Messages to user |
803 | /// c. Messages to and from user |
804 | |
805 | if ($courseid == SITEID) { /// admin is searching all messages |
806 | $m_read = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated |
531e58f1 |
807 | FROM {$CFG->prefix}message_read m |
082864f9 |
808 | WHERE $messagesearch"); |
809 | $m_unread = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated |
531e58f1 |
810 | FROM {$CFG->prefix}message m |
082864f9 |
811 | WHERE $messagesearch"); |
531e58f1 |
812 | |
082864f9 |
813 | if ($m_read === false) $m_read = array(); |
814 | if ($m_unread === false) $m_unread = array(); |
531e58f1 |
815 | |
082864f9 |
816 | } elseif ($courseid !== 'none') { |
817 | /// This has not been implemented due to security concerns |
e8e2d7f1 |
818 | |
082864f9 |
819 | } else { |
531e58f1 |
820 | |
082864f9 |
821 | if ($fromme and $tome) $messagesearch .= "AND (m.useridfrom='$userid' OR m.useridto='$userid') "; |
822 | elseif ($fromme) $messagesearch .= "AND m.useridfrom='$userid' "; |
823 | elseif ($tome) $messagesearch .= "AND m.useridto='$userid' "; |
531e58f1 |
824 | |
082864f9 |
825 | $m_read = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated |
531e58f1 |
826 | FROM {$CFG->prefix}message_read m |
082864f9 |
827 | WHERE $messagesearch"); |
828 | $m_unread = get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.message, m.timecreated |
531e58f1 |
829 | FROM {$CFG->prefix}message m |
082864f9 |
830 | WHERE $messagesearch"); |
531e58f1 |
831 | |
082864f9 |
832 | if ($m_read === false) $m_read = array(); |
833 | if ($m_unread === false) $m_unread = array(); |
531e58f1 |
834 | |
e8e2d7f1 |
835 | } |
836 | |
082864f9 |
837 | /// The keys may be duplicated in $m_read and $m_unread so we can't |
838 | /// do a simple concatenation |
839 | $message = array(); |
840 | foreach ($m_read as $m) $messages[] = $m; |
841 | foreach ($m_unread as $m) $messages[] = $m; |
e8e2d7f1 |
842 | |
e8e2d7f1 |
843 | |
844 | return (empty($messages)) ? false : $messages; |
172186b8 |
845 | } |
846 | |
e8e2d7f1 |
847 | |
848 | |
849 | /// Borrowed with changes from mod/forum/lib.php |
850 | function message_shorten_message($message, $minlength=0) { |
851 | // Given a post object that we already know has a long message |
852 | // this function truncates the message nicely to the first |
853 | // sane place between $CFG->forum_longpost and $CFG->forum_shortpost |
854 | |
855 | $i = 0; |
856 | $tag = false; |
857 | $length = strlen($message); |
858 | $count = 0; |
859 | $stopzone = false; |
860 | $truncate = 0; |
861 | if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; |
531e58f1 |
862 | |
e8e2d7f1 |
863 | |
864 | for ($i=0; $i<$length; $i++) { |
865 | $char = $message[$i]; |
866 | |
867 | switch ($char) { |
868 | case "<": |
869 | $tag = true; |
870 | break; |
871 | case ">": |
872 | $tag = false; |
873 | break; |
874 | default: |
875 | if (!$tag) { |
876 | if ($stopzone) { |
877 | if ($char == '.' or $char == ' ') { |
878 | $truncate = $i+1; |
879 | break 2; |
880 | } |
881 | } |
882 | $count++; |
883 | } |
884 | break; |
885 | } |
886 | if (!$stopzone) { |
887 | if ($count > $minlength) { |
888 | $stopzone = true; |
889 | } |
890 | } |
891 | } |
892 | |
893 | if (!$truncate) { |
894 | $truncate = $i; |
895 | } |
896 | |
897 | return substr($message, 0, $truncate); |
898 | } |
899 | |
38c6a928 |
900 | |
901 | /* |
902 | * Given a string and an array of keywords, this function looks |
903 | * for the first keyword in the string, and then chops out a |
904 | * small section from the text that shows that word in context. |
905 | */ |
906 | function message_get_fragment($message, $keywords) { |
907 | |
908 | $fullsize = 120; |
909 | $halfsize = (int)($fullsize/2); |
910 | |
911 | $message = strip_tags($message); |
912 | |
913 | foreach ($keywords as $keyword) { // Just get the first one |
914 | if ($keyword !== '') { |
915 | break; |
916 | } |
917 | } |
918 | if (empty($keyword)) { // None found, so just return start of message |
919 | return message_shorten_message($message, 30); |
920 | } |
921 | |
922 | $leadin = $leadout = ''; |
923 | |
924 | /// Find the start of the fragment |
925 | $start = 0; |
926 | $length = strlen($message); |
927 | |
928 | $pos = strpos($message, $keyword); |
929 | if ($pos > $halfsize) { |
930 | $start = $pos - $halfsize; |
931 | $leadin = '...'; |
932 | } |
933 | /// Find the end of the fragment |
934 | $end = $start + $fullsize; |
935 | if ($end > $length) { |
936 | $end = $length; |
937 | } else { |
938 | $leadout = '...'; |
939 | } |
940 | |
941 | /// Pull out the fragment and format it |
942 | |
943 | $fragment = substr($message, $start, $end - $start); |
944 | $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; |
945 | return $fragment; |
946 | } |
947 | |
948 | |
b9ee0638 |
949 | function message_get_history($user1, $user2) { |
531e58f1 |
950 | $messages = get_records_select('message_read', "(useridto = '$user1->id' AND useridfrom = '$user2->id') OR |
951 | (useridto = '$user2->id' AND useridfrom = '$user1->id')", |
b9ee0638 |
952 | 'timecreated'); |
531e58f1 |
953 | if ($messages_new = get_records_select('message', "(useridto = '$user1->id' AND useridfrom = '$user2->id') OR |
954 | (useridto = '$user2->id' AND useridfrom = '$user1->id')", |
b9ee0638 |
955 | 'timecreated')) { |
956 | foreach ($messages_new as $message) { |
957 | $messages[] = $message; |
958 | } |
959 | } |
960 | return $messages; |
961 | } |
962 | |
030e3e03 |
963 | function message_format_message(&$message, &$user, $format='', $keywords='', $class='other') { |
acd21a2d |
964 | |
965 | static $dateformat; |
966 | |
967 | if (empty($dateformat)) { |
968 | if ($format) { |
969 | $dateformat = $format; |
970 | } else { |
971 | $format = get_string('strftimedaytime'); |
972 | } |
b9ee0638 |
973 | } |
acd21a2d |
974 | $time = userdate($message->timecreated, $dateformat); |
ff6048dd |
975 | $options->para = false; |
976 | $messagetext = format_text($message->message, $message->format, $options); |
38c6a928 |
977 | if ($keywords) { |
978 | $messagetext = highlight($keywords, $messagetext); |
979 | } |
030e3e03 |
980 | return '<div class="message '.$class.'"><a name="m'.$message->id.'"></a><span class="author">'.s(fullname($user)).'</span> <span class="time">['.$time.']</span>: <span class="content">'.$messagetext.'</span></div>'; |
b9ee0638 |
981 | } |
e8e2d7f1 |
982 | |
405f01ee |
983 | /* |
984 | * Inserts a message into the database, but also forwards it |
985 | * via other means if appropriate. |
986 | */ |
987 | function message_post_message($userfrom, $userto, $message, $format, $messagetype) { |
988 | |
989 | global $CFG, $SITE; |
990 | |
991 | /// Save the new message in the database |
992 | |
993 | $savemessage = NULL; |
994 | $savemessage->useridfrom = $userfrom->id; |
995 | $savemessage->useridto = $userto->id; |
996 | $savemessage->message = $message; |
997 | $savemessage->format = $format; |
998 | $savemessage->timecreated = time(); |
999 | $savemessage->messagetype = 'direct'; |
1000 | |
576ad290 |
1001 | if ($CFG->messaging) { |
1002 | if (!$savemessage->id = insert_record('message', $savemessage)) { |
1003 | return false; |
1004 | } |
1005 | $emailforced = false; |
1006 | } else { // $CFG->messaging is not on, we need to force sending of emails |
1007 | $emailforced = true; |
1008 | $savemessage->id = true; |
405f01ee |
1009 | } |
1010 | |
405f01ee |
1011 | /// Check to see if anything else needs to be done with it |
1012 | |
1013 | $preference = (object)get_user_preferences(NULL, NULL, $userto->id); |
1014 | |
576ad290 |
1015 | if ($emailforced || (!isset($preference->message_emailmessages) || $preference->message_emailmessages)) { // Receiver wants mail forwarding |
54bf5de8 |
1016 | if (!isset($preference->message_emailtimenosee)) { |
1017 | $preference->message_emailtimenosee = 10; |
1018 | } |
7c8b82a9 |
1019 | if (!isset($preference->message_emailformat)) { |
1020 | $preference->message_emailformat = FORMAT_HTML; |
1021 | } |
576ad290 |
1022 | if ($emailforced || (time() - $userto->lastaccess) > ((int)$preference->message_emailtimenosee * 60)) { // Long enough |
405f01ee |
1023 | |
1024 | $message = stripslashes_safe($message); |
1025 | $tagline = get_string('emailtagline', 'message', $SITE->shortname); |
531e58f1 |
1026 | |
405f01ee |
1027 | $messagesubject = message_shorten_message(strip_tags($message), 30).'...'; |
1028 | |
1029 | $messagetext = format_text_email($message, $format). |
1030 | "\n\n--\n".$tagline."\n"."$CFG->wwwroot/message/index.php?popup=1"; |
1031 | |
49487025 |
1032 | if (isset($preference->message_emailformat) and $preference->message_emailformat == FORMAT_HTML) { |
405f01ee |
1033 | $messagehtml = format_text($message, $format); |
576ad290 |
1034 | // MDL-10294, do not print link if messaging is disabled |
1035 | if ($CFG->messaging) { |
1036 | $messagehtml .= '<hr /><p><a href="'.$CFG->wwwroot.'/message/index.php?popup=1">'.$tagline.'</a></p>'; |
1037 | } |
405f01ee |
1038 | } else { |
1039 | $messagehtml = NULL; |
1040 | } |
1041 | |
54bf5de8 |
1042 | if (!empty($preference->message_emailaddress)) { |
1043 | $userto->email = $preference->message_emailaddress; // Use custom messaging address |
1044 | } |
576ad290 |
1045 | |
3aa3cf3d |
1046 | if (email_to_user($userto, $userfrom, $messagesubject, $messagetext, $messagehtml)) { |
1047 | $CFG->messagewasjustemailed = true; |
1048 | } |
576ad290 |
1049 | |
4603b8d6 |
1050 | sleep(3); |
405f01ee |
1051 | } |
1052 | } |
1053 | |
2973ec18 |
1054 | return $savemessage->id; |
405f01ee |
1055 | } |
e8e2d7f1 |
1056 | |
c135a425 |
1057 | |
531e58f1 |
1058 | /* |
c135a425 |
1059 | * Returns a list of all user ids who have used messaging in the site |
1060 | * This was the simple way to code the SQL ... is it going to blow up |
1061 | * on large datasets? |
1062 | */ |
1063 | function message_get_participants() { |
1064 | |
1065 | global $CFG; |
1066 | |
7686b6b1 |
1067 | return get_records_sql("SELECT useridfrom as id,1 FROM {$CFG->prefix}message |
1068 | UNION SELECT useridto as id,1 FROM {$CFG->prefix}message |
1069 | UNION SELECT useridfrom as id,1 FROM {$CFG->prefix}message_read |
1070 | UNION SELECT useridto as id,1 FROM {$CFG->prefix}message_read |
1071 | UNION SELECT userid as id,1 FROM {$CFG->prefix}message_contacts |
1072 | UNION SELECT contactid as id,1 from {$CFG->prefix}message_contacts"); |
c135a425 |
1073 | } |
1074 | |
172186b8 |
1075 | ?> |