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,
68 this._viewFullProfile();
69 data.originalEvent.preventDefault();
71 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILESENDMESSAGE,
74 data.originalEvent.preventDefault();
76 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEUNBLOCKCONTACT,
78 this._unblockContact();
79 data.originalEvent.preventDefault();
81 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEBLOCKCONTACT,
84 data.originalEvent.preventDefault();
86 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEADDCONTACT,
89 data.originalEvent.preventDefault();
91 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.PROFILEREMOVECONTACT,
93 this._removeContact();
94 data.originalEvent.preventDefault();
96 this.messageArea.onDelegateEvent(CustomEvents.events.activate, SELECTORS.SHOWCONTACTS,
97 this._hideMessagingArea.bind(this));
101 * Handles viewing the profile.
103 * @param {Event} event
104 * @param {int} userid
105 * @return {Promise} The promise resolved when the profile has been rendered
108 Profile.prototype._viewProfile = function(event, userid) {
109 // Show loading template.
110 Templates.render('core/loading', {}).done(function(html, js) {
111 Templates.replaceNodeContents(this.messageArea.find(SELECTORS.MESSAGESAREA), html, js);
114 // Call the web service to return the profile.
115 var promises = Ajax.call([{
116 methodname: 'core_message_data_for_messagearea_get_profile',
118 currentuserid: this.messageArea.getCurrentUserId(),
124 return promises[0].then(function(data) {
125 return Templates.render('core_message/message_area_profile', data);
126 }).then(function(html, js) {
127 Templates.replaceNodeContents(this.messageArea.find(SELECTORS.MESSAGESAREA), html, js);
128 }.bind(this)).fail(Notification.exception);
132 * Handles viewing the user's full profile.
136 Profile.prototype._viewFullProfile = function() {
137 window.location.href = Config.wwwroot + '/user/profile.php?id=' + this._getUserId();
141 * Handles viewing the messages with the user.
145 Profile.prototype._sendMessage = function() {
146 this.messageArea.trigger(Events.SENDMESSAGE, this._getUserId());
150 * Handles blocking the contact.
152 * @return {Promise} The promise resolved when the contact has been blocked
155 Profile.prototype._blockContact = function() {
156 var action = this._performAction('core_message_block_user', 'unblockcontact', 'profile-block-contact',
157 'profile-unblock-contact', '');
158 return action.then(function() {
159 this.messageArea.trigger(Events.CONTACTBLOCKED, this._getUserId());
164 * Handles unblocking the contact.
166 * @return {Promise} The promise resolved when the contact has been unblocked
169 Profile.prototype._unblockContact = function() {
170 var action = this._performAction('core_message_unblock_user', 'blockcontact', 'profile-unblock-contact',
171 'profile-block-contact', 'danger');
172 return action.then(function() {
173 this.messageArea.trigger(Events.CONTACTUNBLOCKED, this._getUserId());
178 * Handles adding the contact.
180 * @return {Promise} The promise resolved when the contact has been added
183 Profile.prototype._addContact = function() {
184 var action = this._performAction('core_message_create_contact_request', 'removecontact', 'profile-add-contact',
185 'profile-remove-contact', 'danger');
186 return action.then(function() {
187 this.messageArea.trigger(Events.CONTACTADDED, this._getUserId());
192 * Handles removing the contact.
194 * @return {Promise} The promise resolved when the contact has been removed
197 Profile.prototype._removeContact = function() {
198 var action = this._performAction('core_message_delete_contacts', 'addcontact', 'profile-remove-contact',
199 'profile-add-contact', '');
200 return action.then(function() {
201 this.messageArea.trigger(Events.CONTACTREMOVED, this._getUserId());
206 * Helper function to perform actions on the profile page.
208 * @param {String} service The web service to call.
209 * @param {String} string The string to change the button value to
210 * @param {String} oldaction The data-action of the button
211 * @param {string} newaction The data-action to change the button to
212 * @param {String} newclass The CSS class we want to add
213 * @return {Promise} The promise resolved when the action has been performed
216 Profile.prototype._performAction = function(service, string, oldaction, newaction, newclass) {
217 // This is a temporary hack as we are getting rid of this UI.
220 case 'core_message_block_user':
222 userid: this.messageArea.getCurrentUserId(),
223 blockeduserid: this._getUserId()
226 case 'core_message_unblock_user':
228 userid: this.messageArea.getCurrentUserId(),
229 unblockeduserid: this._getUserId()
232 case 'core_message_create_contact_request':
234 userid: this.messageArea.getCurrentUserId(),
235 requesteduserid: this._getUserId()
240 userid: this.messageArea.getCurrentUserId(),
248 var promises = Ajax.call([{
253 return promises[0].then(function() {
254 return Str.get_string(string, 'message');
255 }).then(function(s) {
256 this._changeText(s, oldaction, newaction, newclass);
257 }.bind(this)).fail(Notification.exception);
261 * Changes the text in the profile area.
263 * @param {String} text The string to change the button value to
264 * @param {string} oldaction The data-action of the button
265 * @param {string} newaction The data-action to change the button to
266 * @param {String} newclass The CSS class we want to add
269 Profile.prototype._changeText = function(text, oldaction, newaction, newclass) {
270 var anchor = this.messageArea.find("[data-action='" + oldaction + "']");
274 anchor.removeClass();
275 // Add the class if there is one.
277 anchor.addClass(newclass);
280 anchor.attr('data-action', newaction);
284 * Returns the ID of the user whos profile we are viewing.
286 * @return {int} The user ID
289 Profile.prototype._getUserId = function() {
290 return this.messageArea.find(SELECTORS.PROFILE).data('userid');
294 * Hide the messaging area. This only applies on smaller screen resolutions.
296 Profile.prototype._hideMessagingArea = function() {
297 this.messageArea.find(SELECTORS.MESSAGINGAREA)
298 .removeClass('show-messages')
299 .addClass('hide-messages');