From 6b036d04cf7fdfd996e22fade02f4e269d27e70e Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Mon, 19 Nov 2018 19:21:38 +0100 Subject: [PATCH] MDL-63692 core_message: Fix the context for favourite conversations The converation favourites were previously set in the system context which is not right, as they should be stored: - In the conversation context when defined. - In the user context, when contextid is null (that means is an individual conversation). --- lib/db/upgrade.php | 20 ++++++++++++++++++++ message/classes/api.php | 34 ++++++++++++++++++++++++++++++---- version.php | 2 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 350bf04a61b..4c31ff5c1b7 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -2784,5 +2784,25 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2018111300.01); } + if ($oldversion < 2018111300.02) { + // Update favourited conversations, so they are saved in the proper context instead of the system. + $sql = "SELECT f.*, mc.contextid as conversationctx + FROM {favourite} f + JOIN {message_conversations} mc + ON mc.id = f.itemid"; + $favouritedconversations = $DB->get_records_sql($sql); + foreach ($favouritedconversations as $fc) { + if (empty($fc->conversationctx)) { + $conversationidctx = \context_user::instance($fc->userid)->id; + } else { + $conversationidctx = $fc->conversationctx; + } + + $DB->set_field('favourite', 'contextid', $conversationidctx, ['id' => $fc->id]); + } + + upgrade_main_savepoint(true, 2018111300.02); + } + return true; } diff --git a/message/classes/api.php b/message/classes/api.php index 291e0127e3b..b26e3c9fccd 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -784,11 +784,24 @@ class api { * @throws \moodle_exception if the user or conversation don't exist. */ public static function set_favourite_conversation(int $conversationid, int $userid) : favourite { + global $DB; + if (!self::is_user_in_conversation($userid, $conversationid)) { throw new \moodle_exception("Conversation doesn't exist or user is not a member"); } - $ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($userid)); - return $ufservice->create_favourite('core_message', 'message_conversations', $conversationid, \context_system::instance()); + // Get the context for this conversation. + $conversation = $DB->get_record('message_conversations', ['id' => $conversationid]); + $userctx = \context_user::instance($userid); + if (empty($conversation->contextid)) { + // When the conversation hasn't any contextid value defined, the favourite will be added to the user context. + $conversationctx = $userctx; + } else { + // If the contextid is defined, the favourite will be added there. + $conversationctx = \context::instance_by_id($conversation->contextid); + } + + $ufservice = \core_favourites\service_factory::get_service_for_user_context($userctx); + return $ufservice->create_favourite('core_message', 'message_conversations', $conversationid, $conversationctx); } /** @@ -799,8 +812,21 @@ class api { * @throws \moodle_exception if the favourite does not exist for the user. */ public static function unset_favourite_conversation(int $conversationid, int $userid) { - $ufservice = \core_favourites\service_factory::get_service_for_user_context(\context_user::instance($userid)); - $ufservice->delete_favourite('core_message', 'message_conversations', $conversationid, \context_system::instance()); + global $DB; + + // Get the context for this conversation. + $conversation = $DB->get_records('message_conversations', ['id' => $conversationid]); + $userctx = \context_user::instance($userid); + if (empty($conversation->contextid)) { + // When the conversation hasn't any contextid value defined, the favourite will be added to the user context. + $conversationctx = $userctx; + } else { + // If the contextid is defined, the favourite will be added there. + $conversationctx = \context::instance_by_id($conversation->contextid); + } + + $ufservice = \core_favourites\service_factory::get_service_for_user_context($userctx); + $ufservice->delete_favourite('core_message', 'message_conversations', $conversationid, $conversationctx); } /** diff --git a/version.php b/version.php index d60bf4f7f7b..64ab2a6f3b3 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2018111300.01; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2018111300.02; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.