Skip to content

Commit

Permalink
Merge branch 'MDL-48108-master' of git://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	version.php
  • Loading branch information
danpoltawski committed Feb 24, 2015
2 parents e09bd10 + 8b52863 commit e6c453b
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 85 deletions.
4 changes: 3 additions & 1 deletion lib/db/install.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20150211" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20150224" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -541,6 +541,7 @@
<INDEXES>
<INDEX NAME="useridfrom" UNIQUE="false" FIELDS="useridfrom"/>
<INDEX NAME="useridto" UNIQUE="false" FIELDS="useridto"/>
<INDEX NAME="useridfromto" UNIQUE="false" FIELDS="useridfrom, useridto"/>
</INDEXES>
</TABLE>
<TABLE NAME="message_read" COMMENT="Stores all messages that have been read">
Expand All @@ -565,6 +566,7 @@
<INDEXES>
<INDEX NAME="useridfrom" UNIQUE="false" FIELDS="useridfrom"/>
<INDEX NAME="useridto" UNIQUE="false" FIELDS="useridto"/>
<INDEX NAME="useridfromto" UNIQUE="false" FIELDS="useridfrom, useridto"/>
</INDEXES>
</TABLE>
<TABLE NAME="message_contacts" COMMENT="Maintains lists of relationships between users">
Expand Down
24 changes: 24 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4177,5 +4177,29 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2015021100.00);
}

if ($oldversion < 2015022401.00) {

// Define index useridfromto (not unique) to be added to message.
$table = new xmldb_table('message');
$index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));

// Conditionally launch add index useridfromto.
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}

// Define index useridfromto (not unique) to be added to message_read.
$table = new xmldb_table('message_read');
$index = new xmldb_index('useridfromto', XMLDB_INDEX_NOTUNIQUE, array('useridfrom', 'useridto'));

// Conditionally launch add index useridfromto.
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2015022401.00);
}

return true;
}
64 changes: 40 additions & 24 deletions message/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,32 +708,41 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) {
// 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,
$uniquefield = $DB->sql_concat_join("'-'", array('message.useridfrom', 'message.useridto'));
$sql = "SELECT $uniquefield, $userfields,
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
JOIN {user} otheruser ON otheruser.id = CASE
WHEN message.useridto = :userid1 THEN message.useridfrom
ELSE message.useridto END
LEFT JOIN {message_contacts} contact ON contact.userid = :userid2 AND contact.contactid = otheruser.id
WHERE otheruser.deleted = 0
AND (message.useridto = :userid3 OR message.useridfrom = :userid4)
AND message.notification = 0
AND NOT EXISTS (
SELECT 1
FROM {message_read} othermessage
WHERE ((othermessage.useridto = :userid5 AND othermessage.useridfrom = otheruser.id) OR
(othermessage.useridfrom = :userid6 AND othermessage.useridto = otheruser.id))
AND (othermessage.timecreated > message.timecreated OR (
othermessage.timecreated = message.timecreated AND othermessage.id > message.id))
)
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
WHERE (recentmessages.useridfrom = :userid1 OR recentmessages.useridto = :userid2)
GROUP BY recentmessages.useridfrom, recentmessages.useridto
) recent ON matchedmessage.useridto = recent.useridto
AND matchedmessage.useridfrom = recent.useridfrom
AND matchedmessage.timecreated = recent.timecreated
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)
LEFT JOIN {message_contacts} contact ON contact.userid = :userid3 AND contact.userid = otheruser.id
WHERE otheruser.deleted = 0 AND message.notification = 0
ORDER BY message.timecreated DESC";
$params = array('userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id,
'userid4' => $user->id, 'userid5' => $user->id, 'userid6' => $user->id);
$params = array(
'userid1' => $user->id,
'userid2' => $user->id,
'userid3' => $user->id,
'userid4' => $user->id,
'userid5' => $user->id,
);
$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
Expand All @@ -742,16 +751,23 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) {
$sql = str_replace('{message_read}', '{message}', $sql);
$unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);

$conversations = array();

// 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.
$conversation_arrays = array($unread, $read);
foreach ($conversation_arrays as $conversation_array) {
foreach ($conversation_array as $conversation) {
if (empty($conversations[$conversation->id]) || $conversations[$conversation->id]->timecreated < $conversation->timecreated ) {
if (!isset($conversations[$conversation->id])) {
$conversations[$conversation->id] = $conversation;
} 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;
}
}
}
}
}
Expand Down
Loading

0 comments on commit e6c453b

Please sign in to comment.