mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 19:50:14 +01:00
MDL-65566 core_message: create the self-conversation when needed
Instead of creating all the self-conversations during the upgrading, some code has been added to the get_conversations and to the get_conversation_counts to create it whe it doesn't exist.
This commit is contained in:
parent
b742fe1403
commit
18e5f9bef8
@ -3135,59 +3135,6 @@ function xmldb_main_upgrade($oldversion) {
|
||||
}
|
||||
$legacyselfmessagesrs->close();
|
||||
|
||||
// STEP 3. For existing users without self-conversations, create and star it.
|
||||
|
||||
// Get all the users without a self-conversation.
|
||||
$sql = "SELECT u.id
|
||||
FROM {user} u
|
||||
WHERE u.deleted = 0 AND u.id NOT IN (SELECT mcm.userid
|
||||
FROM {message_conversation_members} mcm
|
||||
INNER JOIN {message_conversations} mc
|
||||
ON mc.id = mcm.conversationid AND mc.type = ?
|
||||
)";
|
||||
$useridsrs = $DB->get_recordset_sql($sql, [\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF]);
|
||||
// Create the self-conversation for all these users.
|
||||
foreach ($useridsrs as $user) {
|
||||
$conditions = [
|
||||
'type' => \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
'convhash' => \core_message\helper::get_conversation_hash([$user->id])
|
||||
];
|
||||
$selfconversation = $DB->get_record('message_conversations', $conditions);
|
||||
if (empty($selfconversation)) {
|
||||
// Create the self-conversation.
|
||||
$selfconversation = new \stdClass();
|
||||
$selfconversation->type = \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF;
|
||||
$selfconversation->convhash = \core_message\helper::get_conversation_hash([$user->id]);
|
||||
$selfconversation->enabled = 1;
|
||||
$selfconversation->timecreated = time();
|
||||
$selfconversation->timemodified = $selfconversation->timecreated;
|
||||
|
||||
$selfconversation->id = $DB->insert_record('message_conversations', $selfconversation);
|
||||
|
||||
// Add user to this self-conversation.
|
||||
$member = new \stdClass();
|
||||
$member->conversationid = $selfconversation->id;
|
||||
$member->userid = $user->id;
|
||||
$member->timecreated = time();
|
||||
|
||||
$member->id = $DB->insert_record('message_conversation_members', $member);
|
||||
|
||||
// Star the self-conversation.
|
||||
$favouriterecord = new \stdClass();
|
||||
$favouriterecord->component = 'core_message';
|
||||
$favouriterecord->itemtype = 'message_conversations';
|
||||
$favouriterecord->itemid = $selfconversation->id;
|
||||
$userctx = \context_user::instance($user->id);
|
||||
$favouriterecord->contextid = $userctx->id;
|
||||
$favouriterecord->userid = $user->id;
|
||||
$favouriterecord->timecreated = time();
|
||||
$favouriterecord->timemodified = $favouriterecord->timecreated;
|
||||
|
||||
$DB->insert_record('favourite', $favouriterecord);
|
||||
}
|
||||
}
|
||||
$useridsrs->close();
|
||||
|
||||
// Main savepoint reached.
|
||||
upgrade_main_savepoint(true, 2019041800.01);
|
||||
}
|
||||
|
@ -528,6 +528,8 @@ class api {
|
||||
throw new \moodle_exception("Invalid value ($type) for type param, please see api constants.");
|
||||
}
|
||||
|
||||
self::lazy_create_self_conversation($userid);
|
||||
|
||||
// We need to know which conversations are favourites, so we can either:
|
||||
// 1) Include the 'isfavourite' attribute on conversations (when $favourite = null and we're including all conversations)
|
||||
// 2) Restrict the results to ONLY those conversations which are favourites (when $favourite = true)
|
||||
@ -1586,6 +1588,7 @@ class api {
|
||||
*/
|
||||
public static function get_conversation_counts(int $userid) : array {
|
||||
global $DB;
|
||||
self::lazy_create_self_conversation($userid);
|
||||
|
||||
// Some restrictions we need to be aware of:
|
||||
// - Individual conversations containing soft-deleted user must be counted.
|
||||
@ -3396,4 +3399,25 @@ class api {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a self conversation for a user, only if one doesn't already exist.
|
||||
*
|
||||
* @param int $userid the user to whom the conversation belongs.
|
||||
*/
|
||||
protected static function lazy_create_self_conversation(int $userid) : void {
|
||||
global $DB;
|
||||
// Check if the self-conversation for this user exists.
|
||||
// If not, create and star it for the user.
|
||||
// Don't use the API methods here, as they in turn may rely on
|
||||
// lazy creation and we'll end up with recursive loops of doom.
|
||||
$conditions = [
|
||||
'type' => self::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
'convhash' => helper::get_conversation_hash([$userid])
|
||||
];
|
||||
if (empty($DB->get_record('message_conversations', $conditions))) {
|
||||
$selfconversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_SELF, [$userid]);
|
||||
self::set_favourite_conversation($selfconversation->id, $userid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user