3d143595 |
1 | <?php |
2 | ///// library functions for messaging %Id% |
172186b8 |
3 | |
3b120e46 |
4 | require_once($CFG->libdir.'/eventslib.php'); |
5 | |
e8e2d7f1 |
6 | |
7 | define ('MESSAGE_SHORTLENGTH', 300); |
fd7006f6 |
8 | define ('MESSAGE_WINDOW', true); // We are in a message window (so don't pop up a new one!) |
9 | |
05753d2b |
10 | if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds |
2a38a6be |
11 | $CFG->message_contacts_refresh = 60; |
fd7006f6 |
12 | } |
5d6b319b |
13 | if (!isset($CFG->message_chat_refresh)) { // Look for new comments every 5 seconds |
14 | $CFG->message_chat_refresh = 5; |
fd7006f6 |
15 | } |
f520438c |
16 | if (!isset($CFG->message_offline_time)) { |
17 | $CFG->message_offline_time = 300; |
18 | } |
172186b8 |
19 | |
20 | |
21 | function message_print_contacts() { |
7cb1a1ad |
22 | global $USER, $CFG, $DB, $PAGE, $OUTPUT; |
e8e2d7f1 |
23 | |
24 | $timetoshowusers = 300; //Seconds default |
25 | if (isset($CFG->block_online_users_timetosee)) { |
26 | $timetoshowusers = $CFG->block_online_users_timetosee * 60; |
27 | } |
e8e2d7f1 |
28 | |
f46b6587 |
29 | // time which a user is counting as being active since |
30 | $timefrom = time()-$timetoshowusers; |
531e58f1 |
31 | |
f46b6587 |
32 | // people in our contactlist who are online |
33 | $onlinecontacts = array(); |
34 | // people in our contactlist who are offline |
35 | $offlinecontacts = array(); |
36 | // people who are not in our contactlist but have sent us a message |
37 | $strangers = array(); |
38 | |
39 | |
40 | // get all in our contactlist who are not blocked in our contact list |
41 | // and count messages we have waiting from each of them |
42 | $contactsql = "SELECT u.id, u.firstname, u.lastname, u.picture, |
43 | u.imagealt, u.lastaccess, count(m.id) as messagecount |
fd1cb1e8 |
44 | FROM {message_contacts} mc |
45 | JOIN {user} u ON u.id = mc.contactid |
46 | LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ? |
47 | WHERE mc.userid = ? AND mc.blocked = 0 |
48 | GROUP BY u.id, u.firstname, u.lastname, u.picture, |
49 | u.imagealt, u.lastaccess |
bbe973da |
50 | ORDER BY u.firstname ASC"; |
fd1cb1e8 |
51 | |
52 | if ($rs = $DB->get_recordset_sql($contactsql, array($USER->id, $USER->id))){ |
53 | foreach($rs as $rd){ |
f46b6587 |
54 | |
55 | if($rd->lastaccess >= $timefrom){ |
56 | // they have been active recently, so are counted online |
57 | $onlinecontacts[] = $rd; |
58 | }else{ |
59 | $offlinecontacts[] = $rd; |
e8e2d7f1 |
60 | } |
61 | } |
f46b6587 |
62 | unset($rd); |
fd1cb1e8 |
63 | $rs->close(); |
f46b6587 |
64 | } |
65 | |
66 | |
67 | // get messages from anyone who isn't in our contact list and count the number |
68 | // of messages we have from each of them |
69 | $strangersql = "SELECT u.id, u.firstname, u.lastname, u.picture, |
70 | u.imagealt, u.lastaccess, count(m.id) as messagecount |
fd1cb1e8 |
71 | FROM {message} m |
72 | JOIN {user} u ON u.id = m.useridfrom |
73 | LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto |
74 | WHERE mc.id IS NULL AND m.useridto = ? |
75 | GROUP BY u.id, u.firstname, u.lastname, u.picture, |
76 | u.imagealt, u.lastaccess |
bbe973da |
77 | ORDER BY u.firstname ASC"; |
fd1cb1e8 |
78 | |
79 | if($rs = $DB->get_recordset_sql($strangersql, array($USER->id))){ |
80 | foreach($rs as $rd){ |
f46b6587 |
81 | $strangers[] = $rd; |
82 | } |
83 | unset($rd); |
fd1cb1e8 |
84 | $rs->close(); |
e8e2d7f1 |
85 | } |
86 | |
f46b6587 |
87 | $countonlinecontacts = count($onlinecontacts); |
88 | $countofflinecontacts = count($offlinecontacts); |
89 | $countstrangers = count($strangers); |
90 | |
a1157dda |
91 | if ($countonlinecontacts + $countofflinecontacts == 0) { |
669be60c |
92 | echo '<div class="heading">'; |
65ef518b |
93 | print_string('contactlistempty', 'message'); |
a1157dda |
94 | echo '</div>'; |
669be60c |
95 | echo '<div class="note">'; |
65ef518b |
96 | print_string('addsomecontacts', 'message', $CFG->wwwroot.'/message/index.php?tab=search'); |
a1157dda |
97 | echo '</div>'; |
65ef518b |
98 | } |
99 | |
04b687e8 |
100 | echo '<table id="message_contacts" class="boxaligncenter" cellspacing="2" cellpadding="0" border="0">'; |
531e58f1 |
101 | |
f46b6587 |
102 | if($countonlinecontacts) { |
103 | /// print out list of online contacts |
531e58f1 |
104 | |
f46b6587 |
105 | echo '<tr><td colspan="3" class="heading">'; |
106 | echo get_string('onlinecontacts', 'message', $countonlinecontacts); |
107 | echo '</td></tr>'; |
531e58f1 |
108 | |
f46b6587 |
109 | foreach ($onlinecontacts as $contact) { |
110 | message_print_contactlist_user($contact); |
65ef518b |
111 | } |
f46b6587 |
112 | } |
113 | echo '<tr><td colspan="3"> </td></tr>'; |
531e58f1 |
114 | |
f46b6587 |
115 | if ($countofflinecontacts) { |
116 | /// print out list of offline contacts |
531e58f1 |
117 | |
f46b6587 |
118 | echo '<tr><td colspan="3" class="heading">'; |
119 | echo get_string('offlinecontacts', 'message', $countofflinecontacts); |
120 | echo '</td></tr>'; |
576ad290 |
121 | |
f46b6587 |
122 | foreach ($offlinecontacts as $contact) { |
123 | message_print_contactlist_user($contact); |
65ef518b |
124 | } |
f46b6587 |
125 | echo '<tr><td colspan="3"> </td></tr>'; |
126 | } |
65ef518b |
127 | |
f46b6587 |
128 | /// print out list of incoming contacts |
129 | if ($countstrangers) { |
130 | echo '<tr><td colspan="3" class="heading">'; |
131 | echo get_string('incomingcontacts', 'message', $countstrangers); |
132 | echo '</td></tr>'; |
531e58f1 |
133 | |
f46b6587 |
134 | foreach ($strangers as $stranger) { |
135 | message_print_contactlist_user($stranger, false); |
669be60c |
136 | } |
f46b6587 |
137 | } |
531e58f1 |
138 | |
f46b6587 |
139 | echo '</table>'; |
65ef518b |
140 | |
f46b6587 |
141 | if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help |
142 | echo '<div class="note">('; |
143 | print_string('addsomecontactsincoming', 'message'); |
144 | echo ')</div>'; |
65ef518b |
145 | } |
b9ee0638 |
146 | |
531e58f1 |
147 | echo '<br />'; |
a9d9b46a |
148 | |
531e58f1 |
149 | $autorefresh = '<p align="center" class="note">'.get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh).'</p>'; |
4702be4e |
150 | $autorefresh = addslashes_js($autorefresh); // js escaping |
531e58f1 |
151 | |
a1b53dcf |
152 | $PAGE->requires->js('message/message.js'); |
153 | $PAGE->requires->js_function_call('refresh_page', Array(60*1000, $PAGE->url->out())); |
154 | |
7cb1a1ad |
155 | echo $OUTPUT->container_start('messagejsautorefresh note center'); |
a1b53dcf |
156 | echo get_string('pagerefreshes', 'message', $CFG->message_contacts_refresh); |
7cb1a1ad |
157 | echo $OUTPUT->container_end(); |
a1b53dcf |
158 | |
7cb1a1ad |
159 | echo $OUTPUT->container_start('messagejsmanualrefresh aligncenter'); |
160 | echo $OUTPUT->button(html_form::make_button('index.php', false, get_string('refresh'))); |
161 | echo $OUTPUT->container_end(); |
e8e2d7f1 |
162 | } |
163 | |
164 | |
e8e2d7f1 |
165 | /// $messagearray is an array of objects |
166 | /// $field is a valid property of object |
167 | /// $value is the value $field should equal to be counted |
168 | /// if $field is empty then return count of the whole array |
169 | /// if $field is non-existent then return 0; |
170 | function message_count_messages($messagearray, $field='', $value='') { |
171 | if (!is_array($messagearray)) return 0; |
172 | if ($field == '' or empty($messagearray)) return count($messagearray); |
531e58f1 |
173 | |
e8e2d7f1 |
174 | $count = 0; |
175 | foreach ($messagearray as $message) { |
176 | $count += ($message->$field == $value) ? 1 : 0; |
177 | } |
178 | return $count; |
172186b8 |
179 | } |
180 | |
e8e2d7f1 |
181 | |
172186b8 |
182 | function message_print_search() { |
9ae3253e |
183 | global $USER; |
531e58f1 |
184 | |
294ce987 |
185 | if ($frm = data_submitted()) { |
531e58f1 |
186 | |
172186b8 |
187 | message_print_search_results($frm); |
531e58f1 |
188 | |
172186b8 |
189 | } else { |
1ac41751 |
190 | /* |
531e58f1 |
191 | /// unfinished buggy code disabled in search.html anyway |
d76a5a7f |
192 | // find all courses this use has readallmessages capabilities in |
193 | if ($teachers = get_user_capability_course('moodle/site:readallmessages')) { |
172186b8 |
194 | $courses = get_courses('all', 'c.sortorder ASC', 'c.id, c.shortname'); |
195 | $cs = '<select name="courseselect">'; |
196 | foreach ($teachers as $tcourse) { |
d76a5a7f |
197 | $cs .= "<option value=\"$tcourse->course\">".$courses[$tcourse->id]->shortname."</option>\n"; |
172186b8 |
198 | } |
199 | $cs .= '</select>'; |
200 | } |
1ac41751 |
201 | */ |
172186b8 |
202 | include('search.html'); |
203 | } |
204 | } |
205 | |
206 | function message_print_settings() { |
54b16692 |
207 | global $USER, $OUTPUT; |
531e58f1 |
208 | |
d6b4fed0 |
209 | if ($frm = data_submitted() and confirm_sesskey()) { |
acef58f4 |
210 | |
e8e2d7f1 |
211 | $pref = array(); |
212 | $pref['message_showmessagewindow'] = (isset($frm->showmessagewindow)) ? '1' : '0'; |
213 | $pref['message_beepnewmessage'] = (isset($frm->beepnewmessage)) ? '1' : '0'; |
3f85157b |
214 | $pref['message_blocknoncontacts'] = (isset($frm->blocknoncontacts)) ? '1' : '0'; |
e49617af |
215 | $pref['message_usehtmleditor'] = (isset($frm->usehtmleditor)) ? '1' : '0'; |
531e58f1 |
216 | $pref['message_noframesjs'] = (isset($frm->noframesjs)) ? '1' : '0'; |
e8e2d7f1 |
217 | $pref['message_emailmessages'] = (isset($frm->emailmessages)) ? '1' : '0'; |
acef58f4 |
218 | $pref['message_emailtimenosee'] = ((int)$frm->emailtimenosee > 0) ? (int)$frm->emailtimenosee : '10'; |
e8e2d7f1 |
219 | $pref['message_emailaddress'] = (!empty($frm->emailaddress)) ? $frm->emailaddress : $USER->email; |
220 | $pref['message_emailformat'] = (isset($frm->emailformat)) ? $frm->emailformat : FORMAT_PLAIN; |
e8e2d7f1 |
221 | |
222 | set_user_preferences($pref); |
531e58f1 |
223 | |
9ae3253e |
224 | redirect('index.php', get_string('settingssaved', 'message'), 1); |
e8e2d7f1 |
225 | } |
226 | |
227 | $cbshowmessagewindow = (get_user_preferences('message_showmessagewindow', 1) == '1') ? 'checked="checked"' : ''; |
fd7006f6 |
228 | $cbbeepnewmessage = (get_user_preferences('message_beepnewmessage', 0) == '1') ? 'checked="checked"' : ''; |
3f85157b |
229 | $cbblocknoncontacts = (get_user_preferences('message_blocknoncontacts', 0) == '1') ? 'checked="checked"' : ''; |
9aa959f2 |
230 | $cbusehtmleditor = (get_user_preferences('message_usehtmleditor', 0) == '1') ? 'checked="checked"' : ''; |
531e58f1 |
231 | $cbnoframesjs = (get_user_preferences('message_noframesjs', 0) == '1') ? 'checked="checked"' : ''; |
e8e2d7f1 |
232 | $cbemailmessages = (get_user_preferences('message_emailmessages', 1) == '1') ? 'checked="checked"' : ''; |
233 | $txemailaddress = get_user_preferences('message_emailaddress', $USER->email); |
234 | $txemailtimenosee = get_user_preferences('message_emailtimenosee', 10); |
54b16692 |
235 | $format_select = $OUTPUT->select(html_select::make( array(FORMAT_PLAIN => get_string('formatplain'), |
e8e2d7f1 |
236 | FORMAT_HTML => get_string('formathtml')), |
237 | 'emailformat', |
54b16692 |
238 | get_user_preferences('message_emailformat', FORMAT_PLAIN))); |
531e58f1 |
239 | |
e8e2d7f1 |
240 | include('settings.html'); |
241 | } |
242 | |
243 | |
244 | |
245 | function message_add_contact($contactid, $blocked=0) { |
fd1cb1e8 |
246 | global $USER, $DB; |
531e58f1 |
247 | |
fd1cb1e8 |
248 | if (!$DB->record_exists('user', array('id'=>$contactid))) { // invalid userid |
e8e2d7f1 |
249 | return false; |
250 | } |
531e58f1 |
251 | |
fd1cb1e8 |
252 | if (($contact = $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid))) !== false) { |
e8e2d7f1 |
253 | /// record already exists - we may be changing blocking status |
531e58f1 |
254 | |
e8e2d7f1 |
255 | if ($contact->blocked !== $blocked) { |
256 | /// change to blocking status |
257 | $contact->blocked = $blocked; |
fd1cb1e8 |
258 | return $DB->update_record('message_contacts', $contact); |
e8e2d7f1 |
259 | } else { |
260 | /// no changes to blocking status |
261 | return true; |
262 | } |
531e58f1 |
263 | |
172186b8 |
264 | } else { |
e8e2d7f1 |
265 | /// new contact record |
266 | unset($contact); |
267 | $contact->userid = $USER->id; |
268 | $contact->contactid = $contactid; |
269 | $contact->blocked = $blocked; |
fd1cb1e8 |
270 | return $DB->insert_record('message_contacts', $contact, false); |
172186b8 |
271 | } |
272 | } |
273 | |
e8e2d7f1 |
274 | function message_remove_contact($contactid) { |
fd1cb1e8 |
275 | global $USER, $DB; |
276 | return $DB->delete_records('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid)); |
e8e2d7f1 |
277 | } |
278 | |
279 | function message_unblock_contact($contactid) { |
fd1cb1e8 |
280 | global $USER, $DB; |
281 | return $DB->delete_records('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid)); |
e8e2d7f1 |
282 | } |
283 | |
284 | function message_block_contact($contactid) { |
285 | return message_add_contact($contactid, 1); |
286 | } |
287 | |
288 | function message_get_contact($contactid) { |
fd1cb1e8 |
289 | global $USER, $DB; |
290 | return $DB->get_record('message_contacts', array('userid'=>$USER->id, 'contactid'=>$contactid)); |
e8e2d7f1 |
291 | } |
531e58f1 |
292 | |
e8e2d7f1 |
293 | |
294 | |
172186b8 |
295 | function message_print_search_results($frm) { |
aa9a6867 |
296 | global $USER, $CFG, $DB, $OUTPUT; |
e8e2d7f1 |
297 | |
85db96c5 |
298 | echo '<div class="mdl-align">'; |
e8e2d7f1 |
299 | |
300 | /// search for person |
301 | if (!empty($frm->personsubmit) and !empty($frm->name)) { |
531e58f1 |
302 | |
b71d4dd2 |
303 | if (optional_param('mycourses', 0, PARAM_BOOL)) { |
e8e2d7f1 |
304 | $users = array(); |
305 | $mycourses = get_my_courses($USER->id); |
306 | foreach ($mycourses as $mycourse) { |
307 | if (is_array($susers = message_search_users($mycourse->id, $frm->name))) { |
308 | foreach ($susers as $suser) $users[$suser->id] = $suser; |
309 | } |
310 | } |
311 | } else { |
312 | $users = message_search_users(SITEID, $frm->name); |
313 | } |
314 | |
315 | if (!empty($users)) { |
316 | echo '<strong>'.get_string('userssearchresults', 'message', count($users)).'</strong>'; |
317 | echo '<table class="message_users">'; |
318 | foreach ($users as $user) { |
531e58f1 |
319 | |
7390832c |
320 | if ( $user->contactlistid ) { |
321 | if ($user->blocked == 0) { /// not blocked |
082864f9 |
322 | $strcontact = message_contact_link($user->id, 'remove', true); |
323 | $strblock = message_contact_link($user->id, 'block', true); |
e8e2d7f1 |
324 | } else { // blocked |
082864f9 |
325 | $strcontact = message_contact_link($user->id, 'add', true); |
326 | $strblock = message_contact_link($user->id, 'unblock', true); |
e8e2d7f1 |
327 | } |
e8e2d7f1 |
328 | } else { |
082864f9 |
329 | $strcontact = message_contact_link($user->id, 'add', true); |
330 | $strblock = message_contact_link($user->id, 'block', true); |
e8e2d7f1 |
331 | } |
f520438c |
332 | $strhistory = message_history_link($user->id, 0, true, '', '', 'icon'); |
531e58f1 |
333 | |
669be60c |
334 | echo '<tr><td class="pix">'; |
7cb1a1ad |
335 | $userpic = moodle_user_picture::make($user, SITEID); |
336 | $userpic->size = 20; |
337 | $userpic->link = true; |
338 | echo $OUTPUT->user_picture($userpic); |
e8e2d7f1 |
339 | echo '</td>'; |
669be60c |
340 | echo '<td class="contact">'; |
40a26286 |
341 | $popupoptions = array( |
342 | 'height' => 500, |
343 | 'width' => 500, |
344 | 'menubar' => false, |
345 | 'location' => false, |
346 | 'status' => true, |
347 | 'scrollbars' => true, |
348 | 'resizable' => true); |
349 | |
350 | $link = html_link::make("/message/discussion.php?id=$user->id", fullname($user)); |
351 | $link->add_action(new popup_action('click', $link->url, "message_$user->id", $popupoptions)); |
352 | $link->title = get_string('sendmessageto', 'message', fullname($user)); |
353 | echo $OUTPUT->link($link); |
576ad290 |
354 | |
e8e2d7f1 |
355 | echo '</td>'; |
531e58f1 |
356 | |
669be60c |
357 | echo '<td class="link">'.$strcontact.'</td>'; |
358 | echo '<td class="link">'.$strblock.'</td>'; |
359 | echo '<td class="link">'.$strhistory.'</td>'; |
e8e2d7f1 |
360 | echo '</tr>'; |
361 | } |
362 | echo '</table>'; |
531e58f1 |
363 | |
e8e2d7f1 |
364 | } else { |
aa9a6867 |
365 | echo $OUTPUT->notification(get_string('nosearchresults', 'message')); |
e8e2d7f1 |
366 | } |
531e58f1 |
367 | |
368 | |
e8e2d7f1 |
369 | /// search messages for keywords |
ff49c389 |
370 | } else if (!empty($frm->keywordssubmit)) { |
38c6a928 |
371 | $keywordstring = clean_text(trim($frm->keywords)); |
ff49c389 |
372 | if ($keywordstring) { |
373 | $keywords = explode(' ', $keywordstring); |
374 | } else { |
375 | $keywords = array(); |
376 | } |
e8e2d7f1 |
377 | $tome = false; |
378 | $fromme = false; |
379 | $courseid = 'none'; |
380 | |
381 | switch ($frm->keywordsoption) { |
382 | case 'tome': |
383 | $tome = true; |
384 | break; |
385 | case 'fromme': |
386 | $fromme = true; |
387 | break; |
388 | case 'allmine': |
389 | $tome = true; |
390 | $fromme = true; |
391 | break; |
392 | case 'allusers': |
393 | $courseid = SITEID; |
394 | break; |
395 | case 'courseusers': |
396 | $courseid = $frm->courseid; |
397 | break; |
398 | default: |
399 | $tome = true; |
400 | $fromme = true; |
401 | } |
402 | |
403 | if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { |
531e58f1 |
404 | |
e8e2d7f1 |
405 | /// get a list of contacts |
fd1cb1e8 |
406 | if (($contacts = $DB->get_records('message_contacts', array('userid'=>$USER->id), '', 'contactid, blocked') ) === false) { |
082864f9 |
407 | $contacts = array(); |
408 | } |
e8e2d7f1 |
409 | |
082864f9 |
410 | /// print heading with number of results |
669be60c |
411 | echo '<p class="heading">'.get_string('keywordssearchresults', 'message', count($messages)).' ("'.s($keywordstring).'")</p>'; |
082864f9 |
412 | |
413 | /// print table headings |
669be60c |
414 | echo '<table class="searchresults" cellspacing="0">'; |
082864f9 |
415 | echo '<tr>'; |
fd7006f6 |
416 | echo '<td><strong>'.get_string('from').'</strong></td>'; |
417 | echo '<td><strong>'.get_string('to').'</strong></td>'; |
418 | echo '<td><strong>'.get_string('message', 'message').'</strong></td>'; |
419 | echo '<td><strong>'.get_string('timesent', 'message').'</strong></td>'; |
082864f9 |
420 | echo "</tr>\n"; |
421 | |
422 | $blockedcount = 0; |
54d8f804 |
423 | $dateformat = get_string('strftimedatetimeshort'); |
38c6a928 |
424 | $strcontext = get_string('context', 'message'); |
e8e2d7f1 |
425 | foreach ($messages as $message) { |
082864f9 |
426 | |
427 | /// ignore messages to and from blocked users unless $frm->includeblocked is set |
b71d4dd2 |
428 | if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( |
082864f9 |
429 | ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or |
430 | ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) |
431 | ) |
432 | ) { |
433 | $blockedcount ++; |
e8e2d7f1 |
434 | continue; |
435 | } |
531e58f1 |
436 | |
082864f9 |
437 | /// load up user to record |
438 | if ($message->useridto !== $USER->id) { |
fd1cb1e8 |
439 | $userto = $DB->get_record('user', array('id'=>$message->useridto)); |
531e58f1 |
440 | $tocontact = (array_key_exists($message->useridto, $contacts) and |
082864f9 |
441 | ($contacts[$message->useridto]->blocked == 0) ); |
531e58f1 |
442 | $toblocked = (array_key_exists($message->useridto, $contacts) and |
082864f9 |
443 | ($contacts[$message->useridto]->blocked == 1) ); |
444 | } else { |
445 | $userto = false; |
446 | $tocontact = false; |
447 | $toblocked = false; |
448 | } |
531e58f1 |
449 | |
082864f9 |
450 | /// load up user from record |
451 | if ($message->useridfrom !== $USER->id) { |
fd1cb1e8 |
452 | $userfrom = $DB->get_record('user', array('id'=>$message->useridfrom)); |
531e58f1 |
453 | $fromcontact = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 |
454 | ($contacts[$message->useridfrom]->blocked == 0) ); |
531e58f1 |
455 | $fromblocked = (array_key_exists($message->useridfrom, $contacts) and |
082864f9 |
456 | ($contacts[$message->useridfrom]->blocked == 1) ); |
457 | } else { |
458 | $userfrom = false; |
459 | $fromcontact = false; |
460 | $fromblocked = false; |
461 | } |
462 | |
e520509a |
463 | /// find date string for this message |
464 | $date = usergetdate($message->timecreated); |
465 | $datestring = $date['year'].$date['mon'].$date['mday']; |
466 | |
082864f9 |
467 | /// print out message row |
468 | echo '<tr valign="top">'; |
669be60c |
469 | echo '<td class="contact">'; |
b9ee0638 |
470 | message_print_user($userfrom, $fromcontact, $fromblocked); |
e8e2d7f1 |
471 | echo '</td>'; |
669be60c |
472 | echo '<td class="contact">'; |
b9ee0638 |
473 | message_print_user($userto, $tocontact, $toblocked); |
e8e2d7f1 |
474 | echo '</td>'; |
3d143595 |
475 | echo '<td class="summary">'.message_get_fragment($message->fullmessage, $keywords); |
669be60c |
476 | echo '<br /><div class="link">'; |
531e58f1 |
477 | message_history_link($message->useridto, $message->useridfrom, false, |
2973ec18 |
478 | $keywordstring, 'm'.$message->id, $strcontext); |
f520438c |
479 | echo '</div>'; |
e520509a |
480 | echo '</td>'; |
669be60c |
481 | echo '<td class="date">'.userdate($message->timecreated, $dateformat).'</td>'; |
082864f9 |
482 | echo "</tr>\n"; |
483 | } |
531e58f1 |
484 | |
e8e2d7f1 |
485 | |
e520509a |
486 | if ($blockedcount > 0) { |
487 | echo '<tr><td colspan="4" align="center">'.get_string('blockedmessages', 'message', $blockedcount).'</td></tr>'; |
488 | } |
e8e2d7f1 |
489 | echo '</table>'; |
531e58f1 |
490 | |
e8e2d7f1 |
491 | } else { |
aa9a6867 |
492 | echo $OUTPUT->notification(get_string('nosearchresults', 'message')); |
e8e2d7f1 |
493 | } |
494 | |
495 | |
496 | /// what the ????, probably an empty search string, duh! |
497 | } else { |
aa9a6867 |
498 | echo $OUTPUT->notification(get_string('emptysearchstring', 'message')); |
e8e2d7f1 |
499 | } |
500 | |
082864f9 |
501 | echo '<br />'; |
7cb1a1ad |
502 | echo $OUTPUT->button(html_form::make_button('index.php', array( 'tab' => 'search'), get_string('newsearch', 'message'))); |
e8e2d7f1 |
503 | |
504 | echo '</div>'; |
505 | } |
506 | |
507 | |
082864f9 |
508 | function message_print_user ($user=false, $iscontact=false, $isblocked=false) { |
7cb1a1ad |
509 | global $USER, $OUTPUT; |
510 | $userpic = moodle_user_picture::make($USER, SITEID); |
511 | $userpic->size = 20; |
512 | $userpic->link = true; |
513 | |
082864f9 |
514 | if ($user === false) { |
7cb1a1ad |
515 | echo $OUTPUT->user_picture($userpic); |
082864f9 |
516 | } else { |
7cb1a1ad |
517 | echo $OUTPUT->user_picture($userpic); |
e520509a |
518 | echo ' '; |
082864f9 |
519 | if ($iscontact) { |
520 | message_contact_link($user->id, 'remove'); |
521 | } else { |
522 | message_contact_link($user->id, 'add'); |
523 | } |
524 | echo ' '; |
525 | if ($isblocked) { |
526 | message_contact_link($user->id, 'unblock'); |
527 | } else { |
528 | message_contact_link($user->id, 'block'); |
529 | } |
e520509a |
530 | echo '<br />'; |
576ad290 |
531 | |
40a26286 |
532 | $popupoptions = array( |
533 | 'height' => 500, |
534 | 'width' => 500, |
535 | 'menubar' => false, |
536 | 'location' => false, |
537 | 'status' => true, |
538 | 'scrollbars' => true, |
539 | 'resizable' => true); |
540 | |
541 | $link = html_link::make("/message/discussion.php?id=$user->id", fullname($user)); |
542 | $link->add_action(new popup_action('click', $link->url, "message_$user->id", $popupoptions)); |
543 | $link->title = get_string('sendmessageto', 'message', fullname($user)); |
544 | echo $OUTPUT->link($link); |
545 | |
082864f9 |
546 | } |
547 | } |
548 | |
549 | |
550 | /// linktype can be: add, remove, block, unblock |
5d6b319b |
551 | function message_contact_link($userid, $linktype='add', $return=false, $script="index.php?tab=contacts", $text=false) { |
e520509a |
552 | global $USER, $CFG; |
553 | |
554 | static $str; |
555 | |
556 | if (empty($str->blockcontact)) { |
557 | $str->blockcontact = get_string('blockcontact', 'message'); |
558 | $str->unblockcontact = get_string('unblockcontact', 'message'); |
559 | $str->removecontact = get_string('removecontact', 'message'); |
560 | $str->addcontact = get_string('addcontact', 'message'); |
561 | } |
562 | |
5d6b319b |
563 | $command = $linktype.'contact'; |
564 | $string = $str->{$command}; |
d53f1cdb |
565 | $alttext = $text ? '' : $string; |
5d6b319b |
566 | $text = $text ? ' '.$string : ''; |
567 | |
082864f9 |
568 | switch ($linktype) { |
569 | case 'block': |
ddedf979 |
570 | $icon = 't/go'; |
082864f9 |
571 | break; |
572 | case 'unblock': |
ddedf979 |
573 | $icon = 't/stop'; |
082864f9 |
574 | break; |
575 | case 'remove': |
ddedf979 |
576 | $icon = 't/user'; |
082864f9 |
577 | break; |
578 | case 'add': |
579 | default: |
ddedf979 |
580 | $icon = 't/usernot'; |
082864f9 |
581 | } |
5d6b319b |
582 | |
583 | $output = '<span class="'.$linktype.'">'. |
584 | '<a href="'.$script.'&'.$command.'='.$userid. |
0d3bb48d |
585 | '&sesskey='.sesskey().'" title="'.s($string).'">'. |
ddedf979 |
586 | '<img src="'.$OUTPUT->old_icon_url($icon).'" class="iconsmall" alt="'.s($alttext).'" />'. |
5d6b319b |
587 | $text.'</a></span>'; |
588 | |
e520509a |
589 | if ($return) { |
590 | return $output; |
082864f9 |
591 | } else { |
e520509a |
592 | echo $output; |
082864f9 |
593 | return true; |
594 | } |
595 | } |
596 | |
38c6a928 |
597 | function message_history_link($userid1, $userid2=0, $returnstr=false, $keywords='', $position='', $linktext='') { |
f2a1963c |
598 | global $USER, $CFG, $OUTPUT; |
62119d65 |
599 | |
830d0af6 |
600 | static $strmessagehistory; |
601 | |
602 | if (empty($strmessagehistory)) { |
603 | $strmessagehistory = get_string('messagehistory', 'message'); |
604 | } |
2514081c |
605 | |
62119d65 |
606 | if (!$userid2) { |
607 | $userid2 = $USER->id; |
608 | } |
e520509a |
609 | if ($position) { |
610 | $position = "#$position"; |
611 | } |
38c6a928 |
612 | if ($keywords) { |
613 | $keywords = "&search=".urlencode($keywords); |
614 | } |
62119d65 |
615 | |
830d0af6 |
616 | if ($linktext == 'icon') { // Icon only |
f2a1963c |
617 | $fulllink = '<img src="'.$OUTPUT->old_icon_url('t/log') . '" class="iconsmall" alt="'.$strmessagehistory.'" />'; |
830d0af6 |
618 | } else if ($linktext == 'both') { // Icon and standard name |
f2a1963c |
619 | $fulllink = '<img src="'.$OUTPUT->old_icon_url('t/log') . '" class="iconsmall" alt="" />'; |
830d0af6 |
620 | $fulllink .= ' '.$strmessagehistory; |
621 | } else if ($linktext) { // Custom name |
622 | $fulllink = $linktext; |
623 | } else { // Standard name only |
624 | $fulllink = $strmessagehistory; |
fd7006f6 |
625 | } |
626 | |
40a26286 |
627 | $popupoptions = array( |
628 | 'height' => 500, |
629 | 'width' => 500, |
630 | 'menubar' => false, |
631 | 'location' => false, |
632 | 'status' => true, |
633 | 'scrollbars' => true, |
634 | 'resizable' => true); |
635 | |
636 | $link = html_link::make("/message/history.php?user1=$userid1&user2=$userid2$keywords$position", $fulllink); |
637 | $link->add_action(new popup_action('click', $link->url, "message_history_$userid1", $popupoptions)); |
638 | $link->title = $strmessagehistory; |
639 | echo $OUTPUT->link($link); |
62119d65 |
640 | |
5d6b319b |
641 | $str = '<span class="history">'.$str.'</span>'; |
642 | |
62119d65 |
643 | if ($returnstr) { |
644 | return $str; |
645 | } else { |
646 | echo $str; |
647 | return true; |
648 | } |
649 | } |
e8e2d7f1 |
650 | |
651 | |
652 | /** |
653 | * Search through course users |
654 | * |
531e58f1 |
655 | * If $coursid specifies the site course then this function searches |
e8e2d7f1 |
656 | * through all undeleted and confirmed users |
657 | * |
7390832c |
658 | * @uses $CFG, $USER |
e8e2d7f1 |
659 | * @uses SITEID |
660 | * @param int $courseid The course in question. |
661 | * @param string $searchtext ? |
662 | * @param string $sort ? |
531e58f1 |
663 | * @param string $exceptions ? |
e8e2d7f1 |
664 | * @return array An array of {@link $USER} records. |
665 | * @todo Finish documenting this function |
666 | */ |
667 | function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { |
fd1cb1e8 |
668 | global $CFG, $USER, $DB; |
e8e2d7f1 |
669 | |
245ac557 |
670 | $fullname = $DB->sql_fullname(); |
671 | $LIKE = $DB->sql_ilike(); |
e8e2d7f1 |
672 | |
673 | if (!empty($exceptions)) { |
674 | $except = ' AND u.id NOT IN ('. $exceptions .') '; |
675 | } else { |
676 | $except = ''; |
677 | } |
678 | |
679 | if (!empty($sort)) { |
680 | $order = ' ORDER BY '. $sort; |
681 | } else { |
682 | $order = ''; |
683 | } |
684 | |
685 | $select = 'u.deleted = \'0\' AND u.confirmed = \'1\''; |
7390832c |
686 | $fields = 'u.id, u.firstname, u.lastname, u.picture, u.imagealt, mc.id as contactlistid, mc.blocked'; |
e8e2d7f1 |
687 | |
688 | if (!$courseid or $courseid == SITEID) { |
fd1cb1e8 |
689 | $params = array($USER->id, "%$searchtext%"); |
690 | return $DB->get_records_sql("SELECT $fields |
d9eef08a |
691 | FROM {user} u |
fd1cb1e8 |
692 | LEFT JOIN {message_contacts} mc |
693 | ON mc.contactid = u.id AND mc.userid = ? |
694 | WHERE $select |
695 | AND ($fullname $LIKE ?) |
696 | $except |
697 | $order", $params); |
e8e2d7f1 |
698 | } else { |
699 | |
d76a5a7f |
700 | $context = get_context_instance(CONTEXT_COURSE, $courseid); |
701 | $contextlists = get_related_contexts_string($context); |
531e58f1 |
702 | |
d76a5a7f |
703 | // everyone who has a role assignement in this course or higher |
fd1cb1e8 |
704 | $params = array($USER->id, "%$searchtext%"); |
705 | $users = $DB->get_records_sql("SELECT $fields |
706 | FROM {user} u |
707 | JOIN {role_assignments} ra ON ra.userid = u.id |
708 | LEFT JOIN {message_contacts} mc |
709 | ON mc.contactid = u.id AND mc.userid = ? |
710 | WHERE $select |
711 | AND ra.contextid $contextlists |
712 | AND ($fullname $LIKE ?) |
713 | $except |
714 | $order", $params); |
d76a5a7f |
715 | |
716 | return $users; |
e8e2d7f1 |
717 | } |
718 | } |
719 | |
720 | |
721 | |
722 | |
082864f9 |
723 | function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { |
e8e2d7f1 |
724 | /// Returns a list of posts found using an array of search terms |
725 | /// eg word +word -word |
726 | /// |
fd1cb1e8 |
727 | global $CFG, $USER, $DB; |
e8e2d7f1 |
728 | |
082864f9 |
729 | /// If no userid sent then assume current user |
531e58f1 |
730 | if ($userid == 0) $userid = $USER->id; |
082864f9 |
731 | |
6eb7722f |
732 | /// Some differences in SQL syntax |
d9eef08a |
733 | if ($DB->sql_regex_supported()) { |
734 | $REGEXP = $DB->sql_regex(true); |
735 | $NOTREGEXP = $DB->sql_regex(false); |
e8e2d7f1 |
736 | } |
737 | |
d9eef08a |
738 | $LIKE = $DB->sql_ilike(); |
739 | |
740 | $searchcond = array(); |
741 | $params = array(); |
742 | $i = 0; |
e8e2d7f1 |
743 | |
744 | foreach ($searchterms as $searchterm) { |
d9eef08a |
745 | $i++; |
746 | |
747 | $NOT = ''; /// Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle |
748 | |
e8e2d7f1 |
749 | if (strlen($searchterm) < 2) { |
750 | continue; |
751 | } |
6bb4875f |
752 | /// Under Oracle and MSSQL, trim the + and - operators and perform |
753 | /// simpler LIKE search |
d9eef08a |
754 | if (!$DB->sql_regex_supported()) { |
755 | if (substr($searchterm, 0, 1) == '-') { |
756 | $NOT = ' NOT '; |
757 | } |
6bb4875f |
758 | $searchterm = trim($searchterm, '+-'); |
759 | } |
760 | |
e8e2d7f1 |
761 | if (substr($searchterm,0,1) == "+") { |
762 | $searchterm = substr($searchterm,1); |
d9eef08a |
763 | $searchterm = preg_quote($searchterm, '|'); |
3d143595 |
764 | $searchcond[] = "m.fullmessage $REGEXP :ss$i"; |
d9eef08a |
765 | $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; |
766 | |
e8e2d7f1 |
767 | } else if (substr($searchterm,0,1) == "-") { |
768 | $searchterm = substr($searchterm,1); |
d9eef08a |
769 | $searchterm = preg_quote($searchterm, '|'); |
3d143595 |
770 | $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; |
d9eef08a |
771 | $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; |
772 | |
e8e2d7f1 |
773 | } else { |
3d143595 |
774 | $searchcond[] = "m.fullmessage $NOT $LIKE :ss$i"; |
d9eef08a |
775 | $params['ss'.$i] = "%$searchterm%"; |
e8e2d7f1 |
776 | } |
172186b8 |
777 | } |
778 | |
d9eef08a |
779 | if (empty($searchcond)) { |
ff49c389 |
780 | $searchcond = " m.fullmessage $LIKE :ss1"; |
781 | $params['ss1'] = "%"; |
782 | } else { |
783 | $searchcond = implode(" AND ", $searchcond); |
8a51efe9 |
784 | } |
e8e2d7f1 |
785 | |
e8e2d7f1 |
786 | |
082864f9 |
787 | /// There are several possibilities |
788 | /// 1. courseid = SITEID : The admin is searching messages by all users |
789 | /// 2. courseid = ?? : A teacher is searching messages by users in |
790 | /// one of their courses - currently disabled |
791 | /// 3. courseid = none : User is searching their own messages; |
792 | /// a. Messages from user |
793 | /// b. Messages to user |
794 | /// c. Messages to and from user |
795 | |
796 | if ($courseid == SITEID) { /// admin is searching all messages |
3d143595 |
797 | $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a |
798 | FROM {message_read} m |
799 | WHERE $searchcond", $params); |
3d143595 |
800 | $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a |
801 | FROM {message} m |
802 | WHERE $searchcond", $params); |
803 | |
804 | } else if ($courseid !== 'none') { |
082864f9 |
805 | /// This has not been implemented due to security concerns |
d9eef08a |
806 | $m_read = array(); |
807 | $m_unread = array(); |
e8e2d7f1 |
808 | |
082864f9 |
809 | } else { |
531e58f1 |
810 | |
d9eef08a |
811 | if ($fromme and $tome) { |
812 | $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; |
813 | $params['userid1'] = $userid; |
814 | $params['userid2'] = $userid; |
815 | |
816 | } else if ($fromme) { |
817 | $searchcond .= " AND m.useridfrom=:userid"; |
818 | $params['userid'] = $userid; |
531e58f1 |
819 | |
d9eef08a |
820 | } else if ($tome) { |
821 | $searchcond .= " AND m.useridto=:userid"; |
822 | $params['userid'] = $userid; |
823 | } |
531e58f1 |
824 | |
3d143595 |
825 | $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a |
826 | FROM {message_read} m |
827 | WHERE $searchcond", $params); |
3d143595 |
828 | $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated |
d9eef08a |
829 | FROM {message} m |
830 | WHERE $searchcond", $params); |
531e58f1 |
831 | |
e8e2d7f1 |
832 | } |
833 | |
082864f9 |
834 | /// The keys may be duplicated in $m_read and $m_unread so we can't |
835 | /// do a simple concatenation |
836 | $message = array(); |
d9eef08a |
837 | foreach ($m_read as $m) { |
838 | $messages[] = $m; |
839 | } |
840 | foreach ($m_unread as $m) { |
841 | $messages[] = $m; |
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 | |
d9eef08a |
901 | /** |
38c6a928 |
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) { |
fd1cb1e8 |
950 | global $DB; |
951 | |
952 | $messages = $DB->get_records_select('message_read', "(useridto = ? AND useridfrom = ?) OR |
953 | (useridto = ? AND useridfrom = ?)", array($user1->id, $user2->id, $user2->id, $user1->id), |
b9ee0638 |
954 | 'timecreated'); |
fd1cb1e8 |
955 | if ($messages_new = $DB->get_records_select('message', "(useridto = ? AND useridfrom = ?) OR |
956 | (useridto = ? AND useridfrom = ?)", array($user1->id, $user2->id, $user2->id, $user1->id), |
b9ee0638 |
957 | 'timecreated')) { |
958 | foreach ($messages_new as $message) { |
959 | $messages[] = $message; |
960 | } |
961 | } |
962 | return $messages; |
963 | } |
964 | |
030e3e03 |
965 | function message_format_message(&$message, &$user, $format='', $keywords='', $class='other') { |
acd21a2d |
966 | |
967 | static $dateformat; |
968 | |
969 | if (empty($dateformat)) { |
970 | if ($format) { |
971 | $dateformat = $format; |
972 | } else { |
54d8f804 |
973 | $format = get_string('strftimedatetimeshort'); |
acd21a2d |
974 | } |
b9ee0638 |
975 | } |
acd21a2d |
976 | $time = userdate($message->timecreated, $dateformat); |
fd1cb1e8 |
977 | $options = new object(); |
ff6048dd |
978 | $options->para = false; |
3d143595 |
979 | $messagetext = format_text($message->fullmessage, $message->fullmessageformat, $options); |
38c6a928 |
980 | if ($keywords) { |
981 | $messagetext = highlight($keywords, $messagetext); |
982 | } |
030e3e03 |
983 | 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 |
984 | } |
e8e2d7f1 |
985 | |
fd1cb1e8 |
986 | /** |
405f01ee |
987 | * Inserts a message into the database, but also forwards it |
988 | * via other means if appropriate. |
989 | */ |
990 | function message_post_message($userfrom, $userto, $message, $format, $messagetype) { |
fd1cb1e8 |
991 | global $CFG, $SITE, $USER, $DB; |
3b120e46 |
992 | |
993 | $eventdata = new object(); |
1c50df9f |
994 | $eventdata->component = 'message'; |
995 | $eventdata->name = 'instantmessage'; |
3b120e46 |
996 | $eventdata->userfrom = $userfrom; |
997 | $eventdata->userto = $userto; |
1c50df9f |
998 | $eventdata->subject = "IM"; |
3b120e46 |
999 | $eventdata->fullmessage = $message; |
1000 | $eventdata->fullmessageformat = FORMAT_PLAIN; |
1001 | $eventdata->fullmessagehtml = ''; |
a1b53dcf |
1002 | $eventdata->smallmessage = ''; |
1003 | $eventdata->timecreated = time(); |
1c50df9f |
1004 | return events_trigger('message_send', $eventdata); |
f49edc81 |
1005 | |
405f01ee |
1006 | } |
e8e2d7f1 |
1007 | |
c135a425 |
1008 | |
fd1cb1e8 |
1009 | /** |
c135a425 |
1010 | * Returns a list of all user ids who have used messaging in the site |
1011 | * This was the simple way to code the SQL ... is it going to blow up |
1012 | * on large datasets? |
1013 | */ |
1014 | function message_get_participants() { |
fd1cb1e8 |
1015 | global $CFG, $DB; |
1016 | |
76b0191c |
1017 | return $DB->get_records_sql("SELECT useridfrom as id,1 FROM {message} |
1018 | UNION SELECT useridto as id,1 FROM {message} |
1019 | UNION SELECT useridfrom as id,1 FROM {message_read} |
1020 | UNION SELECT useridto as id,1 FROM {message_read} |
1021 | UNION SELECT userid as id,1 FROM {message_contacts} |
1022 | UNION SELECT contactid as id,1 from {message_contacts}"); |
c135a425 |
1023 | } |
1024 | |
f46b6587 |
1025 | /** |
1026 | * Print a row of contactlist displaying user picture, messages waiting and |
1027 | * block links etc |
7cb1a1ad |
1028 | * @param $contact contact object containing all fields required for $OUTPUT->user_picture() |
f46b6587 |
1029 | * @param $incontactlist is the user a contact of ours? |
1030 | */ |
1031 | function message_print_contactlist_user($contact, $incontactlist = true){ |
40a26286 |
1032 | global $OUTPUT; |
f46b6587 |
1033 | $fullname = fullname($contact); |
1034 | $fullnamelink = $fullname; |
1035 | |
1036 | /// are there any unread messages for this contact? |
1037 | if ($contact->messagecount > 0 ){ |
1038 | $fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>'; |
1039 | } |
1040 | |
1041 | |
1042 | if($incontactlist){ |
1043 | $strcontact = message_contact_link($contact->id, 'remove', true); |
1044 | $strblock = ''; |
1045 | }else{ |
1046 | $strcontact = message_contact_link($contact->id, 'add', true); |
1047 | $strblock = ' '. message_contact_link($contact->id, 'block', true); |
1048 | } |
1049 | |
1050 | $strhistory = message_history_link($contact->id, 0, true, '', '', 'icon'); |
1051 | |
1052 | echo '<tr><td class="pix">'; |
7cb1a1ad |
1053 | $userpic = moodle_user_picture::make($contact, SITEID); |
1054 | $userpic->size = 20; |
1055 | $userpic->link = true; |
1056 | echo $OUTPUT->user_picture($userpic); |
f46b6587 |
1057 | echo '</td>'; |
1058 | echo '<td class="contact">'; |
40a26286 |
1059 | |
1060 | $popupoptions = array( |
1061 | 'height' => 500, |
1062 | 'width' => 500, |
1063 | 'menubar' => false, |
1064 | 'location' => false, |
1065 | 'status' => true, |
1066 | 'scrollbars' => true, |
1067 | 'resizable' => true); |
1068 | |
1069 | $link = html_link::make("/message/discussion.php?id=$contact->id", $fullnamelink); |
1070 | $link->add_action(new popup_action('click', $link->url, "message_$contact->id", $popupoptions)); |
1071 | $link->title = get_string('sendmessageto', 'message', $fullname); |
1072 | echo $OUTPUT->link($link); |
f46b6587 |
1073 | |
1074 | echo '</td>'; |
1075 | echo '<td class="link"> '.$strcontact.$strblock.' '.$strhistory.'</td>'; |
1076 | echo '</tr>'; |
1077 | } |
1078 | |
6bdf4c99 |
1079 | /** |
1080 | * Moves unread messages from message table to message_read for a given from user |
1081 | * @param object $userid User id |
1082 | * @return boolean success |
1083 | */ |
1084 | function message_move_userfrom_unread2read($userid) { |
1085 | |
1086 | global $DB; |
1087 | |
1088 | // move all unread messages from message table to messasge_read |
1089 | if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) { |
1090 | foreach ($messages as $message) { |
1091 | $message->timeread = 0; //the message was never read |
1092 | $messageid = $message->id; |
1093 | unset($message->id); |
1094 | if ($DB->insert_record('message_read', $message)) { |
1095 | $DB->delete_records('message', array('id' => $messageid)); |
1096 | $DB->delete_records('message_working', array('unreadmessageid' => $messageid)); |
1097 | } else { |
1098 | return false; |
1099 | } |
1100 | } |
1101 | } |
1102 | return true; |
1103 | } |
1104 | |
1c50df9f |
1105 | function message_get_popup_messages($destuserid, $fromuserid=NULL){ |
1106 | global $DB; |
1107 | |
1108 | $processor = $DB->get_record('message_processors', array('name' => 'popup')); |
1109 | |
a1b53dcf |
1110 | $messagesproc = $DB->get_records('message_working', array('processorid'=>$processor->id), 'id ASC'); |
1c50df9f |
1111 | |
1112 | //for every message to process check if it's for current user and process |
1113 | $messages = array(); |
1114 | foreach ($messagesproc as $msgp){ |
1115 | $query = array('id'=>$msgp->unreadmessageid, 'useridto'=>$destuserid); |
1116 | if ($fromuserid){ |
1117 | $query['useridfrom'] = $fromuserid; |
1118 | } |
1119 | if ($message = $DB->get_record('message', $query)){ |
1120 | $messages[] = $message; |
1121 | /// Move the entry to the other table |
1122 | $message->timeread = time(); |
1123 | $messageid = $message->id; |
1124 | unset($message->id); |
1125 | |
1126 | //delete what we've processed and check if can move message |
1127 | $DB->delete_records('message_working', array('id' => $msgp->id)); |
1128 | if ( $DB->count_records('message_working', array('unreadmessageid'=>$messageid)) == 0){ |
1129 | if ($DB->insert_record('message_read', $message)) { |
1130 | $DB->delete_records('message', array('id' => $messageid)); |
1131 | } |
1132 | } |
1133 | } |
1134 | } |
1135 | return $messages; |
1136 | } |
1137 | |
bbe973da |
1138 | ?> |