Skip to content

Commit

Permalink
Merge branch 'MDL-57370-master' of git://github.com/ryanwyllie/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Feb 7, 2017
2 parents 3587ee2 + b1e6f1a commit 125fa38
Show file tree
Hide file tree
Showing 12 changed files with 871 additions and 173 deletions.
8 changes: 5 additions & 3 deletions lib/db/install.xml
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="20161119" COMMENT="XMLDB file for core Moodle tables"
<XMLDB PATH="lib/db" VERSION="20170207" 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 @@ -545,8 +545,9 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="useridto" UNIQUE="false" FIELDS="useridto"/>
<INDEX NAME="useridfromtodeleted" UNIQUE="false" FIELDS="useridfrom, useridto, timeuserfromdeleted, timeusertodeleted"/>
<INDEX NAME="useridfrom_timeuserfromdeleted_notification" UNIQUE="false" FIELDS="useridfrom, timeuserfromdeleted, notification"/>
<INDEX NAME="useridto_timeusertodeleted_notification" UNIQUE="false" FIELDS="useridto, timeusertodeleted, notification"/>
</INDEXES>
</TABLE>
<TABLE NAME="message_read" COMMENT="Stores all messages that have been read">
Expand All @@ -573,9 +574,10 @@
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="useridto" UNIQUE="false" FIELDS="useridto"/>
<INDEX NAME="useridfromtodeleted" UNIQUE="false" FIELDS="useridfrom, useridto, timeuserfromdeleted, timeusertodeleted"/>
<INDEX NAME="notificationtimeread" UNIQUE="false" FIELDS="notification, timeread"/>
<INDEX NAME="useridfrom_timeuserfromdeleted_notification" UNIQUE="false" FIELDS="useridfrom, timeuserfromdeleted, notification"/>
<INDEX NAME="useridto_timeusertodeleted_notification" UNIQUE="false" FIELDS="useridto, timeusertodeleted, notification"/>
</INDEXES>
</TABLE>
<TABLE NAME="message_contacts" COMMENT="Maintains lists of relationships between users">
Expand Down
60 changes: 60 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2465,5 +2465,65 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2016122800.00);
}

if ($oldversion < 2017020200.01) {

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

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

// Define index useridto_timeusertodeleted_notification (not unique) to be added to message.
$index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));

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

$index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));

// Conditionally launch drop index useridto.
if ($dbman->index_exists($table, $index)) {
$dbman->drop_index($table, $index);
}

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

if ($oldversion < 2017020200.02) {

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

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

// Define index useridto_timeusertodeleted_notification (not unique) to be added to message_read.
$index = new xmldb_index('useridto_timeusertodeleted_notification', XMLDB_INDEX_NOTUNIQUE, array('useridto', 'timeusertodeleted', 'notification'));

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

$index = new xmldb_index('useridto', XMLDB_INDEX_NOTUNIQUE, array('useridto'));

// Conditionally launch drop index useridto.
if ($dbman->index_exists($table, $index)) {
$dbman->drop_index($table, $index);
}

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

return true;
}
141 changes: 141 additions & 0 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -6385,3 +6385,144 @@ function prevent_form_autofill_password() {
debugging('prevent_form_autofill_password has been deprecated and is no longer in use.', DEBUG_DEVELOPER);
return '';
}

/**
* 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
*
* @deprecated since Moodle 3.3 MDL-57370
* @param object|int $userorid the current user or user id
* @param int $limitfrom can be used for paging
* @param int $limitto can be used for paging
* @return array
*/
function message_get_recent_conversations($userorid, $limitfrom = 0, $limitto = 100) {
global $DB;

debugging('message_get_recent_conversations() is deprecated. Please use \core_message\api::get_conversations() instead.', DEBUG_DEVELOPER);

if (is_object($userorid)) {
$user = $userorid;
} else {
$userid = $userorid;
$user = new stdClass();
$user->id = $userid;
}

$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.
$uniquefield = $DB->sql_concat('message.useridfrom', "'-'", 'message.useridto');
$sql = "SELECT $uniquefield, $userfields,
message.id as mid, message.notification, message.useridfrom, message.useridto,
message.smallmessage, message.fullmessage, message.fullmessagehtml,
message.fullmessageformat, message.timecreated,
contact.id as contactlistid, contact.blocked
FROM {message_read} message
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 AND recentmessages.timeuserfromdeleted = 0) OR
(recentmessages.useridto = :userid2 AND recentmessages.timeusertodeleted = 0)
)
GROUP BY recentmessages.useridfrom, recentmessages.useridto
) recent ON matchedmessage.useridto = recent.useridto
AND matchedmessage.useridfrom = recent.useridfrom
AND matchedmessage.timecreated = recent.timecreated
WHERE (
(matchedmessage.useridfrom = :userid6 AND matchedmessage.timeuserfromdeleted = 0) OR
(matchedmessage.useridto = :userid7 AND matchedmessage.timeusertodeleted = 0)
)
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.contactid = 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,
'userid7' => $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
// 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);

$unreadcountssql = 'SELECT useridfrom, count(*) as count
FROM {message}
WHERE useridto = :userid
AND timeusertodeleted = 0
AND notification = 0
GROUP BY useridfrom';
$unreadcounts = $DB->get_records_sql($unreadcountssql, array('userid' => $user->id));

// 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.
$conversations = array();
$conversation_arrays = array($unread, $read);
foreach ($conversation_arrays as $conversation_array) {
foreach ($conversation_array as $conversation) {
// Only consider it unread if $user has unread messages.
if (isset($unreadcounts[$conversation->useridfrom])) {
$conversation->isread = 0;
$conversation->unreadcount = $unreadcounts[$conversation->useridfrom]->count;
} else {
$conversation->isread = 1;
}

if (!isset($conversations[$conversation->id])) {
$conversations[$conversation->id] = $conversation;
} else {
$current = $conversations[$conversation->id];
// We need to maintain the isread and unreadcount values from existing
// parts of the conversation if we're replacing it.
$conversation->isread = ($conversation->isread && $current->isread);
if (isset($current->unreadcount) && !isset($conversation->unreadcount)) {
$conversation->unreadcount = $current->unreadcount;
}

if ($current->timecreated < $conversation->timecreated) {
$conversations[$conversation->id] = $conversation;
} else if ($current->timecreated == $conversation->timecreated) {
if ($current->mid < $conversation->mid) {
$conversations[$conversation->id] = $conversation;
}
}
}
}
}

// 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
$result = core_collator::asort_objects_by_property($conversations, 'timecreated', core_collator::SORT_NUMERIC);
$conversations = array_reverse($conversations);

return $conversations;
}
Loading

0 comments on commit 125fa38

Please sign in to comment.