diff --git a/lib/db/services.php b/lib/db/services.php index 951b90a16b2..ea1ca90c14c 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -660,6 +660,14 @@ $functions = array( 'type' => 'read', 'ajax' => true, ), + 'core_message_data_for_messagearea_get_most_recent_message' => array( + 'classname' => 'core_message_external', + 'methodname' => 'data_for_messagearea_get_most_recent_message', + 'classpath' => 'message/externallib.php', + 'description' => 'Retrieve the template data for the most recent message', + 'type' => 'read', + 'ajax' => true, + ), 'core_message_get_contacts' => array( 'classname' => 'core_message_external', 'methodname' => 'get_contacts', @@ -699,6 +707,7 @@ $functions = array( 'description' => 'Send instant messages', 'type' => 'write', 'capabilities' => 'moodle/site:sendmessage', + 'ajax' => true, 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), 'core_message_unblock_contacts' => array( diff --git a/message/amd/src/message-area.js b/message/amd/src/message-area.js index 33ad986a230..d1c5c914365 100644 --- a/message/amd/src/message-area.js +++ b/message/amd/src/message-area.js @@ -29,6 +29,8 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification'], function( this._init(); } + Messagearea.prototype.maxstringlength = 60; + Messagearea.prototype.find = function(selector) { return this._node.find(selector); }; @@ -37,6 +39,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification'], function( this._node.on('click', '.tabconversations', this._loadConversations.bind(this)); this._node.on('click', '.tabcontacts', this._loadContacts.bind(this)); this._node.on('click', '.contact-msg', this._loadMessages.bind(this)); + this._node.on('click', '.sendmessagebtn', this._sendMessage.bind(this)); }; Messagearea.prototype._loadConversations = function() { @@ -106,6 +109,61 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification'], function( }.bind(this)).fail(notification.exception); }; + Messagearea.prototype._sendMessage = function() { + // Call the web service to save our message. + var promises = ajax.call([{ + methodname: 'core_message_send_instant_messages', + args: { + messages: [ + { + touserid: this.find('.messages').data('userid'), + text: this.find('#sendmessagetxt').val() + } + ] + } + }]); + + // Update the DOM when we get some data back. + promises[0].then(function() { + // Update the messaging area. + this._addMessageToDom(); + }.bind(this)).fail(notification.exception); + }; + + Messagearea.prototype._addMessageToDom = function() { + // Get the variables we are going to use. + var userid = this.find('.messages').data('userid'); + var text = this.find('#sendmessagetxt').val(); + + // Call the web service to return how the message should look. + var promises = ajax.call([{ + methodname: 'core_message_data_for_messagearea_get_most_recent_message', + args: { + currentuserid: this._getCurrentUserId(), + otheruserid: userid + } + }]); + + // Add the message. + promises[0].then(function(data) { + templates.render('core_message/message', data).then(function(html, js) { + this.find('.messages').append(html); + // And execute any JS that was in the template. + templates.runTemplateJS(js); + + // Update the conversation on the left. + var leftmsg = text.substr(0, this.maxstringlength); + if (text.length > this.maxstringlength) { + leftmsg += " ..."; + } + this.find('#contact-' + userid + ' .lastmessage').empty().append(leftmsg); + + // Empty the response text area. + this.find('#sendmessagetxt').val(''); + }.bind(this)); + }.bind(this)).fail(notification.exception); + }; + Messagearea.prototype._getCurrentUserId = function() { return this._node.data('userid'); }; diff --git a/message/classes/api.php b/message/classes/api.php index 5fc10171c14..143310c0fa3 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -101,4 +101,23 @@ class api { return new \core_message\output\messages($userid, $otheruserid, $arrmessages); } + + /** + * Returns the most recent message between two users. + * + * @param int $userid the current user + * @param int $otheruserid the other user + * @return \core_message\output\message|null + */ + public static function get_most_recent_message($userid, $otheruserid) { + // We want two messages here so we get an accurate 'blocktime' value. + if ($messages = \core_message\helper::get_messages($userid, $otheruserid, 0, 2, 'timecreated DESC')) { + // Swap the order so we now have them in historical order. + $messages = array_reverse($messages); + $arrmessages = \core_message\helper::create_messages($userid, $messages); + return array_pop($arrmessages); + } + + return null; + } } diff --git a/message/externallib.php b/message/externallib.php index 1f3d47e3e76..447a6f45580 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -605,6 +605,65 @@ class core_message_external extends external_api { ); } + /** + * Get the most recent message in a conversation parameters. + * + * @return external_function_parameters + */ + public static function data_for_messagearea_get_most_recent_message_parameters() { + return new external_function_parameters( + array( + 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), + 'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'), + ) + ); + } + + /** + * Get the most recent message in a conversation. + * + * @param int $currentuserid The current user's id + * @param int $otheruserid The other user's id + * @return external_single_structure + */ + public static function data_for_messagearea_get_most_recent_message($currentuserid, $otheruserid) { + global $CFG, $PAGE; + + // Check if messaging is enabled. + if (!$CFG->messaging) { + throw new moodle_exception('disabled', 'message'); + } + + $params = array( + 'currentuserid' => $currentuserid, + 'otheruserid' => $otheruserid + ); + self::validate_parameters(self::data_for_messagearea_get_most_recent_message_parameters(), $params); + + self::validate_context(context_user::instance($currentuserid)); + + $message = \core_message\api::get_most_recent_message($currentuserid, $otheruserid); + + $renderer = $PAGE->get_renderer('core_message'); + return $message->export_for_template($renderer); + } + + /** + * Get messagearea get most recent message returns. + * + * @return external_single_structure + */ + public static function data_for_messagearea_get_most_recent_message_returns() { + return new external_single_structure( + array( + 'text' => new external_value(PARAM_RAW, 'The text of the message'), + 'blocktime' => new external_value(PARAM_NOTAGS, 'The time to display above the message'), + 'position' => new external_value(PARAM_ALPHA, 'The position of the text'), + 'timesent' => new external_value(PARAM_NOTAGS, 'The time the message was sent'), + ) + ); + } + /** * Get contacts parameters description. * diff --git a/message/templates/contact.mustache b/message/templates/contact.mustache index 58159bd3dd8..aad8f5c211f 100644 --- a/message/templates/contact.mustache +++ b/message/templates/contact.mustache @@ -1,4 +1,4 @@ -