MDL-54687 core_message: added ability to view messages
This commit is contained in:
@@ -652,6 +652,14 @@ $functions = array(
|
||||
'type' => 'read',
|
||||
'ajax' => true,
|
||||
),
|
||||
'core_message_data_for_messagearea_messages' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'data_for_messagearea_messages',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Retrieve the template data for the messages',
|
||||
'type' => 'read',
|
||||
'ajax' => true,
|
||||
),
|
||||
'core_message_get_contacts' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'get_contacts',
|
||||
|
||||
@@ -36,6 +36,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification'], function(
|
||||
Messagearea.prototype._init = 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));
|
||||
};
|
||||
|
||||
Messagearea.prototype._loadConversations = function() {
|
||||
@@ -71,6 +72,40 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification'], function(
|
||||
}.bind(this)).fail(notification.exception);
|
||||
};
|
||||
|
||||
Messagearea.prototype._loadMessages = function(event) {
|
||||
// Show loading template.
|
||||
templates.render('core_message/loading', {}).done(function(html) {
|
||||
this.find('.messages-area').empty().append(html);
|
||||
}.bind(this));
|
||||
|
||||
// Get the user that was clicked.
|
||||
var userid = $(event.currentTarget).data('userid');
|
||||
// Remove the 'selected' class from any other contact.
|
||||
this.find('.contact').removeClass('selected');
|
||||
// Set the tab for the user to selected.
|
||||
this.find('#contact-' + userid).addClass('selected');
|
||||
|
||||
// Call the web service to get our data.
|
||||
var promises = ajax.call([{
|
||||
methodname: 'core_message_data_for_messagearea_messages',
|
||||
args: {
|
||||
currentuserid: this._getCurrentUserId(),
|
||||
otheruserid: userid
|
||||
}
|
||||
}]);
|
||||
|
||||
// Do stuff when we get data back.
|
||||
promises[0].then(function(data) {
|
||||
// We have the data - lets re-render the template with it.
|
||||
return templates.render('core_message/messages', data).then(function(html, js) {
|
||||
// Append the message.
|
||||
this.find('.messages-area').empty().append(html);
|
||||
// And execute any JS that was in the template.
|
||||
templates.runTemplateJS(js);
|
||||
}.bind(this));
|
||||
}.bind(this)).fail(notification.exception);
|
||||
};
|
||||
|
||||
Messagearea.prototype._getCurrentUserId = function() {
|
||||
return this._node.data('userid');
|
||||
};
|
||||
|
||||
@@ -530,6 +530,81 @@ class core_message_external extends external_api {
|
||||
return self::data_for_messagearea_conversations_returns();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messagearea messages parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
*/
|
||||
public static function data_for_messagearea_messages_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'),
|
||||
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
|
||||
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messagearea messages.
|
||||
*
|
||||
* @param int $currentuserid The current user's id
|
||||
* @param int $otheruserid The other user's id
|
||||
* @param int $limitfrom
|
||||
* @param int $limitnum
|
||||
* @return external_description
|
||||
*/
|
||||
public static function data_for_messagearea_messages($currentuserid, $otheruserid, $limitfrom = 0, $limitnum = 0) {
|
||||
global $CFG, $PAGE;
|
||||
|
||||
// Check if messaging is enabled.
|
||||
if (!$CFG->messaging) {
|
||||
throw new moodle_exception('disabled', 'message');
|
||||
}
|
||||
|
||||
$params = array(
|
||||
'currentuserid' => $currentuserid,
|
||||
'otheruserid' => $otheruserid,
|
||||
'limitfrom' => $limitfrom,
|
||||
'limitnum' => $limitnum
|
||||
);
|
||||
self::validate_parameters(self::data_for_messagearea_messages_parameters(), $params);
|
||||
|
||||
self::validate_context(context_user::instance($currentuserid));
|
||||
|
||||
$messages = \core_message\api::get_messages($currentuserid, $otheruserid, $limitfrom, $limitnum);
|
||||
|
||||
$renderer = $PAGE->get_renderer('core_message');
|
||||
return $messages->export_for_template($renderer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messagearea messages returns.
|
||||
*
|
||||
* @return external_description
|
||||
*/
|
||||
public static function data_for_messagearea_messages_returns() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing the messages on behalf of?'),
|
||||
'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
|
||||
'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
|
||||
'otheruserfullname' => new external_value(PARAM_NOTAGS, 'The other user\'s fullname'),
|
||||
'messages' => new external_multiple_structure(
|
||||
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,4 +1,4 @@
|
||||
<div class="row-fluid contact {{#selected}}selected{{/selected}}">
|
||||
<div 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>
|
||||
|
||||
Reference in New Issue
Block a user