|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once($CFG->libdir.'/eventslib.php'); 00027 00028 define ('MESSAGE_SHORTLENGTH', 300); 00029 00030 //$PAGE isnt set if we're being loaded by cron which doesnt display popups anyway 00031 if (isset($PAGE)) { 00032 //TODO: this is a mega crazy hack - it is not acceptable to call anything when including lib!!! (skodak) 00033 $PAGE->set_popup_notification_allowed(false); // We are in a message window (so don't pop up a new one) 00034 } 00035 00036 define ('MESSAGE_DISCUSSION_WIDTH',600); 00037 define ('MESSAGE_DISCUSSION_HEIGHT',500); 00038 00039 define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history 00040 00041 define('MESSAGE_HISTORY_SHORT',0); 00042 define('MESSAGE_HISTORY_ALL',1); 00043 00044 define('MESSAGE_VIEW_UNREAD_MESSAGES','unread'); 00045 define('MESSAGE_VIEW_RECENT_CONVERSATIONS','recentconversations'); 00046 define('MESSAGE_VIEW_RECENT_NOTIFICATIONS','recentnotifications'); 00047 define('MESSAGE_VIEW_CONTACTS','contacts'); 00048 define('MESSAGE_VIEW_BLOCKED','blockedusers'); 00049 define('MESSAGE_VIEW_COURSE','course_'); 00050 define('MESSAGE_VIEW_SEARCH','search'); 00051 00052 define('MESSAGE_SEARCH_MAX_RESULTS', 200); 00053 00054 define('MESSAGE_CONTACTS_PER_PAGE',10); 00055 define('MESSAGE_MAX_COURSE_NAME_LENGTH', 30); 00056 00067 define('MESSAGE_DEFAULT_LOGGEDIN', 0x01); // 0001 00068 define('MESSAGE_DEFAULT_LOGGEDOFF', 0x02); // 0010 00069 00070 define('MESSAGE_DISALLOWED', 0x04); // 0100 00071 define('MESSAGE_PERMITTED', 0x08); // 1000 00072 define('MESSAGE_FORCED', 0x0c); // 1100 00073 00074 define('MESSAGE_PERMITTED_MASK', 0x0c); // 1100 00075 00079 define('MESSAGE_DEFAULT_PERMITTED', 'permitted'); 00080 00081 //TODO: defaults must be initialised via settings - this is a bad hack! (skodak) 00082 if (!isset($CFG->message_contacts_refresh)) { // Refresh the contacts list every 60 seconds 00083 $CFG->message_contacts_refresh = 60; 00084 } 00085 if (!isset($CFG->message_chat_refresh)) { // Look for new comments every 5 seconds 00086 $CFG->message_chat_refresh = 5; 00087 } 00088 if (!isset($CFG->message_offline_time)) { 00089 $CFG->message_offline_time = 300; 00090 } 00091 00108 function message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showcontactactionlinks, $page=0) { 00109 global $PAGE; 00110 00111 echo html_writer::start_tag('div', array('class' => 'contactselector mdl-align')); 00112 00113 //if 0 unread messages and they've requested unread messages then show contacts 00114 if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { 00115 $viewing = MESSAGE_VIEW_CONTACTS; 00116 } 00117 00118 //if they have no blocked users and they've requested blocked users switch them over to contacts 00119 if (count($blockedusers) == 0 && $viewing == MESSAGE_VIEW_BLOCKED) { 00120 $viewing = MESSAGE_VIEW_CONTACTS; 00121 } 00122 00123 $onlyactivecourses = true; 00124 $courses = enrol_get_users_courses($user1->id, $onlyactivecourses); 00125 $coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them 00126 00127 $strunreadmessages = null; 00128 if ($countunreadtotal>0) { //if there are unread messages 00129 $strunreadmessages = get_string('unreadmessages','message', $countunreadtotal); 00130 } 00131 00132 message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages); 00133 00134 if ($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) { 00135 message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 1, $showcontactactionlinks,$strunreadmessages, $user2); 00136 } else if ($viewing == MESSAGE_VIEW_CONTACTS || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) { 00137 message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 0, $showcontactactionlinks, $strunreadmessages, $user2); 00138 } else if ($viewing == MESSAGE_VIEW_BLOCKED) { 00139 message_print_blocked_users($blockedusers, $PAGE->url, $showcontactactionlinks, null, $user2); 00140 } else if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) { 00141 $courseidtoshow = intval(substr($viewing, 7)); 00142 00143 if (!empty($courseidtoshow) 00144 && array_key_exists($courseidtoshow, $coursecontexts) 00145 && has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) { 00146 00147 message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showcontactactionlinks, null, $page, $user2); 00148 } else { 00149 //shouldn't get here. User trying to access a course they're not in perhaps. 00150 add_to_log(SITEID, 'message', 'view', 'index.php', $viewing); 00151 } 00152 } 00153 00154 echo html_writer::start_tag('form', array('action' => 'index.php','method' => 'GET')); 00155 echo html_writer::start_tag('fieldset'); 00156 $managebuttonclass = 'visible'; 00157 if ($viewing == MESSAGE_VIEW_SEARCH) { 00158 $managebuttonclass = 'hiddenelement'; 00159 } 00160 $strmanagecontacts = get_string('search','message'); 00161 echo html_writer::empty_tag('input', array('type' => 'hidden','name' => 'viewing','value' => MESSAGE_VIEW_SEARCH)); 00162 echo html_writer::empty_tag('input', array('type' => 'submit','value' => $strmanagecontacts,'class' => $managebuttonclass)); 00163 echo html_writer::end_tag('fieldset'); 00164 echo html_writer::end_tag('form'); 00165 00166 echo html_writer::end_tag('div'); 00167 } 00168 00181 function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $page=0, $user2=null) { 00182 global $DB, $USER, $PAGE, $OUTPUT; 00183 00184 if (empty($titletodisplay)) { 00185 $titletodisplay = get_string('participants'); 00186 } 00187 00188 $countparticipants = count_enrolled_users($context); 00189 $participants = get_enrolled_users($context, '', 0, 'u.*', '', $page*MESSAGE_CONTACTS_PER_PAGE, MESSAGE_CONTACTS_PER_PAGE); 00190 00191 $pagingbar = new paging_bar($countparticipants, $page, MESSAGE_CONTACTS_PER_PAGE, $PAGE->url, 'page'); 00192 echo $OUTPUT->render($pagingbar); 00193 00194 echo html_writer::start_tag('table', array('id' => 'message_participants', 'class' => 'boxaligncenter', 'cellspacing' => '2', 'cellpadding' => '0', 'border' => '0')); 00195 00196 echo html_writer::start_tag('tr'); 00197 echo html_writer::tag('td', $titletodisplay, array('colspan' => 3, 'class' => 'heading')); 00198 echo html_writer::end_tag('tr'); 00199 00200 //todo these need to come from somewhere if the course participants list is to show users with unread messages 00201 $iscontact = true; 00202 $isblocked = false; 00203 foreach ($participants as $participant) { 00204 if ($participant->id != $USER->id) { 00205 $participant->messagecount = 0;//todo it would be nice if the course participant could report new messages 00206 message_print_contactlist_user($participant, $iscontact, $isblocked, $contactselecturl, $showactionlinks, $user2); 00207 } 00208 } 00209 00210 echo html_writer::end_tag('table'); 00211 } 00212 00221 function message_get_blocked_users($user1=null, $user2=null) { 00222 global $DB, $USER; 00223 00224 if (empty($user1)) { 00225 $user1 = $USER; 00226 } 00227 00228 if (!empty($user2)) { 00229 $user2->isblocked = false; 00230 } 00231 00232 $blockedusers = array(); 00233 00234 $userfields = user_picture::fields('u', array('lastaccess')); 00235 $blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount 00236 FROM {message_contacts} mc 00237 JOIN {user} u ON u.id = mc.contactid 00238 LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1 00239 WHERE mc.userid = :user1id2 AND mc.blocked = 1 00240 GROUP BY $userfields 00241 ORDER BY u.firstname ASC"; 00242 $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1' => $user1->id, 'user1id2' => $user1->id)); 00243 00244 foreach($rs as $rd) { 00245 $blockedusers[] = $rd; 00246 00247 if (!empty($user2) && $user2->id == $rd->id) { 00248 $user2->isblocked = true; 00249 } 00250 } 00251 $rs->close(); 00252 00253 return $blockedusers; 00254 } 00255 00266 function message_print_blocked_users($blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $user2=null) { 00267 global $DB, $USER; 00268 00269 $countblocked = count($blockedusers); 00270 00271 echo html_writer::start_tag('table', array('id' => 'message_contacts', 'class' => 'boxaligncenter')); 00272 00273 if (!empty($titletodisplay)) { 00274 echo html_writer::start_tag('tr'); 00275 echo html_writer::tag('td', $titletodisplay, array('colspan' => 3, 'class' => 'heading')); 00276 echo html_writer::end_tag('tr'); 00277 } 00278 00279 if ($countblocked) { 00280 echo html_writer::start_tag('tr'); 00281 echo html_writer::tag('td', get_string('blockedusers', 'message', $countblocked), array('colspan' => 3, 'class' => 'heading')); 00282 echo html_writer::end_tag('tr'); 00283 00284 $isuserblocked = true; 00285 $isusercontact = false; 00286 foreach ($blockedusers as $blockeduser) { 00287 message_print_contactlist_user($blockeduser, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); 00288 } 00289 } 00290 00291 echo html_writer::end_tag('table'); 00292 } 00293 00302 function message_get_contacts($user1=null, $user2=null) { 00303 global $DB, $CFG, $USER; 00304 00305 if (empty($user1)) { 00306 $user1 = $USER; 00307 } 00308 00309 if (!empty($user2)) { 00310 $user2->iscontact = false; 00311 } 00312 00313 $timetoshowusers = 300; //Seconds default 00314 if (isset($CFG->block_online_users_timetosee)) { 00315 $timetoshowusers = $CFG->block_online_users_timetosee * 60; 00316 } 00317 00318 // time which a user is counting as being active since 00319 $timefrom = time()-$timetoshowusers; 00320 00321 // people in our contactlist who are online 00322 $onlinecontacts = array(); 00323 // people in our contactlist who are offline 00324 $offlinecontacts = array(); 00325 // people who are not in our contactlist but have sent us a message 00326 $strangers = array(); 00327 00328 $userfields = user_picture::fields('u', array('lastaccess')); 00329 00330 // get all in our contactlist who are not blocked in our contact list 00331 // and count messages we have waiting from each of them 00332 $contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount 00333 FROM {message_contacts} mc 00334 JOIN {user} u ON u.id = mc.contactid 00335 LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ? 00336 WHERE mc.userid = ? AND mc.blocked = 0 00337 GROUP BY $userfields 00338 ORDER BY u.firstname ASC"; 00339 00340 $rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id)); 00341 foreach ($rs as $rd) { 00342 if ($rd->lastaccess >= $timefrom) { 00343 // they have been active recently, so are counted online 00344 $onlinecontacts[] = $rd; 00345 00346 } else { 00347 $offlinecontacts[] = $rd; 00348 } 00349 00350 if (!empty($user2) && $user2->id == $rd->id) { 00351 $user2->iscontact = true; 00352 } 00353 } 00354 $rs->close(); 00355 00356 // get messages from anyone who isn't in our contact list and count the number 00357 // of messages we have from each of them 00358 $strangersql = "SELECT $userfields, count(m.id) as messagecount 00359 FROM {message} m 00360 JOIN {user} u ON u.id = m.useridfrom 00361 LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto 00362 WHERE mc.id IS NULL AND m.useridto = ? 00363 GROUP BY $userfields 00364 ORDER BY u.firstname ASC"; 00365 00366 $rs = $DB->get_recordset_sql($strangersql, array($USER->id)); 00367 foreach ($rs as $rd) { 00368 $strangers[] = $rd; 00369 } 00370 $rs->close(); 00371 00372 return array($onlinecontacts, $offlinecontacts, $strangers); 00373 } 00374 00389 function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null, $user2=null) { 00390 global $CFG, $PAGE, $OUTPUT; 00391 00392 $countonlinecontacts = count($onlinecontacts); 00393 $countofflinecontacts = count($offlinecontacts); 00394 $countstrangers = count($strangers); 00395 $isuserblocked = null; 00396 00397 if ($countonlinecontacts + $countofflinecontacts == 0) { 00398 echo html_writer::tag('div', get_string('contactlistempty', 'message'), array('class' => 'heading')); 00399 } 00400 00401 echo html_writer::start_tag('table', array('id' => 'message_contacts', 'class' => 'boxaligncenter')); 00402 00403 if (!empty($titletodisplay)) { 00404 message_print_heading($titletodisplay); 00405 } 00406 00407 if($countonlinecontacts) { 00409 00410 if (empty($titletodisplay)) { 00411 message_print_heading(get_string('onlinecontacts', 'message', $countonlinecontacts)); 00412 } 00413 00414 $isuserblocked = false; 00415 $isusercontact = true; 00416 foreach ($onlinecontacts as $contact) { 00417 if ($minmessages == 0 || $contact->messagecount >= $minmessages) { 00418 message_print_contactlist_user($contact, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); 00419 } 00420 } 00421 } 00422 00423 if ($countofflinecontacts) { 00425 00426 if (empty($titletodisplay)) { 00427 message_print_heading(get_string('offlinecontacts', 'message', $countofflinecontacts)); 00428 } 00429 00430 $isuserblocked = false; 00431 $isusercontact = true; 00432 foreach ($offlinecontacts as $contact) { 00433 if ($minmessages == 0 || $contact->messagecount >= $minmessages) { 00434 message_print_contactlist_user($contact, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); 00435 } 00436 } 00437 00438 } 00439 00441 if ($countstrangers) { 00442 message_print_heading(get_string('incomingcontacts', 'message', $countstrangers)); 00443 00444 $isuserblocked = false; 00445 $isusercontact = false; 00446 foreach ($strangers as $stranger) { 00447 if ($minmessages == 0 || $stranger->messagecount >= $minmessages) { 00448 message_print_contactlist_user($stranger, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2); 00449 } 00450 } 00451 } 00452 00453 echo html_writer::end_tag('table'); 00454 00455 if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help 00456 echo html_writer::tag('div','('.get_string('addsomecontactsincoming', 'message').')',array('class' => 'note')); 00457 } 00458 } 00459 00472 function message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages) { 00473 $options = array(); 00474 $textlib = textlib_get_instance(); // going to use textlib services 00475 00476 if ($countunreadtotal>0) { //if there are unread messages 00477 $options[MESSAGE_VIEW_UNREAD_MESSAGES] = $strunreadmessages; 00478 } 00479 00480 $str = get_string('mycontacts', 'message'); 00481 $options[MESSAGE_VIEW_CONTACTS] = $str; 00482 00483 $options[MESSAGE_VIEW_RECENT_CONVERSATIONS] = get_string('mostrecentconversations', 'message'); 00484 $options[MESSAGE_VIEW_RECENT_NOTIFICATIONS] = get_string('mostrecentnotifications', 'message'); 00485 00486 if (!empty($courses)) { 00487 $courses_options = array(); 00488 00489 foreach($courses as $course) { 00490 if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) { 00491 //Not using short_text() as we want the end of the course name. Not the beginning. 00492 $shortname = format_string($course->shortname, true, array('context' => $coursecontexts[$course->id])); 00493 if ($textlib->strlen($shortname) > MESSAGE_MAX_COURSE_NAME_LENGTH) { 00494 $courses_options[MESSAGE_VIEW_COURSE.$course->id] = '...'.$textlib->substr($shortname, -MESSAGE_MAX_COURSE_NAME_LENGTH); 00495 } else { 00496 $courses_options[MESSAGE_VIEW_COURSE.$course->id] = $shortname; 00497 } 00498 } 00499 } 00500 00501 if (!empty($courses_options)) { 00502 $options[] = array(get_string('courses') => $courses_options); 00503 } 00504 } 00505 00506 if ($countblocked>0) { 00507 $str = get_string('blockedusers','message', $countblocked); 00508 $options[MESSAGE_VIEW_BLOCKED] = $str; 00509 } 00510 00511 echo html_writer::start_tag('form', array('id' => 'usergroupform','method' => 'get','action' => '')); 00512 echo html_writer::start_tag('fieldset'); 00513 echo html_writer::select($options, 'viewing', $viewing, false, array('id' => 'viewing','onchange' => 'this.form.submit()')); 00514 echo html_writer::end_tag('fieldset'); 00515 echo html_writer::end_tag('form'); 00516 } 00517 00524 function message_get_course_contexts($courses) { 00525 $coursecontexts = array(); 00526 00527 foreach($courses as $course) { 00528 $coursecontexts[$course->id] = get_context_instance(CONTEXT_COURSE, $course->id); 00529 } 00530 00531 return $coursecontexts; 00532 } 00533 00540 function message_remove_url_params($moodleurl) { 00541 $newurl = new moodle_url($moodleurl); 00542 $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact'); 00543 return $newurl->out(); 00544 } 00545 00555 function message_count_messages($messagearray, $field='', $value='') { 00556 if (!is_array($messagearray)) return 0; 00557 if ($field == '' or empty($messagearray)) return count($messagearray); 00558 00559 $count = 0; 00560 foreach ($messagearray as $message) { 00561 $count += ($message->$field == $value) ? 1 : 0; 00562 } 00563 return $count; 00564 } 00565 00573 function message_count_unread_messages($user1=null, $user2=null) { 00574 global $USER, $DB; 00575 00576 if (empty($user1)) { 00577 $user1 = $USER; 00578 } 00579 00580 if (!empty($user2)) { 00581 return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", 00582 array($user1->id, $user2->id), "COUNT('id')"); 00583 } else { 00584 return $DB->count_records_select('message', "useridto = ?", 00585 array($user1->id), "COUNT('id')"); 00586 } 00587 } 00588 00595 function message_count_blocked_users($user1=null) { 00596 global $USER, $DB; 00597 00598 if (empty($user1)) { 00599 $user1 = $USER; 00600 } 00601 00602 $sql = "SELECT count(mc.id) 00603 FROM {message_contacts} mc 00604 WHERE mc.userid = :userid AND mc.blocked = 1"; 00605 $params = array('userid' => $user1->id); 00606 00607 return $DB->count_records_sql($sql, $params); 00608 } 00609 00617 function message_print_search($advancedsearch = false, $user1=null) { 00618 $frm = data_submitted(); 00619 00620 $doingsearch = false; 00621 if ($frm) { 00622 if (confirm_sesskey()) { 00623 $doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name)); 00624 } else { 00625 $frm = false; 00626 } 00627 } 00628 00629 if (!empty($frm->combinedsearch)) { 00630 $combinedsearchstring = $frm->combinedsearch; 00631 } else { 00632 //$combinedsearchstring = get_string('searchcombined','message').'...'; 00633 $combinedsearchstring = ''; 00634 } 00635 00636 if ($doingsearch) { 00637 if ($advancedsearch) { 00638 00639 $messagesearch = ''; 00640 if (!empty($frm->keywords)) { 00641 $messagesearch = $frm->keywords; 00642 } 00643 $personsearch = ''; 00644 if (!empty($frm->name)) { 00645 $personsearch = $frm->name; 00646 } 00647 include('search_advanced.html'); 00648 } else { 00649 include('search.html'); 00650 } 00651 00652 $showicontext = false; 00653 message_print_search_results($frm, $showicontext, $user1); 00654 00655 return true; 00656 } else { 00657 00658 if ($advancedsearch) { 00659 $personsearch = $messagesearch = ''; 00660 include('search_advanced.html'); 00661 } else { 00662 include('search.html'); 00663 } 00664 return false; 00665 } 00666 } 00667 00677 function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { 00678 global $DB; 00679 00680 $userfields = user_picture::fields('u', array('lastaccess')); 00681 //This query retrieves the last message received from and sent to each user 00682 //It unions that data then, within that set, it finds the most recent message you've exchanged with each user over all 00683 //It then joins with some other tables to get some additional data we need 00684 00685 //message ID is used instead of timecreated as it should sort the same and will be much faster 00686 00687 //There is a separate query for read and unread queries as they are stored in different tables 00688 //They were originally retrieved in one query but it was so large that it was difficult to be confident in its correctness 00689 $sql = "SELECT $userfields, mr.id as mid, mr.smallmessage, mr.fullmessage, mr.timecreated, mc.id as contactlistid, mc.blocked 00690 FROM {message_read} mr 00691 JOIN ( 00692 SELECT messages.userid AS userid, MAX(messages.mid) AS mid 00693 FROM ( 00694 SELECT mr1.useridto AS userid, MAX(mr1.id) AS mid 00695 FROM {message_read} mr1 00696 WHERE mr1.useridfrom = :userid1 00697 AND mr1.notification = 0 00698 GROUP BY mr1.useridto 00699 UNION 00700 SELECT mr2.useridfrom AS userid, MAX(mr2.id) AS mid 00701 FROM {message_read} mr2 00702 WHERE mr2.useridto = :userid2 00703 AND mr2.notification = 0 00704 GROUP BY mr2.useridfrom 00705 ) messages 00706 GROUP BY messages.userid 00707 ) messages2 ON mr.id = messages2.mid AND (mr.useridto = messages2.userid OR mr.useridfrom = messages2.userid) 00708 JOIN {user} u ON u.id = messages2.userid 00709 LEFT JOIN {message_contacts} mc ON mc.userid = :userid3 AND mc.contactid = u.id 00710 WHERE u.deleted = '0' 00711 ORDER BY mr.id DESC"; 00712 $params = array('userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id); 00713 $read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); 00714 00715 $sql = "SELECT $userfields, m.id as mid, m.smallmessage, m.fullmessage, m.timecreated, mc.id as contactlistid, mc.blocked 00716 FROM {message} m 00717 JOIN ( 00718 SELECT messages.userid AS userid, MAX(messages.mid) AS mid 00719 FROM ( 00720 SELECT m1.useridto AS userid, MAX(m1.id) AS mid 00721 FROM {message} m1 00722 WHERE m1.useridfrom = :userid1 00723 AND m1.notification = 0 00724 GROUP BY m1.useridto 00725 UNION 00726 SELECT m2.useridfrom AS userid, MAX(m2.id) AS mid 00727 FROM {message} m2 00728 WHERE m2.useridto = :userid2 00729 AND m2.notification = 0 00730 GROUP BY m2.useridfrom 00731 ) messages 00732 GROUP BY messages.userid 00733 ) messages2 ON m.id = messages2.mid AND (m.useridto = messages2.userid OR m.useridfrom = messages2.userid) 00734 JOIN {user} u ON u.id = messages2.userid 00735 LEFT JOIN {message_contacts} mc ON mc.userid = :userid3 AND mc.contactid = u.id 00736 WHERE u.deleted = '0' 00737 ORDER BY m.id DESC"; 00738 $unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); 00739 00740 $conversations = array(); 00741 00742 //Union the 2 result sets together looking for the message with the most recent timecreated for each other user 00743 //$conversation->id (the array key) is the other user's ID 00744 $conversation_arrays = array($unread, $read); 00745 foreach ($conversation_arrays as $conversation_array) { 00746 foreach ($conversation_array as $conversation) { 00747 if (empty($conversations[$conversation->id]) || $conversations[$conversation->id]->timecreated < $conversation->timecreated ) { 00748 $conversations[$conversation->id] = $conversation; 00749 } 00750 } 00751 } 00752 00753 //Sort the conversations. This is a bit complicated as we need to sort by $conversation->timecreated 00754 //and there may be multiple conversations with the same timecreated value. 00755 //The conversations array contains both read and unread messages (different tables) so sorting by ID won't work 00756 usort($conversations, "conversationsort"); 00757 00758 return $conversations; 00759 } 00760 00768 function conversationsort($a, $b) 00769 { 00770 if ($a->timecreated == $b->timecreated) { 00771 return 0; 00772 } 00773 return ($a->timecreated > $b->timecreated) ? -1 : 1; 00774 } 00775 00784 function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) { 00785 global $DB; 00786 00787 $userfields = user_picture::fields('u', array('lastaccess')); 00788 $sql = "SELECT mr.id AS message_read_id, $userfields, mr.smallmessage, mr.fullmessage, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname 00789 FROM {message_read} mr 00790 JOIN {user} u ON u.id=mr.useridfrom 00791 WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification 00792 ORDER BY mr.id DESC";//ordering by id should give the same result as ordering by timecreated but will be faster 00793 $params = array('userid1' => $user->id, 'notification' => 1); 00794 00795 $notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto); 00796 return $notifications; 00797 } 00798 00806 function message_print_recent_conversations($user=null, $showicontext=false) { 00807 global $USER; 00808 00809 echo html_writer::start_tag('p', array('class' => 'heading')); 00810 echo get_string('mostrecentconversations', 'message'); 00811 echo html_writer::end_tag('p'); 00812 00813 if (empty($user)) { 00814 $user = $USER; 00815 } 00816 00817 $conversations = message_get_recent_conversations($user); 00818 00819 $showotheruser = true; 00820 message_print_recent_messages_table($conversations, $user, $showotheruser, $showicontext); 00821 } 00822 00829 function message_print_recent_notifications($user=null) { 00830 global $USER; 00831 00832 echo html_writer::start_tag('p', array('class' => 'heading')); 00833 echo get_string('mostrecentnotifications', 'message'); 00834 echo html_writer::end_tag('p'); 00835 00836 if (empty($user)) { 00837 $user = $USER; 00838 } 00839 00840 $notifications = message_get_recent_notifications($user); 00841 00842 $showicontext = false; 00843 $showotheruser = false; 00844 message_print_recent_messages_table($notifications, $user, $showotheruser, $showicontext); 00845 } 00846 00857 function message_print_recent_messages_table($messages, $user=null, $showotheruser=true, $showicontext=false) { 00858 global $OUTPUT; 00859 static $dateformat; 00860 00861 if (empty($dateformat)) { 00862 $dateformat = get_string('strftimedatetimeshort'); 00863 } 00864 00865 echo html_writer::start_tag('div', array('class' => 'messagerecent')); 00866 foreach ($messages as $message) { 00867 echo html_writer::start_tag('div', array('class' => 'singlemessage')); 00868 00869 if ($showotheruser) { 00870 if ( $message->contactlistid ) { 00871 if ($message->blocked == 0) { 00872 $strcontact = message_contact_link($message->id, 'remove', true, null, $showicontext); 00873 $strblock = message_contact_link($message->id, 'block', true, null, $showicontext); 00874 } else { // blocked 00875 $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext); 00876 $strblock = message_contact_link($message->id, 'unblock', true, null, $showicontext); 00877 } 00878 } else { 00879 $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext); 00880 $strblock = message_contact_link($message->id, 'block', true, null, $showicontext); 00881 } 00882 00883 //should we show just the icon or icon and text? 00884 $histicontext = 'icon'; 00885 if ($showicontext) { 00886 $histicontext = 'both'; 00887 } 00888 $strhistory = message_history_link($user->id, $message->id, true, '', '', $histicontext); 00889 00890 echo html_writer::start_tag('span', array('class' => 'otheruser')); 00891 00892 echo html_writer::start_tag('span', array('class' => 'pix')); 00893 echo $OUTPUT->user_picture($message, array('size' => 20, 'courseid' => SITEID)); 00894 echo html_writer::end_tag('span'); 00895 00896 echo html_writer::start_tag('span', array('class' => 'contact')); 00897 00898 $link = new moodle_url("/message/index.php?id=$message->id"); 00899 $action = null; 00900 echo $OUTPUT->action_link($link, fullname($message), $action, array('title' => get_string('sendmessageto', 'message', fullname($message)))); 00901 00902 echo html_writer::end_tag('span');//end contact 00903 00904 echo $strcontact.$strblock.$strhistory; 00905 echo html_writer::end_tag('span');//end otheruser 00906 } 00907 $messagetoprint = null; 00908 if (!empty($message->smallmessage)) { 00909 $messagetoprint = $message->smallmessage; 00910 } else { 00911 $messagetoprint = $message->fullmessage; 00912 } 00913 00914 echo html_writer::tag('span', userdate($message->timecreated, $dateformat), array('class' => 'messagedate')); 00915 echo html_writer::tag('span', format_text($messagetoprint, FORMAT_HTML), array('class' => 'themessage')); 00916 echo message_format_contexturl($message); 00917 echo html_writer::end_tag('div');//end singlemessage 00918 } 00919 echo html_writer::end_tag('div');//end messagerecent 00920 } 00921 00930 function message_add_contact($contactid, $blocked=0) { 00931 global $USER, $DB; 00932 00933 if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid 00934 return false; 00935 } 00936 00937 if (($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) !== false) { 00939 00940 if ($contact->blocked !== $blocked) { 00942 $contact->blocked = $blocked; 00943 return $DB->update_record('message_contacts', $contact); 00944 } else { 00946 return true; 00947 } 00948 00949 } else { 00951 $contact = new stdClass(); 00952 $contact->userid = $USER->id; 00953 $contact->contactid = $contactid; 00954 $contact->blocked = $blocked; 00955 return $DB->insert_record('message_contacts', $contact, false); 00956 } 00957 } 00958 00965 function message_remove_contact($contactid) { 00966 global $USER, $DB; 00967 return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); 00968 } 00969 00976 function message_unblock_contact($contactid) { 00977 global $USER, $DB; 00978 return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); 00979 } 00980 00986 function message_block_contact($contactid) { 00987 return message_add_contact($contactid, 1); 00988 } 00989 00996 function message_get_contact($contactid) { 00997 global $USER, $DB; 00998 return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid)); 00999 } 01000 01009 function message_print_search_results($frm, $showicontext=false, $currentuser=null) { 01010 global $USER, $DB, $OUTPUT; 01011 01012 if (empty($currentuser)) { 01013 $currentuser = $USER; 01014 } 01015 01016 echo html_writer::start_tag('div', array('class' => 'mdl-left')); 01017 01018 $personsearch = false; 01019 $personsearchstring = null; 01020 if (!empty($frm->personsubmit) and !empty($frm->name)) { 01021 $personsearch = true; 01022 $personsearchstring = $frm->name; 01023 } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { 01024 $personsearch = true; 01025 $personsearchstring = $frm->combinedsearch; 01026 } 01027 01029 if ($personsearch) { 01030 if (optional_param('mycourses', 0, PARAM_BOOL)) { 01031 $users = array(); 01032 $mycourses = enrol_get_my_courses(); 01033 foreach ($mycourses as $mycourse) { 01034 if (is_array($susers = message_search_users($mycourse->id, $personsearchstring))) { 01035 foreach ($susers as $suser) $users[$suser->id] = $suser; 01036 } 01037 } 01038 } else { 01039 $users = message_search_users(SITEID, $personsearchstring); 01040 } 01041 01042 if (!empty($users)) { 01043 echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); 01044 echo get_string('userssearchresults', 'message', count($users)); 01045 echo html_writer::end_tag('p'); 01046 01047 echo html_writer::start_tag('table', array('class' => 'messagesearchresults')); 01048 foreach ($users as $user) { 01049 01050 if ( $user->contactlistid ) { 01051 if ($user->blocked == 0) { 01052 $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext); 01053 $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); 01054 } else { // blocked 01055 $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); 01056 $strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext); 01057 } 01058 } else { 01059 $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext); 01060 $strblock = message_contact_link($user->id, 'block', true, null, $showicontext); 01061 } 01062 01063 //should we show just the icon or icon and text? 01064 $histicontext = 'icon'; 01065 if ($showicontext) { 01066 $histicontext = 'both'; 01067 } 01068 $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext); 01069 01070 echo html_writer::start_tag('tr'); 01071 01072 echo html_writer::start_tag('td', array('class' => 'pix')); 01073 echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID)); 01074 echo html_writer::end_tag('td'); 01075 01076 echo html_writer::start_tag('td',array('class' => 'contact')); 01077 $action = null; 01078 $link = new moodle_url("/message/index.php?id=$user->id"); 01079 echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user)))); 01080 echo html_writer::end_tag('td'); 01081 01082 echo html_writer::tag('td', $strcontact, array('class' => 'link')); 01083 echo html_writer::tag('td', $strblock, array('class' => 'link')); 01084 echo html_writer::tag('td', $strhistory, array('class' => 'link')); 01085 01086 echo html_writer::end_tag('tr'); 01087 } 01088 echo html_writer::end_tag('table'); 01089 01090 } else { 01091 echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); 01092 echo get_string('userssearchresults', 'message', 0).'<br /><br />'; 01093 echo html_writer::end_tag('p'); 01094 } 01095 } 01096 01097 // search messages for keywords 01098 $messagesearch = false; 01099 $messagesearchstring = null; 01100 if (!empty($frm->keywords)) { 01101 $messagesearch = true; 01102 $messagesearchstring = clean_text(trim($frm->keywords)); 01103 } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) { 01104 $messagesearch = true; 01105 $messagesearchstring = clean_text(trim($frm->combinedsearch)); 01106 } 01107 01108 if ($messagesearch) { 01109 if ($messagesearchstring) { 01110 $keywords = explode(' ', $messagesearchstring); 01111 } else { 01112 $keywords = array(); 01113 } 01114 $tome = false; 01115 $fromme = false; 01116 $courseid = 'none'; 01117 01118 if (empty($frm->keywordsoption)) { 01119 $frm->keywordsoption = 'allmine'; 01120 } 01121 01122 switch ($frm->keywordsoption) { 01123 case 'tome': 01124 $tome = true; 01125 break; 01126 case 'fromme': 01127 $fromme = true; 01128 break; 01129 case 'allmine': 01130 $tome = true; 01131 $fromme = true; 01132 break; 01133 case 'allusers': 01134 $courseid = SITEID; 01135 break; 01136 case 'courseusers': 01137 $courseid = $frm->courseid; 01138 break; 01139 default: 01140 $tome = true; 01141 $fromme = true; 01142 } 01143 01144 if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) { 01145 01147 if (($contacts = $DB->get_records('message_contacts', array('userid' => $USER->id), '', 'contactid, blocked') ) === false) { 01148 $contacts = array(); 01149 } 01150 01152 echo html_writer::start_tag('p', array('class' => 'heading searchresultcount')); 01153 $countresults = count($messages); 01154 if ($countresults == MESSAGE_SEARCH_MAX_RESULTS) { 01155 echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")'; 01156 } else { 01157 echo get_string('keywordssearchresults', 'message', $countresults); 01158 } 01159 echo html_writer::end_tag('p'); 01160 01162 echo html_writer::start_tag('table', array('class' => 'messagesearchresults', 'cellspacing' => '0')); 01163 01164 $headertdstart = html_writer::start_tag('td', array('class' => 'messagesearchresultscol')); 01165 $headertdend = html_writer::end_tag('td'); 01166 echo html_writer::start_tag('tr'); 01167 echo $headertdstart.get_string('from').$headertdend; 01168 echo $headertdstart.get_string('to').$headertdend; 01169 echo $headertdstart.get_string('message', 'message').$headertdend; 01170 echo $headertdstart.get_string('timesent', 'message').$headertdend; 01171 echo html_writer::end_tag('tr'); 01172 01173 $blockedcount = 0; 01174 $dateformat = get_string('strftimedatetimeshort'); 01175 $strcontext = get_string('context', 'message'); 01176 foreach ($messages as $message) { 01177 01179 if (!optional_param('includeblocked', 0, PARAM_BOOL) and ( 01180 ( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or 01181 ( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1)) 01182 ) 01183 ) { 01184 $blockedcount ++; 01185 continue; 01186 } 01187 01189 if ($message->useridto !== $USER->id) { 01190 $userto = $DB->get_record('user', array('id' => $message->useridto)); 01191 $tocontact = (array_key_exists($message->useridto, $contacts) and 01192 ($contacts[$message->useridto]->blocked == 0) ); 01193 $toblocked = (array_key_exists($message->useridto, $contacts) and 01194 ($contacts[$message->useridto]->blocked == 1) ); 01195 } else { 01196 $userto = false; 01197 $tocontact = false; 01198 $toblocked = false; 01199 } 01200 01202 if ($message->useridfrom !== $USER->id) { 01203 $userfrom = $DB->get_record('user', array('id' => $message->useridfrom)); 01204 $fromcontact = (array_key_exists($message->useridfrom, $contacts) and 01205 ($contacts[$message->useridfrom]->blocked == 0) ); 01206 $fromblocked = (array_key_exists($message->useridfrom, $contacts) and 01207 ($contacts[$message->useridfrom]->blocked == 1) ); 01208 } else { 01209 $userfrom = false; 01210 $fromcontact = false; 01211 $fromblocked = false; 01212 } 01213 01215 $date = usergetdate($message->timecreated); 01216 $datestring = $date['year'].$date['mon'].$date['mday']; 01217 01219 echo html_writer::start_tag('tr', array('valign' => 'top')); 01220 01221 echo html_writer::start_tag('td', array('class' => 'contact')); 01222 message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext); 01223 echo html_writer::end_tag('td'); 01224 01225 echo html_writer::start_tag('td', array('class' => 'contact')); 01226 message_print_user($userto, $tocontact, $toblocked, $showicontext); 01227 echo html_writer::end_tag('td'); 01228 01229 echo html_writer::start_tag('td', array('class' => 'summary')); 01230 echo message_get_fragment($message->fullmessage, $keywords); 01231 echo html_writer::start_tag('div', array('class' => 'link')); 01232 01233 //If the user clicks the context link display message sender on the left 01234 //EXCEPT if the current user is in the conversation. Current user == always on the left 01235 $leftsideuserid = $rightsideuserid = null; 01236 if ($currentuser->id == $message->useridto) { 01237 $leftsideuserid = $message->useridto; 01238 $rightsideuserid = $message->useridfrom; 01239 } else { 01240 $leftsideuserid = $message->useridfrom; 01241 $rightsideuserid = $message->useridto; 01242 } 01243 message_history_link($leftsideuserid, $rightsideuserid, false, 01244 $messagesearchstring, 'm'.$message->id, $strcontext); 01245 echo html_writer::end_tag('div'); 01246 echo html_writer::end_tag('td'); 01247 01248 echo html_writer::tag('td', userdate($message->timecreated, $dateformat), array('class' => 'date')); 01249 01250 echo html_writer::end_tag('tr'); 01251 } 01252 01253 01254 if ($blockedcount > 0) { 01255 echo html_writer::start_tag('tr'); 01256 echo html_writer::tag('td', get_string('blockedmessages', 'message', $blockedcount), array('colspan' => 4, 'align' => 'center')); 01257 echo html_writer::end_tag('tr'); 01258 } 01259 echo html_writer::end_tag('table'); 01260 01261 } else { 01262 echo html_writer::tag('p', get_string('keywordssearchresults', 'message', 0), array('class' => 'heading')); 01263 } 01264 } 01265 01266 if (!$personsearch && !$messagesearch) { 01267 //they didn't enter any search terms 01268 echo $OUTPUT->notification(get_string('emptysearchstring', 'message')); 01269 } 01270 01271 echo html_writer::end_tag('div'); 01272 } 01273 01283 function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) { 01284 global $USER, $OUTPUT; 01285 01286 if ($user === false) { 01287 echo $OUTPUT->user_picture($USER, array('size' => 20, 'courseid' => SITEID)); 01288 } else { 01289 echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID)); 01290 echo ' '; 01291 01292 $return = false; 01293 $script = null; 01294 if ($iscontact) { 01295 message_contact_link($user->id, 'remove', $return, $script, $includeicontext); 01296 } else { 01297 message_contact_link($user->id, 'add', $return, $script, $includeicontext); 01298 } 01299 echo ' '; 01300 if ($isblocked) { 01301 message_contact_link($user->id, 'unblock', $return, $script, $includeicontext); 01302 } else { 01303 message_contact_link($user->id, 'block', $return, $script, $includeicontext); 01304 } 01305 01306 $popupoptions = array( 01307 'height' => MESSAGE_DISCUSSION_HEIGHT, 01308 'width' => MESSAGE_DISCUSSION_WIDTH, 01309 'menubar' => false, 01310 'location' => false, 01311 'status' => true, 01312 'scrollbars' => true, 01313 'resizable' => true); 01314 01315 $link = new moodle_url("/message/index.php?id=$user->id"); 01316 //$action = new popup_action('click', $link, "message_$user->id", $popupoptions); 01317 $action = null; 01318 echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user)))); 01319 01320 } 01321 } 01322 01335 function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) { 01336 global $OUTPUT, $PAGE; 01337 01338 //hold onto the strings as we're probably creating a bunch of links 01339 static $str; 01340 01341 if (empty($script)) { 01342 //strip off previous action params like 'removecontact' 01343 $script = message_remove_url_params($PAGE->url); 01344 } 01345 01346 if (empty($str->blockcontact)) { 01347 $str = new stdClass(); 01348 $str->blockcontact = get_string('blockcontact', 'message'); 01349 $str->unblockcontact = get_string('unblockcontact', 'message'); 01350 $str->removecontact = get_string('removecontact', 'message'); 01351 $str->addcontact = get_string('addcontact', 'message'); 01352 } 01353 01354 $command = $linktype.'contact'; 01355 $string = $str->{$command}; 01356 01357 $safealttext = s($string); 01358 01359 $safestring = ''; 01360 if (!empty($text)) { 01361 $safestring = $safealttext; 01362 } 01363 01364 $img = ''; 01365 if ($icon) { 01366 $iconpath = null; 01367 switch ($linktype) { 01368 case 'block': 01369 $iconpath = 't/block'; 01370 break; 01371 case 'unblock': 01372 $iconpath = 't/userblue'; 01373 break; 01374 case 'remove': 01375 $iconpath = 'i/cross_red_big'; 01376 break; 01377 case 'add': 01378 default: 01379 $iconpath = 't/addgreen'; 01380 } 01381 01382 $img = '<img src="'.$OUTPUT->pix_url($iconpath).'" class="iconsmall" alt="'.$safealttext.'" />'; 01383 } 01384 01385 $output = '<span class="'.$linktype.'contact">'. 01386 '<a href="'.$script.'&'.$command.'='.$userid. 01387 '&sesskey='.sesskey().'" title="'.$safealttext.'">'. 01388 $img. 01389 $safestring.'</a></span>'; 01390 01391 if ($return) { 01392 return $output; 01393 } else { 01394 echo $output; 01395 return true; 01396 } 01397 } 01398 01411 function message_history_link($userid1, $userid2, $return=false, $keywords='', $position='', $linktext='') { 01412 global $OUTPUT; 01413 static $strmessagehistory; 01414 01415 if (empty($strmessagehistory)) { 01416 $strmessagehistory = get_string('messagehistory', 'message'); 01417 } 01418 01419 if ($position) { 01420 $position = "#$position"; 01421 } 01422 if ($keywords) { 01423 $keywords = "&search=".urlencode($keywords); 01424 } 01425 01426 if ($linktext == 'icon') { // Icon only 01427 $fulllink = '<img src="'.$OUTPUT->pix_url('t/log') . '" class="iconsmall" alt="'.$strmessagehistory.'" />'; 01428 } else if ($linktext == 'both') { // Icon and standard name 01429 $fulllink = '<img src="'.$OUTPUT->pix_url('t/log') . '" class="iconsmall" alt="" />'; 01430 $fulllink .= ' '.$strmessagehistory; 01431 } else if ($linktext) { // Custom name 01432 $fulllink = $linktext; 01433 } else { // Standard name only 01434 $fulllink = $strmessagehistory; 01435 } 01436 01437 $popupoptions = array( 01438 'height' => 500, 01439 'width' => 500, 01440 'menubar' => false, 01441 'location' => false, 01442 'status' => true, 01443 'scrollbars' => true, 01444 'resizable' => true); 01445 01446 $link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user1=$userid1&user2=$userid2$keywords$position"); 01447 $action = null; 01448 $str = $OUTPUT->action_link($link, $fulllink, $action, array('title' => $strmessagehistory)); 01449 01450 $str = '<span class="history">'.$str.'</span>'; 01451 01452 if ($return) { 01453 return $str; 01454 } else { 01455 echo $str; 01456 return true; 01457 } 01458 } 01459 01460 01472 function message_search_users($courseid, $searchtext, $sort='', $exceptions='') { 01473 global $CFG, $USER, $DB; 01474 01475 $fullname = $DB->sql_fullname(); 01476 01477 if (!empty($exceptions)) { 01478 $except = ' AND u.id NOT IN ('. $exceptions .') '; 01479 } else { 01480 $except = ''; 01481 } 01482 01483 if (!empty($sort)) { 01484 $order = ' ORDER BY '. $sort; 01485 } else { 01486 $order = ''; 01487 } 01488 01489 $ufields = user_picture::fields('u'); 01490 if (!$courseid or $courseid == SITEID) { 01491 $params = array($USER->id, "%$searchtext%"); 01492 return $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked 01493 FROM {user} u 01494 LEFT JOIN {message_contacts} mc 01495 ON mc.contactid = u.id AND mc.userid = ? 01496 WHERE u.deleted = '0' AND u.confirmed = '1' 01497 AND (".$DB->sql_like($fullname, '?', false).") 01498 $except 01499 $order", $params); 01500 } else { 01501 //TODO: add enabled enrolment join here (skodak) 01502 $context = get_context_instance(CONTEXT_COURSE, $courseid); 01503 $contextlists = get_related_contexts_string($context); 01504 01505 // everyone who has a role assignment in this course or higher 01506 $params = array($USER->id, "%$searchtext%"); 01507 $users = $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked 01508 FROM {user} u 01509 JOIN {role_assignments} ra ON ra.userid = u.id 01510 LEFT JOIN {message_contacts} mc 01511 ON mc.contactid = u.id AND mc.userid = ? 01512 WHERE u.deleted = '0' AND u.confirmed = '1' 01513 AND ra.contextid $contextlists 01514 AND (".$DB->sql_like($fullname, '?', false).") 01515 $except 01516 $order", $params); 01517 01518 return $users; 01519 } 01520 } 01521 01532 function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) { 01536 global $CFG, $USER, $DB; 01537 01539 if ($userid == 0) $userid = $USER->id; 01540 01542 if ($DB->sql_regex_supported()) { 01543 $REGEXP = $DB->sql_regex(true); 01544 $NOTREGEXP = $DB->sql_regex(false); 01545 } 01546 01547 $searchcond = array(); 01548 $params = array(); 01549 $i = 0; 01550 01551 //preprocess search terms to check whether we have at least 1 eligible search term 01552 //if we do we can drop words around it like 'a' 01553 $dropshortwords = false; 01554 foreach ($searchterms as $searchterm) { 01555 if (strlen($searchterm) >= 2) { 01556 $dropshortwords = true; 01557 } 01558 } 01559 01560 foreach ($searchterms as $searchterm) { 01561 $i++; 01562 01563 $NOT = false; 01564 01565 if ($dropshortwords && strlen($searchterm) < 2) { 01566 continue; 01567 } 01570 if (!$DB->sql_regex_supported()) { 01571 if (substr($searchterm, 0, 1) == '-') { 01572 $NOT = true; 01573 } 01574 $searchterm = trim($searchterm, '+-'); 01575 } 01576 01577 if (substr($searchterm,0,1) == "+") { 01578 $searchterm = substr($searchterm,1); 01579 $searchterm = preg_quote($searchterm, '|'); 01580 $searchcond[] = "m.fullmessage $REGEXP :ss$i"; 01581 $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; 01582 01583 } else if (substr($searchterm,0,1) == "-") { 01584 $searchterm = substr($searchterm,1); 01585 $searchterm = preg_quote($searchterm, '|'); 01586 $searchcond[] = "m.fullmessage $NOTREGEXP :ss$i"; 01587 $params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)"; 01588 01589 } else { 01590 $searchcond[] = $DB->sql_like("m.fullmessage", ":ss$i", false, true, $NOT); 01591 $params['ss'.$i] = "%$searchterm%"; 01592 } 01593 } 01594 01595 if (empty($searchcond)) { 01596 $searchcond = " ".$DB->sql_like('m.fullmessage', ':ss1', false); 01597 $params['ss1'] = "%"; 01598 } else { 01599 $searchcond = implode(" AND ", $searchcond); 01600 } 01601 01610 01611 if ($courseid == SITEID) { 01612 $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated 01613 FROM {message_read} m 01614 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 01615 $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated 01616 FROM {message} m 01617 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 01618 01619 } else if ($courseid !== 'none') { 01621 $m_read = array(); 01622 $m_unread = array(); 01623 01624 } else { 01625 01626 if ($fromme and $tome) { 01627 $searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)"; 01628 $params['userid1'] = $userid; 01629 $params['userid2'] = $userid; 01630 01631 } else if ($fromme) { 01632 $searchcond .= " AND m.useridfrom=:userid"; 01633 $params['userid'] = $userid; 01634 01635 } else if ($tome) { 01636 $searchcond .= " AND m.useridto=:userid"; 01637 $params['userid'] = $userid; 01638 } 01639 01640 $m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated 01641 FROM {message_read} m 01642 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 01643 $m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.fullmessage, m.timecreated 01644 FROM {message} m 01645 WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS); 01646 01647 } 01648 01651 $messages = array(); 01652 foreach ($m_read as $m) { 01653 $messages[] = $m; 01654 } 01655 foreach ($m_unread as $m) { 01656 $messages[] = $m; 01657 } 01658 01659 return (empty($messages)) ? false : $messages; 01660 } 01661 01671 function message_shorten_message($message, $minlength = 0) { 01672 $i = 0; 01673 $tag = false; 01674 $length = strlen($message); 01675 $count = 0; 01676 $stopzone = false; 01677 $truncate = 0; 01678 if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH; 01679 01680 01681 for ($i=0; $i<$length; $i++) { 01682 $char = $message[$i]; 01683 01684 switch ($char) { 01685 case "<": 01686 $tag = true; 01687 break; 01688 case ">": 01689 $tag = false; 01690 break; 01691 default: 01692 if (!$tag) { 01693 if ($stopzone) { 01694 if ($char == '.' or $char == ' ') { 01695 $truncate = $i+1; 01696 break 2; 01697 } 01698 } 01699 $count++; 01700 } 01701 break; 01702 } 01703 if (!$stopzone) { 01704 if ($count > $minlength) { 01705 $stopzone = true; 01706 } 01707 } 01708 } 01709 01710 if (!$truncate) { 01711 $truncate = $i; 01712 } 01713 01714 return substr($message, 0, $truncate); 01715 } 01716 01717 01726 function message_get_fragment($message, $keywords) { 01727 01728 $fullsize = 160; 01729 $halfsize = (int)($fullsize/2); 01730 01731 $message = strip_tags($message); 01732 01733 foreach ($keywords as $keyword) { // Just get the first one 01734 if ($keyword !== '') { 01735 break; 01736 } 01737 } 01738 if (empty($keyword)) { // None found, so just return start of message 01739 return message_shorten_message($message, 30); 01740 } 01741 01742 $leadin = $leadout = ''; 01743 01745 $start = 0; 01746 $length = strlen($message); 01747 01748 $pos = strpos($message, $keyword); 01749 if ($pos > $halfsize) { 01750 $start = $pos - $halfsize; 01751 $leadin = '...'; 01752 } 01754 $end = $start + $fullsize; 01755 if ($end > $length) { 01756 $end = $length; 01757 } else { 01758 $leadout = '...'; 01759 } 01760 01762 01763 $fragment = substr($message, $start, $end - $start); 01764 $fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout; 01765 return $fragment; 01766 } 01767 01776 function message_get_history($user1, $user2, $limitnum=0, $viewingnewmessages=false) { 01777 global $DB, $CFG; 01778 01779 $messages = array(); 01780 01781 //we want messages sorted oldest to newest but if getting a subset of messages we need to sort 01782 //desc to get the last $limitnum messages then flip the order in php 01783 $sort = 'asc'; 01784 if ($limitnum>0) { 01785 $sort = 'desc'; 01786 } 01787 01788 $notificationswhere = null; 01789 //we have just moved new messages to read. If theyre here to see new messages dont hide notifications 01790 if (!$viewingnewmessages && $CFG->messaginghidereadnotifications) { 01791 $notificationswhere = 'AND notification=0'; 01792 } 01793 01794 //prevent notifications of your own actions appearing in your own message history 01795 $ownnotificationwhere = ' AND NOT (useridfrom=? AND notification=1)'; 01796 01797 if ($messages_read = $DB->get_records_select('message_read', "((useridto = ? AND useridfrom = ?) OR 01798 (useridto = ? AND useridfrom = ?)) $notificationswhere $ownnotificationwhere", 01799 array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), 01800 "timecreated $sort", '*', 0, $limitnum)) { 01801 foreach ($messages_read as $message) { 01802 $messages[$message->timecreated] = $message; 01803 } 01804 } 01805 if ($messages_new = $DB->get_records_select('message', "((useridto = ? AND useridfrom = ?) OR 01806 (useridto = ? AND useridfrom = ?)) $ownnotificationwhere", 01807 array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id), 01808 "timecreated $sort", '*', 0, $limitnum)) { 01809 foreach ($messages_new as $message) { 01810 $messages[$message->timecreated] = $message; 01811 } 01812 } 01813 01814 //if we only want the last $limitnum messages 01815 ksort($messages); 01816 $messagecount = count($messages); 01817 if ($limitnum>0 && $messagecount>$limitnum) { 01818 $messages = array_slice($messages, $messagecount-$limitnum, $limitnum, true); 01819 } 01820 01821 return $messages; 01822 } 01823 01834 function message_print_message_history($user1,$user2,$search='',$messagelimit=0, $messagehistorylink=false, $viewingnewmessages=false) { 01835 global $CFG, $OUTPUT; 01836 01837 echo $OUTPUT->box_start('center'); 01838 echo html_writer::start_tag('table', array('cellpadding' => '10', 'class' => 'message_user_pictures')); 01839 echo html_writer::start_tag('tr'); 01840 01841 echo html_writer::start_tag('td', array('align' => 'center', 'id' => 'user1')); 01842 echo $OUTPUT->user_picture($user1, array('size' => 100, 'courseid' => SITEID)); 01843 echo html_writer::tag('div', fullname($user1), array('class' => 'heading')); 01844 echo html_writer::end_tag('td'); 01845 01846 echo html_writer::start_tag('td', array('align' => 'center')); 01847 echo html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/left'), 'alt' => get_string('from'))); 01848 echo html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/right'), 'alt' => get_string('to'))); 01849 echo html_writer::end_tag('td'); 01850 01851 echo html_writer::start_tag('td', array('align' => 'center', 'id' => 'user2')); 01852 echo $OUTPUT->user_picture($user2, array('size' => 100, 'courseid' => SITEID)); 01853 echo html_writer::tag('div', fullname($user2), array('class' => 'heading')); 01854 01855 if (isset($user2->iscontact) && isset($user2->isblocked)) { 01856 $incontactlist = $user2->iscontact; 01857 $isblocked = $user2->isblocked; 01858 01859 $script = null; 01860 $text = true; 01861 $icon = false; 01862 01863 $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $user2, $script, $text, $icon); 01864 $strblock = message_get_contact_block_link($incontactlist, $isblocked, $user2, $script, $text, $icon); 01865 $useractionlinks = $strcontact.' |'.$strblock; 01866 01867 echo html_writer::tag('div', $useractionlinks, array('class' => 'useractionlinks')); 01868 } 01869 01870 echo html_writer::end_tag('td'); 01871 echo html_writer::end_tag('tr'); 01872 echo html_writer::end_tag('table'); 01873 echo $OUTPUT->box_end(); 01874 01875 if (!empty($messagehistorylink)) { 01876 echo $messagehistorylink; 01877 } 01878 01880 if ($messages = message_get_history($user1, $user2, $messagelimit, $viewingnewmessages)) { 01881 $tablecontents = ''; 01882 01883 $current = new stdClass(); 01884 $current->mday = ''; 01885 $current->month = ''; 01886 $current->year = ''; 01887 $messagedate = get_string('strftimetime'); 01888 $blockdate = get_string('strftimedaydate'); 01889 foreach ($messages as $message) { 01890 if ($message->notification) { 01891 $notificationclass = ' notification'; 01892 } else { 01893 $notificationclass = null; 01894 } 01895 $date = usergetdate($message->timecreated); 01896 if ($current->mday != $date['mday'] | $current->month != $date['month'] | $current->year != $date['year']) { 01897 $current->mday = $date['mday']; 01898 $current->month = $date['month']; 01899 $current->year = $date['year']; 01900 01901 $datestring = html_writer::empty_tag('a', array('name' => $date['year'].$date['mon'].$date['mday'])); 01902 $tablecontents .= html_writer::tag('div', $datestring, array('class' => 'mdl-align heading')); 01903 01904 $tablecontents .= $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'mdl-align'); 01905 } 01906 01907 $formatted_message = $side = null; 01908 if ($message->useridfrom == $user1->id) { 01909 $formatted_message = message_format_message($message, $messagedate, $search, 'me'); 01910 $side = 'left'; 01911 } else { 01912 $formatted_message = message_format_message($message, $messagedate, $search, 'other'); 01913 $side = 'right'; 01914 } 01915 $tablecontents .= html_writer::tag('div', $formatted_message, array('class' => "mdl-left $side $notificationclass")); 01916 } 01917 01918 echo html_writer::nonempty_tag('div', $tablecontents, array('class' => 'mdl-left messagehistory')); 01919 } else { 01920 echo html_writer::nonempty_tag('div', '('.get_string('nomessagesfound', 'message').')', array('class' => 'mdl-align messagehistory')); 01921 } 01922 } 01923 01933 function message_format_message($message, $format='', $keywords='', $class='other') { 01934 01935 static $dateformat; 01936 01937 //if we haven't previously set the date format or they've supplied a new one 01938 if ( empty($dateformat) || (!empty($format) && $dateformat != $format) ) { 01939 if ($format) { 01940 $dateformat = $format; 01941 } else { 01942 $dateformat = get_string('strftimedatetimeshort'); 01943 } 01944 } 01945 $time = userdate($message->timecreated, $dateformat); 01946 $options = new stdClass(); 01947 $options->para = false; 01948 01949 //if supplied display small messages as fullmessage may contain boilerplate text that shouldnt appear in the messaging UI 01950 if (!empty($message->smallmessage)) { 01951 $messagetext = $message->smallmessage; 01952 } else { 01953 $messagetext = $message->fullmessage; 01954 } 01955 if ($message->fullmessageformat == FORMAT_HTML) { 01956 //dont escape html tags by calling s() if html format or they will display in the UI 01957 $messagetext = html_to_text(format_text($messagetext, $message->fullmessageformat, $options)); 01958 } else { 01959 $messagetext = format_text(s($messagetext), $message->fullmessageformat, $options); 01960 } 01961 01962 $messagetext .= message_format_contexturl($message); 01963 01964 if ($keywords) { 01965 $messagetext = highlight($keywords, $messagetext); 01966 } 01967 01968 return '<div class="message '.$class.'"><a name="m'.$message->id.'"></a> <span class="time">'.$time.'</span>: <span class="content">'.$messagetext.'</span></div>'; 01969 } 01970 01977 function message_format_contexturl($message) { 01978 $s = null; 01979 01980 if (!empty($message->contexturl)) { 01981 $displaytext = null; 01982 if (!empty($message->contexturlname)) { 01983 $displaytext= $message->contexturlname; 01984 } else { 01985 $displaytext= $message->contexturl; 01986 } 01987 $s .= html_writer::start_tag('div',array('class' => 'messagecontext')); 01988 $s .= get_string('view').': '.html_writer::tag('a', $displaytext, array('href' => $message->contexturl)); 01989 $s .= html_writer::end_tag('div'); 01990 } 01991 01992 return $s; 01993 } 01994 02004 function message_post_message($userfrom, $userto, $message, $format) { 02005 global $SITE, $CFG, $USER; 02006 02007 $eventdata = new stdClass(); 02008 $eventdata->component = 'moodle'; 02009 $eventdata->name = 'instantmessage'; 02010 $eventdata->userfrom = $userfrom; 02011 $eventdata->userto = $userto; 02012 02013 //using string manager directly so that strings in the message will be in the message recipients language rather than the senders 02014 $eventdata->subject = get_string_manager()->get_string('unreadnewmessage', 'message', fullname($userfrom), $userto->lang); 02015 02016 if ($format == FORMAT_HTML) { 02017 $eventdata->fullmessagehtml = $message; 02018 //some message processors may revert to sending plain text even if html is supplied 02019 //so we keep both plain and html versions if we're intending to send html 02020 $eventdata->fullmessage = html_to_text($eventdata->fullmessagehtml); 02021 } else { 02022 $eventdata->fullmessage = $message; 02023 $eventdata->fullmessagehtml = ''; 02024 } 02025 02026 $eventdata->fullmessageformat = $format; 02027 $eventdata->smallmessage = $message;//store the message unfiltered. Clean up on output. 02028 02029 $s = new stdClass(); 02030 $s->sitename = format_string($SITE->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); 02031 $s->url = $CFG->wwwroot.'/message/index.php?user='.$userto->id.'&id='.$userfrom->id; 02032 02033 $emailtagline = get_string_manager()->get_string('emailtagline', 'message', $s, $userto->lang); 02034 if (!empty($eventdata->fullmessage)) { 02035 $eventdata->fullmessage .= "\n\n---------------------------------------------------------------------\n".$emailtagline; 02036 } 02037 if (!empty($eventdata->fullmessagehtml)) { 02038 $eventdata->fullmessagehtml .= "<br /><br />---------------------------------------------------------------------<br />".$emailtagline; 02039 } 02040 02041 $eventdata->timecreated = time(); 02042 return message_send($eventdata); 02043 } 02044 02045 02054 function message_get_participants() { 02055 global $CFG, $DB; 02056 02057 return $DB->get_records_sql("SELECT useridfrom as id,1 FROM {message} 02058 UNION SELECT useridto as id,1 FROM {message} 02059 UNION SELECT useridfrom as id,1 FROM {message_read} 02060 UNION SELECT useridto as id,1 FROM {message_read} 02061 UNION SELECT userid as id,1 FROM {message_contacts} 02062 UNION SELECT contactid as id,1 from {message_contacts}"); 02063 } 02064 02077 function message_print_contactlist_user($contact, $incontactlist = true, $isblocked = false, $selectcontacturl = null, $showactionlinks = true, $selecteduser=null) { 02078 global $OUTPUT, $USER; 02079 $fullname = fullname($contact); 02080 $fullnamelink = $fullname; 02081 02082 $linkclass = ''; 02083 if (!empty($selecteduser) && $contact->id == $selecteduser->id) { 02084 $linkclass = 'messageselecteduser'; 02085 } 02086 02088 if ($contact->messagecount > 0 ){ 02089 $fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>'; 02090 } 02091 02092 $strcontact = $strblock = $strhistory = null; 02093 02094 if ($showactionlinks) { 02095 $strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $contact); 02096 $strblock = message_get_contact_block_link($incontactlist, $isblocked, $contact); 02097 $strhistory = message_history_link($USER->id, $contact->id, true, '', '', 'icon'); 02098 } 02099 02100 echo html_writer::start_tag('tr'); 02101 echo html_writer::start_tag('td', array('class' => 'pix')); 02102 echo $OUTPUT->user_picture($contact, array('size' => 20, 'courseid' => SITEID)); 02103 echo html_writer::end_tag('td'); 02104 02105 echo html_writer::start_tag('td', array('class' => 'contact')); 02106 02107 $popupoptions = array( 02108 'height' => MESSAGE_DISCUSSION_HEIGHT, 02109 'width' => MESSAGE_DISCUSSION_WIDTH, 02110 'menubar' => false, 02111 'location' => false, 02112 'status' => true, 02113 'scrollbars' => true, 02114 'resizable' => true); 02115 02116 $link = $action = null; 02117 if (!empty($selectcontacturl)) { 02118 $link = new moodle_url($selectcontacturl.'&user2='.$contact->id); 02119 } else { 02120 //can $selectcontacturl be removed and maybe the be removed and hardcoded? 02121 $link = new moodle_url("/message/index.php?id=$contact->id"); 02122 $action = new popup_action('click', $link, "message_$contact->id", $popupoptions); 02123 } 02124 echo $OUTPUT->action_link($link, $fullnamelink, $action, array('class' => $linkclass,'title' => get_string('sendmessageto', 'message', $fullname))); 02125 02126 echo html_writer::end_tag('td'); 02127 02128 echo html_writer::tag('td', ' '.$strcontact.$strblock.' '.$strhistory, array('class' => 'link')); 02129 02130 echo html_writer::end_tag('tr'); 02131 } 02132 02144 function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { 02145 $strcontact = ''; 02146 02147 if($incontactlist){ 02148 $strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon); 02149 } else if ($isblocked) { 02150 $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); 02151 } else{ 02152 $strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon); 02153 } 02154 02155 return $strcontact; 02156 } 02157 02169 function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) { 02170 $strblock = ''; 02171 02172 //commented out to allow the user to block a contact without having to remove them first 02173 /*if ($incontactlist) { 02174 //$strblock = ''; 02175 } else*/ 02176 if ($isblocked) { 02177 $strblock = ' '.message_contact_link($contact->id, 'unblock', true, $script, $text, $icon); 02178 } else{ 02179 $strblock = ' '.message_contact_link($contact->id, 'block', true, $script, $text, $icon); 02180 } 02181 02182 return $strblock; 02183 } 02184 02192 function message_move_userfrom_unread2read($userid) { 02193 global $DB; 02194 02195 // move all unread messages from message table to message_read 02196 if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) { 02197 foreach ($messages as $message) { 02198 message_mark_message_read($message, 0); //set timeread to 0 as the message was never read 02199 } 02200 } 02201 return true; 02202 } 02203 02211 function message_mark_messages_read($touserid, $fromuserid){ 02212 global $DB; 02213 02214 $sql = 'SELECT m.* FROM {message} m WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom'; 02215 $messages = $DB->get_recordset_sql($sql, array('useridto' => $touserid,'useridfrom' => $fromuserid)); 02216 02217 foreach ($messages as $message) { 02218 message_mark_message_read($message, time()); 02219 } 02220 02221 $messages->close(); 02222 } 02223 02232 function message_mark_message_read($message, $timeread, $messageworkingempty=false) { 02233 global $DB; 02234 02235 $message->timeread = $timeread; 02236 02237 $messageid = $message->id; 02238 unset($message->id);//unset because it will get a new id on insert into message_read 02239 02240 //If any processors have pending actions abort them 02241 if (!$messageworkingempty) { 02242 $DB->delete_records('message_working', array('unreadmessageid' => $messageid)); 02243 } 02244 $messagereadid = $DB->insert_record('message_read', $message); 02245 $DB->delete_records('message', array('id' => $messageid)); 02246 return $messagereadid; 02247 } 02248 02256 function message_print_heading($title, $colspan=3) { 02257 echo html_writer::start_tag('tr'); 02258 echo html_writer::tag('td', $title, array('colspan' => $colspan, 'class' => 'heading')); 02259 echo html_writer::end_tag('tr'); 02260 } 02261 02269 function get_message_processors($ready = false) { 02270 global $DB, $CFG; 02271 02272 static $processors; 02273 02274 if (empty($processors)) { 02275 // Get all processors, ensure the name column is the first so it will be the array key 02276 $processors = $DB->get_records('message_processors', null, 'name DESC', 'name, id, enabled'); 02277 foreach ($processors as &$processor){ 02278 $processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php'; 02279 if (is_readable($processorfile)) { 02280 include_once($processorfile); 02281 $processclass = 'message_output_' . $processor->name; 02282 if (class_exists($processclass)) { 02283 $pclass = new $processclass(); 02284 $processor->object = $pclass; 02285 $processor->configured = 0; 02286 if ($pclass->is_system_configured()) { 02287 $processor->configured = 1; 02288 } 02289 $processor->hassettings = 0; 02290 if (is_readable($CFG->dirroot.'/message/output/'.$processor->name.'/settings.php')) { 02291 $processor->hassettings = 1; 02292 } 02293 $processor->available = 1; 02294 } else { 02295 print_error('errorcallingprocessor', 'message'); 02296 } 02297 } else { 02298 $processor->available = 0; 02299 } 02300 } 02301 } 02302 if ($ready) { 02303 // Filter out enabled and system_configured processors 02304 $readyprocessors = $processors; 02305 foreach ($readyprocessors as $readyprocessor) { 02306 if (!($readyprocessor->enabled && $readyprocessor->configured)) { 02307 unset($readyprocessors[$readyprocessor->name]); 02308 } 02309 } 02310 return $readyprocessors; 02311 } 02312 02313 return $processors; 02314 } 02315 02321 function get_message_processor($type) { 02322 global $CFG; 02323 02324 // Note, we cannot use the get_message_processors function here, becaues this 02325 // code is called during install after installing each messaging plugin, and 02326 // get_message_processors caches the list of installed plugins. 02327 02328 $processorfile = $CFG->dirroot . "/message/output/{$type}/message_output_{$type}.php"; 02329 if (!is_readable($processorfile)) { 02330 throw new coding_exception('Unknown message processor type ' . $type); 02331 } 02332 02333 include_once($processorfile); 02334 02335 $processclass = 'message_output_' . $type; 02336 if (!class_exists($processclass)) { 02337 throw new coding_exception('Message processor ' . $type . 02338 ' does not define the right class'); 02339 } 02340 02341 return new $processclass(); 02342 } 02343 02349 function get_message_output_default_preferences() { 02350 return get_config('message'); 02351 } 02352 02362 function translate_message_default_setting($plugindefault, $processorname) { 02363 // Preset translation arrays 02364 $permittedvalues = array( 02365 0x04 => 'disallowed', 02366 0x08 => 'permitted', 02367 0x0c => 'forced', 02368 ); 02369 02370 $loggedinstatusvalues = array( 02371 0x00 => null, // use null if loggedin/loggedoff is not defined 02372 0x01 => 'loggedin', 02373 0x02 => 'loggedoff', 02374 ); 02375 02376 // define the default setting 02377 $processor = get_message_processor($processorname); 02378 $default = $processor->get_default_messaging_settings(); 02379 02380 // Validate the value. It should not exceed the maximum size 02381 if (!is_int($plugindefault) || ($plugindefault > 0x0f)) { 02382 debugging(get_string('errortranslatingdefault', 'message')); 02383 $plugindefault = $default; 02384 } 02385 // Use plugin default setting of 'permitted' is 0 02386 if (!($plugindefault & MESSAGE_PERMITTED_MASK)) { 02387 $plugindefault = $default; 02388 } 02389 02390 $permitted = $permittedvalues[$plugindefault & MESSAGE_PERMITTED_MASK]; 02391 $loggedin = $loggedoff = null; 02392 02393 if (($plugindefault & MESSAGE_PERMITTED_MASK) == MESSAGE_PERMITTED) { 02394 $loggedin = $loggedinstatusvalues[$plugindefault & MESSAGE_DEFAULT_LOGGEDIN]; 02395 $loggedoff = $loggedinstatusvalues[$plugindefault & MESSAGE_DEFAULT_LOGGEDOFF]; 02396 } 02397 02398 return array($permitted, $loggedin, $loggedoff); 02399 } 02400 02407 function message_page_type_list($pagetype, $parentcontext, $currentcontext) { 02408 return array('messages-*'=>get_string('page-message-x', 'message')); 02409 }