From 3cd13828ffebe42a3a88b7349bb7b23694609fa0 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 4 Jul 2016 13:26:07 +0800 Subject: [PATCH] MDL-54687 core_message: added ability to view messages --- lib/db/services.php | 8 ++++ message/amd/src/message-area.js | 35 ++++++++++++++ message/externallib.php | 75 ++++++++++++++++++++++++++++++ message/templates/contact.mustache | 2 +- 4 files changed, 119 insertions(+), 1 deletion(-) diff --git a/lib/db/services.php b/lib/db/services.php index 410881d76fe..951b90a16b2 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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', diff --git a/message/amd/src/message-area.js b/message/amd/src/message-area.js index e65d254fb8d..33ad986a230 100644 --- a/message/amd/src/message-area.js +++ b/message/amd/src/message-area.js @@ -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'); }; diff --git a/message/externallib.php b/message/externallib.php index 3d268991600..1f3d47e3e76 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -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. * diff --git a/message/templates/contact.mustache b/message/templates/contact.mustache index 7ac7d26a5ba..58159bd3dd8 100644 --- a/message/templates/contact.mustache +++ b/message/templates/contact.mustache @@ -1,4 +1,4 @@ -
+