2009-09-29 03:54:14 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Library functions for messaging
|
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @package core_message
|
|
|
|
* @copyright 2008 Luis Rodrigues
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2009-09-29 03:54:14 +00:00
|
|
|
*/
|
2004-12-16 12:40:36 +00:00
|
|
|
|
2008-07-24 08:38:03 +00:00
|
|
|
require_once($CFG->libdir.'/eventslib.php');
|
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
define ('MESSAGE_SHORTLENGTH', 300);
|
2010-11-11 06:11:43 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
define ('MESSAGE_DISCUSSION_WIDTH',600);
|
|
|
|
define ('MESSAGE_DISCUSSION_HEIGHT',500);
|
|
|
|
|
|
|
|
define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history
|
|
|
|
|
|
|
|
define('MESSAGE_HISTORY_SHORT',0);
|
|
|
|
define('MESSAGE_HISTORY_ALL',1);
|
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
define('MESSAGE_VIEW_UNREAD_MESSAGES','unread');
|
|
|
|
define('MESSAGE_VIEW_RECENT_CONVERSATIONS','recentconversations');
|
|
|
|
define('MESSAGE_VIEW_RECENT_NOTIFICATIONS','recentnotifications');
|
|
|
|
define('MESSAGE_VIEW_CONTACTS','contacts');
|
|
|
|
define('MESSAGE_VIEW_BLOCKED','blockedusers');
|
|
|
|
define('MESSAGE_VIEW_COURSE','course_');
|
|
|
|
define('MESSAGE_VIEW_SEARCH','search');
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2010-07-07 07:49:08 +00:00
|
|
|
define('MESSAGE_SEARCH_MAX_RESULTS', 200);
|
|
|
|
|
2010-10-21 06:40:15 +00:00
|
|
|
define('MESSAGE_CONTACTS_PER_PAGE',10);
|
2011-02-20 10:25:21 +08:00
|
|
|
define('MESSAGE_MAX_COURSE_NAME_LENGTH', 30);
|
2010-10-21 06:40:15 +00:00
|
|
|
|
2011-05-20 15:10:27 +01:00
|
|
|
/**
|
|
|
|
* Define contants for messaging default settings population. For unambiguity of
|
|
|
|
* plugin developer intentions we use 4-bit value (LSB numbering):
|
|
|
|
* bit 0 - whether to send message when user is loggedin (MESSAGE_DEFAULT_LOGGEDIN)
|
|
|
|
* bit 1 - whether to send message when user is loggedoff (MESSAGE_DEFAULT_LOGGEDOFF)
|
|
|
|
* bit 2..3 - messaging permission (MESSAGE_DISALLOWED|MESSAGE_PERMITTED|MESSAGE_FORCED)
|
|
|
|
*
|
|
|
|
* MESSAGE_PERMITTED_MASK contains the mask we use to distinguish permission setting
|
|
|
|
*/
|
|
|
|
|
|
|
|
define('MESSAGE_DEFAULT_LOGGEDIN', 0x01); // 0001
|
|
|
|
define('MESSAGE_DEFAULT_LOGGEDOFF', 0x02); // 0010
|
|
|
|
|
|
|
|
define('MESSAGE_DISALLOWED', 0x04); // 0100
|
|
|
|
define('MESSAGE_PERMITTED', 0x08); // 1000
|
|
|
|
define('MESSAGE_FORCED', 0x0c); // 1100
|
|
|
|
|
|
|
|
define('MESSAGE_PERMITTED_MASK', 0x0c); // 1100
|
|
|
|
|
2011-05-13 11:13:13 +01:00
|
|
|
/**
|
|
|
|
* Set default value for default outputs permitted setting
|
|
|
|
*/
|
2011-05-17 10:47:21 +01:00
|
|
|
define('MESSAGE_DEFAULT_PERMITTED', 'permitted');
|
2011-05-13 11:13:13 +01:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Print the selector that allows the user to view their contacts, course participants, their recent
|
|
|
|
* conversations etc
|
|
|
|
*
|
|
|
|
* @param int $countunreadtotal how many unread messages does the user have?
|
|
|
|
* @param int $viewing What is the user viewing? ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_SEARCH etc
|
|
|
|
* @param object $user1 the user whose messages are being viewed
|
|
|
|
* @param object $user2 the user $user1 is talking to
|
|
|
|
* @param array $blockedusers an array of users blocked by $user1
|
|
|
|
* @param array $onlinecontacts an array of $user1's online contacts
|
|
|
|
* @param array $offlinecontacts an array of $user1's offline contacts
|
|
|
|
* @param array $strangers an array of users who have messaged $user1 who aren't contacts
|
2012-11-12 15:25:49 +08:00
|
|
|
* @param bool $showactionlinks show action links (add/remove contact etc)
|
2011-05-27 09:28:09 +01:00
|
|
|
* @param int $page if there are so many users listed that they have to be split into pages what page are we viewing
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-11-12 15:25:49 +08:00
|
|
|
function message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showactionlinks, $page=0) {
|
2010-06-25 08:16:10 +00:00
|
|
|
global $PAGE;
|
2010-07-04 18:36:34 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('div', array('class' => 'contactselector mdl-align'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
//if 0 unread messages and they've requested unread messages then show contacts
|
2011-02-16 15:48:59 +08:00
|
|
|
if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
|
|
|
|
$viewing = MESSAGE_VIEW_CONTACTS;
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
2010-06-29 03:01:14 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
//if they have no blocked users and they've requested blocked users switch them over to contacts
|
2011-02-16 15:48:59 +08:00
|
|
|
if (count($blockedusers) == 0 && $viewing == MESSAGE_VIEW_BLOCKED) {
|
|
|
|
$viewing = MESSAGE_VIEW_CONTACTS;
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
$onlyactivecourses = true;
|
|
|
|
$courses = enrol_get_users_courses($user1->id, $onlyactivecourses);
|
|
|
|
$coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them
|
2010-06-25 08:37:01 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
$strunreadmessages = null;
|
|
|
|
if ($countunreadtotal>0) { //if there are unread messages
|
|
|
|
$strunreadmessages = get_string('unreadmessages','message', $countunreadtotal);
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages, $user1);
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
if ($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
|
2012-11-12 15:25:49 +08:00
|
|
|
message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 1, $showactionlinks,$strunreadmessages, $user2);
|
2011-02-16 15:48:59 +08:00
|
|
|
} else if ($viewing == MESSAGE_VIEW_CONTACTS || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
|
2012-11-12 15:25:49 +08:00
|
|
|
message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 0, $showactionlinks, $strunreadmessages, $user2);
|
2011-02-16 15:48:59 +08:00
|
|
|
} else if ($viewing == MESSAGE_VIEW_BLOCKED) {
|
2012-11-12 15:25:49 +08:00
|
|
|
message_print_blocked_users($blockedusers, $PAGE->url, $showactionlinks, null, $user2);
|
2011-02-16 15:48:59 +08:00
|
|
|
} else if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) {
|
2011-01-12 17:15:43 +08:00
|
|
|
$courseidtoshow = intval(substr($viewing, 7));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if (!empty($courseidtoshow)
|
|
|
|
&& array_key_exists($courseidtoshow, $coursecontexts)
|
|
|
|
&& has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) {
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showactionlinks, null, $page, $user2);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2014-09-27 21:54:03 +08:00
|
|
|
// Only show the search button if we're viewing our own contacts.
|
|
|
|
if ($viewing == MESSAGE_VIEW_CONTACTS && $user2 == null) {
|
2012-11-12 15:25:49 +08:00
|
|
|
echo html_writer::start_tag('form', array('action' => 'index.php','method' => 'GET'));
|
|
|
|
echo html_writer::start_tag('fieldset');
|
|
|
|
$managebuttonclass = 'visible';
|
|
|
|
$strmanagecontacts = get_string('search','message');
|
|
|
|
echo html_writer::empty_tag('input', array('type' => 'hidden','name' => 'viewing','value' => MESSAGE_VIEW_SEARCH));
|
|
|
|
echo html_writer::empty_tag('input', array('type' => 'submit','value' => $strmanagecontacts,'class' => $managebuttonclass));
|
|
|
|
echo html_writer::end_tag('fieldset');
|
|
|
|
echo html_writer::end_tag('form');
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
2010-11-02 06:09:10 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
echo html_writer::end_tag('div');
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Print course participants. Called by message_print_contact_selector()
|
|
|
|
*
|
|
|
|
* @param object $context the course context
|
|
|
|
* @param int $courseid the course ID
|
|
|
|
* @param string $contactselecturl the url to send the user to when a contact's name is clicked
|
|
|
|
* @param bool $showactionlinks show action links (add/remove contact etc) next to the users
|
|
|
|
* @param string $titletodisplay Optionally specify a title to display above the participants
|
|
|
|
* @param int $page if there are so many users listed that they have to be split into pages what page are we viewing
|
|
|
|
* @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of participants
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-11-02 06:09:10 +00:00
|
|
|
function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $page=0, $user2=null) {
|
2010-10-21 06:40:15 +00:00
|
|
|
global $DB, $USER, $PAGE, $OUTPUT;
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if (empty($titletodisplay)) {
|
|
|
|
$titletodisplay = get_string('participants');
|
|
|
|
}
|
|
|
|
|
2010-10-21 06:40:15 +00:00
|
|
|
$countparticipants = count_enrolled_users($context);
|
2012-11-05 15:47:30 +08:00
|
|
|
|
|
|
|
list($esql, $params) = get_enrolled_sql($context);
|
|
|
|
$params['mcuserid'] = $USER->id;
|
|
|
|
$ufields = user_picture::fields('u');
|
|
|
|
|
|
|
|
$sql = "SELECT $ufields, mc.id as contactlistid, mc.blocked
|
|
|
|
FROM {user} u
|
|
|
|
JOIN ($esql) je ON je.id = u.id
|
|
|
|
LEFT JOIN {message_contacts} mc ON mc.contactid = u.id AND mc.userid = :mcuserid
|
|
|
|
WHERE u.deleted = 0";
|
|
|
|
|
|
|
|
$participants = $DB->get_records_sql($sql, $params, $page * MESSAGE_CONTACTS_PER_PAGE, MESSAGE_CONTACTS_PER_PAGE);
|
2011-01-27 11:46:37 +01:00
|
|
|
|
2010-10-21 06:40:15 +00:00
|
|
|
$pagingbar = new paging_bar($countparticipants, $page, MESSAGE_CONTACTS_PER_PAGE, $PAGE->url, 'page');
|
2015-05-25 15:42:17 +08:00
|
|
|
$pagingbar->maxdisplay = 4;
|
2010-10-21 06:40:15 +00:00
|
|
|
echo $OUTPUT->render($pagingbar);
|
2010-07-04 18:36:34 +00:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::start_tag('div', array('id' => 'message_participants', 'class' => 'boxaligncenter'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div' , $titletodisplay, array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
$users = '';
|
2010-06-25 08:16:10 +00:00
|
|
|
foreach ($participants as $participant) {
|
|
|
|
if ($participant->id != $USER->id) {
|
2012-11-05 15:47:30 +08:00
|
|
|
|
|
|
|
$iscontact = false;
|
|
|
|
$isblocked = false;
|
|
|
|
if ( $participant->contactlistid ) {
|
|
|
|
if ($participant->blocked == 0) {
|
|
|
|
// Is contact. Is not blocked.
|
|
|
|
$iscontact = true;
|
|
|
|
$isblocked = false;
|
|
|
|
} else {
|
|
|
|
// Is blocked.
|
|
|
|
$iscontact = false;
|
|
|
|
$isblocked = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$participant->messagecount = 0;//todo it would be nice if the course participant could report new messages
|
2014-11-14 15:49:18 +13:00
|
|
|
$content = message_print_contactlist_user($participant, $iscontact, $isblocked,
|
|
|
|
$contactselecturl, $showactionlinks, $user2);
|
|
|
|
$users .= html_writer::tag('li', $content);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 15:49:18 +13:00
|
|
|
if (strlen($users) > 0) {
|
|
|
|
echo html_writer::tag('ul', $users, array('id' => 'message-courseparticipants', 'class' => 'message-contacts'));
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::end_tag('div');
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Retrieve users blocked by $user1
|
|
|
|
*
|
|
|
|
* @param object $user1 the user whose messages are being viewed
|
|
|
|
* @param object $user2 the user $user1 is talking to. If they are being blocked
|
|
|
|
* they will have a variable called 'isblocked' added to their user object
|
|
|
|
* @return array the users blocked by $user1
|
|
|
|
*/
|
2011-01-12 17:15:43 +08:00
|
|
|
function message_get_blocked_users($user1=null, $user2=null) {
|
2010-06-25 08:16:10 +00:00
|
|
|
global $DB, $USER;
|
|
|
|
|
|
|
|
if (empty($user1)) {
|
|
|
|
$user1 = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($user2)) {
|
|
|
|
$user2->isblocked = false;
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:31:23 +08:00
|
|
|
$blockedusers = array();
|
|
|
|
|
2010-07-04 18:36:34 +00:00
|
|
|
$userfields = user_picture::fields('u', array('lastaccess'));
|
|
|
|
$blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount
|
|
|
|
FROM {message_contacts} mc
|
|
|
|
JOIN {user} u ON u.id = mc.contactid
|
|
|
|
LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1
|
2015-06-04 22:27:38 +02:00
|
|
|
WHERE u.deleted = 0 AND mc.userid = :user1id2 AND mc.blocked = 1
|
2010-07-04 18:36:34 +00:00
|
|
|
GROUP BY $userfields
|
|
|
|
ORDER BY u.firstname ASC";
|
2011-01-12 17:15:43 +08:00
|
|
|
$rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1' => $user1->id, 'user1id2' => $user1->id));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-05-10 16:31:23 +08:00
|
|
|
foreach($rs as $rd) {
|
|
|
|
$blockedusers[] = $rd;
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-05-10 16:31:23 +08:00
|
|
|
if (!empty($user2) && $user2->id == $rd->id) {
|
|
|
|
$user2->isblocked = true;
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-10 16:31:23 +08:00
|
|
|
$rs->close();
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
return $blockedusers;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Print users blocked by $user1. Called by message_print_contact_selector()
|
|
|
|
*
|
|
|
|
* @param array $blockedusers the users blocked by $user1
|
|
|
|
* @param string $contactselecturl the url to send the user to when a contact's name is clicked
|
|
|
|
* @param bool $showactionlinks show action links (add/remove contact etc) next to the users
|
|
|
|
* @param string $titletodisplay Optionally specify a title to display above the participants
|
|
|
|
* @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of blocked users
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-01-12 17:15:43 +08:00
|
|
|
function message_print_blocked_users($blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $user2=null) {
|
2014-11-14 15:49:18 +13:00
|
|
|
global $OUTPUT;
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
$countblocked = count($blockedusers);
|
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::start_tag('div', array('id' => 'message_contacts', 'class' => 'boxaligncenter'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
if (!empty($titletodisplay)) {
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div', $titletodisplay, array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($countblocked) {
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div', get_string('blockedusers', 'message', $countblocked), array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
$isuserblocked = true;
|
|
|
|
$isusercontact = false;
|
2014-11-14 15:49:18 +13:00
|
|
|
$blockeduserslist = '';
|
2010-06-25 08:16:10 +00:00
|
|
|
foreach ($blockedusers as $blockeduser) {
|
2014-11-14 15:49:18 +13:00
|
|
|
$content = message_print_contactlist_user($blockeduser, $isusercontact, $isuserblocked,
|
|
|
|
$contactselecturl, $showactionlinks, $user2);
|
|
|
|
$blockeduserslist .= html_writer::tag('li', $content);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('ul', $blockeduserslist, array('id' => 'message-blockedusers', 'class' => 'message-contacts'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::end_tag('div');
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Retrieve $user1's contacts (online, offline and strangers)
|
|
|
|
*
|
|
|
|
* @param object $user1 the user whose messages are being viewed
|
|
|
|
* @param object $user2 the user $user1 is talking to. If they are a contact
|
|
|
|
* they will have a variable called 'iscontact' added to their user object
|
|
|
|
* @return array containing 3 arrays. array($onlinecontacts, $offlinecontacts, $strangers)
|
|
|
|
*/
|
2011-01-12 17:15:43 +08:00
|
|
|
function message_get_contacts($user1=null, $user2=null) {
|
2010-06-25 08:16:10 +00:00
|
|
|
global $DB, $CFG, $USER;
|
2004-12-16 12:40:36 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (empty($user1)) {
|
|
|
|
$user1 = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($user2)) {
|
|
|
|
$user2->iscontact = false;
|
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
|
|
|
$timetoshowusers = 300; //Seconds default
|
|
|
|
if (isset($CFG->block_online_users_timetosee)) {
|
|
|
|
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
|
|
|
|
}
|
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
// time which a user is counting as being active since
|
|
|
|
$timefrom = time()-$timetoshowusers;
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
// people in our contactlist who are online
|
|
|
|
$onlinecontacts = array();
|
|
|
|
// people in our contactlist who are offline
|
|
|
|
$offlinecontacts = array();
|
|
|
|
// people who are not in our contactlist but have sent us a message
|
2010-07-01 02:12:46 +00:00
|
|
|
$strangers = array();
|
|
|
|
|
2010-07-04 18:36:34 +00:00
|
|
|
$userfields = user_picture::fields('u', array('lastaccess'));
|
2008-01-15 13:48:34 +00:00
|
|
|
|
2009-11-01 12:22:45 +00:00
|
|
|
// get all in our contactlist who are not blocked in our contact list
|
2008-01-15 13:48:34 +00:00
|
|
|
// and count messages we have waiting from each of them
|
2010-07-04 18:36:34 +00:00
|
|
|
$contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount
|
2008-06-05 13:41:16 +00:00
|
|
|
FROM {message_contacts} mc
|
|
|
|
JOIN {user} u ON u.id = mc.contactid
|
|
|
|
LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ?
|
2015-06-04 22:27:38 +02:00
|
|
|
WHERE u.deleted = 0 AND mc.userid = ? AND mc.blocked = 0
|
2010-07-04 18:36:34 +00:00
|
|
|
GROUP BY $userfields
|
2009-09-16 22:28:44 +00:00
|
|
|
ORDER BY u.firstname ASC";
|
2008-06-05 13:41:16 +00:00
|
|
|
|
2011-01-04 17:36:47 +01:00
|
|
|
$rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id));
|
|
|
|
foreach ($rs as $rd) {
|
|
|
|
if ($rd->lastaccess >= $timefrom) {
|
|
|
|
// they have been active recently, so are counted online
|
|
|
|
$onlinecontacts[] = $rd;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$offlinecontacts[] = $rd;
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if (!empty($user2) && $user2->id == $rd->id) {
|
2011-01-04 17:36:47 +01:00
|
|
|
$user2->iscontact = true;
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2008-01-15 13:48:34 +00:00
|
|
|
}
|
2011-01-04 17:36:47 +01:00
|
|
|
$rs->close();
|
2008-01-15 13:48:34 +00:00
|
|
|
|
|
|
|
// get messages from anyone who isn't in our contact list and count the number
|
|
|
|
// of messages we have from each of them
|
2010-07-04 18:36:34 +00:00
|
|
|
$strangersql = "SELECT $userfields, count(m.id) as messagecount
|
2008-06-05 13:41:16 +00:00
|
|
|
FROM {message} m
|
|
|
|
JOIN {user} u ON u.id = m.useridfrom
|
|
|
|
LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto
|
2015-06-04 22:27:38 +02:00
|
|
|
WHERE u.deleted = 0 AND mc.id IS NULL AND m.useridto = ?
|
2010-07-04 18:36:34 +00:00
|
|
|
GROUP BY $userfields
|
2009-09-16 22:28:44 +00:00
|
|
|
ORDER BY u.firstname ASC";
|
2008-06-05 13:41:16 +00:00
|
|
|
|
2011-01-04 17:36:47 +01:00
|
|
|
$rs = $DB->get_recordset_sql($strangersql, array($USER->id));
|
2014-05-19 11:59:32 +08:00
|
|
|
// Add user id as array index, so supportuser and noreply user don't get duplicated (if they are real users).
|
2011-01-04 17:36:47 +01:00
|
|
|
foreach ($rs as $rd) {
|
2014-05-19 11:59:32 +08:00
|
|
|
$strangers[$rd->id] = $rd;
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2011-01-04 17:36:47 +01:00
|
|
|
$rs->close();
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2014-05-19 11:59:32 +08:00
|
|
|
// Add noreply user and support user to the list, if they don't exist.
|
2013-11-07 12:56:01 +08:00
|
|
|
$supportuser = core_user::get_support_user();
|
2014-05-19 11:59:32 +08:00
|
|
|
if (!isset($strangers[$supportuser->id])) {
|
|
|
|
$supportuser->messagecount = message_count_unread_messages($USER, $supportuser);
|
|
|
|
if ($supportuser->messagecount > 0) {
|
|
|
|
$strangers[$supportuser->id] = $supportuser;
|
|
|
|
}
|
2013-11-07 12:56:01 +08:00
|
|
|
}
|
2014-05-19 11:59:32 +08:00
|
|
|
|
2013-11-07 12:56:01 +08:00
|
|
|
$noreplyuser = core_user::get_noreply_user();
|
2014-05-19 11:59:32 +08:00
|
|
|
if (!isset($strangers[$noreplyuser->id])) {
|
|
|
|
$noreplyuser->messagecount = message_count_unread_messages($USER, $noreplyuser);
|
|
|
|
if ($noreplyuser->messagecount > 0) {
|
|
|
|
$strangers[$noreplyuser->id] = $noreplyuser;
|
|
|
|
}
|
2013-11-07 12:56:01 +08:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
return array($onlinecontacts, $offlinecontacts, $strangers);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Print $user1's contacts. Called by message_print_contact_selector()
|
|
|
|
*
|
|
|
|
* @param array $onlinecontacts $user1's contacts which are online
|
|
|
|
* @param array $offlinecontacts $user1's contacts which are offline
|
|
|
|
* @param array $strangers users which are not contacts but who have messaged $user1
|
|
|
|
* @param string $contactselecturl the url to send the user to when a contact's name is clicked
|
|
|
|
* @param int $minmessages The minimum number of unread messages required from a user for them to be displayed
|
|
|
|
* Typically 0 (show all contacts) or 1 (only show contacts from whom we have a new message)
|
|
|
|
* @param bool $showactionlinks show action links (add/remove contact etc) next to the users
|
|
|
|
* @param string $titletodisplay Optionally specify a title to display above the participants
|
|
|
|
* @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of contacts
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-01-12 17:15:43 +08:00
|
|
|
function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null, $user2=null) {
|
2010-06-25 08:16:10 +00:00
|
|
|
global $CFG, $PAGE, $OUTPUT;
|
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
$countonlinecontacts = count($onlinecontacts);
|
|
|
|
$countofflinecontacts = count($offlinecontacts);
|
|
|
|
$countstrangers = count($strangers);
|
2011-02-16 15:48:59 +08:00
|
|
|
$isuserblocked = null;
|
2008-01-15 13:48:34 +00:00
|
|
|
|
2005-04-21 04:57:54 +00:00
|
|
|
if ($countonlinecontacts + $countofflinecontacts == 0) {
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::tag('div', get_string('contactlistempty', 'message'), array('class' => 'heading'));
|
2005-04-20 19:28:14 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (!empty($titletodisplay)) {
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div', $titletodisplay, array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
if($countonlinecontacts) {
|
2012-11-12 15:25:49 +08:00
|
|
|
// Print out list of online contacts.
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (empty($titletodisplay)) {
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div',
|
|
|
|
get_string('onlinecontacts', 'message', $countonlinecontacts),
|
|
|
|
array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
$isuserblocked = false;
|
|
|
|
$isusercontact = true;
|
2014-11-14 15:49:18 +13:00
|
|
|
$contacts = '';
|
2008-01-15 13:48:34 +00:00
|
|
|
foreach ($onlinecontacts as $contact) {
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($minmessages == 0 || $contact->messagecount >= $minmessages) {
|
2014-11-14 15:49:18 +13:00
|
|
|
$content = message_print_contactlist_user($contact, $isusercontact, $isuserblocked,
|
|
|
|
$contactselecturl, $showactionlinks, $user2);
|
|
|
|
$contacts .= html_writer::tag('li', $content);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2005-04-20 19:28:14 +00:00
|
|
|
}
|
2014-11-14 15:49:18 +13:00
|
|
|
if (strlen($contacts) > 0) {
|
|
|
|
echo html_writer::tag('ul', $contacts, array('id' => 'message-onlinecontacts', 'class' => 'message-contacts'));
|
|
|
|
}
|
2008-01-15 13:48:34 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
if ($countofflinecontacts) {
|
2012-11-12 15:25:49 +08:00
|
|
|
// Print out list of offline contacts.
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (empty($titletodisplay)) {
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div',
|
|
|
|
get_string('offlinecontacts', 'message', $countofflinecontacts),
|
|
|
|
array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2007-11-21 07:53:42 +00:00
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
$isuserblocked = false;
|
|
|
|
$isusercontact = true;
|
2014-11-14 15:49:18 +13:00
|
|
|
$contacts = '';
|
2008-01-15 13:48:34 +00:00
|
|
|
foreach ($offlinecontacts as $contact) {
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($minmessages == 0 || $contact->messagecount >= $minmessages) {
|
2014-11-14 15:49:18 +13:00
|
|
|
$content = message_print_contactlist_user($contact, $isusercontact, $isuserblocked,
|
|
|
|
$contactselecturl, $showactionlinks, $user2);
|
|
|
|
$contacts .= html_writer::tag('li', $content);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2005-04-20 19:28:14 +00:00
|
|
|
}
|
2014-11-14 15:49:18 +13:00
|
|
|
if (strlen($contacts) > 0) {
|
|
|
|
echo html_writer::tag('ul', $contacts, array('id' => 'message-offlinecontacts', 'class' => 'message-contacts'));
|
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
}
|
2005-04-20 19:28:14 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Print out list of incoming contacts.
|
2008-01-15 13:48:34 +00:00
|
|
|
if ($countstrangers) {
|
2014-11-14 15:49:18 +13:00
|
|
|
echo html_writer::tag('div', get_string('incomingcontacts', 'message', $countstrangers), array('class' => 'heading'));
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
$isuserblocked = false;
|
|
|
|
$isusercontact = false;
|
2014-11-14 15:49:18 +13:00
|
|
|
$contacts = '';
|
2008-01-15 13:48:34 +00:00
|
|
|
foreach ($strangers as $stranger) {
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($minmessages == 0 || $stranger->messagecount >= $minmessages) {
|
2014-11-14 15:49:18 +13:00
|
|
|
$content = message_print_contactlist_user($stranger, $isusercontact, $isuserblocked,
|
|
|
|
$contactselecturl, $showactionlinks, $user2);
|
|
|
|
$contacts .= html_writer::tag('li', $content);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2005-04-23 14:38:57 +00:00
|
|
|
}
|
2014-11-14 15:49:18 +13:00
|
|
|
if (strlen($contacts) > 0) {
|
|
|
|
echo html_writer::tag('ul', $contacts, array('id' => 'message-incommingcontacts', 'class' => 'message-contacts'));
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
}
|
2005-04-20 19:28:14 +00:00
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::tag('div','('.get_string('addsomecontactsincoming', 'message').')',array('class' => 'note'));
|
2005-04-20 19:28:14 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Print a select box allowing the user to choose to view new messages, course participants etc.
|
|
|
|
*
|
|
|
|
* Called by message_print_contact_selector()
|
|
|
|
* @param int $viewing What page is the user viewing ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_RECENT_CONVERSATIONS etc
|
|
|
|
* @param array $courses array of course objects. The courses the user is enrolled in.
|
|
|
|
* @param array $coursecontexts array of course contexts. Keyed on course id.
|
|
|
|
* @param int $countunreadtotal how many unread messages does the user have?
|
|
|
|
* @param int $countblocked how many users has the current user blocked?
|
2012-11-12 15:25:49 +08:00
|
|
|
* @param stdClass $user1 The user whose messages we are viewing.
|
2011-05-27 09:28:09 +01:00
|
|
|
* @param string $strunreadmessages a preconstructed message about the number of unread messages the user has
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-11-12 15:25:49 +08:00
|
|
|
function message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages, $user1 = null) {
|
2014-12-01 14:14:25 +13:00
|
|
|
global $PAGE;
|
2010-06-29 03:01:14 +00:00
|
|
|
$options = array();
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
if ($countunreadtotal>0) { //if there are unread messages
|
2011-02-16 15:48:59 +08:00
|
|
|
$options[MESSAGE_VIEW_UNREAD_MESSAGES] = $strunreadmessages;
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 11:35:45 +08:00
|
|
|
$str = get_string('contacts', 'message');
|
2011-02-16 15:48:59 +08:00
|
|
|
$options[MESSAGE_VIEW_CONTACTS] = $str;
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2011-02-16 15:48:59 +08:00
|
|
|
$options[MESSAGE_VIEW_RECENT_CONVERSATIONS] = get_string('mostrecentconversations', 'message');
|
|
|
|
$options[MESSAGE_VIEW_RECENT_NOTIFICATIONS] = get_string('mostrecentnotifications', 'message');
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
if (!empty($courses)) {
|
|
|
|
$courses_options = array();
|
|
|
|
|
|
|
|
foreach($courses as $course) {
|
|
|
|
if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) {
|
2011-02-20 10:25:21 +08:00
|
|
|
//Not using short_text() as we want the end of the course name. Not the beginning.
|
2011-09-07 11:46:28 +12:00
|
|
|
$shortname = format_string($course->shortname, true, array('context' => $coursecontexts[$course->id]));
|
2013-08-06 20:58:28 +02:00
|
|
|
if (core_text::strlen($shortname) > MESSAGE_MAX_COURSE_NAME_LENGTH) {
|
|
|
|
$courses_options[MESSAGE_VIEW_COURSE.$course->id] = '...'.core_text::substr($shortname, -MESSAGE_MAX_COURSE_NAME_LENGTH);
|
2011-02-20 10:25:21 +08:00
|
|
|
} else {
|
2011-09-07 11:46:28 +12:00
|
|
|
$courses_options[MESSAGE_VIEW_COURSE.$course->id] = $shortname;
|
2011-02-20 10:25:21 +08:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($courses_options)) {
|
2011-01-12 17:15:43 +08:00
|
|
|
$options[] = array(get_string('courses') => $courses_options);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-01 09:01:45 +00:00
|
|
|
if ($countblocked>0) {
|
2011-01-12 17:15:43 +08:00
|
|
|
$str = get_string('blockedusers','message', $countblocked);
|
2011-02-16 15:48:59 +08:00
|
|
|
$options[MESSAGE_VIEW_BLOCKED] = $str;
|
2010-11-01 09:01:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 14:14:25 +13:00
|
|
|
$select = new single_select($PAGE->url, 'viewing', $options, $viewing, false);
|
|
|
|
$select->set_label(get_string('messagenavigation', 'message'));
|
|
|
|
|
|
|
|
$renderer = $PAGE->get_renderer('core');
|
|
|
|
echo $renderer->render($select);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Load the course contexts for all of the users courses
|
|
|
|
*
|
|
|
|
* @param array $courses array of course objects. The courses the user is enrolled in.
|
|
|
|
* @return array of course contexts
|
|
|
|
*/
|
2011-01-12 17:15:43 +08:00
|
|
|
function message_get_course_contexts($courses) {
|
2010-06-25 08:16:10 +00:00
|
|
|
$coursecontexts = array();
|
|
|
|
|
|
|
|
foreach($courses as $course) {
|
2012-08-02 11:20:48 +08:00
|
|
|
$coursecontexts[$course->id] = context_course::instance($course->id);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $coursecontexts;
|
|
|
|
}
|
|
|
|
|
2010-07-02 08:08:11 +00:00
|
|
|
/**
|
|
|
|
* strip off action parameters like 'removecontact'
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param moodle_url/string $moodleurl a URL. Typically the current page URL.
|
|
|
|
* @return string the URL minus parameters that perform actions (like adding/removing/blocking a contact).
|
2010-07-02 08:08:11 +00:00
|
|
|
*/
|
2010-06-25 08:16:10 +00:00
|
|
|
function message_remove_url_params($moodleurl) {
|
|
|
|
$newurl = new moodle_url($moodleurl);
|
|
|
|
$newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact');
|
|
|
|
return $newurl->out();
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Count the number of messages with a field having a specified value.
|
|
|
|
* if $field is empty then return count of the whole array
|
|
|
|
* if $field is non-existent then return 0
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param array $messagearray array of message objects
|
|
|
|
* @param string $field the field to inspect on the message objects
|
|
|
|
* @param string $value the value to test the field against
|
|
|
|
*/
|
2004-12-28 13:49:15 +00:00
|
|
|
function message_count_messages($messagearray, $field='', $value='') {
|
|
|
|
if (!is_array($messagearray)) return 0;
|
|
|
|
if ($field == '' or empty($messagearray)) return count($messagearray);
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
$count = 0;
|
|
|
|
foreach ($messagearray as $message) {
|
|
|
|
$count += ($message->$field == $value) ? 1 : 0;
|
|
|
|
}
|
|
|
|
return $count;
|
2004-12-16 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the count of unread messages for user. Either from a specific user or from all users.
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2010-06-25 08:16:10 +00:00
|
|
|
* @param object $user1 the first user. Defaults to $USER
|
|
|
|
* @param object $user2 the second user. If null this function will count all of user 1's unread messages.
|
|
|
|
* @return int the count of $user1's unread messages
|
|
|
|
*/
|
|
|
|
function message_count_unread_messages($user1=null, $user2=null) {
|
|
|
|
global $USER, $DB;
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (empty($user1)) {
|
|
|
|
$user1 = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($user2)) {
|
|
|
|
return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?",
|
|
|
|
array($user1->id, $user2->id), "COUNT('id')");
|
|
|
|
} else {
|
|
|
|
return $DB->count_records_select('message', "useridto = ?",
|
|
|
|
array($user1->id), "COUNT('id')");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Count the number of users blocked by $user1
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
|
|
|
* @param object $user1 user object
|
|
|
|
* @return int the number of blocked users
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
2010-06-25 08:16:10 +00:00
|
|
|
function message_count_blocked_users($user1=null) {
|
|
|
|
global $USER, $DB;
|
|
|
|
|
|
|
|
if (empty($user1)) {
|
|
|
|
$user1 = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = "SELECT count(mc.id)
|
|
|
|
FROM {message_contacts} mc
|
|
|
|
WHERE mc.userid = :userid AND mc.blocked = 1";
|
2011-01-12 17:15:43 +08:00
|
|
|
$params = array('userid' => $user1->id);
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
return $DB->count_records_sql($sql, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-01-12 17:15:43 +08:00
|
|
|
* Print the search form and search results if a search has been performed
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param boolean $advancedsearch show basic or advanced search form
|
|
|
|
* @param object $user1 the current user
|
|
|
|
* @return boolean true if a search was performed
|
2010-06-25 08:16:10 +00:00
|
|
|
*/
|
|
|
|
function message_print_search($advancedsearch = false, $user1=null) {
|
|
|
|
$frm = data_submitted();
|
2010-07-04 18:36:34 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$doingsearch = false;
|
|
|
|
if ($frm) {
|
2011-01-27 11:46:37 +01:00
|
|
|
if (confirm_sesskey()) {
|
|
|
|
$doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name));
|
|
|
|
} else {
|
|
|
|
$frm = false;
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (!empty($frm->combinedsearch)) {
|
|
|
|
$combinedsearchstring = $frm->combinedsearch;
|
|
|
|
} else {
|
2010-10-25 09:29:34 +00:00
|
|
|
//$combinedsearchstring = get_string('searchcombined','message').'...';
|
|
|
|
$combinedsearchstring = '';
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if ($doingsearch) {
|
|
|
|
if ($advancedsearch) {
|
2010-07-04 18:36:34 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$messagesearch = '';
|
|
|
|
if (!empty($frm->keywords)) {
|
|
|
|
$messagesearch = $frm->keywords;
|
|
|
|
}
|
|
|
|
$personsearch = '';
|
|
|
|
if (!empty($frm->name)) {
|
|
|
|
$personsearch = $frm->name;
|
|
|
|
}
|
|
|
|
include('search_advanced.html');
|
|
|
|
} else {
|
|
|
|
include('search.html');
|
|
|
|
}
|
|
|
|
|
|
|
|
$showicontext = false;
|
|
|
|
message_print_search_results($frm, $showicontext, $user1);
|
|
|
|
|
|
|
|
return true;
|
2004-12-16 12:40:36 +00:00
|
|
|
} else {
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
if ($advancedsearch) {
|
|
|
|
$personsearch = $messagesearch = '';
|
|
|
|
include('search_advanced.html');
|
|
|
|
} else {
|
|
|
|
include('search.html');
|
|
|
|
}
|
|
|
|
return false;
|
2004-12-16 12:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Get the users recent conversations meaning all the people they've recently
|
|
|
|
* sent or received a message from plus the most recent message sent to or received from each other user
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $user the current user
|
|
|
|
* @param int $limitfrom can be used for paging
|
|
|
|
* @param int $limitto can be used for paging
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) {
|
|
|
|
global $DB;
|
|
|
|
|
2014-06-26 16:45:05 +01:00
|
|
|
$userfields = user_picture::fields('otheruser', array('lastaccess'));
|
|
|
|
|
|
|
|
// This query retrieves the most recent message received from or sent to
|
|
|
|
// seach other user.
|
|
|
|
//
|
|
|
|
// If two messages have the same timecreated, we take the one with the
|
|
|
|
// larger id.
|
|
|
|
//
|
|
|
|
// There is a separate query for read and unread messages as they are stored
|
|
|
|
// in different tables. They were originally retrieved in one query but it
|
|
|
|
// was so large that it was difficult to be confident in its correctness.
|
2015-02-25 14:11:19 +08:00
|
|
|
$uniquefield = $DB->sql_concat('message.useridfrom', "'-'", 'message.useridto');
|
2015-02-12 14:11:08 +08:00
|
|
|
$sql = "SELECT $uniquefield, $userfields,
|
2014-06-26 16:45:05 +01:00
|
|
|
message.id as mid, message.notification, message.smallmessage, message.fullmessage,
|
|
|
|
message.fullmessagehtml, message.fullmessageformat, message.timecreated,
|
|
|
|
contact.id as contactlistid, contact.blocked
|
|
|
|
FROM {message_read} message
|
2015-02-12 14:11:08 +08:00
|
|
|
JOIN (
|
|
|
|
SELECT MAX(id) AS messageid,
|
|
|
|
matchedmessage.useridto,
|
|
|
|
matchedmessage.useridfrom
|
|
|
|
FROM {message_read} matchedmessage
|
|
|
|
INNER JOIN (
|
|
|
|
SELECT MAX(recentmessages.timecreated) timecreated,
|
|
|
|
recentmessages.useridfrom,
|
|
|
|
recentmessages.useridto
|
|
|
|
FROM {message_read} recentmessages
|
2015-03-24 17:11:15 -07:00
|
|
|
WHERE (
|
|
|
|
(recentmessages.useridfrom = :userid1 AND recentmessages.timeuserfromdeleted = 0) OR
|
|
|
|
(recentmessages.useridto = :userid2 AND recentmessages.timeusertodeleted = 0)
|
|
|
|
)
|
2015-02-12 14:11:08 +08:00
|
|
|
GROUP BY recentmessages.useridfrom, recentmessages.useridto
|
|
|
|
) recent ON matchedmessage.useridto = recent.useridto
|
|
|
|
AND matchedmessage.useridfrom = recent.useridfrom
|
|
|
|
AND matchedmessage.timecreated = recent.timecreated
|
2015-03-24 17:11:15 -07:00
|
|
|
WHERE (
|
|
|
|
(matchedmessage.useridfrom = :userid6 AND matchedmessage.timeuserfromdeleted = 0) OR
|
|
|
|
(matchedmessage.useridto = :userid7 AND matchedmessage.timeusertodeleted = 0)
|
|
|
|
)
|
2015-02-12 14:11:08 +08:00
|
|
|
GROUP BY matchedmessage.useridto, matchedmessage.useridfrom
|
|
|
|
) messagesubset ON messagesubset.messageid = message.id
|
|
|
|
JOIN {user} otheruser ON (message.useridfrom = :userid4 AND message.useridto = otheruser.id)
|
|
|
|
OR (message.useridto = :userid5 AND message.useridfrom = otheruser.id)
|
2016-02-09 13:38:56 +08:00
|
|
|
LEFT JOIN {message_contacts} contact ON contact.userid = :userid3 AND contact.contactid = otheruser.id
|
2015-02-12 14:11:08 +08:00
|
|
|
WHERE otheruser.deleted = 0 AND message.notification = 0
|
2014-06-26 16:45:05 +01:00
|
|
|
ORDER BY message.timecreated DESC";
|
2015-02-12 14:11:08 +08:00
|
|
|
$params = array(
|
|
|
|
'userid1' => $user->id,
|
|
|
|
'userid2' => $user->id,
|
|
|
|
'userid3' => $user->id,
|
|
|
|
'userid4' => $user->id,
|
|
|
|
'userid5' => $user->id,
|
2015-03-24 17:11:15 -07:00
|
|
|
'userid6' => $user->id,
|
|
|
|
'userid7' => $user->id
|
2015-02-12 14:11:08 +08:00
|
|
|
);
|
2014-06-26 16:45:05 +01:00
|
|
|
$read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
|
|
|
|
|
|
|
// We want to get the messages that have not been read. These are stored in the 'message' table. It is the
|
|
|
|
// exact same query as the one above, except for the table we are querying. So, simply replace references to
|
|
|
|
// the 'message_read' table with the 'message' table.
|
|
|
|
$sql = str_replace('{message_read}', '{message}', $sql);
|
|
|
|
$unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2014-06-26 16:45:05 +01:00
|
|
|
// Union the 2 result sets together looking for the message with the most
|
|
|
|
// recent timecreated for each other user.
|
|
|
|
// $conversation->id (the array key) is the other user's ID.
|
2015-04-23 12:08:33 +08:00
|
|
|
$conversations = array();
|
2011-01-12 17:15:43 +08:00
|
|
|
$conversation_arrays = array($unread, $read);
|
|
|
|
foreach ($conversation_arrays as $conversation_array) {
|
|
|
|
foreach ($conversation_array as $conversation) {
|
2015-02-12 14:11:08 +08:00
|
|
|
if (!isset($conversations[$conversation->id])) {
|
2011-01-12 17:15:43 +08:00
|
|
|
$conversations[$conversation->id] = $conversation;
|
2015-02-12 14:11:08 +08:00
|
|
|
} else {
|
|
|
|
$current = $conversations[$conversation->id];
|
|
|
|
if ($current->timecreated < $conversation->timecreated) {
|
|
|
|
$conversations[$conversation->id] = $conversation;
|
|
|
|
} else if ($current->timecreated == $conversation->timecreated) {
|
|
|
|
if ($current->mid < $conversation->mid) {
|
|
|
|
$conversations[$conversation->id] = $conversation;
|
|
|
|
}
|
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-21 11:58:13 +08:00
|
|
|
// Sort the conversations by $conversation->timecreated, newest to oldest
|
|
|
|
// There may be multiple conversations with the same timecreated
|
|
|
|
// The conversations array contains both read and unread messages (different tables) so sorting by ID won't work
|
2013-08-06 20:58:28 +02:00
|
|
|
$result = core_collator::asort_objects_by_property($conversations, 'timecreated', core_collator::SORT_NUMERIC);
|
2012-08-21 11:58:13 +08:00
|
|
|
$conversations = array_reverse($conversations);
|
2011-01-12 17:15:43 +08:00
|
|
|
|
|
|
|
return $conversations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the users recent event notifications
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $user the current user
|
|
|
|
* @param int $limitfrom can be used for paging
|
|
|
|
* @param int $limitto can be used for paging
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$userfields = user_picture::fields('u', array('lastaccess'));
|
2013-10-08 18:29:02 +02:00
|
|
|
$sql = "SELECT mr.id AS message_read_id, $userfields, mr.notification, mr.smallmessage, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname
|
2011-01-12 17:15:43 +08:00
|
|
|
FROM {message_read} mr
|
|
|
|
JOIN {user} u ON u.id=mr.useridfrom
|
|
|
|
WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification
|
2014-06-29 16:18:24 -07:00
|
|
|
ORDER BY mr.timecreated DESC";
|
2011-01-12 17:15:43 +08:00
|
|
|
$params = array('userid1' => $user->id, 'notification' => 1);
|
|
|
|
|
|
|
|
$notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
|
|
|
return $notifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print the user's recent conversations
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @param stdClass $user the current user
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param bool $showicontext flag indicating whether or not to show text next to the action icons
|
|
|
|
*/
|
2012-11-12 15:25:49 +08:00
|
|
|
function message_print_recent_conversations($user1 = null, $showicontext = false, $showactionlinks = true) {
|
2011-01-12 17:15:43 +08:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
echo html_writer::start_tag('p', array('class' => 'heading'));
|
|
|
|
echo get_string('mostrecentconversations', 'message');
|
|
|
|
echo html_writer::end_tag('p');
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
if (empty($user1)) {
|
|
|
|
$user1 = $USER;
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
$conversations = message_get_recent_conversations($user1);
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2012-03-03 13:47:41 +07:00
|
|
|
// Attach context url information to create the "View this conversation" type links
|
|
|
|
foreach($conversations as $conversation) {
|
2012-11-12 15:25:49 +08:00
|
|
|
$conversation->contexturl = new moodle_url("/message/index.php?user1={$user1->id}&user2={$conversation->id}");
|
2012-03-03 13:47:41 +07:00
|
|
|
$conversation->contexturlname = get_string('thisconversation', 'message');
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
$showotheruser = true;
|
2012-11-12 15:25:49 +08:00
|
|
|
message_print_recent_messages_table($conversations, $user1, $showotheruser, $showicontext, false, $showactionlinks);
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print the user's recent notifications
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @param stdClass $user the current user
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
|
|
|
function message_print_recent_notifications($user=null) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
echo html_writer::start_tag('p', array('class' => 'heading'));
|
|
|
|
echo get_string('mostrecentnotifications', 'message');
|
|
|
|
echo html_writer::end_tag('p');
|
|
|
|
|
|
|
|
if (empty($user)) {
|
|
|
|
$user = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notifications = message_get_recent_notifications($user);
|
|
|
|
|
|
|
|
$showicontext = false;
|
|
|
|
$showotheruser = false;
|
2012-08-21 15:13:41 +08:00
|
|
|
message_print_recent_messages_table($notifications, $user, $showotheruser, $showicontext, true);
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Print a list of recent messages
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2013-10-08 18:29:02 +02:00
|
|
|
* @access private
|
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param array $messages the messages to display
|
2013-10-08 18:29:02 +02:00
|
|
|
* @param stdClass $user the current user
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param bool $showotheruser display information on the other user?
|
|
|
|
* @param bool $showicontext show text next to the action icons?
|
2012-08-21 15:13:41 +08:00
|
|
|
* @param bool $forcetexttohtml Force text to go through @see text_to_html() via @see format_text()
|
2013-10-08 18:29:02 +02:00
|
|
|
* @param bool $showactionlinks
|
2011-05-27 09:28:09 +01:00
|
|
|
* @return void
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
2012-11-12 15:25:49 +08:00
|
|
|
function message_print_recent_messages_table($messages, $user = null, $showotheruser = true, $showicontext = false, $forcetexttohtml = false, $showactionlinks = true) {
|
2011-01-12 17:15:43 +08:00
|
|
|
global $OUTPUT;
|
|
|
|
static $dateformat;
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if (empty($dateformat)) {
|
|
|
|
$dateformat = get_string('strftimedatetimeshort');
|
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('div', array('class' => 'messagerecent'));
|
|
|
|
foreach ($messages as $message) {
|
|
|
|
echo html_writer::start_tag('div', array('class' => 'singlemessage'));
|
|
|
|
|
|
|
|
if ($showotheruser) {
|
2012-11-12 15:25:49 +08:00
|
|
|
$strcontact = $strblock = $strhistory = null;
|
|
|
|
|
|
|
|
if ($showactionlinks) {
|
|
|
|
if ( $message->contactlistid ) {
|
|
|
|
if ($message->blocked == 0) { // The other user isn't blocked.
|
|
|
|
$strcontact = message_contact_link($message->id, 'remove', true, null, $showicontext);
|
|
|
|
$strblock = message_contact_link($message->id, 'block', true, null, $showicontext);
|
|
|
|
} else { // The other user is blocked.
|
|
|
|
$strcontact = message_contact_link($message->id, 'add', true, null, $showicontext);
|
|
|
|
$strblock = message_contact_link($message->id, 'unblock', true, null, $showicontext);
|
|
|
|
}
|
|
|
|
} else {
|
2011-01-12 17:15:43 +08:00
|
|
|
$strcontact = message_contact_link($message->id, 'add', true, null, $showicontext);
|
2012-11-12 15:25:49 +08:00
|
|
|
$strblock = message_contact_link($message->id, 'block', true, null, $showicontext);
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
//should we show just the icon or icon and text?
|
|
|
|
$histicontext = 'icon';
|
|
|
|
if ($showicontext) {
|
|
|
|
$histicontext = 'both';
|
|
|
|
}
|
|
|
|
$strhistory = message_history_link($user->id, $message->id, true, '', '', $histicontext);
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
|
|
|
echo html_writer::start_tag('span', array('class' => 'otheruser'));
|
|
|
|
|
|
|
|
echo html_writer::start_tag('span', array('class' => 'pix'));
|
|
|
|
echo $OUTPUT->user_picture($message, array('size' => 20, 'courseid' => SITEID));
|
|
|
|
echo html_writer::end_tag('span');
|
|
|
|
|
|
|
|
echo html_writer::start_tag('span', array('class' => 'contact'));
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
$link = new moodle_url("/message/index.php?user1={$user->id}&user2=$message->id");
|
2011-01-12 17:15:43 +08:00
|
|
|
$action = null;
|
|
|
|
echo $OUTPUT->action_link($link, fullname($message), $action, array('title' => get_string('sendmessageto', 'message', fullname($message))));
|
|
|
|
|
|
|
|
echo html_writer::end_tag('span');//end contact
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
if ($showactionlinks) {
|
|
|
|
echo $strcontact.$strblock.$strhistory;
|
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('span');//end otheruser
|
|
|
|
}
|
2013-10-08 18:29:02 +02:00
|
|
|
|
|
|
|
$messagetext = message_format_message_text($message, $forcetexttohtml);
|
2011-01-12 17:15:43 +08:00
|
|
|
|
|
|
|
echo html_writer::tag('span', userdate($message->timecreated, $dateformat), array('class' => 'messagedate'));
|
2013-10-08 18:29:02 +02:00
|
|
|
echo html_writer::tag('span', $messagetext, array('class' => 'themessage'));
|
2011-01-12 17:15:43 +08:00
|
|
|
echo message_format_contexturl($message);
|
|
|
|
echo html_writer::end_tag('div');//end singlemessage
|
|
|
|
}
|
|
|
|
echo html_writer::end_tag('div');//end messagerecent
|
|
|
|
}
|
|
|
|
|
2013-10-08 18:29:02 +02:00
|
|
|
/**
|
|
|
|
* Try to guess how to convert the message to html.
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param stdClass $message
|
|
|
|
* @param bool $forcetexttohtml
|
|
|
|
* @return string html fragment
|
|
|
|
*/
|
|
|
|
function message_format_message_text($message, $forcetexttohtml = false) {
|
|
|
|
// Note: this is a very nasty hack that tries to work around the weird messaging rules and design.
|
|
|
|
|
|
|
|
$options = new stdClass();
|
|
|
|
$options->para = false;
|
|
|
|
|
|
|
|
$format = $message->fullmessageformat;
|
|
|
|
|
2015-08-19 16:01:42 +08:00
|
|
|
if (strval($message->smallmessage) !== '') {
|
2013-10-08 18:29:02 +02:00
|
|
|
if ($message->notification == 1) {
|
2015-08-19 16:01:42 +08:00
|
|
|
if (strval($message->fullmessagehtml) !== '' or strval($message->fullmessage) !== '') {
|
2013-10-08 18:29:02 +02:00
|
|
|
$format = FORMAT_PLAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$messagetext = $message->smallmessage;
|
|
|
|
|
|
|
|
} else if ($message->fullmessageformat == FORMAT_HTML) {
|
2015-08-19 16:01:42 +08:00
|
|
|
if (strval($message->fullmessagehtml) !== '') {
|
2013-10-08 18:29:02 +02:00
|
|
|
$messagetext = $message->fullmessagehtml;
|
|
|
|
} else {
|
|
|
|
$messagetext = $message->fullmessage;
|
|
|
|
$format = FORMAT_MOODLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2015-08-19 16:01:42 +08:00
|
|
|
if (strval($message->fullmessage) !== '') {
|
2013-10-08 18:29:02 +02:00
|
|
|
$messagetext = $message->fullmessage;
|
|
|
|
} else {
|
|
|
|
$messagetext = $message->fullmessagehtml;
|
|
|
|
$format = FORMAT_HTML;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($forcetexttohtml) {
|
|
|
|
// This is a crazy hack, why not set proper format when creating the notifications?
|
|
|
|
if ($format === FORMAT_PLAIN) {
|
|
|
|
$format = FORMAT_MOODLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return format_text($messagetext, $format, $options);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Add the selected user as a contact for the current user
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param int $contactid the ID of the user to add as a contact
|
|
|
|
* @param int $blocked 1 if you wish to block the contact
|
|
|
|
* @return bool/int false if the $contactid isnt a valid user id. True if no changes made.
|
|
|
|
* Otherwise returns the result of update_record() or insert_record()
|
|
|
|
*/
|
2004-12-28 13:49:15 +00:00
|
|
|
function message_add_contact($contactid, $blocked=0) {
|
2008-06-05 13:41:16 +00:00
|
|
|
global $USER, $DB;
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid
|
2004-12-28 13:49:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2014-02-18 22:03:45 -08:00
|
|
|
// Check if a record already exists as we may be changing blocking status.
|
2011-01-12 17:15:43 +08:00
|
|
|
if (($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) !== false) {
|
2014-02-18 22:03:45 -08:00
|
|
|
// Check if blocking status has been changed.
|
2015-10-06 15:09:42 +11:00
|
|
|
if ($contact->blocked != $blocked) {
|
2004-12-28 13:49:15 +00:00
|
|
|
$contact->blocked = $blocked;
|
2014-02-18 22:03:45 -08:00
|
|
|
$DB->update_record('message_contacts', $contact);
|
|
|
|
|
|
|
|
if ($blocked == 1) {
|
|
|
|
// Trigger event for blocking a contact.
|
|
|
|
$event = \core\event\message_contact_blocked::create(array(
|
|
|
|
'objectid' => $contact->id,
|
|
|
|
'userid' => $contact->userid,
|
|
|
|
'relateduserid' => $contact->contactid,
|
|
|
|
'context' => context_user::instance($contact->userid)
|
|
|
|
));
|
|
|
|
$event->add_record_snapshot('message_contacts', $contact);
|
|
|
|
$event->trigger();
|
2014-02-18 22:11:07 -08:00
|
|
|
} else {
|
|
|
|
// Trigger event for unblocking a contact.
|
|
|
|
$event = \core\event\message_contact_unblocked::create(array(
|
|
|
|
'objectid' => $contact->id,
|
|
|
|
'userid' => $contact->userid,
|
|
|
|
'relateduserid' => $contact->contactid,
|
|
|
|
'context' => context_user::instance($contact->userid)
|
|
|
|
));
|
|
|
|
$event->add_record_snapshot('message_contacts', $contact);
|
|
|
|
$event->trigger();
|
2014-02-18 22:03:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2004-12-28 13:49:15 +00:00
|
|
|
} else {
|
2012-11-12 15:25:49 +08:00
|
|
|
// No change to blocking status.
|
2004-12-28 13:49:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-16 12:40:36 +00:00
|
|
|
} else {
|
2012-11-12 15:25:49 +08:00
|
|
|
// New contact record.
|
2012-01-15 21:49:30 +01:00
|
|
|
$contact = new stdClass();
|
2004-12-28 13:49:15 +00:00
|
|
|
$contact->userid = $USER->id;
|
|
|
|
$contact->contactid = $contactid;
|
|
|
|
$contact->blocked = $blocked;
|
2014-02-17 20:36:39 -08:00
|
|
|
$contact->id = $DB->insert_record('message_contacts', $contact);
|
|
|
|
|
2014-05-15 16:08:46 +08:00
|
|
|
$eventparams = array(
|
2014-02-17 20:36:39 -08:00
|
|
|
'objectid' => $contact->id,
|
|
|
|
'userid' => $contact->userid,
|
|
|
|
'relateduserid' => $contact->contactid,
|
|
|
|
'context' => context_user::instance($contact->userid)
|
2014-05-15 16:08:46 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($blocked) {
|
|
|
|
$event = \core\event\message_contact_blocked::create($eventparams);
|
|
|
|
} else {
|
|
|
|
$event = \core\event\message_contact_added::create($eventparams);
|
|
|
|
}
|
|
|
|
// Trigger event.
|
2014-02-17 20:36:39 -08:00
|
|
|
$event->trigger();
|
|
|
|
|
|
|
|
return true;
|
2004-12-16 12:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* remove a contact
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @param int $contactid the user ID of the contact to remove
|
2011-01-12 17:15:43 +08:00
|
|
|
* @return bool returns the result of delete_records()
|
|
|
|
*/
|
2004-12-28 13:49:15 +00:00
|
|
|
function message_remove_contact($contactid) {
|
2008-06-05 13:41:16 +00:00
|
|
|
global $USER, $DB;
|
2014-02-17 20:51:36 -08:00
|
|
|
|
|
|
|
if ($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) {
|
|
|
|
$DB->delete_records('message_contacts', array('id' => $contact->id));
|
|
|
|
|
|
|
|
// Trigger event for removing a contact.
|
|
|
|
$event = \core\event\message_contact_removed::create(array(
|
|
|
|
'objectid' => $contact->id,
|
|
|
|
'userid' => $contact->userid,
|
|
|
|
'relateduserid' => $contact->contactid,
|
|
|
|
'context' => context_user::instance($contact->userid)
|
|
|
|
));
|
|
|
|
$event->add_record_snapshot('message_contacts', $contact);
|
|
|
|
$event->trigger();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Unblock a contact. Note that this reverts the previously blocked user back to a non-contact.
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param int $contactid the user ID of the contact to unblock
|
|
|
|
* @return bool returns the result of delete_records()
|
|
|
|
*/
|
2004-12-28 13:49:15 +00:00
|
|
|
function message_unblock_contact($contactid) {
|
2014-02-18 22:11:07 -08:00
|
|
|
return message_add_contact($contactid, 0);
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2014-02-18 22:03:45 -08:00
|
|
|
* Block a user.
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param int $contactid the user ID of the user to block
|
2014-02-18 22:03:45 -08:00
|
|
|
* @return bool
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
2004-12-28 13:49:15 +00:00
|
|
|
function message_block_contact($contactid) {
|
|
|
|
return message_add_contact($contactid, 1);
|
|
|
|
}
|
|
|
|
|
2015-03-24 17:11:15 -07:00
|
|
|
/**
|
|
|
|
* Checks if a user can delete a message.
|
|
|
|
*
|
|
|
|
* @param stdClass $message the message to delete
|
|
|
|
* @param string $userid the user id of who we want to delete the message for (this may be done by the admin
|
|
|
|
* but will still seem as if it was by the user)
|
|
|
|
* @return bool Returns true if a user can delete the message, false otherwise.
|
|
|
|
*/
|
|
|
|
function message_can_delete_message($message, $userid) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if ($message->useridfrom == $userid) {
|
|
|
|
$userdeleting = 'useridfrom';
|
|
|
|
} else if ($message->useridto == $userid) {
|
|
|
|
$userdeleting = 'useridto';
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$systemcontext = context_system::instance();
|
|
|
|
|
|
|
|
// Let's check if the user is allowed to delete this message.
|
|
|
|
if (has_capability('moodle/site:deleteanymessage', $systemcontext) ||
|
|
|
|
((has_capability('moodle/site:deleteownmessage', $systemcontext) &&
|
|
|
|
$USER->id == $message->$userdeleting))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a message.
|
|
|
|
*
|
|
|
|
* This function does not verify any permissions.
|
|
|
|
*
|
|
|
|
* @param stdClass $message the message to delete
|
|
|
|
* @param string $userid the user id of who we want to delete the message for (this may be done by the admin
|
|
|
|
* but will still seem as if it was by the user)
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function message_delete_message($message, $userid) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
// The column we want to alter.
|
|
|
|
if ($message->useridfrom == $userid) {
|
|
|
|
$coltimedeleted = 'timeuserfromdeleted';
|
|
|
|
} else if ($message->useridto == $userid) {
|
|
|
|
$coltimedeleted = 'timeusertodeleted';
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't update it if it's already been deleted.
|
|
|
|
if ($message->$coltimedeleted > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the table we want to update.
|
|
|
|
if (isset($message->timeread)) {
|
|
|
|
$messagetable = 'message_read';
|
|
|
|
} else {
|
|
|
|
$messagetable = 'message';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the message as deleted.
|
|
|
|
$updatemessage = new stdClass();
|
|
|
|
$updatemessage->id = $message->id;
|
|
|
|
$updatemessage->$coltimedeleted = time();
|
2015-09-21 18:18:07 -07:00
|
|
|
$success = $DB->update_record($messagetable, $updatemessage);
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
// Trigger event for deleting a message.
|
|
|
|
\core\event\message_deleted::create_from_ids($message->useridfrom, $message->useridto,
|
|
|
|
$userid, $messagetable, $message->id)->trigger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
2015-03-24 17:11:15 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Load a user's contact record
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param int $contactid the user ID of the user whose contact record you want
|
2011-05-27 09:28:09 +01:00
|
|
|
* @return array message contacts
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
2004-12-28 13:49:15 +00:00
|
|
|
function message_get_contact($contactid) {
|
2008-06-05 13:41:16 +00:00
|
|
|
global $USER, $DB;
|
2011-01-12 17:15:43 +08:00
|
|
|
return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Print the results of a message search
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param mixed $frm submitted form data
|
|
|
|
* @param bool $showicontext show text next to action icons?
|
2011-07-27 14:13:47 +08:00
|
|
|
* @param object $currentuser the current user
|
2011-05-27 09:28:09 +01:00
|
|
|
* @return void
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
2011-07-27 14:13:47 +08:00
|
|
|
function message_print_search_results($frm, $showicontext=false, $currentuser=null) {
|
2011-01-12 17:15:43 +08:00
|
|
|
global $USER, $DB, $OUTPUT;
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2011-07-27 14:13:47 +08:00
|
|
|
if (empty($currentuser)) {
|
|
|
|
$currentuser = $USER;
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('div', array('class' => 'mdl-left'));
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$personsearch = false;
|
|
|
|
$personsearchstring = null;
|
2004-12-28 13:49:15 +00:00
|
|
|
if (!empty($frm->personsubmit) and !empty($frm->name)) {
|
2010-06-25 08:16:10 +00:00
|
|
|
$personsearch = true;
|
|
|
|
$personsearchstring = $frm->name;
|
|
|
|
} else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
|
|
|
|
$personsearch = true;
|
|
|
|
$personsearchstring = $frm->combinedsearch;
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Search for person.
|
2010-06-25 08:16:10 +00:00
|
|
|
if ($personsearch) {
|
2006-12-18 17:10:35 +00:00
|
|
|
if (optional_param('mycourses', 0, PARAM_BOOL)) {
|
2004-12-28 13:49:15 +00:00
|
|
|
$users = array();
|
2012-12-14 12:01:14 +08:00
|
|
|
$mycourses = enrol_get_my_courses('id');
|
|
|
|
$mycoursesids = array();
|
2004-12-28 13:49:15 +00:00
|
|
|
foreach ($mycourses as $mycourse) {
|
2012-12-14 12:01:14 +08:00
|
|
|
$mycoursesids[] = $mycourse->id;
|
|
|
|
}
|
|
|
|
$susers = message_search_users($mycoursesids, $personsearchstring);
|
|
|
|
foreach ($susers as $suser) {
|
|
|
|
$users[$suser->id] = $suser;
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-06-25 08:16:10 +00:00
|
|
|
$users = message_search_users(SITEID, $personsearchstring);
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($users)) {
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
|
2010-07-07 07:49:08 +00:00
|
|
|
echo get_string('userssearchresults', 'message', count($users));
|
|
|
|
echo html_writer::end_tag('p');
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('table', array('class' => 'messagesearchresults'));
|
2004-12-28 13:49:15 +00:00
|
|
|
foreach ($users as $user) {
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2008-01-15 16:10:58 +00:00
|
|
|
if ( $user->contactlistid ) {
|
2012-11-12 15:25:49 +08:00
|
|
|
if ($user->blocked == 0) { // User is not blocked.
|
2010-06-25 08:16:10 +00:00
|
|
|
$strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext);
|
|
|
|
$strblock = message_contact_link($user->id, 'block', true, null, $showicontext);
|
2004-12-28 13:49:15 +00:00
|
|
|
} else { // blocked
|
2010-06-25 08:16:10 +00:00
|
|
|
$strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
|
|
|
|
$strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext);
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-06-25 08:16:10 +00:00
|
|
|
$strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
|
|
|
|
$strblock = message_contact_link($user->id, 'block', true, null, $showicontext);
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Should we show just the icon or icon and text?
|
2010-06-25 08:16:10 +00:00
|
|
|
$histicontext = 'icon';
|
|
|
|
if ($showicontext) {
|
|
|
|
$histicontext = 'both';
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
$strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext);
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('tr');
|
2009-08-20 13:15:29 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('td', array('class' => 'pix'));
|
|
|
|
echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID));
|
|
|
|
echo html_writer::end_tag('td');
|
|
|
|
|
|
|
|
echo html_writer::start_tag('td',array('class' => 'contact'));
|
2010-06-25 08:16:10 +00:00
|
|
|
$action = null;
|
2011-01-12 17:15:43 +08:00
|
|
|
$link = new moodle_url("/message/index.php?id=$user->id");
|
|
|
|
echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user))));
|
|
|
|
echo html_writer::end_tag('td');
|
2007-11-21 07:53:42 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::tag('td', $strcontact, array('class' => 'link'));
|
|
|
|
echo html_writer::tag('td', $strblock, array('class' => 'link'));
|
|
|
|
echo html_writer::tag('td', $strhistory, array('class' => 'link'));
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('tr');
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('table');
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
} else {
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
|
2010-06-25 08:16:10 +00:00
|
|
|
echo get_string('userssearchresults', 'message', 0).'<br /><br />';
|
2010-07-07 07:49:08 +00:00
|
|
|
echo html_writer::end_tag('p');
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
// search messages for keywords
|
|
|
|
$messagesearch = false;
|
|
|
|
$messagesearchstring = null;
|
|
|
|
if (!empty($frm->keywords)) {
|
|
|
|
$messagesearch = true;
|
|
|
|
$messagesearchstring = clean_text(trim($frm->keywords));
|
|
|
|
} else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
|
|
|
|
$messagesearch = true;
|
|
|
|
$messagesearchstring = clean_text(trim($frm->combinedsearch));
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if ($messagesearch) {
|
|
|
|
if ($messagesearchstring) {
|
|
|
|
$keywords = explode(' ', $messagesearchstring);
|
2009-01-19 04:53:26 +00:00
|
|
|
} else {
|
|
|
|
$keywords = array();
|
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
$tome = false;
|
|
|
|
$fromme = false;
|
|
|
|
$courseid = 'none';
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (empty($frm->keywordsoption)) {
|
|
|
|
$frm->keywordsoption = 'allmine';
|
|
|
|
}
|
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
switch ($frm->keywordsoption) {
|
|
|
|
case 'tome':
|
|
|
|
$tome = true;
|
|
|
|
break;
|
|
|
|
case 'fromme':
|
|
|
|
$fromme = true;
|
|
|
|
break;
|
|
|
|
case 'allmine':
|
|
|
|
$tome = true;
|
|
|
|
$fromme = true;
|
|
|
|
break;
|
|
|
|
case 'allusers':
|
|
|
|
$courseid = SITEID;
|
|
|
|
break;
|
|
|
|
case 'courseusers':
|
|
|
|
$courseid = $frm->courseid;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$tome = true;
|
|
|
|
$fromme = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) {
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Get a list of contacts.
|
2011-01-12 17:15:43 +08:00
|
|
|
if (($contacts = $DB->get_records('message_contacts', array('userid' => $USER->id), '', 'contactid, blocked') ) === false) {
|
2004-12-29 03:54:33 +00:00
|
|
|
$contacts = array();
|
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Print heading with number of results.
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
|
2010-07-07 07:49:08 +00:00
|
|
|
$countresults = count($messages);
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($countresults == MESSAGE_SEARCH_MAX_RESULTS) {
|
2010-07-07 07:49:08 +00:00
|
|
|
echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")';
|
|
|
|
} else {
|
2010-10-25 09:29:34 +00:00
|
|
|
echo get_string('keywordssearchresults', 'message', $countresults);
|
2010-07-07 07:49:08 +00:00
|
|
|
}
|
|
|
|
echo html_writer::end_tag('p');
|
2004-12-29 03:54:33 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Print table headings.
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('table', array('class' => 'messagesearchresults', 'cellspacing' => '0'));
|
2010-07-07 07:49:08 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
$headertdstart = html_writer::start_tag('td', array('class' => 'messagesearchresultscol'));
|
2010-07-07 07:49:08 +00:00
|
|
|
$headertdend = html_writer::end_tag('td');
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('tr');
|
2010-07-07 07:49:08 +00:00
|
|
|
echo $headertdstart.get_string('from').$headertdend;
|
|
|
|
echo $headertdstart.get_string('to').$headertdend;
|
|
|
|
echo $headertdstart.get_string('message', 'message').$headertdend;
|
|
|
|
echo $headertdstart.get_string('timesent', 'message').$headertdend;
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('tr');
|
2004-12-29 03:54:33 +00:00
|
|
|
|
|
|
|
$blockedcount = 0;
|
2008-05-08 03:49:51 +00:00
|
|
|
$dateformat = get_string('strftimedatetimeshort');
|
2005-01-03 12:16:06 +00:00
|
|
|
$strcontext = get_string('context', 'message');
|
2004-12-28 13:49:15 +00:00
|
|
|
foreach ($messages as $message) {
|
2004-12-29 03:54:33 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Ignore messages to and from blocked users unless $frm->includeblocked is set.
|
2006-12-18 17:10:35 +00:00
|
|
|
if (!optional_param('includeblocked', 0, PARAM_BOOL) and (
|
2004-12-29 03:54:33 +00:00
|
|
|
( isset($contacts[$message->useridfrom]) and ($contacts[$message->useridfrom]->blocked == 1)) or
|
|
|
|
( isset($contacts[$message->useridto] ) and ($contacts[$message->useridto]->blocked == 1))
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
$blockedcount ++;
|
2004-12-28 13:49:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Load user-to record.
|
2004-12-29 03:54:33 +00:00
|
|
|
if ($message->useridto !== $USER->id) {
|
2013-08-30 14:28:17 +08:00
|
|
|
$userto = core_user::get_user($message->useridto);
|
2016-03-14 12:09:22 +05:30
|
|
|
if ($userto === false) {
|
|
|
|
$userto = core_user::get_noreply_user();
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
$tocontact = (array_key_exists($message->useridto, $contacts) and
|
2004-12-29 03:54:33 +00:00
|
|
|
($contacts[$message->useridto]->blocked == 0) );
|
2006-12-25 22:34:05 +00:00
|
|
|
$toblocked = (array_key_exists($message->useridto, $contacts) and
|
2004-12-29 03:54:33 +00:00
|
|
|
($contacts[$message->useridto]->blocked == 1) );
|
|
|
|
} else {
|
|
|
|
$userto = false;
|
|
|
|
$tocontact = false;
|
|
|
|
$toblocked = false;
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Load user-from record.
|
2004-12-29 03:54:33 +00:00
|
|
|
if ($message->useridfrom !== $USER->id) {
|
2013-08-30 14:28:17 +08:00
|
|
|
$userfrom = core_user::get_user($message->useridfrom);
|
2016-03-14 12:09:22 +05:30
|
|
|
if ($userfrom === false) {
|
|
|
|
$userfrom = core_user::get_noreply_user();
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
$fromcontact = (array_key_exists($message->useridfrom, $contacts) and
|
2004-12-29 03:54:33 +00:00
|
|
|
($contacts[$message->useridfrom]->blocked == 0) );
|
2006-12-25 22:34:05 +00:00
|
|
|
$fromblocked = (array_key_exists($message->useridfrom, $contacts) and
|
2004-12-29 03:54:33 +00:00
|
|
|
($contacts[$message->useridfrom]->blocked == 1) );
|
|
|
|
} else {
|
|
|
|
$userfrom = false;
|
|
|
|
$fromcontact = false;
|
|
|
|
$fromblocked = false;
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Find date string for this message.
|
2005-01-01 11:47:18 +00:00
|
|
|
$date = usergetdate($message->timecreated);
|
|
|
|
$datestring = $date['year'].$date['mon'].$date['mday'];
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Print out message row.
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('tr', array('valign' => 'top'));
|
|
|
|
|
|
|
|
echo html_writer::start_tag('td', array('class' => 'contact'));
|
2010-06-25 08:16:10 +00:00
|
|
|
message_print_user($userfrom, $fromcontact, $fromblocked, $showicontext);
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('td');
|
|
|
|
|
|
|
|
echo html_writer::start_tag('td', array('class' => 'contact'));
|
2010-06-25 08:16:10 +00:00
|
|
|
message_print_user($userto, $tocontact, $toblocked, $showicontext);
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('td');
|
|
|
|
|
|
|
|
echo html_writer::start_tag('td', array('class' => 'summary'));
|
2012-10-26 09:42:51 +08:00
|
|
|
echo message_get_fragment($message->smallmessage, $keywords);
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('div', array('class' => 'link'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// If the user clicks the context link display message sender on the left.
|
|
|
|
// EXCEPT if the current user is in the conversation. Current user == always on the left.
|
2011-07-27 14:13:47 +08:00
|
|
|
$leftsideuserid = $rightsideuserid = null;
|
|
|
|
if ($currentuser->id == $message->useridto) {
|
|
|
|
$leftsideuserid = $message->useridto;
|
|
|
|
$rightsideuserid = $message->useridfrom;
|
2010-06-25 08:16:10 +00:00
|
|
|
} else {
|
2011-07-27 14:13:47 +08:00
|
|
|
$leftsideuserid = $message->useridfrom;
|
|
|
|
$rightsideuserid = $message->useridto;
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2011-07-27 14:13:47 +08:00
|
|
|
message_history_link($leftsideuserid, $rightsideuserid, false,
|
2010-06-25 08:16:10 +00:00
|
|
|
$messagesearchstring, 'm'.$message->id, $strcontext);
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('div');
|
|
|
|
echo html_writer::end_tag('td');
|
|
|
|
|
|
|
|
echo html_writer::tag('td', userdate($message->timecreated, $dateformat), array('class' => 'date'));
|
|
|
|
|
|
|
|
echo html_writer::end_tag('tr');
|
2004-12-29 03:54:33 +00:00
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2005-01-01 11:47:18 +00:00
|
|
|
if ($blockedcount > 0) {
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::start_tag('tr');
|
|
|
|
echo html_writer::tag('td', get_string('blockedmessages', 'message', $blockedcount), array('colspan' => 4, 'align' => 'center'));
|
|
|
|
echo html_writer::end_tag('tr');
|
2005-01-01 11:47:18 +00:00
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('table');
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
} else {
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::tag('p', get_string('keywordssearchresults', 'message', 0), array('class' => 'heading'));
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (!$personsearch && !$messagesearch) {
|
2010-09-17 19:41:02 +00:00
|
|
|
//they didn't enter any search terms
|
2009-08-18 05:20:12 +00:00
|
|
|
echo $OUTPUT->notification(get_string('emptysearchstring', 'message'));
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::end_tag('div');
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Print information on a user. Used when printing search results.
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object/bool $user the user to display or false if you just want $USER
|
|
|
|
* @param bool $iscontact is the user being displayed a contact?
|
|
|
|
* @param bool $isblocked is the user being displayed blocked?
|
|
|
|
* @param bool $includeicontext include text next to the action icons?
|
2011-05-27 09:28:09 +01:00
|
|
|
* @return void
|
2011-01-12 17:15:43 +08:00
|
|
|
*/
|
2010-06-25 08:16:10 +00:00
|
|
|
function message_print_user ($user=false, $iscontact=false, $isblocked=false, $includeicontext=false) {
|
2009-08-20 08:41:47 +00:00
|
|
|
global $USER, $OUTPUT;
|
2009-11-01 12:22:45 +00:00
|
|
|
|
2013-08-30 14:28:17 +08:00
|
|
|
$userpictureparams = array('size' => 20, 'courseid' => SITEID);
|
|
|
|
|
2004-12-29 03:54:33 +00:00
|
|
|
if ($user === false) {
|
2013-08-30 14:28:17 +08:00
|
|
|
echo $OUTPUT->user_picture($USER, $userpictureparams);
|
2016-02-14 15:45:02 +05:30
|
|
|
} else if (core_user::is_real_user($user->id)) {
|
2013-08-30 14:28:17 +08:00
|
|
|
echo $OUTPUT->user_picture($user, $userpictureparams);
|
2012-11-21 15:19:45 +08:00
|
|
|
|
|
|
|
$link = new moodle_url("/message/index.php?id=$user->id");
|
|
|
|
echo $OUTPUT->action_link($link, fullname($user), null, array('title' =>
|
|
|
|
get_string('sendmessageto', 'message', fullname($user))));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
$return = false;
|
|
|
|
$script = null;
|
2004-12-29 03:54:33 +00:00
|
|
|
if ($iscontact) {
|
2010-06-25 08:16:10 +00:00
|
|
|
message_contact_link($user->id, 'remove', $return, $script, $includeicontext);
|
2004-12-29 03:54:33 +00:00
|
|
|
} else {
|
2010-06-25 08:16:10 +00:00
|
|
|
message_contact_link($user->id, 'add', $return, $script, $includeicontext);
|
2004-12-29 03:54:33 +00:00
|
|
|
}
|
2012-11-21 15:19:45 +08:00
|
|
|
|
2004-12-29 03:54:33 +00:00
|
|
|
if ($isblocked) {
|
2010-06-25 08:16:10 +00:00
|
|
|
message_contact_link($user->id, 'unblock', $return, $script, $includeicontext);
|
2004-12-29 03:54:33 +00:00
|
|
|
} else {
|
2010-06-25 08:16:10 +00:00
|
|
|
message_contact_link($user->id, 'block', $return, $script, $includeicontext);
|
2004-12-29 03:54:33 +00:00
|
|
|
}
|
2016-03-14 12:09:22 +05:30
|
|
|
} else {
|
|
|
|
// If not real user, then don't show any links.
|
2016-02-14 15:45:02 +05:30
|
|
|
$userpictureparams['link'] = false;
|
2016-03-14 12:09:22 +05:30
|
|
|
// Stock profile picture should be displayed.
|
|
|
|
echo $OUTPUT->user_picture($user, $userpictureparams);
|
2004-12-29 03:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Print a message contact link
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param int $userid the ID of the user to apply to action to
|
|
|
|
* @param string $linktype can be add, remove, block or unblock
|
|
|
|
* @param bool $return if true return the link as a string. If false echo the link.
|
|
|
|
* @param string $script the URL to send the user to when the link is clicked. If null, the current page.
|
|
|
|
* @param bool $text include text next to the icons?
|
|
|
|
* @param bool $icon include a graphical icon?
|
|
|
|
* @return string if $return is true otherwise bool
|
|
|
|
*/
|
2010-06-25 08:16:10 +00:00
|
|
|
function message_contact_link($userid, $linktype='add', $return=false, $script=null, $text=false, $icon=true) {
|
2011-01-12 17:15:43 +08:00
|
|
|
global $OUTPUT, $PAGE;
|
2005-01-01 11:47:18 +00:00
|
|
|
|
2010-06-28 02:07:45 +00:00
|
|
|
//hold onto the strings as we're probably creating a bunch of links
|
2005-01-01 11:47:18 +00:00
|
|
|
static $str;
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if (empty($script)) {
|
2010-07-02 08:08:11 +00:00
|
|
|
//strip off previous action params like 'removecontact'
|
2010-11-02 07:58:16 +00:00
|
|
|
$script = message_remove_url_params($PAGE->url);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2005-01-01 11:47:18 +00:00
|
|
|
if (empty($str->blockcontact)) {
|
2012-01-15 13:37:53 +01:00
|
|
|
$str = new stdClass();
|
2005-01-01 11:47:18 +00:00
|
|
|
$str->blockcontact = get_string('blockcontact', 'message');
|
|
|
|
$str->unblockcontact = get_string('unblockcontact', 'message');
|
|
|
|
$str->removecontact = get_string('removecontact', 'message');
|
|
|
|
$str->addcontact = get_string('addcontact', 'message');
|
|
|
|
}
|
|
|
|
|
2005-04-14 09:28:07 +00:00
|
|
|
$command = $linktype.'contact';
|
|
|
|
$string = $str->{$command};
|
2010-06-28 02:07:45 +00:00
|
|
|
|
|
|
|
$safealttext = s($string);
|
|
|
|
|
|
|
|
$safestring = '';
|
2010-07-04 18:36:34 +00:00
|
|
|
if (!empty($text)) {
|
2010-06-28 02:07:45 +00:00
|
|
|
$safestring = $safealttext;
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$img = '';
|
|
|
|
if ($icon) {
|
2010-06-28 02:07:45 +00:00
|
|
|
$iconpath = null;
|
|
|
|
switch ($linktype) {
|
|
|
|
case 'block':
|
|
|
|
$iconpath = 't/block';
|
|
|
|
break;
|
|
|
|
case 'unblock':
|
2012-11-21 15:19:45 +08:00
|
|
|
$iconpath = 't/unblock';
|
2010-06-28 02:07:45 +00:00
|
|
|
break;
|
|
|
|
case 'remove':
|
2012-11-21 15:19:45 +08:00
|
|
|
$iconpath = 't/removecontact';
|
2010-06-28 02:07:45 +00:00
|
|
|
break;
|
|
|
|
case 'add':
|
|
|
|
default:
|
2012-11-21 15:19:45 +08:00
|
|
|
$iconpath = 't/addcontact';
|
2010-06-28 02:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$img = '<img src="'.$OUTPUT->pix_url($iconpath).'" class="iconsmall" alt="'.$safealttext.'" />';
|
2004-12-29 03:54:33 +00:00
|
|
|
}
|
2005-04-14 09:28:07 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$output = '<span class="'.$linktype.'contact">'.
|
2005-04-14 09:28:07 +00:00
|
|
|
'<a href="'.$script.'&'.$command.'='.$userid.
|
2010-10-13 03:28:16 +00:00
|
|
|
'&sesskey='.sesskey().'" title="'.$safealttext.'">'.
|
2010-06-25 08:16:10 +00:00
|
|
|
$img.
|
2010-06-28 02:07:45 +00:00
|
|
|
$safestring.'</a></span>';
|
2005-04-14 09:28:07 +00:00
|
|
|
|
2005-01-01 11:47:18 +00:00
|
|
|
if ($return) {
|
|
|
|
return $output;
|
2004-12-29 03:54:33 +00:00
|
|
|
} else {
|
2005-01-01 11:47:18 +00:00
|
|
|
echo $output;
|
2004-12-29 03:54:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* echo or return a link to take the user to the full message history between themselves and another user
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-07-27 14:13:47 +08:00
|
|
|
* @param int $userid1 the ID of the user displayed on the left (usually the current user)
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param int $userid2 the ID of the other user
|
|
|
|
* @param bool $return true to return the link as a string. False to echo the link.
|
|
|
|
* @param string $keywords any keywords to highlight in the message history
|
|
|
|
* @param string $position anchor name to jump to within the message history
|
|
|
|
* @param string $linktext optionally specify the link text
|
|
|
|
* @return string|bool. Returns a string if $return is true. Otherwise returns a boolean.
|
|
|
|
*/
|
|
|
|
function message_history_link($userid1, $userid2, $return=false, $keywords='', $position='', $linktext='') {
|
2013-09-04 16:40:01 +10:00
|
|
|
global $OUTPUT, $PAGE;
|
2005-04-23 06:08:33 +00:00
|
|
|
static $strmessagehistory;
|
|
|
|
|
|
|
|
if (empty($strmessagehistory)) {
|
|
|
|
$strmessagehistory = get_string('messagehistory', 'message');
|
|
|
|
}
|
2005-03-27 06:30:41 +00:00
|
|
|
|
2005-01-01 11:47:18 +00:00
|
|
|
if ($position) {
|
|
|
|
$position = "#$position";
|
|
|
|
}
|
2005-01-03 12:16:06 +00:00
|
|
|
if ($keywords) {
|
|
|
|
$keywords = "&search=".urlencode($keywords);
|
|
|
|
}
|
2005-01-01 04:37:40 +00:00
|
|
|
|
2005-04-23 06:08:33 +00:00
|
|
|
if ($linktext == 'icon') { // Icon only
|
2012-11-21 15:19:45 +08:00
|
|
|
$fulllink = '<img src="'.$OUTPUT->pix_url('t/messages') . '" class="iconsmall" alt="'.$strmessagehistory.'" />';
|
2005-04-23 06:08:33 +00:00
|
|
|
} else if ($linktext == 'both') { // Icon and standard name
|
2012-11-21 15:19:45 +08:00
|
|
|
$fulllink = '<img src="'.$OUTPUT->pix_url('t/messages') . '" class="iconsmall" alt="" />';
|
2005-04-23 06:08:33 +00:00
|
|
|
$fulllink .= ' '.$strmessagehistory;
|
|
|
|
} else if ($linktext) { // Custom name
|
|
|
|
$fulllink = $linktext;
|
|
|
|
} else { // Standard name only
|
|
|
|
$fulllink = $strmessagehistory;
|
2005-01-03 10:49:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 13:15:29 +00:00
|
|
|
$popupoptions = array(
|
|
|
|
'height' => 500,
|
|
|
|
'width' => 500,
|
|
|
|
'menubar' => false,
|
|
|
|
'location' => false,
|
|
|
|
'status' => true,
|
|
|
|
'scrollbars' => true,
|
|
|
|
'resizable' => true);
|
|
|
|
|
2011-07-27 14:13:47 +08:00
|
|
|
$link = new moodle_url('/message/index.php?history='.MESSAGE_HISTORY_ALL."&user1=$userid1&user2=$userid2$keywords$position");
|
2013-09-04 16:40:01 +10:00
|
|
|
if ($PAGE->url && $PAGE->url->get_param('viewing')) {
|
|
|
|
$link->param('viewing', $PAGE->url->get_param('viewing'));
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
$action = null;
|
2011-01-12 17:15:43 +08:00
|
|
|
$str = $OUTPUT->action_link($link, $fulllink, $action, array('title' => $strmessagehistory));
|
2005-01-01 04:37:40 +00:00
|
|
|
|
2005-04-14 09:28:07 +00:00
|
|
|
$str = '<span class="history">'.$str.'</span>';
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($return) {
|
2005-01-01 04:37:40 +00:00
|
|
|
return $str;
|
|
|
|
} else {
|
|
|
|
echo $str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-12-14 12:01:14 +08:00
|
|
|
* Search through course users.
|
|
|
|
*
|
|
|
|
* If $courseids contains the site course then this function searches
|
|
|
|
* through all undeleted and confirmed users.
|
2004-12-28 13:49:15 +00:00
|
|
|
*
|
2012-12-14 12:01:14 +08:00
|
|
|
* @param int|array $courseids Course ID or array of course IDs.
|
|
|
|
* @param string $searchtext the text to search for.
|
|
|
|
* @param string $sort the column name to order by.
|
2013-01-14 09:08:22 +08:00
|
|
|
* @param string|array $exceptions comma separated list or array of user IDs to exclude.
|
2012-12-14 12:01:14 +08:00
|
|
|
* @return array An array of {@link $USER} records.
|
2004-12-28 13:49:15 +00:00
|
|
|
*/
|
2012-12-14 12:01:14 +08:00
|
|
|
function message_search_users($courseids, $searchtext, $sort='', $exceptions='') {
|
2008-06-05 13:41:16 +00:00
|
|
|
global $CFG, $USER, $DB;
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2012-12-14 12:01:14 +08:00
|
|
|
// Basic validation to ensure that the parameter $courseids is not an empty array or an empty value.
|
|
|
|
if (!$courseids) {
|
|
|
|
$courseids = array(SITEID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow an integer to be passed.
|
|
|
|
if (!is_array($courseids)) {
|
|
|
|
$courseids = array($courseids);
|
|
|
|
}
|
|
|
|
|
2008-06-09 19:48:24 +00:00
|
|
|
$fullname = $DB->sql_fullname();
|
2013-01-14 09:08:22 +08:00
|
|
|
$ufields = user_picture::fields('u');
|
2004-12-28 13:49:15 +00:00
|
|
|
|
|
|
|
if (!empty($sort)) {
|
|
|
|
$order = ' ORDER BY '. $sort;
|
|
|
|
} else {
|
|
|
|
$order = '';
|
|
|
|
}
|
|
|
|
|
2013-01-14 09:08:22 +08:00
|
|
|
$params = array(
|
|
|
|
'userid' => $USER->id,
|
|
|
|
'query' => "%$searchtext%"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($exceptions)) {
|
|
|
|
$exceptions = array();
|
|
|
|
} else if (!empty($exceptions) && is_string($exceptions)) {
|
|
|
|
$exceptions = explode(',', $exceptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore self and guest account.
|
|
|
|
$exceptions[] = $USER->id;
|
|
|
|
$exceptions[] = $CFG->siteguest;
|
|
|
|
|
|
|
|
// Exclude exceptions from the search result.
|
|
|
|
list($except, $params_except) = $DB->get_in_or_equal($exceptions, SQL_PARAMS_NAMED, 'param', false);
|
|
|
|
$except = ' AND u.id ' . $except;
|
|
|
|
$params = array_merge($params_except, $params);
|
2012-12-14 12:01:14 +08:00
|
|
|
|
|
|
|
if (in_array(SITEID, $courseids)) {
|
|
|
|
// Search on site level.
|
2010-07-04 18:36:34 +00:00
|
|
|
return $DB->get_records_sql("SELECT $ufields, mc.id as contactlistid, mc.blocked
|
2008-06-05 13:54:30 +00:00
|
|
|
FROM {user} u
|
2008-06-05 13:41:16 +00:00
|
|
|
LEFT JOIN {message_contacts} mc
|
2013-01-14 09:08:22 +08:00
|
|
|
ON mc.contactid = u.id AND mc.userid = :userid
|
2010-07-04 18:36:34 +00:00
|
|
|
WHERE u.deleted = '0' AND u.confirmed = '1'
|
2013-01-14 09:08:22 +08:00
|
|
|
AND (".$DB->sql_like($fullname, ':query', false).")
|
2008-06-05 13:41:16 +00:00
|
|
|
$except
|
|
|
|
$order", $params);
|
2004-12-28 13:49:15 +00:00
|
|
|
} else {
|
2012-12-14 12:01:14 +08:00
|
|
|
// Search in courses.
|
|
|
|
|
|
|
|
// Getting the context IDs or each course.
|
|
|
|
$contextids = array();
|
|
|
|
foreach ($courseids as $courseid) {
|
|
|
|
$context = context_course::instance($courseid);
|
|
|
|
$contextids = array_merge($contextids, $context->get_parent_context_ids(true));
|
|
|
|
}
|
2013-01-14 09:08:22 +08:00
|
|
|
list($contextwhere, $contextparams) = $DB->get_in_or_equal(array_unique($contextids), SQL_PARAMS_NAMED, 'context');
|
2012-12-14 12:01:14 +08:00
|
|
|
$params = array_merge($params, $contextparams);
|
|
|
|
|
|
|
|
// Everyone who has a role assignment in this course or higher.
|
|
|
|
// TODO: add enabled enrolment join here (skodak)
|
2012-03-05 12:18:58 +10:30
|
|
|
$users = $DB->get_records_sql("SELECT DISTINCT $ufields, mc.id as contactlistid, mc.blocked
|
2011-03-16 10:58:07 +08:00
|
|
|
FROM {user} u
|
2008-06-05 13:41:16 +00:00
|
|
|
JOIN {role_assignments} ra ON ra.userid = u.id
|
|
|
|
LEFT JOIN {message_contacts} mc
|
2013-01-14 09:08:22 +08:00
|
|
|
ON mc.contactid = u.id AND mc.userid = :userid
|
2010-07-04 18:36:34 +00:00
|
|
|
WHERE u.deleted = '0' AND u.confirmed = '1'
|
2013-01-14 09:08:22 +08:00
|
|
|
AND (".$DB->sql_like($fullname, ':query', false).")
|
2012-12-14 12:01:14 +08:00
|
|
|
AND ra.contextid $contextwhere
|
2008-06-05 13:41:16 +00:00
|
|
|
$except
|
|
|
|
$order", $params);
|
2006-09-19 01:44:33 +00:00
|
|
|
|
|
|
|
return $users;
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
2012-11-12 15:25:49 +08:00
|
|
|
* Search a user's messages
|
|
|
|
*
|
|
|
|
* Returns a list of posts found using an array of search terms
|
|
|
|
* eg word +word -word
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param array $searchterms an array of search terms (strings)
|
|
|
|
* @param bool $fromme include messages from the user?
|
|
|
|
* @param bool $tome include messages to the user?
|
|
|
|
* @param mixed $courseid SITEID for admins searching all messages. Other behaviour not yet implemented
|
|
|
|
* @param int $userid the user ID of the current user
|
|
|
|
* @return mixed An array of messages or false if no matching messages were found
|
|
|
|
*/
|
2004-12-29 03:54:33 +00:00
|
|
|
function message_search($searchterms, $fromme=true, $tome=true, $courseid='none', $userid=0) {
|
2008-06-05 13:41:16 +00:00
|
|
|
global $CFG, $USER, $DB;
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// If user is searching all messages check they are allowed to before doing anything else.
|
2012-08-02 11:20:48 +08:00
|
|
|
if ($courseid == SITEID && !has_capability('moodle/site:readallmessages', context_system::instance())) {
|
2012-03-01 13:53:48 +07:00
|
|
|
print_error('accessdenied','admin');
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// If no userid sent then assume current user.
|
2006-12-25 22:34:05 +00:00
|
|
|
if ($userid == 0) $userid = $USER->id;
|
2004-12-29 03:54:33 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Some differences in SQL syntax.
|
2008-06-05 13:54:30 +00:00
|
|
|
if ($DB->sql_regex_supported()) {
|
|
|
|
$REGEXP = $DB->sql_regex(true);
|
|
|
|
$NOTREGEXP = $DB->sql_regex(false);
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 13:54:30 +00:00
|
|
|
$searchcond = array();
|
|
|
|
$params = array();
|
|
|
|
$i = 0;
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Preprocess search terms to check whether we have at least 1 eligible search term.
|
|
|
|
// If we do we can drop words around it like 'a'.
|
2010-07-07 07:49:08 +00:00
|
|
|
$dropshortwords = false;
|
|
|
|
foreach ($searchterms as $searchterm) {
|
|
|
|
if (strlen($searchterm) >= 2) {
|
|
|
|
$dropshortwords = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
foreach ($searchterms as $searchterm) {
|
2008-06-05 13:54:30 +00:00
|
|
|
$i++;
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
$NOT = false; // Initially we aren't going to perform NOT LIKE searches, only MSSQL and Oracle.
|
2008-06-05 13:54:30 +00:00
|
|
|
|
2010-07-07 07:49:08 +00:00
|
|
|
if ($dropshortwords && strlen($searchterm) < 2) {
|
2004-12-28 13:49:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-12 15:25:49 +08:00
|
|
|
// Under Oracle and MSSQL, trim the + and - operators and perform simpler LIKE search.
|
2008-06-05 13:54:30 +00:00
|
|
|
if (!$DB->sql_regex_supported()) {
|
|
|
|
if (substr($searchterm, 0, 1) == '-') {
|
2010-09-04 14:52:47 +00:00
|
|
|
$NOT = true;
|
2008-06-05 13:54:30 +00:00
|
|
|
}
|
2006-10-31 20:22:10 +00:00
|
|
|
$searchterm = trim($searchterm, '+-');
|
|
|
|
}
|
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
if (substr($searchterm,0,1) == "+") {
|
|
|
|
$searchterm = substr($searchterm,1);
|
2008-06-05 13:54:30 +00:00
|
|
|
$searchterm = preg_quote($searchterm, '|');
|
2009-01-19 04:35:09 +00:00
|
|
|
$searchcond[] = "m.fullmessage $REGEXP :ss$i";
|
2008-06-05 13:54:30 +00:00
|
|
|
$params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
|
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
} else if (substr($searchterm,0,1) == "-") {
|
|
|
|
$searchterm = substr($searchterm,1);
|
2008-06-05 13:54:30 +00:00
|
|
|
$searchterm = preg_quote($searchterm, '|');
|
2009-01-19 04:35:09 +00:00
|
|
|
$searchcond[] = "m.fullmessage $NOTREGEXP :ss$i";
|
2008-06-05 13:54:30 +00:00
|
|
|
$params['ss'.$i] = "(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)";
|
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
} else {
|
2010-09-04 14:52:47 +00:00
|
|
|
$searchcond[] = $DB->sql_like("m.fullmessage", ":ss$i", false, true, $NOT);
|
2008-06-05 13:54:30 +00:00
|
|
|
$params['ss'.$i] = "%$searchterm%";
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
2004-12-16 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 13:54:30 +00:00
|
|
|
if (empty($searchcond)) {
|
2010-09-04 13:05:51 +00:00
|
|
|
$searchcond = " ".$DB->sql_like('m.fullmessage', ':ss1', false);
|
2009-01-19 04:53:26 +00:00
|
|
|
$params['ss1'] = "%";
|
|
|
|
} else {
|
|
|
|
$searchcond = implode(" AND ", $searchcond);
|
2006-10-22 19:42:27 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// There are several possibilities
|
|
|
|
// 1. courseid = SITEID : The admin is searching messages by all users
|
|
|
|
// 2. courseid = ?? : A teacher is searching messages by users in
|
|
|
|
// one of their courses - currently disabled
|
|
|
|
// 3. courseid = none : User is searching their own messages;
|
|
|
|
// a. Messages from user
|
|
|
|
// b. Messages to user
|
|
|
|
// c. Messages to and from user
|
2004-12-29 03:54:33 +00:00
|
|
|
|
2015-03-24 17:11:15 -07:00
|
|
|
if ($fromme && $tome) {
|
|
|
|
$searchcond .= " AND ((useridto = :useridto AND timeusertodeleted = 0) OR
|
|
|
|
(useridfrom = :useridfrom AND timeuserfromdeleted = 0))";
|
|
|
|
$params['useridto'] = $userid;
|
|
|
|
$params['useridfrom'] = $userid;
|
|
|
|
} else if ($fromme) {
|
|
|
|
$searchcond .= " AND (useridfrom = :useridfrom AND timeuserfromdeleted = 0)";
|
|
|
|
$params['useridfrom'] = $userid;
|
|
|
|
} else if ($tome) {
|
|
|
|
$searchcond .= " AND (useridto = :useridto AND timeusertodeleted = 0)";
|
|
|
|
$params['useridto'] = $userid;
|
|
|
|
}
|
2012-11-12 15:25:49 +08:00
|
|
|
if ($courseid == SITEID) { // Admin is searching all messages.
|
2012-10-26 09:42:51 +08:00
|
|
|
$m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated
|
2008-06-05 13:54:30 +00:00
|
|
|
FROM {message_read} m
|
2010-07-07 07:49:08 +00:00
|
|
|
WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS);
|
2012-10-26 09:42:51 +08:00
|
|
|
$m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated
|
2008-06-05 13:54:30 +00:00
|
|
|
FROM {message} m
|
2010-07-07 07:49:08 +00:00
|
|
|
WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS);
|
2008-06-05 13:54:30 +00:00
|
|
|
|
|
|
|
} else if ($courseid !== 'none') {
|
2012-11-12 15:25:49 +08:00
|
|
|
// This has not been implemented due to security concerns.
|
2008-06-05 13:54:30 +00:00
|
|
|
$m_read = array();
|
|
|
|
$m_unread = array();
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2004-12-29 03:54:33 +00:00
|
|
|
} else {
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2008-06-05 13:54:30 +00:00
|
|
|
if ($fromme and $tome) {
|
|
|
|
$searchcond .= " AND (m.useridfrom=:userid1 OR m.useridto=:userid2)";
|
|
|
|
$params['userid1'] = $userid;
|
|
|
|
$params['userid2'] = $userid;
|
|
|
|
|
|
|
|
} else if ($fromme) {
|
|
|
|
$searchcond .= " AND m.useridfrom=:userid";
|
|
|
|
$params['userid'] = $userid;
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2008-06-05 13:54:30 +00:00
|
|
|
} else if ($tome) {
|
|
|
|
$searchcond .= " AND m.useridto=:userid";
|
|
|
|
$params['userid'] = $userid;
|
|
|
|
}
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2012-10-26 09:42:51 +08:00
|
|
|
$m_read = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated
|
2008-06-05 13:54:30 +00:00
|
|
|
FROM {message_read} m
|
2010-07-07 07:49:08 +00:00
|
|
|
WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS);
|
2012-10-26 09:42:51 +08:00
|
|
|
$m_unread = $DB->get_records_sql("SELECT m.id, m.useridto, m.useridfrom, m.smallmessage, m.fullmessage, m.timecreated
|
2008-06-05 13:54:30 +00:00
|
|
|
FROM {message} m
|
2010-07-07 07:49:08 +00:00
|
|
|
WHERE $searchcond", $params, 0, MESSAGE_SEARCH_MAX_RESULTS);
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
}
|
|
|
|
|
2004-12-29 03:54:33 +00:00
|
|
|
/// The keys may be duplicated in $m_read and $m_unread so we can't
|
|
|
|
/// do a simple concatenation
|
2012-01-15 21:49:30 +01:00
|
|
|
$messages = array();
|
2008-06-05 13:54:30 +00:00
|
|
|
foreach ($m_read as $m) {
|
|
|
|
$messages[] = $m;
|
|
|
|
}
|
|
|
|
foreach ($m_unread as $m) {
|
|
|
|
$messages[] = $m;
|
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
|
|
|
return (empty($messages)) ? false : $messages;
|
2004-12-16 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Given a message object that we already know has a long message
|
|
|
|
* this function truncates the message nicely to the first
|
|
|
|
* sane place between $CFG->forum_longpost and $CFG->forum_shortpost
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param string $message the message
|
|
|
|
* @param int $minlength the minimum length to trim the message to
|
|
|
|
* @return string the shortened message
|
|
|
|
*/
|
|
|
|
function message_shorten_message($message, $minlength = 0) {
|
2004-12-28 13:49:15 +00:00
|
|
|
$i = 0;
|
|
|
|
$tag = false;
|
|
|
|
$length = strlen($message);
|
|
|
|
$count = 0;
|
|
|
|
$stopzone = false;
|
|
|
|
$truncate = 0;
|
|
|
|
if ($minlength == 0) $minlength = MESSAGE_SHORTLENGTH;
|
2006-12-25 22:34:05 +00:00
|
|
|
|
2004-12-28 13:49:15 +00:00
|
|
|
|
|
|
|
for ($i=0; $i<$length; $i++) {
|
|
|
|
$char = $message[$i];
|
|
|
|
|
|
|
|
switch ($char) {
|
|
|
|
case "<":
|
|
|
|
$tag = true;
|
|
|
|
break;
|
|
|
|
case ">":
|
|
|
|
$tag = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!$tag) {
|
|
|
|
if ($stopzone) {
|
|
|
|
if ($char == '.' or $char == ' ') {
|
|
|
|
$truncate = $i+1;
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!$stopzone) {
|
|
|
|
if ($count > $minlength) {
|
|
|
|
$stopzone = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$truncate) {
|
|
|
|
$truncate = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return substr($message, 0, $truncate);
|
|
|
|
}
|
|
|
|
|
2005-01-03 12:16:06 +00:00
|
|
|
|
2008-06-05 13:54:30 +00:00
|
|
|
/**
|
2005-01-03 12:16:06 +00:00
|
|
|
* Given a string and an array of keywords, this function looks
|
|
|
|
* for the first keyword in the string, and then chops out a
|
|
|
|
* small section from the text that shows that word in context.
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param string $message the text to search
|
|
|
|
* @param array $keywords array of keywords to find
|
2005-01-03 12:16:06 +00:00
|
|
|
*/
|
|
|
|
function message_get_fragment($message, $keywords) {
|
|
|
|
|
2010-11-08 03:10:30 +00:00
|
|
|
$fullsize = 160;
|
2005-01-03 12:16:06 +00:00
|
|
|
$halfsize = (int)($fullsize/2);
|
|
|
|
|
|
|
|
$message = strip_tags($message);
|
|
|
|
|
|
|
|
foreach ($keywords as $keyword) { // Just get the first one
|
|
|
|
if ($keyword !== '') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($keyword)) { // None found, so just return start of message
|
|
|
|
return message_shorten_message($message, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
$leadin = $leadout = '';
|
|
|
|
|
|
|
|
/// Find the start of the fragment
|
|
|
|
$start = 0;
|
|
|
|
$length = strlen($message);
|
|
|
|
|
|
|
|
$pos = strpos($message, $keyword);
|
|
|
|
if ($pos > $halfsize) {
|
|
|
|
$start = $pos - $halfsize;
|
|
|
|
$leadin = '...';
|
|
|
|
}
|
|
|
|
/// Find the end of the fragment
|
|
|
|
$end = $start + $fullsize;
|
|
|
|
if ($end > $length) {
|
|
|
|
$end = $length;
|
|
|
|
} else {
|
|
|
|
$leadout = '...';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pull out the fragment and format it
|
|
|
|
|
|
|
|
$fragment = substr($message, $start, $end - $start);
|
|
|
|
$fragment = $leadin.highlight(implode(' ',$keywords), $fragment).$leadout;
|
|
|
|
return $fragment;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Retrieve the messages between two users
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $user1 the current user
|
|
|
|
* @param object $user2 the other user
|
|
|
|
* @param int $limitnum the maximum number of messages to retrieve
|
|
|
|
* @param bool $viewingnewmessages are we currently viewing new messages?
|
|
|
|
*/
|
2010-10-25 09:29:34 +00:00
|
|
|
function message_get_history($user1, $user2, $limitnum=0, $viewingnewmessages=false) {
|
|
|
|
global $DB, $CFG;
|
2008-06-05 13:41:16 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$messages = array();
|
|
|
|
|
|
|
|
//we want messages sorted oldest to newest but if getting a subset of messages we need to sort
|
|
|
|
//desc to get the last $limitnum messages then flip the order in php
|
|
|
|
$sort = 'asc';
|
|
|
|
if ($limitnum>0) {
|
|
|
|
$sort = 'desc';
|
|
|
|
}
|
|
|
|
|
2010-10-25 09:29:34 +00:00
|
|
|
$notificationswhere = null;
|
|
|
|
//we have just moved new messages to read. If theyre here to see new messages dont hide notifications
|
|
|
|
if (!$viewingnewmessages && $CFG->messaginghidereadnotifications) {
|
|
|
|
$notificationswhere = 'AND notification=0';
|
|
|
|
}
|
|
|
|
|
2010-11-01 02:27:34 +00:00
|
|
|
//prevent notifications of your own actions appearing in your own message history
|
|
|
|
$ownnotificationwhere = ' AND NOT (useridfrom=? AND notification=1)';
|
|
|
|
|
2015-03-24 17:11:15 -07:00
|
|
|
$sql = "((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0) OR
|
|
|
|
(useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))";
|
|
|
|
if ($messages_read = $DB->get_records_select('message_read', $sql . $notificationswhere . $ownnotificationwhere,
|
2010-11-01 02:27:34 +00:00
|
|
|
array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id),
|
2010-06-25 08:16:10 +00:00
|
|
|
"timecreated $sort", '*', 0, $limitnum)) {
|
|
|
|
foreach ($messages_read as $message) {
|
2012-08-21 11:58:13 +08:00
|
|
|
$messages[] = $message;
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 17:11:15 -07:00
|
|
|
if ($messages_new = $DB->get_records_select('message', $sql . $ownnotificationwhere,
|
2010-11-01 02:27:34 +00:00
|
|
|
array($user1->id, $user2->id, $user2->id, $user1->id, $user1->id),
|
2010-06-25 08:16:10 +00:00
|
|
|
"timecreated $sort", '*', 0, $limitnum)) {
|
2005-01-01 04:23:09 +00:00
|
|
|
foreach ($messages_new as $message) {
|
2012-08-21 11:58:13 +08:00
|
|
|
$messages[] = $message;
|
2005-01-01 04:23:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2013-08-06 20:58:28 +02:00
|
|
|
$result = core_collator::asort_objects_by_property($messages, 'timecreated', core_collator::SORT_NUMERIC);
|
2012-08-21 11:58:13 +08:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
//if we only want the last $limitnum messages
|
2010-11-17 06:36:26 +00:00
|
|
|
$messagecount = count($messages);
|
2012-08-21 11:58:13 +08:00
|
|
|
if ($limitnum > 0 && $messagecount > $limitnum) {
|
|
|
|
$messages = array_slice($messages, $messagecount - $limitnum, $limitnum, true);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2010-07-04 18:36:34 +00:00
|
|
|
|
2005-01-01 04:23:09 +00:00
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Print the message history between two users
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $user1 the current user
|
|
|
|
* @param object $user2 the other user
|
|
|
|
* @param string $search search terms to highlight
|
|
|
|
* @param int $messagelimit maximum number of messages to return
|
|
|
|
* @param string $messagehistorylink the html for the message history link or false
|
|
|
|
* @param bool $viewingnewmessages are we currently viewing new messages?
|
|
|
|
*/
|
2012-11-12 15:25:49 +08:00
|
|
|
function message_print_message_history($user1, $user2 ,$search = '', $messagelimit = 0, $messagehistorylink = false, $viewingnewmessages = false, $showactionlinks = true) {
|
2015-03-24 17:11:15 -07:00
|
|
|
global $OUTPUT, $PAGE;
|
|
|
|
|
|
|
|
$PAGE->requires->yui_module(
|
|
|
|
array('moodle-core_message-toolbox'),
|
|
|
|
'M.core_message.toolbox.deletemsg.init',
|
|
|
|
array(array())
|
|
|
|
);
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2014-11-05 10:37:41 +13:00
|
|
|
echo $OUTPUT->box_start('center', 'message_user_pictures');
|
|
|
|
echo $OUTPUT->box_start('user');
|
|
|
|
echo $OUTPUT->box_start('generalbox', 'user1');
|
2011-01-12 17:15:43 +08:00
|
|
|
echo $OUTPUT->user_picture($user1, array('size' => 100, 'courseid' => SITEID));
|
|
|
|
echo html_writer::tag('div', fullname($user1), array('class' => 'heading'));
|
2014-11-05 10:37:41 +13:00
|
|
|
echo $OUTPUT->box_end();
|
|
|
|
echo $OUTPUT->box_end();
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2014-11-05 10:37:41 +13:00
|
|
|
$imgattr = array('src' => $OUTPUT->pix_url('i/twoway'), 'alt' => '', 'width' => 16, 'height' => 16);
|
|
|
|
echo $OUTPUT->box(html_writer::empty_tag('img', $imgattr), 'between');
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2014-11-05 10:37:41 +13:00
|
|
|
echo $OUTPUT->box_start('user');
|
|
|
|
echo $OUTPUT->box_start('generalbox', 'user2');
|
2013-11-07 12:56:01 +08:00
|
|
|
// Show user picture with link is real user else without link.
|
|
|
|
if (core_user::is_real_user($user2->id)) {
|
|
|
|
echo $OUTPUT->user_picture($user2, array('size' => 100, 'courseid' => SITEID));
|
|
|
|
} else {
|
|
|
|
echo $OUTPUT->user_picture($user2, array('size' => 100, 'courseid' => SITEID, 'link' => false));
|
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::tag('div', fullname($user2), array('class' => 'heading'));
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
if ($showactionlinks && isset($user2->iscontact) && isset($user2->isblocked)) {
|
2010-06-25 08:16:10 +00:00
|
|
|
|
|
|
|
$script = null;
|
|
|
|
$text = true;
|
|
|
|
$icon = false;
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
$strcontact = message_get_contact_add_remove_link($user2->iscontact, $user2->isblocked, $user2, $script, $text, $icon);
|
|
|
|
$strblock = message_get_contact_block_link($user2->iscontact, $user2->isblocked, $user2, $script, $text, $icon);
|
2014-11-14 15:49:18 +13:00
|
|
|
$useractionlinks = $strcontact.' | '.$strblock;
|
2010-06-28 02:59:06 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::tag('div', $useractionlinks, array('class' => 'useractionlinks'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2014-11-05 10:37:41 +13:00
|
|
|
echo $OUTPUT->box_end();
|
|
|
|
echo $OUTPUT->box_end();
|
2010-06-25 08:16:10 +00:00
|
|
|
echo $OUTPUT->box_end();
|
|
|
|
|
|
|
|
if (!empty($messagehistorylink)) {
|
|
|
|
echo $messagehistorylink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get all the messages and print them
|
2010-10-25 09:29:34 +00:00
|
|
|
if ($messages = message_get_history($user1, $user2, $messagelimit, $viewingnewmessages)) {
|
2010-06-25 08:16:10 +00:00
|
|
|
$tablecontents = '';
|
|
|
|
|
2010-09-21 08:18:23 +00:00
|
|
|
$current = new stdClass();
|
2010-06-25 08:16:10 +00:00
|
|
|
$current->mday = '';
|
|
|
|
$current->month = '';
|
|
|
|
$current->year = '';
|
|
|
|
$messagedate = get_string('strftimetime');
|
|
|
|
$blockdate = get_string('strftimedaydate');
|
2015-03-24 17:11:15 -07:00
|
|
|
$messagenumber = 0;
|
2010-06-25 08:16:10 +00:00
|
|
|
foreach ($messages as $message) {
|
2015-03-24 17:11:15 -07:00
|
|
|
$messagenumber++;
|
2010-10-25 09:29:34 +00:00
|
|
|
if ($message->notification) {
|
|
|
|
$notificationclass = ' notification';
|
|
|
|
} else {
|
|
|
|
$notificationclass = null;
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
$date = usergetdate($message->timecreated);
|
|
|
|
if ($current->mday != $date['mday'] | $current->month != $date['month'] | $current->year != $date['year']) {
|
|
|
|
$current->mday = $date['mday'];
|
|
|
|
$current->month = $date['month'];
|
|
|
|
$current->year = $date['year'];
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
$datestring = html_writer::empty_tag('a', array('name' => $date['year'].$date['mon'].$date['mday']));
|
|
|
|
$tablecontents .= html_writer::tag('div', $datestring, array('class' => 'mdl-align heading'));
|
|
|
|
|
|
|
|
$tablecontents .= $OUTPUT->heading(userdate($message->timecreated, $blockdate), 4, 'mdl-align');
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if ($message->useridfrom == $user1->id) {
|
2011-01-12 17:15:43 +08:00
|
|
|
$formatted_message = message_format_message($message, $messagedate, $search, 'me');
|
|
|
|
$side = 'left';
|
2010-06-25 08:16:10 +00:00
|
|
|
} else {
|
2011-01-12 17:15:43 +08:00
|
|
|
$formatted_message = message_format_message($message, $messagedate, $search, 'other');
|
|
|
|
$side = 'right';
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2015-03-24 17:11:15 -07:00
|
|
|
|
|
|
|
// Check if it is a read message or not.
|
|
|
|
if (isset($message->timeread)) {
|
|
|
|
$type = 'message_read';
|
|
|
|
} else {
|
|
|
|
$type = 'message';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message_can_delete_message($message, $user1->id)) {
|
|
|
|
$usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
|
|
|
|
$viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT);
|
|
|
|
$deleteurl = new moodle_url('/message/index.php', array('user1' => $user1->id, 'user2' => $user2->id,
|
|
|
|
'viewing' => $viewing, 'deletemessageid' => $message->id, 'deletemessagetype' => $type,
|
|
|
|
'sesskey' => sesskey()));
|
|
|
|
|
|
|
|
$deleteicon = $OUTPUT->action_icon($deleteurl, new pix_icon('t/delete', get_string('delete')));
|
|
|
|
$deleteicon = html_writer::tag('div', $deleteicon, array('class' => 'deleteicon accesshide'));
|
|
|
|
$formatted_message .= $deleteicon;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tablecontents .= html_writer::tag('div', $formatted_message, array('class' => "mdl-left messagecontent
|
|
|
|
$side $notificationclass", 'id' => 'message_' . $messagenumber));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::nonempty_tag('div', $tablecontents, array('class' => 'mdl-left messagehistory'));
|
2010-06-25 08:16:10 +00:00
|
|
|
} else {
|
2011-01-12 17:15:43 +08:00
|
|
|
echo html_writer::nonempty_tag('div', '('.get_string('nomessagesfound', 'message').')', array('class' => 'mdl-align messagehistory'));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Format a message for display in the message history
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $message the message object
|
|
|
|
* @param string $format optional date format
|
|
|
|
* @param string $keywords keywords to highlight
|
|
|
|
* @param string $class CSS class to apply to the div around the message
|
|
|
|
* @return string the formatted message
|
|
|
|
*/
|
|
|
|
function message_format_message($message, $format='', $keywords='', $class='other') {
|
2005-03-04 14:53:23 +00:00
|
|
|
|
|
|
|
static $dateformat;
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
//if we haven't previously set the date format or they've supplied a new one
|
|
|
|
if ( empty($dateformat) || (!empty($format) && $dateformat != $format) ) {
|
2005-03-04 14:53:23 +00:00
|
|
|
if ($format) {
|
|
|
|
$dateformat = $format;
|
|
|
|
} else {
|
2011-01-12 17:15:43 +08:00
|
|
|
$dateformat = get_string('strftimedatetimeshort');
|
2005-03-04 14:53:23 +00:00
|
|
|
}
|
2005-01-01 04:23:09 +00:00
|
|
|
}
|
2005-03-04 14:53:23 +00:00
|
|
|
$time = userdate($message->timecreated, $dateformat);
|
2010-10-15 06:59:02 +00:00
|
|
|
|
2013-10-08 18:29:02 +02:00
|
|
|
$messagetext = message_format_message_text($message, false);
|
2011-01-12 17:15:43 +08:00
|
|
|
|
|
|
|
if ($keywords) {
|
|
|
|
$messagetext = highlight($keywords, $messagetext);
|
|
|
|
}
|
|
|
|
|
2013-10-08 18:29:02 +02:00
|
|
|
$messagetext .= message_format_contexturl($message);
|
|
|
|
|
2013-10-08 18:38:31 +02:00
|
|
|
$messagetext = clean_text($messagetext, FORMAT_HTML);
|
|
|
|
|
2012-07-11 11:12:14 +08:00
|
|
|
return <<<TEMPLATE
|
|
|
|
<div class='message $class'>
|
2014-01-05 13:42:12 +01:00
|
|
|
<a name="m{$message->id}"></a>
|
2012-07-11 11:12:14 +08:00
|
|
|
<span class="message-meta"><span class="time">$time</span></span>: <span class="text">$messagetext</span>
|
|
|
|
</div>
|
|
|
|
TEMPLATE;
|
2011-01-12 17:15:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a the context url and context url name of a message for display
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $message the message object
|
|
|
|
* @return string the formatted string
|
|
|
|
*/
|
|
|
|
function message_format_contexturl($message) {
|
|
|
|
$s = null;
|
|
|
|
|
2010-10-26 08:00:29 +00:00
|
|
|
if (!empty($message->contexturl)) {
|
|
|
|
$displaytext = null;
|
|
|
|
if (!empty($message->contexturlname)) {
|
|
|
|
$displaytext= $message->contexturlname;
|
|
|
|
} else {
|
|
|
|
$displaytext= $message->contexturl;
|
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
$s .= html_writer::start_tag('div',array('class' => 'messagecontext'));
|
|
|
|
$s .= get_string('view').': '.html_writer::tag('a', $displaytext, array('href' => $message->contexturl));
|
|
|
|
$s .= html_writer::end_tag('div');
|
2005-01-03 12:16:06 +00:00
|
|
|
}
|
2010-10-26 08:00:29 +00:00
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
return $s;
|
2005-01-01 04:23:09 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2008-06-05 13:41:16 +00:00
|
|
|
/**
|
2011-01-12 17:15:43 +08:00
|
|
|
* Send a message from one user to another. Will be delivered according to the message recipients messaging preferences
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $userfrom the message sender
|
|
|
|
* @param object $userto the message recipient
|
|
|
|
* @param string $message the message
|
|
|
|
* @param int $format message format such as FORMAT_PLAIN or FORMAT_HTML
|
2011-02-10 13:25:38 +08:00
|
|
|
* @return int|false the ID of the new message or false
|
2005-01-01 16:00:35 +00:00
|
|
|
*/
|
2011-01-12 17:15:43 +08:00
|
|
|
function message_post_message($userfrom, $userto, $message, $format) {
|
2010-10-08 02:31:57 +00:00
|
|
|
global $SITE, $CFG, $USER;
|
2010-11-02 07:37:25 +00:00
|
|
|
|
2010-09-21 08:18:23 +00:00
|
|
|
$eventdata = new stdClass();
|
2010-07-06 01:52:32 +00:00
|
|
|
$eventdata->component = 'moodle';
|
2008-08-30 17:53:30 +00:00
|
|
|
$eventdata->name = 'instantmessage';
|
2008-07-24 08:38:03 +00:00
|
|
|
$eventdata->userfrom = $userfrom;
|
|
|
|
$eventdata->userto = $userto;
|
2010-11-02 07:37:25 +00:00
|
|
|
|
|
|
|
//using string manager directly so that strings in the message will be in the message recipients language rather than the senders
|
|
|
|
$eventdata->subject = get_string_manager()->get_string('unreadnewmessage', 'message', fullname($userfrom), $userto->lang);
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($format == FORMAT_HTML) {
|
2010-11-24 04:17:52 +00:00
|
|
|
$eventdata->fullmessagehtml = $message;
|
2011-11-19 15:35:23 +08:00
|
|
|
//some message processors may revert to sending plain text even if html is supplied
|
|
|
|
//so we keep both plain and html versions if we're intending to send html
|
|
|
|
$eventdata->fullmessage = html_to_text($eventdata->fullmessagehtml);
|
2010-11-24 04:17:52 +00:00
|
|
|
} else {
|
|
|
|
$eventdata->fullmessage = $message;
|
|
|
|
$eventdata->fullmessagehtml = '';
|
|
|
|
}
|
2011-01-27 11:46:37 +01:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$eventdata->fullmessageformat = $format;
|
2011-03-23 11:24:04 +08:00
|
|
|
$eventdata->smallmessage = $message;//store the message unfiltered. Clean up on output.
|
2010-10-08 02:31:57 +00:00
|
|
|
|
|
|
|
$s = new stdClass();
|
2012-08-02 11:20:48 +08:00
|
|
|
$s->sitename = format_string($SITE->shortname, true, array('context' => context_course::instance(SITEID)));
|
2010-11-22 01:44:10 +00:00
|
|
|
$s->url = $CFG->wwwroot.'/message/index.php?user='.$userto->id.'&id='.$userfrom->id;
|
2010-10-08 02:31:57 +00:00
|
|
|
|
2010-11-02 07:37:25 +00:00
|
|
|
$emailtagline = get_string_manager()->get_string('emailtagline', 'message', $s, $userto->lang);
|
2010-10-15 06:59:02 +00:00
|
|
|
if (!empty($eventdata->fullmessage)) {
|
|
|
|
$eventdata->fullmessage .= "\n\n---------------------------------------------------------------------\n".$emailtagline;
|
|
|
|
}
|
|
|
|
if (!empty($eventdata->fullmessagehtml)) {
|
|
|
|
$eventdata->fullmessagehtml .= "<br /><br />---------------------------------------------------------------------<br />".$emailtagline;
|
|
|
|
}
|
2011-01-27 11:46:37 +01:00
|
|
|
|
2009-06-30 08:33:29 +00:00
|
|
|
$eventdata->timecreated = time();
|
2014-04-04 17:53:25 +02:00
|
|
|
$eventdata->notification = 0;
|
2009-11-07 10:27:57 +00:00
|
|
|
return message_send($eventdata);
|
2005-01-01 16:00:35 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
/**
|
2009-11-01 12:22:45 +00:00
|
|
|
* Print a row of contactlist displaying user picture, messages waiting and
|
2008-01-15 13:48:34 +00:00
|
|
|
* block links etc
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param object $contact contact object containing all fields required for $OUTPUT->user_picture()
|
|
|
|
* @param bool $incontactlist is the user a contact of ours?
|
|
|
|
* @param bool $isblocked is the user blocked?
|
|
|
|
* @param string $selectcontacturl the url to send the user to when a contact's name is clicked
|
|
|
|
* @param bool $showactionlinks display action links next to the other users (add contact, block user etc)
|
|
|
|
* @param object $selecteduser the user the current user is viewing (if any). They will be highlighted.
|
2011-05-27 09:28:09 +01:00
|
|
|
* @return void
|
2008-01-15 13:48:34 +00:00
|
|
|
*/
|
2010-11-02 03:05:11 +00:00
|
|
|
function message_print_contactlist_user($contact, $incontactlist = true, $isblocked = false, $selectcontacturl = null, $showactionlinks = true, $selecteduser=null) {
|
2013-09-04 16:40:01 +10:00
|
|
|
global $OUTPUT, $USER, $COURSE;
|
2008-01-15 13:48:34 +00:00
|
|
|
$fullname = fullname($contact);
|
|
|
|
$fullnamelink = $fullname;
|
2014-11-14 15:49:18 +13:00
|
|
|
$output = '';
|
2008-01-15 13:48:34 +00:00
|
|
|
|
2010-11-02 03:05:11 +00:00
|
|
|
$linkclass = '';
|
2011-01-12 17:15:43 +08:00
|
|
|
if (!empty($selecteduser) && $contact->id == $selecteduser->id) {
|
2010-11-02 03:05:11 +00:00
|
|
|
$linkclass = 'messageselecteduser';
|
|
|
|
}
|
|
|
|
|
2012-11-12 15:25:49 +08:00
|
|
|
// Are there any unread messages for this contact?
|
2008-01-15 13:48:34 +00:00
|
|
|
if ($contact->messagecount > 0 ){
|
|
|
|
$fullnamelink = '<strong>'.$fullnamelink.' ('.$contact->messagecount.')</strong>';
|
|
|
|
}
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$strcontact = $strblock = $strhistory = null;
|
2008-01-15 13:48:34 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
if ($showactionlinks) {
|
2013-11-07 12:56:01 +08:00
|
|
|
// Show block and delete links if user is real user.
|
|
|
|
if (core_user::is_real_user($contact->id)) {
|
|
|
|
$strcontact = message_get_contact_add_remove_link($incontactlist, $isblocked, $contact);
|
|
|
|
$strblock = message_get_contact_block_link($incontactlist, $isblocked, $contact);
|
|
|
|
}
|
2010-06-25 08:16:10 +00:00
|
|
|
$strhistory = message_history_link($USER->id, $contact->id, true, '', '', 'icon');
|
2008-01-15 13:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
$output .= html_writer::start_tag('div', array('class' => 'pix'));
|
|
|
|
$output .= $OUTPUT->user_picture($contact, array('size' => 20, 'courseid' => $COURSE->id));
|
|
|
|
$output .= html_writer::end_tag('div');
|
2009-11-01 12:22:45 +00:00
|
|
|
|
2009-08-20 13:15:29 +00:00
|
|
|
$popupoptions = array(
|
2010-06-25 08:16:10 +00:00
|
|
|
'height' => MESSAGE_DISCUSSION_HEIGHT,
|
|
|
|
'width' => MESSAGE_DISCUSSION_WIDTH,
|
2009-08-20 13:15:29 +00:00
|
|
|
'menubar' => false,
|
|
|
|
'location' => false,
|
|
|
|
'status' => true,
|
|
|
|
'scrollbars' => true,
|
|
|
|
'resizable' => true);
|
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
$link = $action = null;
|
|
|
|
if (!empty($selectcontacturl)) {
|
2011-02-16 15:48:59 +08:00
|
|
|
$link = new moodle_url($selectcontacturl.'&user2='.$contact->id);
|
2010-06-25 08:16:10 +00:00
|
|
|
} else {
|
2010-11-01 07:34:32 +00:00
|
|
|
//can $selectcontacturl be removed and maybe the be removed and hardcoded?
|
|
|
|
$link = new moodle_url("/message/index.php?id=$contact->id");
|
2010-06-25 08:16:10 +00:00
|
|
|
$action = new popup_action('click', $link, "message_$contact->id", $popupoptions);
|
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2008-01-15 13:48:34 +00:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
if (strlen($strcontact . $strblock . $strhistory) > 0) {
|
|
|
|
$output .= html_writer::tag('div', $strcontact . $strblock . $strhistory, array('class' => 'link'));
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2014-11-14 15:49:18 +13:00
|
|
|
$output .= html_writer::start_tag('div', array('class' => 'contact'));
|
|
|
|
$linkattr = array('class' => $linkclass, 'title' => get_string('sendmessageto', 'message', $fullname));
|
|
|
|
$output .= $OUTPUT->action_link($link, $fullnamelink, $action, $linkattr);
|
|
|
|
$output .= html_writer::end_tag('div');
|
|
|
|
} else {
|
|
|
|
$output .= html_writer::start_tag('div', array('class' => 'contact nolinks'));
|
|
|
|
$linkattr = array('class' => $linkclass, 'title' => get_string('sendmessageto', 'message', $fullname));
|
|
|
|
$output .= $OUTPUT->action_link($link, $fullnamelink, $action, $linkattr);
|
|
|
|
$output .= html_writer::end_tag('div');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
2008-01-15 13:48:34 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Constructs the add/remove contact link to display next to other users
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param bool $incontactlist is the user a contact
|
|
|
|
* @param bool $isblocked is the user blocked
|
2012-01-05 12:05:02 +07:00
|
|
|
* @param stdClass $contact contact object
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param string $script the URL to send the user to when the link is clicked. If null, the current page.
|
|
|
|
* @param bool $text include text next to the icons?
|
|
|
|
* @param bool $icon include a graphical icon?
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-06-25 08:16:10 +00:00
|
|
|
function message_get_contact_add_remove_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) {
|
|
|
|
$strcontact = '';
|
|
|
|
|
|
|
|
if($incontactlist){
|
|
|
|
$strcontact = message_contact_link($contact->id, 'remove', true, $script, $text, $icon);
|
|
|
|
} else if ($isblocked) {
|
|
|
|
$strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon);
|
|
|
|
} else{
|
|
|
|
$strcontact = message_contact_link($contact->id, 'add', true, $script, $text, $icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strcontact;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:15:43 +08:00
|
|
|
/**
|
|
|
|
* Constructs the block contact link to display next to other users
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @param bool $incontactlist is the user a contact?
|
|
|
|
* @param bool $isblocked is the user blocked?
|
|
|
|
* @param stdClass $contact contact object
|
2011-01-12 17:15:43 +08:00
|
|
|
* @param string $script the URL to send the user to when the link is clicked. If null, the current page.
|
|
|
|
* @param bool $text include text next to the icons?
|
|
|
|
* @param bool $icon include a graphical icon?
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-06-25 08:16:10 +00:00
|
|
|
function message_get_contact_block_link($incontactlist, $isblocked, $contact, $script=null, $text=false, $icon=true) {
|
|
|
|
$strblock = '';
|
|
|
|
|
|
|
|
//commented out to allow the user to block a contact without having to remove them first
|
|
|
|
/*if ($incontactlist) {
|
|
|
|
//$strblock = '';
|
|
|
|
} else*/
|
|
|
|
if ($isblocked) {
|
2014-11-14 15:49:18 +13:00
|
|
|
$strblock = message_contact_link($contact->id, 'unblock', true, $script, $text, $icon);
|
2010-06-25 08:16:10 +00:00
|
|
|
} else{
|
2014-11-14 15:49:18 +13:00
|
|
|
$strblock = message_contact_link($contact->id, 'block', true, $script, $text, $icon);
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $strblock;
|
|
|
|
}
|
|
|
|
|
2011-05-27 09:28:09 +01:00
|
|
|
/**
|
|
|
|
* Moves messages from a particular user from the message table (unread messages) to message_read
|
|
|
|
* This is typically only used when a user is deleted
|
|
|
|
*
|
|
|
|
* @param object $userid User id
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
2009-04-26 22:56:56 +00:00
|
|
|
function message_move_userfrom_unread2read($userid) {
|
|
|
|
global $DB;
|
|
|
|
|
2010-09-17 19:41:02 +00:00
|
|
|
// move all unread messages from message table to message_read
|
2009-04-26 22:56:56 +00:00
|
|
|
if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) {
|
|
|
|
foreach ($messages as $message) {
|
2010-10-07 03:59:07 +00:00
|
|
|
message_mark_message_read($message, 0); //set timeread to 0 as the message was never read
|
2009-04-26 22:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-10-07 03:59:07 +00:00
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* marks ALL messages being sent from $fromuserid to $touserid as read
|
|
|
|
*
|
|
|
|
* @param int $touserid the id of the message recipient
|
|
|
|
* @param int $fromuserid the id of the message sender
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-06-27 10:10:55 +08:00
|
|
|
function message_mark_messages_read($touserid, $fromuserid) {
|
2010-06-25 08:16:10 +00:00
|
|
|
global $DB;
|
|
|
|
|
2010-10-07 03:59:07 +00:00
|
|
|
$sql = 'SELECT m.* FROM {message} m WHERE m.useridto=:useridto AND m.useridfrom=:useridfrom';
|
2011-01-12 17:15:43 +08:00
|
|
|
$messages = $DB->get_recordset_sql($sql, array('useridto' => $touserid,'useridfrom' => $fromuserid));
|
2010-07-06 01:52:32 +00:00
|
|
|
|
2010-06-25 08:16:10 +00:00
|
|
|
foreach ($messages as $message) {
|
2010-10-07 03:59:07 +00:00
|
|
|
message_mark_message_read($message, time());
|
|
|
|
}
|
2011-05-10 16:31:23 +08:00
|
|
|
|
|
|
|
$messages->close();
|
2010-10-07 03:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-27 09:28:09 +01:00
|
|
|
* Mark a single message as read
|
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @param stdClass $message An object with an object property ie $message->id which is an id in the message table
|
2011-05-27 09:28:09 +01:00
|
|
|
* @param int $timeread the timestamp for when the message should be marked read. Usually time().
|
|
|
|
* @param bool $messageworkingempty Is the message_working table already confirmed empty for this message?
|
|
|
|
* @return int the ID of the message in the message_read table
|
|
|
|
*/
|
2010-10-07 03:59:07 +00:00
|
|
|
function message_mark_message_read($message, $timeread, $messageworkingempty=false) {
|
|
|
|
global $DB;
|
2011-01-27 11:46:37 +01:00
|
|
|
|
2010-10-07 03:59:07 +00:00
|
|
|
$message->timeread = $timeread;
|
2010-06-25 08:16:10 +00:00
|
|
|
|
2010-10-07 03:59:07 +00:00
|
|
|
$messageid = $message->id;
|
|
|
|
unset($message->id);//unset because it will get a new id on insert into message_read
|
2010-07-06 01:52:32 +00:00
|
|
|
|
2010-10-07 03:59:07 +00:00
|
|
|
//If any processors have pending actions abort them
|
|
|
|
if (!$messageworkingempty) {
|
|
|
|
$DB->delete_records('message_working', array('unreadmessageid' => $messageid));
|
2010-06-25 08:16:10 +00:00
|
|
|
}
|
2011-02-10 13:25:38 +08:00
|
|
|
$messagereadid = $DB->insert_record('message_read', $message);
|
2014-02-25 22:27:38 -08:00
|
|
|
|
2010-10-07 03:59:07 +00:00
|
|
|
$DB->delete_records('message', array('id' => $messageid));
|
2014-02-25 22:27:38 -08:00
|
|
|
|
2014-08-29 15:20:55 -07:00
|
|
|
// Get the context for the user who received the message.
|
|
|
|
$context = context_user::instance($message->useridto, IGNORE_MISSING);
|
|
|
|
// If the user no longer exists the context value will be false, in this case use the system context.
|
|
|
|
if ($context === false) {
|
|
|
|
$context = context_system::instance();
|
|
|
|
}
|
|
|
|
|
2014-02-25 22:27:38 -08:00
|
|
|
// Trigger event for reading a message.
|
2014-04-17 15:52:34 -07:00
|
|
|
$event = \core\event\message_viewed::create(array(
|
2014-02-25 22:27:38 -08:00
|
|
|
'objectid' => $messagereadid,
|
|
|
|
'userid' => $message->useridto, // Using the user who read the message as they are the ones performing the action.
|
2014-08-29 15:20:55 -07:00
|
|
|
'context' => $context,
|
2014-02-25 22:27:38 -08:00
|
|
|
'relateduserid' => $message->useridfrom,
|
|
|
|
'other' => array(
|
|
|
|
'messageid' => $messageid
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$event->trigger();
|
|
|
|
|
2011-02-10 13:25:38 +08:00
|
|
|
return $messagereadid;
|
2010-06-29 14:57:45 +00:00
|
|
|
}
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2011-05-11 16:48:04 +01:00
|
|
|
/**
|
2011-05-17 10:47:21 +01:00
|
|
|
* Get all message processors, validate corresponding plugin existance and
|
|
|
|
* system configuration
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-05-17 10:47:21 +01:00
|
|
|
* @param bool $ready only return ready-to-use processors
|
2013-08-19 08:40:57 +12:00
|
|
|
* @param bool $reset Reset list of message processors (used in unit tests)
|
2015-11-20 13:55:27 -08:00
|
|
|
* @param bool $resetonly Just reset, then exit
|
2011-05-17 10:47:21 +01:00
|
|
|
* @return mixed $processors array of objects containing information on message processors
|
2011-05-11 16:48:04 +01:00
|
|
|
*/
|
2015-11-20 13:55:27 -08:00
|
|
|
function get_message_processors($ready = false, $reset = false, $resetonly = false) {
|
2011-05-11 16:48:04 +01:00
|
|
|
global $DB, $CFG;
|
|
|
|
|
2011-05-13 11:13:13 +01:00
|
|
|
static $processors;
|
2013-08-19 08:40:57 +12:00
|
|
|
if ($reset) {
|
|
|
|
$processors = array();
|
2015-11-20 13:55:27 -08:00
|
|
|
|
|
|
|
if ($resetonly) {
|
|
|
|
return $processors;
|
|
|
|
}
|
2013-08-19 08:40:57 +12:00
|
|
|
}
|
2011-05-13 11:13:13 +01:00
|
|
|
|
|
|
|
if (empty($processors)) {
|
2011-05-17 10:47:21 +01:00
|
|
|
// Get all processors, ensure the name column is the first so it will be the array key
|
|
|
|
$processors = $DB->get_records('message_processors', null, 'name DESC', 'name, id, enabled');
|
2011-05-13 11:13:13 +01:00
|
|
|
foreach ($processors as &$processor){
|
|
|
|
$processorfile = $CFG->dirroot. '/message/output/'.$processor->name.'/message_output_'.$processor->name.'.php';
|
|
|
|
if (is_readable($processorfile)) {
|
|
|
|
include_once($processorfile);
|
|
|
|
$processclass = 'message_output_' . $processor->name;
|
|
|
|
if (class_exists($processclass)) {
|
|
|
|
$pclass = new $processclass();
|
2011-05-17 10:47:21 +01:00
|
|
|
$processor->object = $pclass;
|
2011-05-13 11:13:13 +01:00
|
|
|
$processor->configured = 0;
|
|
|
|
if ($pclass->is_system_configured()) {
|
|
|
|
$processor->configured = 1;
|
|
|
|
}
|
|
|
|
$processor->hassettings = 0;
|
|
|
|
if (is_readable($CFG->dirroot.'/message/output/'.$processor->name.'/settings.php')) {
|
|
|
|
$processor->hassettings = 1;
|
|
|
|
}
|
|
|
|
$processor->available = 1;
|
|
|
|
} else {
|
|
|
|
print_error('errorcallingprocessor', 'message');
|
2011-05-11 16:48:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
2011-05-13 11:13:13 +01:00
|
|
|
$processor->available = 0;
|
2011-05-11 16:48:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-17 10:47:21 +01:00
|
|
|
if ($ready) {
|
2011-05-31 11:18:11 +01:00
|
|
|
// Filter out enabled and system_configured processors
|
|
|
|
$readyprocessors = $processors;
|
|
|
|
foreach ($readyprocessors as $readyprocessor) {
|
|
|
|
if (!($readyprocessor->enabled && $readyprocessor->configured)) {
|
|
|
|
unset($readyprocessors[$readyprocessor->name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $readyprocessors;
|
2011-05-13 11:13:13 +01:00
|
|
|
}
|
|
|
|
|
2011-05-11 16:48:04 +01:00
|
|
|
return $processors;
|
|
|
|
}
|
2011-05-17 10:47:21 +01:00
|
|
|
|
2012-05-15 16:55:21 +01:00
|
|
|
/**
|
|
|
|
* Get all message providers, validate their plugin existance and
|
|
|
|
* system configuration
|
|
|
|
*
|
|
|
|
* @return mixed $processors array of objects containing information on message processors
|
|
|
|
*/
|
|
|
|
function get_message_providers() {
|
|
|
|
global $CFG, $DB;
|
2013-10-04 22:40:44 +02:00
|
|
|
|
|
|
|
$pluginman = core_plugin_manager::instance();
|
2012-05-15 16:55:21 +01:00
|
|
|
|
|
|
|
$providers = $DB->get_records('message_providers', null, 'name');
|
|
|
|
|
|
|
|
// Remove all the providers whose plugins are disabled or don't exist
|
|
|
|
foreach ($providers as $providerid => $provider) {
|
|
|
|
$plugin = $pluginman->get_plugin_info($provider->component);
|
|
|
|
if ($plugin) {
|
2013-10-04 22:40:44 +02:00
|
|
|
if ($plugin->get_status() === core_plugin_manager::PLUGIN_STATUS_MISSING) {
|
2012-05-15 16:55:21 +01:00
|
|
|
unset($providers[$providerid]); // Plugins does not exist
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($plugin->is_enabled() === false) {
|
|
|
|
unset($providers[$providerid]); // Plugin disabled
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $providers;
|
|
|
|
}
|
|
|
|
|
2011-09-27 18:05:38 +01:00
|
|
|
/**
|
|
|
|
* Get an instance of the message_output class for one of the output plugins.
|
|
|
|
* @param string $type the message output type. E.g. 'email' or 'jabber'.
|
|
|
|
* @return message_output message_output the requested class.
|
|
|
|
*/
|
|
|
|
function get_message_processor($type) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// Note, we cannot use the get_message_processors function here, becaues this
|
|
|
|
// code is called during install after installing each messaging plugin, and
|
|
|
|
// get_message_processors caches the list of installed plugins.
|
|
|
|
|
|
|
|
$processorfile = $CFG->dirroot . "/message/output/{$type}/message_output_{$type}.php";
|
|
|
|
if (!is_readable($processorfile)) {
|
|
|
|
throw new coding_exception('Unknown message processor type ' . $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
include_once($processorfile);
|
|
|
|
|
|
|
|
$processclass = 'message_output_' . $type;
|
|
|
|
if (!class_exists($processclass)) {
|
|
|
|
throw new coding_exception('Message processor ' . $type .
|
|
|
|
' does not define the right class');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $processclass();
|
|
|
|
}
|
|
|
|
|
2011-05-17 10:47:21 +01:00
|
|
|
/**
|
|
|
|
* Get messaging outputs default (site) preferences
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-05-17 10:47:21 +01:00
|
|
|
* @return object $processors object containing information on message processors
|
|
|
|
*/
|
|
|
|
function get_message_output_default_preferences() {
|
2012-01-21 11:15:30 +01:00
|
|
|
return get_config('message');
|
2011-05-17 10:47:21 +01:00
|
|
|
}
|
2011-05-20 15:10:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate message default settings from binary value to the array of string
|
|
|
|
* representing the settings to be stored. Also validate the provided value and
|
|
|
|
* use default if it is malformed.
|
2011-05-27 09:28:09 +01:00
|
|
|
*
|
2011-05-20 15:10:27 +01:00
|
|
|
* @param int $plugindefault Default setting suggested by plugin
|
|
|
|
* @param string $processorname The name of processor
|
|
|
|
* @return array $settings array of strings in the order: $permitted, $loggedin, $loggedoff.
|
|
|
|
*/
|
|
|
|
function translate_message_default_setting($plugindefault, $processorname) {
|
|
|
|
// Preset translation arrays
|
|
|
|
$permittedvalues = array(
|
|
|
|
0x04 => 'disallowed',
|
|
|
|
0x08 => 'permitted',
|
|
|
|
0x0c => 'forced',
|
|
|
|
);
|
|
|
|
|
|
|
|
$loggedinstatusvalues = array(
|
|
|
|
0x00 => null, // use null if loggedin/loggedoff is not defined
|
|
|
|
0x01 => 'loggedin',
|
|
|
|
0x02 => 'loggedoff',
|
|
|
|
);
|
|
|
|
|
|
|
|
// define the default setting
|
2011-09-27 18:05:38 +01:00
|
|
|
$processor = get_message_processor($processorname);
|
|
|
|
$default = $processor->get_default_messaging_settings();
|
2011-05-20 15:10:27 +01:00
|
|
|
|
|
|
|
// Validate the value. It should not exceed the maximum size
|
|
|
|
if (!is_int($plugindefault) || ($plugindefault > 0x0f)) {
|
2012-01-21 16:59:16 +01:00
|
|
|
debugging(get_string('errortranslatingdefault', 'message'));
|
2011-05-20 15:10:27 +01:00
|
|
|
$plugindefault = $default;
|
|
|
|
}
|
|
|
|
// Use plugin default setting of 'permitted' is 0
|
|
|
|
if (!($plugindefault & MESSAGE_PERMITTED_MASK)) {
|
|
|
|
$plugindefault = $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
$permitted = $permittedvalues[$plugindefault & MESSAGE_PERMITTED_MASK];
|
2011-05-31 12:30:03 +01:00
|
|
|
$loggedin = $loggedoff = null;
|
2011-05-20 15:10:27 +01:00
|
|
|
|
|
|
|
if (($plugindefault & MESSAGE_PERMITTED_MASK) == MESSAGE_PERMITTED) {
|
2011-05-31 12:30:03 +01:00
|
|
|
$loggedin = $loggedinstatusvalues[$plugindefault & MESSAGE_DEFAULT_LOGGEDIN];
|
|
|
|
$loggedoff = $loggedinstatusvalues[$plugindefault & MESSAGE_DEFAULT_LOGGEDOFF];
|
2011-05-20 15:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return array($permitted, $loggedin, $loggedoff);
|
|
|
|
}
|
2011-06-13 16:54:52 +08:00
|
|
|
|
2011-06-10 13:52:53 +08:00
|
|
|
/**
|
|
|
|
* Return a list of page types
|
|
|
|
* @param string $pagetype current page type
|
|
|
|
* @param stdClass $parentcontext Block's parent context
|
|
|
|
* @param stdClass $currentcontext Current context of block
|
|
|
|
*/
|
2011-06-17 16:23:10 +08:00
|
|
|
function message_page_type_list($pagetype, $parentcontext, $currentcontext) {
|
2011-06-10 13:52:53 +08:00
|
|
|
return array('messages-*'=>get_string('page-message-x', 'message'));
|
|
|
|
}
|
2012-04-04 09:58:22 +07:00
|
|
|
|
2014-10-01 11:10:42 +02:00
|
|
|
/**
|
2014-10-06 10:44:30 +13:00
|
|
|
* Get messages sent or/and received by the specified users.
|
2016-01-07 12:10:32 +01:00
|
|
|
* Please note that this function return deleted messages too.
|
2014-10-01 11:10:42 +02:00
|
|
|
*
|
|
|
|
* @param int $useridto the user id who received the message
|
2014-10-06 10:44:30 +13:00
|
|
|
* @param int $useridfrom the user id who sent the message. -10 or -20 for no-reply or support user
|
2014-10-01 11:10:42 +02:00
|
|
|
* @param int $notifications 1 for retrieving notifications, 0 for messages, -1 for both
|
2014-10-06 10:44:30 +13:00
|
|
|
* @param bool $read true for retrieving read messages, false for unread
|
2014-10-01 11:10:42 +02:00
|
|
|
* @param string $sort the column name to order by including optionally direction
|
|
|
|
* @param int $limitfrom limit from
|
|
|
|
* @param int $limitnum limit num
|
|
|
|
* @return external_description
|
|
|
|
* @since 2.8
|
|
|
|
*/
|
|
|
|
function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $read = true,
|
|
|
|
$sort = 'mr.timecreated DESC', $limitfrom = 0, $limitnum = 0) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$messagetable = $read ? '{message_read}' : '{message}';
|
|
|
|
$params = array('deleted' => 0);
|
|
|
|
|
|
|
|
// Empty useridto means that we are going to retrieve messages send by the useridfrom to any user.
|
|
|
|
if (empty($useridto)) {
|
|
|
|
$userfields = get_all_user_name_fields(true, 'u', '', 'userto');
|
|
|
|
$joinsql = "JOIN {user} u ON u.id = mr.useridto";
|
|
|
|
$usersql = "mr.useridfrom = :useridfrom AND u.deleted = :deleted";
|
|
|
|
$params['useridfrom'] = $useridfrom;
|
|
|
|
} else {
|
|
|
|
$userfields = get_all_user_name_fields(true, 'u', '', 'userfrom');
|
|
|
|
// Left join because useridfrom may be -10 or -20 (no-reply and support users).
|
|
|
|
$joinsql = "LEFT JOIN {user} u ON u.id = mr.useridfrom";
|
|
|
|
$usersql = "mr.useridto = :useridto AND (u.deleted IS NULL OR u.deleted = :deleted)";
|
|
|
|
$params['useridto'] = $useridto;
|
|
|
|
if (!empty($useridfrom)) {
|
|
|
|
$usersql .= " AND mr.useridfrom = :useridfrom";
|
|
|
|
$params['useridfrom'] = $useridfrom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, if retrieve notifications, conversations or both.
|
|
|
|
$typesql = "";
|
|
|
|
if ($notifications !== -1) {
|
|
|
|
$typesql = "AND mr.notification = :notification";
|
|
|
|
$params['notification'] = ($notifications) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = "SELECT mr.*, $userfields
|
|
|
|
FROM $messagetable mr
|
2014-10-06 10:44:30 +13:00
|
|
|
$joinsql
|
|
|
|
WHERE $usersql
|
|
|
|
$typesql
|
2014-10-01 11:10:42 +02:00
|
|
|
ORDER BY $sort";
|
|
|
|
|
|
|
|
$messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
|
|
|
|
return $messages;
|
|
|
|
}
|
2015-01-15 10:53:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Requires the JS libraries to send a message using a dialog.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function message_messenger_requirejs() {
|
|
|
|
global $PAGE;
|
|
|
|
|
|
|
|
static $done = false;
|
|
|
|
if ($done) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$PAGE->requires->yui_module(
|
|
|
|
array('moodle-core_message-messenger'),
|
|
|
|
'Y.M.core_message.messenger.init',
|
|
|
|
array(array())
|
|
|
|
);
|
|
|
|
$PAGE->requires->strings_for_js(array(
|
|
|
|
'errorwhilesendingmessage',
|
|
|
|
'messagesent',
|
|
|
|
'messagetosend',
|
|
|
|
'sendingmessage',
|
|
|
|
'sendmessage',
|
|
|
|
'viewconversation',
|
|
|
|
), 'core_message');
|
2015-10-13 03:45:24 +00:00
|
|
|
$PAGE->requires->strings_for_js(array(
|
|
|
|
'userisblockingyou',
|
|
|
|
'userisblockingyounoncontact'
|
|
|
|
), 'message');
|
2015-01-15 10:53:09 +08:00
|
|
|
$PAGE->requires->string_for_js('error', 'core');
|
|
|
|
|
|
|
|
$done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the attributes to place on a link to open the 'Send message' dialog.
|
|
|
|
*
|
|
|
|
* @param object $user User object.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function message_messenger_sendmessage_link_params($user) {
|
2015-10-13 03:45:24 +00:00
|
|
|
$params = array(
|
2015-01-15 10:53:09 +08:00
|
|
|
'data-trigger' => 'core_message-messenger::sendmessage',
|
|
|
|
'data-fullname' => fullname($user),
|
|
|
|
'data-userid' => $user->id,
|
|
|
|
'role' => 'button'
|
|
|
|
);
|
2015-10-13 03:45:24 +00:00
|
|
|
|
|
|
|
if (message_is_user_non_contact_blocked($user)) {
|
|
|
|
$params['data-blocked-string'] = 'userisblockingyounoncontact';
|
|
|
|
} else if (message_is_user_blocked($user)) {
|
|
|
|
$params['data-blocked-string'] = 'userisblockingyou';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a user is permitted to send another user a private message.
|
|
|
|
* If no sender is provided then it defaults to the logged in user.
|
|
|
|
*
|
|
|
|
* @param object $recipient User object.
|
|
|
|
* @param object $sender User object.
|
|
|
|
* @return bool true if user is permitted, false otherwise.
|
|
|
|
*/
|
|
|
|
function message_can_post_message($recipient, $sender = null) {
|
|
|
|
global $USER, $DB;
|
|
|
|
|
|
|
|
if (is_null($sender)) {
|
|
|
|
// The message is from the logged in user, unless otherwise specified.
|
|
|
|
$sender = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_capability('moodle/site:sendmessage', context_system::instance(), $sender)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The recipient blocks messages from non-contacts and the
|
|
|
|
// sender isn't a contact.
|
|
|
|
if (message_is_user_non_contact_blocked($recipient, $sender)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The recipient has specifically blocked this sender.
|
|
|
|
if (message_is_user_blocked($recipient, $sender)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the recipient is allowing messages from users that aren't a
|
|
|
|
* contact. If not then it checks to make sure the sender is in the
|
|
|
|
* recipient's contacts.
|
|
|
|
*
|
|
|
|
* @param object $recipient User object.
|
|
|
|
* @param object $sender User object.
|
|
|
|
* @return bool true if $sender is blocked, false otherwise.
|
|
|
|
*/
|
|
|
|
function message_is_user_non_contact_blocked($recipient, $sender = null) {
|
|
|
|
global $USER, $DB;
|
|
|
|
|
|
|
|
if (is_null($sender)) {
|
|
|
|
// The message is from the logged in user, unless otherwise specified.
|
|
|
|
$sender = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
$blockednoncontacts = get_user_preferences('message_blocknoncontacts', '', $recipient->id);
|
|
|
|
if (!empty($blockednoncontacts)) {
|
|
|
|
// Confirm the sender is a contact of the recipient.
|
|
|
|
$exists = $DB->record_exists('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id));
|
|
|
|
if ($exists) {
|
|
|
|
// All good, the recipient is a contact of the sender.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Oh no, the recipient is not a contact. Looks like we can't send the message.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the recipient has specifically blocked the sending user.
|
|
|
|
*
|
|
|
|
* Note: This function will always return false if the sender has the
|
|
|
|
* readallmessages capability at the system context level.
|
|
|
|
*
|
|
|
|
* @param object $recipient User object.
|
|
|
|
* @param object $sender User object.
|
|
|
|
* @return bool true if $sender is blocked, false otherwise.
|
|
|
|
*/
|
|
|
|
function message_is_user_blocked($recipient, $sender = null) {
|
|
|
|
global $USER, $DB;
|
|
|
|
|
|
|
|
if (is_null($sender)) {
|
|
|
|
// The message is from the logged in user, unless otherwise specified.
|
|
|
|
$sender = $USER;
|
|
|
|
}
|
|
|
|
|
|
|
|
$systemcontext = context_system::instance();
|
|
|
|
if (has_capability('moodle/site:readallmessages', $systemcontext, $sender)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($contact = $DB->get_record('message_contacts', array('userid' => $recipient->id, 'contactid' => $sender->id))) {
|
|
|
|
if ($contact->blocked) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-01-15 10:53:09 +08:00
|
|
|
}
|