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