MDL-63915 core_message: 'message' link on profile opens drawer

This commit is contained in:
Mark Nelson 2019-02-13 19:02:16 +08:00
parent fd998fc6f0
commit 6f9d34bd46
4 changed files with 112 additions and 1 deletions

View File

@ -4261,7 +4261,7 @@ EOD;
'title' => get_string('message', 'message'),
'url' => new moodle_url('/message/index.php', array('id' => $user->id)),
'image' => 'message',
'linkattributes' => array('role' => 'button'),
'linkattributes' => \core_message\helper::messageuser_link_params($user->id),
'page' => $this->page
),
'togglecontact' => array(
@ -4346,6 +4346,9 @@ EOD;
if ($button['buttontype'] === 'togglecontact') {
\core_message\helper::togglecontact_requirejs();
}
if ($button['buttontype'] === 'message') {
\core_message\helper::messageuser_requirejs();
}
$image = $this->pix_icon($button['formattedimage'], $button['title'], 'moodle', array(
'class' => 'iconsmall',
'role' => 'presentation'

View File

@ -0,0 +1 @@
define(["jquery","core/custom_interaction_events","core_message/message_drawer_helper"],function(a,b,c){var d=function(a){return parseInt(a.attr("data-userid"))},e=function(a){return parseInt(a.attr("data-conversationid"))},f=function(f){f=a(f),b.define(f,[b.events.activate]),f.on(b.events.activate,function(a,b){var g=e(f);g?c.showConversation(g):c.createConversationWithUser(d(f)),a.preventDefault(),b.originalEvent.preventDefault()})};return{send:f}});

View File

@ -0,0 +1,73 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Module to message a user from their profile page.
*
* @module core_message/message_user_button
* @copyright 2019 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['jquery', 'core/custom_interaction_events', 'core_message/message_drawer_helper'],
function($, CustomEvents, MessageDrawerHelper) {
/**
* Get the id for the user being messaged.
*
* @param {object} element jQuery object for the button
* @return {int}
*/
var getUserId = function(element) {
return parseInt(element.attr('data-userid'));
};
/**
* Returns the conversation id, 0 if none.
*
* @param {object} element jQuery object for the button
* @return {int}
*/
var getConversationId = function(element) {
return parseInt(element.attr('data-conversationid'));
};
/**
* Handles opening the messaging drawer to send a
* message to a given user.
*
* @method enhance
* @param {object} element jQuery object for the button
*/
var send = function(element) {
element = $(element);
CustomEvents.define(element, [CustomEvents.events.activate]);
element.on(CustomEvents.events.activate, function(e, data) {
var conversationid = getConversationId(element);
if (conversationid) {
MessageDrawerHelper.showConversation(conversationid);
} else {
MessageDrawerHelper.createConversationWithUser(getUserId(element));
}
e.preventDefault();
data.originalEvent.preventDefault();
});
};
return /** @alias module:core_message/message_user_button */ {
send: send
};
});

View File

@ -435,6 +435,40 @@ class helper {
return $params;
}
/**
* Requires the JS libraries for the message user button.
*
* @return void
*/
public static function messageuser_requirejs() {
global $PAGE;
static $done = false;
if ($done) {
return;
}
$PAGE->requires->js_call_amd('core_message/message_user_button', 'send', array('#message-user-button'));
$done = true;
}
/**
* Returns the attributes to place on the message user button.
*
* @param int $useridto
* @return array
*/
public static function messageuser_link_params(int $useridto) : array {
global $USER;
return [
'id' => 'message-user-button',
'role' => 'button',
'data-conversationid' => api::get_conversation_between_users([$USER->id, $useridto]),
'data-userid' => $useridto,
];
}
/**
* Returns the conversation hash between users for easy look-ups in the DB.
*