From 9aa012b5a15ec85049483d649296fd894a3dbe58 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 4 Jul 2016 20:26:09 +0800 Subject: [PATCH] MDL-54687 core_message: added ability to toggle between tabs --- lang/en/message.php | 1 + lib/db/services.php | 16 ++++ message/amd/src/message-area.js | 79 +++++++++++++++ message/externallib.php | 122 ++++++++++++++++++++++++ message/templates/loading.mustache | 34 +++++++ message/templates/message_area.mustache | 9 +- 6 files changed, 259 insertions(+), 2 deletions(-) create mode 100644 message/amd/src/message-area.js create mode 100644 message/templates/loading.mustache diff --git a/lang/en/message.php b/lang/en/message.php index 6230e7ba81f..63b3d1ebe1b 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -74,6 +74,7 @@ $string['incomingcontacts'] = 'Incoming contacts ({$a})'; $string['keywords'] = 'Keywords'; $string['keywordssearchresults'] = 'Messages found: {$a}'; $string['keywordssearchresultstoomany'] = 'More than {$a} messages found. Refine your search.'; +$string['loading'] = 'Loading ...'; $string['loggedin'] = 'Online'; $string['loggedindescription'] = 'When I\'m logged in'; $string['loggedoff'] = 'Not online'; diff --git a/lib/db/services.php b/lib/db/services.php index 38b9a93f441..410881d76fe 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -636,6 +636,22 @@ $functions = array( 'type' => 'read', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE), ), + 'core_message_data_for_messagearea_conversations' => array( + 'classname' => 'core_message_external', + 'methodname' => 'data_for_messagearea_conversations', + 'classpath' => 'message/externallib.php', + 'description' => 'Retrieve the template data for the conversation list', + 'type' => 'read', + 'ajax' => true, + ), + 'core_message_data_for_messagearea_contacts' => array( + 'classname' => 'core_message_external', + 'methodname' => 'data_for_messagearea_contacts', + 'classpath' => 'message/externallib.php', + 'description' => 'Retrieve the template data for the contact list', + '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 new file mode 100644 index 00000000000..e65d254fb8d --- /dev/null +++ b/message/amd/src/message-area.js @@ -0,0 +1,79 @@ +// 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 . + +/** + * This module handles toggling between the 'Conversations' and 'Contacts' + * tabs in the message area. + * + * @module core_message/message-area + * @package core_message + * @copyright 2016 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +define(['jquery', 'core/ajax', 'core/templates', 'core/notification'], function($, ajax, templates, notification) { + + function Messagearea(selector) { + this._node = $(selector); + this._init(); + } + + Messagearea.prototype.find = function(selector) { + return this._node.find(selector); + }; + + Messagearea.prototype._init = function() { + this._node.on('click', '.tabconversations', this._loadConversations.bind(this)); + this._node.on('click', '.tabcontacts', this._loadContacts.bind(this)); + }; + + Messagearea.prototype._loadConversations = function() { + this._loadContactArea('core_message_data_for_messagearea_conversations'); + }; + + Messagearea.prototype._loadContacts = function() { + this._loadContactArea('core_message_data_for_messagearea_contacts'); + }; + + Messagearea.prototype._loadContactArea = function(methodname) { + // Show loading template. + templates.render('core_message/loading', {}).done(function(html) { + this.find('.contacts').empty().append(html); + }.bind(this)); + + // Call the web service to return the data we want to view. + var promises = ajax.call([{ + methodname: methodname, + args: { + userid: this._getCurrentUserId() + } + }]); + + // After the request render the contacts area. + promises[0].then(function(data) { + // We have the data - lets re-render the template with it. + return templates.render('core_message/contacts', data).then(function(html, js) { + this.find('.contacts-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'); + }; + + return Messagearea; +}); diff --git a/message/externallib.php b/message/externallib.php index 6702c3b5438..3d268991600 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -408,6 +408,128 @@ class core_message_external extends external_api { return null; } + /** + * Get messagearea conversations parameters. + * + * @return external_function_parameters + */ + public static function data_for_messagearea_conversations_parameters() { + return new external_function_parameters( + array( + 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), + 'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0), + 'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0) + ) + ); + } + + /** + * Get messagearea conversations. + * + * @param int $userid The id of the user who we are viewing conversations for + * @param int $limitfrom + * @param int $limitnum + * @return external_function_parameters + */ + public static function data_for_messagearea_conversations($userid, $limitfrom = 0, $limitnum = 0) { + global $CFG, $PAGE; + + // Check if messaging is enabled. + if (!$CFG->messaging) { + throw new moodle_exception('disabled', 'message'); + } + + $params = array( + 'userid' => $userid, + 'limitfrom' => $limitfrom, + 'limitnum' => $limitnum + ); + self::validate_parameters(self::data_for_messagearea_conversations_parameters(), $params); + + self::validate_context(context_user::instance($userid)); + + $contacts = \core_message\api::get_conversations($userid, 0, $limitfrom, $limitnum); + + $renderer = $PAGE->get_renderer('core_message'); + return $contacts->export_for_template($renderer); + } + + /** + * Get messagearea conversations returns. + * + * @return external_function_parameters + */ + public static function data_for_messagearea_conversations_returns() { + return new external_function_parameters( + array( + 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), + 'conversationsselected' => new external_value(PARAM_BOOL, 'Determines if conversations were selected, + otherwise contacts were'), + 'contacts' => new external_multiple_structure( + new external_single_structure( + array( + 'userid' => new external_value(PARAM_INT, 'The user\'s id'), + 'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'), + 'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'), + 'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'), + 'lastmessage' => new external_value(PARAM_NOTAGS, 'The user\'s last message', VALUE_OPTIONAL), + 'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status', VALUE_OPTIONAL) + ) + ) + ) + ) + ); + } + + /** + * Get messagearea contacts parameters. + * + * @return external_function_parameters + */ + public static function data_for_messagearea_contacts_parameters() { + return self::data_for_messagearea_conversations_parameters(); + } + + /** + * Get messagearea contacts parameters. + * + * @param int $userid The id of the user who we are viewing conversations for + * @param int $limitfrom + * @param int $limitnum + * @return external_function_parameters + */ + public static function data_for_messagearea_contacts($userid, $limitfrom = 0, $limitnum = 0) { + global $CFG, $PAGE; + + // Check if messaging is enabled. + if (!$CFG->messaging) { + throw new moodle_exception('disabled', 'message'); + } + + $params = array( + 'userid' => $userid, + 'limitfrom' => $limitfrom, + 'limitnum' => $limitnum + ); + self::validate_parameters(self::data_for_messagearea_contacts_parameters(), $params); + + self::validate_context(context_user::instance($userid)); + + $contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum); + + $renderer = $PAGE->get_renderer('core_message'); + return $contacts->export_for_template($renderer); + } + + /** + * Get messagearea contacts returns. + * + * @return external_function_parameters + */ + public static function data_for_messagearea_contacts_returns() { + return self::data_for_messagearea_conversations_returns(); + } + /** * Get contacts parameters description. * diff --git a/message/templates/loading.mustache b/message/templates/loading.mustache new file mode 100644 index 00000000000..19dcd8438cf --- /dev/null +++ b/message/templates/loading.mustache @@ -0,0 +1,34 @@ +{{! + 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 . +}} +{{! + @template core_message/loading + + Loading spinner shown while waiting for an ajax request + + Classes required for JS: + * None + + Data attibutes required for JS: + * None + + Context variables required for this template: + * None + + Example context (json): + { } +}} +{{#pix}}y/loading, core, {{#str}}loading, core_message{{/str}}{{/pix}} diff --git a/message/templates/message_area.mustache b/message/templates/message_area.mustache index b266941a2f7..3e0943f385c 100644 --- a/message/templates/message_area.mustache +++ b/message/templates/message_area.mustache @@ -1,4 +1,4 @@ -
+
{{#contacts}} @@ -11,4 +11,9 @@ {{/messages}}
-
\ No newline at end of file +
+{{#js}} + require(['core_message/message-area'], function(Messagearea) { + var messageArea = new Messagearea('.messaging-area'); + }); +{{/js}} \ No newline at end of file