1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * This module handles the profile area of the messaging area.
19 * @module core_message/message_area_profile
20 * @package core_message
21 * @copyright 2016 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str', 'core/config',
25 'core/custom_interaction_events', 'core_message/message_area_events'],
26 function($, Ajax, Templates, Notification, Str, Config, CustomEvents, Events) {
28 /** @type {Object} The list of selectors for the message area. */
30 PROFILE: "[data-region='profile']",
31 PROFILEADDCONTACT: "[data-action='profile-add-contact']",
32 PROFILEBLOCKCONTACT: "[data-action='profile-block-contact']",
33 PROFILEREMOVECONTACT: "[data-action='profile-remove-contact']",
34 PROFILESENDMESSAGE: "[data-action='profile-send-message']",
35 PROFILEUNBLOCKCONTACT: "[data-action='profile-unblock-contact']",
36 PROFILEVIEW: "[data-action='profile-view']",
37 SHOWCONTACTS: "[data-action='show-contacts']",
38 MESSAGESAREA: "[data-region='messages-area']",
39 MESSAGINGAREA: "[data-region='messaging-area']"
45 * @param {Messagearea} messageArea The messaging area object.
47 function Profile(messageArea) {
48 this.messageArea = messageArea;
52 /** @type {Messagearea} The messaging area object. */
53 Profile.prototype.messageArea = null;
56 * Initialise the event listeners.
60 Profile.prototype._init = function() {
61 CustomEvents.define(this.messageArea.node, [
62 CustomEvents.events.activate
65 this.messageArea.onCustomEvent(Events.CONTACTSELECTED, this._viewProfile.bind(this));
66 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEVIEW,
67 this._viewFullProfile.bind(this));
68 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILESENDMESSAGE,
69 this._sendMessage.bind(this));
70 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEUNBLOCKCONTACT,
71 this._unblockContact.bind(this));
72 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEBLOCKCONTACT,
73 this._blockContact.bind(this));
74 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEADDCONTACT,
75 this._addContact.bind(this));
76 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEREMOVECONTACT,
77 this._removeContact.bind(this));
78 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.SHOWCONTACTS,
79 this._hideMessagingArea.bind(this));
83 * Handles viewing the profile.
85 * @param {Event} event
87 * @return {Promise} The promise resolved when the profile has been rendered
90 Profile.prototype._viewProfile = function(event, userid) {
91 // Show loading template.
92 Templates.render('core/loading', {}).done(function(html, js) {
93 Templates.replaceNodeContents(this.messageArea.find(SELECTORS.MESSAGESAREA), html, js);
96 // Call the web service to return the profile.
97 var promises = Ajax.call([{
98 methodname: 'core_message_data_for_messagearea_get_profile',
100 currentuserid: this.messageArea.getCurrentUserId(),
106 return promises[0].then(function(data) {
107 return Templates.render('core_message/message_area_profile', data);
108 }).then(function(html, js) {
109 Templates.replaceNodeContents(this.messageArea.find(SELECTORS.MESSAGESAREA), html, js);
110 }.bind(this)).fail(Notification.exception);
114 * Handles viewing the user's full profile.
118 Profile.prototype._viewFullProfile = function() {
119 window.location.href = Config.wwwroot + '/user/profile.php?id=' + this._getUserId();
123 * Handles viewing the messages with the user.
127 Profile.prototype._sendMessage = function() {
128 this.messageArea.trigger(Events.SENDMESSAGE, this._getUserId());
132 * Handles blocking the contact.
134 * @return {Promise} The promise resolved when the contact has been blocked
137 Profile.prototype._blockContact = function() {
138 var action = this._performAction('core_message_block_contacts', 'unblockcontact', 'profile-block-contact',
139 'profile-unblock-contact', '');
140 return action.then(function() {
141 this.messageArea.trigger(Events.CONTACTBLOCKED, this._getUserId());
146 * Handles unblocking the contact.
148 * @return {Promise} The promise resolved when the contact has been unblocked
151 Profile.prototype._unblockContact = function() {
152 var action = this._performAction('core_message_unblock_contacts', 'blockcontact', 'profile-unblock-contact',
153 'profile-block-contact', 'danger');
154 return action.then(function() {
155 this.messageArea.trigger(Events.CONTACTUNBLOCKED, this._getUserId());
160 * Handles adding the contact.
162 * @return {Promise} The promise resolved when the contact has been added
165 Profile.prototype._addContact = function() {
166 var action = this._performAction('core_message_create_contacts', 'removecontact', 'profile-add-contact',
167 'profile-remove-contact', 'danger');
168 return action.then(function() {
169 this.messageArea.trigger(Events.CONTACTADDED, this._getUserId());
174 * Handles removing the contact.
176 * @return {Promise} The promise resolved when the contact has been removed
179 Profile.prototype._removeContact = function() {
180 var action = this._performAction('core_message_delete_contacts', 'addcontact', 'profile-remove-contact',
181 'profile-add-contact', '');
182 return action.then(function() {
183 this.messageArea.trigger(Events.CONTACTREMOVED, this._getUserId());
188 * Helper function to perform actions on the profile page.
190 * @param {String} service The web service to call.
191 * @param {String} string The string to change the button value to
192 * @param {String} oldaction The data-action of the button
193 * @param {string} newaction The data-action to change the button to
194 * @param {String} newclass The CSS class we want to add
195 * @return {Promise} The promise resolved when the action has been performed
198 Profile.prototype._performAction = function(service, string, oldaction, newaction, newclass) {
199 var promises = Ajax.call([{
202 userid: this.messageArea.getCurrentUserId(),
209 return promises[0].then(function() {
210 return Str.get_string(string, 'message');
211 }).then(function(s) {
212 this._changeText(s, oldaction, newaction, newclass);
213 }.bind(this)).fail(Notification.exception);
217 * Changes the text in the profile area.
219 * @param {String} text The string to change the button value to
220 * @param {string} oldaction The data-action of the button
221 * @param {string} newaction The data-action to change the button to
222 * @param {String} newclass The CSS class we want to add
225 Profile.prototype._changeText = function(text, oldaction, newaction, newclass) {
226 var anchor = this.messageArea.find("[data-action='" + oldaction + "']");
230 anchor.removeClass();
231 // Add the class if there is one.
233 anchor.addClass(newclass);
236 anchor.attr('data-action', newaction);
240 * Returns the ID of the user whos profile we are viewing.
242 * @return {int} The user ID
245 Profile.prototype._getUserId = function() {
246 return this.messageArea.find(SELECTORS.PROFILE).data('userid');
250 * Hide the messaging area. This only applies on smaller screen resolutions.
252 Profile.prototype._hideMessagingArea = function() {
253 this.messageArea.find(SELECTORS.MESSAGINGAREA)
254 .removeClass('show-messages')
255 .addClass('hide-messages');