mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00
MDL-63691 core_message: added api::create_conversation
This commit is contained in:
parent
e66b915c40
commit
f2ac0a3e5b
7 changed files with 186 additions and 16 deletions
|
@ -61,6 +61,16 @@ class api {
|
|||
*/
|
||||
const MESSAGE_PRIVACY_SITE = 2;
|
||||
|
||||
/**
|
||||
* An individual conversation.
|
||||
*/
|
||||
const MESSAGE_CONVERSATION_TYPE_INDIVIDUAL = 1;
|
||||
|
||||
/**
|
||||
* A group conversation.
|
||||
*/
|
||||
const MESSAGE_CONVERSATION_TYPE_GROUP = 2;
|
||||
|
||||
/**
|
||||
* Handles searching for messages in the message area.
|
||||
*
|
||||
|
@ -1348,7 +1358,11 @@ class api {
|
|||
|
||||
$hash = helper::get_conversation_hash($userids);
|
||||
|
||||
if ($conversation = $DB->get_record('message_conversations', ['convhash' => $hash])) {
|
||||
$params = [
|
||||
'type' => self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
'convhash' => $hash
|
||||
];
|
||||
if ($conversation = $DB->get_record('message_conversations', $params)) {
|
||||
return $conversation->id;
|
||||
}
|
||||
|
||||
|
@ -1366,23 +1380,55 @@ class api {
|
|||
debugging('\core_message\api::create_conversation_between_users is deprecated, please use ' .
|
||||
'\core_message\api::create_conversation instead.', DEBUG_DEVELOPER);
|
||||
|
||||
// This method was always used for individual conversations.
|
||||
$conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $userids);
|
||||
|
||||
return $conversation->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a conversation with selected users and messages.
|
||||
*
|
||||
* @param int $type The type of conversation
|
||||
* @param int[] $userids The array of users to add to the conversation
|
||||
* @param string $name The name of the conversation
|
||||
* @return \stdClass
|
||||
*/
|
||||
public static function create_conversation(int $type, array $userids, string $name = null) {
|
||||
global $DB;
|
||||
|
||||
// Sanity check.
|
||||
if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
|
||||
if (count($userids) > 2) {
|
||||
throw new \moodle_exception('An individual conversation can not have more than two users.');
|
||||
}
|
||||
}
|
||||
|
||||
$conversation = new \stdClass();
|
||||
$conversation->convhash = helper::get_conversation_hash($userids);
|
||||
$conversation->type = $type;
|
||||
$conversation->name = $name;
|
||||
$conversation->convhash = null;
|
||||
if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
|
||||
$conversation->convhash = helper::get_conversation_hash($userids);
|
||||
}
|
||||
$conversation->timecreated = time();
|
||||
$conversation->id = $DB->insert_record('message_conversations', $conversation);
|
||||
|
||||
// Add members to this conversation.
|
||||
// Add users to this conversation.
|
||||
$arrmembers = [];
|
||||
foreach ($userids as $userid) {
|
||||
$member = new \stdClass();
|
||||
$member->conversationid = $conversation->id;
|
||||
$member->userid = $userid;
|
||||
$member->timecreated = time();
|
||||
$DB->insert_record('message_conversation_members', $member);
|
||||
$member->id = $DB->insert_record('message_conversation_members', $member);
|
||||
|
||||
$arrmembers[] = $member;
|
||||
}
|
||||
|
||||
return $conversation->id;
|
||||
$conversation->members = $arrmembers;
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -123,7 +123,14 @@ class migrate_message_data extends \core\task\adhoc_task {
|
|||
global $DB;
|
||||
|
||||
if (!$conversationid = \core_message\api::get_conversation_between_users([$userid, $otheruserid])) {
|
||||
$conversationid = \core_message\api::create_conversation_between_users([$userid, $otheruserid]);
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
[
|
||||
$userid,
|
||||
$otheruserid
|
||||
]
|
||||
);
|
||||
$conversationid = $conversation->id;
|
||||
}
|
||||
|
||||
// First, get the rows from the 'message' table.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue