Merge branch 'MDL-45374' of git://github.com/jleyva/moodle
This commit is contained in:
@@ -2614,3 +2614,59 @@ function message_current_user_is_involved($user1, $user2) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages sent or/and received by the specified users.
|
||||
*
|
||||
* @param int $useridto the user id who received the message
|
||||
* @param int $useridfrom the user id who sent the message. -10 or -20 for no-reply or support user
|
||||
* @param int $notifications 1 for retrieving notifications, 0 for messages, -1 for both
|
||||
* @param bool $read true for retrieving read messages, false for unread
|
||||
* @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
|
||||
$joinsql
|
||||
WHERE $usersql
|
||||
$typesql
|
||||
ORDER BY $sort";
|
||||
|
||||
$messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
|
||||
return $messages;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user