Skip to content

Commit

Permalink
MDL-54687 core_message: added ability to toggle between tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Oct 7, 2016
1 parent 879e2be commit 9aa012b
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 2 deletions.
1 change: 1 addition & 0 deletions lang/en/message.php
Expand Up @@ -74,6 +74,7 @@
$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';
Expand Down
16 changes: 16 additions & 0 deletions lib/db/services.php
Expand Up @@ -636,6 +636,22 @@
'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',
Expand Down
79 changes: 79 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.

/**
* 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 <markn@moodle.com>
* @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;
});
122 changes: 122 additions & 0 deletions message/externallib.php
Expand Up @@ -408,6 +408,128 @@ public static function unblock_contacts_returns() {
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.
*
Expand Down
34 changes: 34 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
}}
{{!
@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}}
9 changes: 7 additions & 2 deletions message/templates/message_area.mustache
@@ -1,4 +1,4 @@
<div class="messaging-area">
<div class="messaging-area" data-userid="{{userid}}">
<div class="row-fluid">
<div class="span4 contacts-area">
{{#contacts}}
Expand All @@ -11,4 +11,9 @@
{{/messages}}
</div>
</div>
</div>
</div>
{{#js}}
require(['core_message/message-area'], function(Messagearea) {
var messageArea = new Messagearea('.messaging-area');
});
{{/js}}

0 comments on commit 9aa012b

Please sign in to comment.