mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
Merge branch 'MDL-45594_master_alt' of git://github.com/markn86/moodle
This commit is contained in:
commit
fe43ab9b75
2 changed files with 125 additions and 60 deletions
100
message/lib.php
100
message/lib.php
|
@ -704,70 +704,56 @@ function message_print_search($advancedsearch = false, $user1=null) {
|
||||||
function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) {
|
function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
|
||||||
$userfields = user_picture::fields('u', array('lastaccess'));
|
$userfields = user_picture::fields('otheruser', array('lastaccess'));
|
||||||
//This query retrieves the last message received from and sent to each user
|
|
||||||
//It unions that data then, within that set, it finds the most recent message you've exchanged with each user over all
|
|
||||||
//It then joins with some other tables to get some additional data we need
|
|
||||||
|
|
||||||
//message ID is used instead of timecreated as it should sort the same and will be much faster
|
// 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.
|
||||||
|
$sql = "SELECT $userfields,
|
||||||
|
message.id as mid, message.notification, message.smallmessage, message.fullmessage,
|
||||||
|
message.fullmessagehtml, message.fullmessageformat, message.timecreated,
|
||||||
|
contact.id as contactlistid, contact.blocked
|
||||||
|
|
||||||
//There is a separate query for read and unread queries as they are stored in different tables
|
FROM {message_read} message
|
||||||
//They were originally retrieved in one query but it was so large that it was difficult to be confident in its correctness
|
JOIN {user} otheruser ON otheruser.id = CASE
|
||||||
$sql = "SELECT $userfields, mr.id as mid, mr.notification, mr.smallmessage, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat, mr.timecreated, mc.id as contactlistid, mc.blocked
|
WHEN message.useridto = :userid1 THEN message.useridfrom
|
||||||
FROM {message_read} mr
|
ELSE message.useridto END
|
||||||
JOIN (
|
LEFT JOIN {message_contacts} contact ON contact.userid = :userid2 AND contact.contactid = otheruser.id
|
||||||
SELECT messages.userid AS userid, MAX(messages.mid) AS mid
|
|
||||||
FROM (
|
WHERE otheruser.deleted = 0
|
||||||
SELECT mr1.useridto AS userid, MAX(mr1.id) AS mid
|
AND (message.useridto = :userid3 OR message.useridfrom = :userid4)
|
||||||
FROM {message_read} mr1
|
AND message.notification = 0
|
||||||
WHERE mr1.useridfrom = :userid1
|
AND NOT EXISTS (
|
||||||
AND mr1.notification = 0
|
SELECT 1
|
||||||
GROUP BY mr1.useridto
|
FROM {message_read} othermessage
|
||||||
UNION
|
WHERE ((othermessage.useridto = :userid5 AND othermessage.useridfrom = otheruser.id) OR
|
||||||
SELECT mr2.useridfrom AS userid, MAX(mr2.id) AS mid
|
(othermessage.useridfrom = :userid6 AND othermessage.useridto = otheruser.id))
|
||||||
FROM {message_read} mr2
|
AND (othermessage.timecreated > message.timecreated OR (
|
||||||
WHERE mr2.useridto = :userid2
|
othermessage.timecreated = message.timecreated AND othermessage.id > message.id))
|
||||||
AND mr2.notification = 0
|
)
|
||||||
GROUP BY mr2.useridfrom
|
|
||||||
) messages
|
ORDER BY message.timecreated DESC";
|
||||||
GROUP BY messages.userid
|
$params = array('userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id,
|
||||||
) messages2 ON mr.id = messages2.mid AND (mr.useridto = messages2.userid OR mr.useridfrom = messages2.userid)
|
'userid4' => $user->id, 'userid5' => $user->id, 'userid6' => $user->id);
|
||||||
JOIN {user} u ON u.id = messages2.userid
|
|
||||||
LEFT JOIN {message_contacts} mc ON mc.userid = :userid3 AND mc.contactid = u.id
|
|
||||||
WHERE u.deleted = '0'
|
|
||||||
ORDER BY mr.id DESC";
|
|
||||||
$params = array('userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id);
|
|
||||||
$read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
$read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
||||||
|
|
||||||
$sql = "SELECT $userfields, m.id as mid, m.notification, m.smallmessage, m.fullmessage, m.fullmessagehtml, m.fullmessageformat, m.timecreated, mc.id as contactlistid, mc.blocked
|
// We want to get the messages that have not been read. These are stored in the 'message' table. It is the
|
||||||
FROM {message} m
|
// exact same query as the one above, except for the table we are querying. So, simply replace references to
|
||||||
JOIN (
|
// the 'message_read' table with the 'message' table.
|
||||||
SELECT messages.userid AS userid, MAX(messages.mid) AS mid
|
$sql = str_replace('{message_read}', '{message}', $sql);
|
||||||
FROM (
|
|
||||||
SELECT m1.useridto AS userid, MAX(m1.id) AS mid
|
|
||||||
FROM {message} m1
|
|
||||||
WHERE m1.useridfrom = :userid1
|
|
||||||
AND m1.notification = 0
|
|
||||||
GROUP BY m1.useridto
|
|
||||||
UNION
|
|
||||||
SELECT m2.useridfrom AS userid, MAX(m2.id) AS mid
|
|
||||||
FROM {message} m2
|
|
||||||
WHERE m2.useridto = :userid2
|
|
||||||
AND m2.notification = 0
|
|
||||||
GROUP BY m2.useridfrom
|
|
||||||
) messages
|
|
||||||
GROUP BY messages.userid
|
|
||||||
) messages2 ON m.id = messages2.mid AND (m.useridto = messages2.userid OR m.useridfrom = messages2.userid)
|
|
||||||
JOIN {user} u ON u.id = messages2.userid
|
|
||||||
LEFT JOIN {message_contacts} mc ON mc.userid = :userid3 AND mc.contactid = u.id
|
|
||||||
WHERE u.deleted = '0'
|
|
||||||
ORDER BY m.id DESC";
|
|
||||||
$unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
$unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
||||||
|
|
||||||
$conversations = array();
|
$conversations = array();
|
||||||
|
|
||||||
//Union the 2 result sets together looking for the message with the most recent timecreated for each other user
|
// Union the 2 result sets together looking for the message with the most
|
||||||
//$conversation->id (the array key) is the other user's ID
|
// recent timecreated for each other user.
|
||||||
|
// $conversation->id (the array key) is the other user's ID.
|
||||||
$conversation_arrays = array($unread, $read);
|
$conversation_arrays = array($unread, $read);
|
||||||
foreach ($conversation_arrays as $conversation_array) {
|
foreach ($conversation_arrays as $conversation_array) {
|
||||||
foreach ($conversation_array as $conversation) {
|
foreach ($conversation_array as $conversation) {
|
||||||
|
@ -802,7 +788,7 @@ function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) {
|
||||||
FROM {message_read} mr
|
FROM {message_read} mr
|
||||||
JOIN {user} u ON u.id=mr.useridfrom
|
JOIN {user} u ON u.id=mr.useridfrom
|
||||||
WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification
|
WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification
|
||||||
ORDER BY mr.id DESC";//ordering by id should give the same result as ordering by timecreated but will be faster
|
ORDER BY mr.timecreated DESC";
|
||||||
$params = array('userid1' => $user->id, 'notification' => 1);
|
$params = array('userid1' => $user->id, 'notification' => 1);
|
||||||
|
|
||||||
$notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
$notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
|
||||||
|
|
|
@ -62,6 +62,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
|
||||||
* @param stdClass $userfrom user object of the one sending the message.
|
* @param stdClass $userfrom user object of the one sending the message.
|
||||||
* @param stdClass $userto user object of the one receiving the message.
|
* @param stdClass $userto user object of the one receiving the message.
|
||||||
* @param string $message message to send.
|
* @param string $message message to send.
|
||||||
|
* @return int the id of the message
|
||||||
*/
|
*/
|
||||||
protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
|
protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
@ -72,7 +73,8 @@ class core_message_messagelib_testcase extends advanced_testcase {
|
||||||
$record->subject = 'No subject';
|
$record->subject = 'No subject';
|
||||||
$record->fullmessage = $message;
|
$record->fullmessage = $message;
|
||||||
$record->timecreated = time();
|
$record->timecreated = time();
|
||||||
$insert = $DB->insert_record('message', $record);
|
|
||||||
|
return $DB->insert_record('message', $record);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -358,4 +360,81 @@ class core_message_messagelib_testcase extends advanced_testcase {
|
||||||
$this->assertEquals(false, message_search(array('Message'), true, true, 2));
|
$this->assertEquals(false, message_search(array('Message'), true, true, 2));
|
||||||
$this->assertCount(5, message_search(array('Message'), true, true, SITEID));
|
$this->assertCount(5, message_search(array('Message'), true, true, SITEID));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test message_get_recent_conversations.
|
||||||
|
*/
|
||||||
|
public function test_message_get_recent_conversations() {
|
||||||
|
global $DB, $USER;
|
||||||
|
|
||||||
|
// Set this user as the admin.
|
||||||
|
$this->setAdminUser();
|
||||||
|
|
||||||
|
// Create user's to send messages to/from.
|
||||||
|
$user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
|
||||||
|
$user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2'));
|
||||||
|
|
||||||
|
// Add a few messages that have been read and some that are unread.
|
||||||
|
$m1 = $this->send_fake_message($USER, $user1, 'Message 1'); // An unread message.
|
||||||
|
$m2 = $this->send_fake_message($user1, $USER, 'Message 2'); // An unread message.
|
||||||
|
$m3 = $this->send_fake_message($USER, $user1, 'Message 3'); // An unread message.
|
||||||
|
$m4 = message_post_message($USER, $user2, 'Message 4', FORMAT_PLAIN);
|
||||||
|
$m5 = message_post_message($user2, $USER, 'Message 5', FORMAT_PLAIN);
|
||||||
|
$m6 = message_post_message($USER, $user2, 'Message 6', FORMAT_PLAIN);
|
||||||
|
|
||||||
|
// We want to alter the timecreated values so we can ensure message_get_recent_conversations orders by timecreated.
|
||||||
|
$updatemessage = new stdClass();
|
||||||
|
$updatemessage->id = $m3;
|
||||||
|
$updatemessage->timecreated = 0;
|
||||||
|
$DB->update_record('message', $updatemessage);
|
||||||
|
|
||||||
|
$updatemessage->id = $m6;
|
||||||
|
$DB->update_record('message_read', $updatemessage);
|
||||||
|
|
||||||
|
// Get the recent conversations for the current user.
|
||||||
|
$conversations = message_get_recent_conversations($USER);
|
||||||
|
|
||||||
|
// Confirm that we have received the messages with the maximum timecreated, rather than the max id.
|
||||||
|
$this->assertEquals('Message 2', $conversations[0]->fullmessage);
|
||||||
|
$this->assertEquals('Message 5', $conversations[1]->smallmessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test message_get_recent_notifications.
|
||||||
|
*/
|
||||||
|
public function test_message_get_recent_notifications() {
|
||||||
|
global $DB, $USER;
|
||||||
|
|
||||||
|
// Set this user as the admin.
|
||||||
|
$this->setAdminUser();
|
||||||
|
|
||||||
|
// Create a user to send messages from.
|
||||||
|
$user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1'));
|
||||||
|
|
||||||
|
// Add two messages - will mark them as notifications later.
|
||||||
|
$m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN);
|
||||||
|
$m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN);
|
||||||
|
|
||||||
|
// Mark the second message as a notification.
|
||||||
|
$updatemessage = new stdClass();
|
||||||
|
$updatemessage->id = $m2;
|
||||||
|
$updatemessage->notification = 1;
|
||||||
|
$DB->update_record('message_read', $updatemessage);
|
||||||
|
|
||||||
|
// Mark the first message as a notification and change the timecreated to 0.
|
||||||
|
$updatemessage->id = $m1;
|
||||||
|
$updatemessage->notification = 1;
|
||||||
|
$updatemessage->timecreated = 0;
|
||||||
|
$DB->update_record('message_read', $updatemessage);
|
||||||
|
|
||||||
|
$notifications = message_get_recent_notifications($USER);
|
||||||
|
|
||||||
|
// Get the messages.
|
||||||
|
$firstmessage = array_shift($notifications);
|
||||||
|
$secondmessage = array_shift($notifications);
|
||||||
|
|
||||||
|
// Confirm that we have received the notifications with the maximum timecreated, rather than the max id.
|
||||||
|
$this->assertEquals('Message 2', $firstmessage->smallmessage);
|
||||||
|
$this->assertEquals('Message 1', $secondmessage->smallmessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue