MDL-54687 core_message: added ability to send messages

This commit is contained in:
Mark Nelson
2016-10-07 16:20:51 +08:00
parent 3cd13828ff
commit c060cd4904
6 changed files with 147 additions and 2 deletions
+9
View File
@@ -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(
+58
View File
@@ -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');
};
+19
View File
@@ -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;
}
}
+59
View File
@@ -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.
*
+1 -1
View File
@@ -1,4 +1,4 @@
<div class="row-fluid contact {{#lastmessage}}contact-msg{{/lastmessage}} {{#selected}}selected{{/selected}}" data-userid="{{userid}}">
<div id="contact-{{userid}}" class="row-fluid contact {{#lastmessage}}contact-msg{{/lastmessage}} {{#selected}}selected{{/selected}}" data-userid="{{userid}}">
<div class="span2 picture">
<img height="64" src="{{profileimageurl}}" alt="" />
</div>
+1 -1
View File
@@ -1,4 +1,4 @@
<div class="messages">
<div class="messages" data-userid="{{otheruserid}}">
<div class="name">
{{otheruserfullname}}
</div>