MDL-60918 message: use current user in message preference update
[moodle.git] / message / amd / src / message_preferences.js
CommitLineData
8c125526
RW
1// This file is part of Moodle - http://moodle.org/
2//
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.
7//
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.
12//
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/>.
15
16/**
17 * Controls the message preference page.
18 *
b6723f5b
RW
19 * @module core_message/message_preferences
20 * @class message_preferences
8c125526
RW
21 * @package message
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8c125526
RW
24 */
25define(['jquery', 'core/ajax', 'core/notification',
b6723f5b
RW
26 'core_message/message_notification_preference', 'core/custom_interaction_events'],
27 function($, Ajax, Notification, MessageNotificationPreference, CustomEvents) {
8c125526
RW
28
29 var SELECTORS = {
b6723f5b 30 PREFERENCE: '[data-state]',
6af2bd09
RW
31 PREFERENCES_CONTAINER: '[data-region="preferences-container"]',
32 BLOCK_NON_CONTACTS: '[data-region="block-non-contacts-container"] [data-block-non-contacts]',
33 BLOCK_NON_CONTACTS_CONTAINER: '[data-region="block-non-contacts-container"]',
8c125526
RW
34 };
35
36 /**
37 * Constructor for the MessagePreferences.
38 *
7b55aaa1 39 * @param {object} element The root element for the message preferences
8c125526 40 */
b6723f5b
RW
41 var MessagePreferences = function(element) {
42 this.root = $(element);
745783fa 43 this.userId = this.root.find(SELECTORS.PREFERENCES_CONTAINER).attr('data-user-id');
8c125526 44
b6723f5b
RW
45 this.registerEventListeners();
46 };
8c125526 47
b6723f5b
RW
48 /**
49 * Check if the preferences have been disabled on this page.
50 *
51 * @method preferencesDisabled
7b55aaa1 52 * @return {bool}
b6723f5b
RW
53 */
54 MessagePreferences.prototype.preferencesDisabled = function() {
55 return this.root.find(SELECTORS.PREFERENCES_CONTAINER).hasClass('disabled');
8c125526
RW
56 };
57
58 /**
59 * Update the block messages from non-contacts user preference in the DOM and
60 * send a request to update on the server.
61 *
7b55aaa1 62 * @return {Promise}
8c125526
RW
63 * @method saveBlockNonContactsStatus
64 */
65 MessagePreferences.prototype.saveBlockNonContactsStatus = function() {
b6723f5b
RW
66 var checkbox = this.root.find(SELECTORS.BLOCK_NON_CONTACTS);
67 var container = this.root.find(SELECTORS.BLOCK_NON_CONTACTS_CONTAINER);
8c125526
RW
68 var ischecked = checkbox.prop('checked');
69
70 if (container.hasClass('loading')) {
71 return $.Deferred().resolve();
72 }
73
74 container.addClass('loading');
75
76 var request = {
c598f278 77 methodname: 'core_user_update_user_preferences',
8c125526 78 args: {
745783fa 79 userid: this.userId,
c598f278
MN
80 preferences: [
81 {
82 type: checkbox.attr('data-preference-key'),
83 value: ischecked ? 1 : 0,
84 }
85 ]
8c125526
RW
86 }
87 };
88
89 return Ajax.call([request])[0]
90 .fail(Notification.exception)
91 .always(function() {
92 container.removeClass('loading');
93 });
94 };
95
b6723f5b
RW
96 /**
97 * Create all of the event listeners for the message preferences page.
98 *
99 * @method registerEventListeners
100 */
101 MessagePreferences.prototype.registerEventListeners = function() {
102 CustomEvents.define(this.root, [
103 CustomEvents.events.activate
104 ]);
105
412a4a6d 106 this.root.on(CustomEvents.events.activate, SELECTORS.BLOCK_NON_CONTACTS, function() {
b6723f5b
RW
107 this.saveBlockNonContactsStatus();
108 }.bind(this));
109
110 this.root.on('change', function(e) {
111 if (!this.preferencesDisabled()) {
112 var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
113 var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
745783fa 114 var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);
b6723f5b
RW
115
116 preferenceElement.addClass('loading');
117 messagePreference.save().always(function() {
118 preferenceElement.removeClass('loading');
119 });
120 }
121 }.bind(this));
122 };
123
8c125526
RW
124 return MessagePreferences;
125});