From 6f9d34bd46ae546d00c0193faddc31720ca913b2 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 13 Feb 2019 19:02:16 +0800 Subject: [PATCH] MDL-63915 core_message: 'message' link on profile opens drawer --- lib/outputrenderers.php | 5 +- message/amd/build/message_user_button.min.js | 1 + message/amd/src/message_user_button.js | 73 ++++++++++++++++++++ message/classes/helper.php | 34 +++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 message/amd/build/message_user_button.min.js create mode 100644 message/amd/src/message_user_button.js diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 40a726f7780..f3e52752f7a 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -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' diff --git a/message/amd/build/message_user_button.min.js b/message/amd/build/message_user_button.min.js new file mode 100644 index 00000000000..d422b1457f0 --- /dev/null +++ b/message/amd/build/message_user_button.min.js @@ -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}}); \ No newline at end of file diff --git a/message/amd/src/message_user_button.js b/message/amd/src/message_user_button.js new file mode 100644 index 00000000000..bedfe91c2c2 --- /dev/null +++ b/message/amd/src/message_user_button.js @@ -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 . + +/** + * Module to message a user from their profile page. + * + * @module core_message/message_user_button + * @copyright 2019 Mark Nelson + * @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 + }; + }); diff --git a/message/classes/helper.php b/message/classes/helper.php index acaefab5bac..9dbb3a94a64 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -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. *