Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 *
19
 * @module     core_message/message_preferences
20
 * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core/ajax', 'core/notification',
24
        'core_message/message_notification_preference', 'core/custom_interaction_events'],
25
        function($, Ajax, Notification, MessageNotificationPreference, CustomEvents) {
26
 
27
    var SELECTORS = {
28
        PREFERENCE: '[data-state]',
29
        PREFERENCES_CONTAINER: '[data-region="preferences-container"]',
30
        CONTACTABLE_PRIVACY_CONTAINER: '[data-region="privacy-setting-container"]',
31
    };
32
 
33
    /**
34
     * Constructor for the MessagePreferences.
35
     *
36
     * @class
37
     * @param {object} element The root element for the message preferences
38
     */
39
    var MessagePreferences = function(element) {
40
        this.root = $(element);
41
        this.userId = this.root.find(SELECTORS.PREFERENCES_CONTAINER).attr('data-user-id');
42
 
43
        this.registerEventListeners();
44
    };
45
 
46
    /**
47
     * Check if the preferences have been disabled on this page.
48
     *
49
     * @method preferencesDisabled
50
     * @return {bool}
51
     */
52
    MessagePreferences.prototype.preferencesDisabled = function() {
53
        return this.root.find(SELECTORS.PREFERENCES_CONTAINER).hasClass('disabled');
54
    };
55
 
56
    /**
57
     * Update the contactable privacy user preference in the DOM and
58
     * send a request to update on the server.
59
     *
60
     * @return {Promise}
61
     * @method saveContactablePrivacySetting
62
     */
63
    MessagePreferences.prototype.saveContactablePrivacySetting = function() {
64
        var container = this.root.find(SELECTORS.CONTACTABLE_PRIVACY_CONTAINER);
65
        var value = $("input[type='radio']:checked").val();
66
 
67
        if (container.hasClass('loading')) {
68
            return $.Deferred().resolve();
69
        }
70
 
71
        container.addClass('loading');
72
 
73
        var request = {
74
            methodname: 'core_user_update_user_preferences',
75
            args: {
76
                userid: this.userId,
77
                preferences: [
78
                    {
79
                        type: container.attr('data-preference-key'),
80
                        value: value,
81
                    }
82
                ]
83
            }
84
        };
85
 
86
        return Ajax.call([request])[0]
87
            .fail(Notification.exception)
88
            .always(function() {
89
                container.removeClass('loading');
90
            });
91
    };
92
 
93
    /**
94
     * Create all of the event listeners for the message preferences page.
95
     *
96
     * @method registerEventListeners
97
     */
98
    MessagePreferences.prototype.registerEventListeners = function() {
99
        CustomEvents.define(this.root, [
100
            CustomEvents.events.activate
101
        ]);
102
 
103
        this.root.on('change', function(e) {
104
            // Add listener for privacy setting radio buttons change.
105
            if (e.target.name == 'message_blocknoncontacts') {
106
                this.saveContactablePrivacySetting();
107
            } else {
108
                // Add listener for processor preferences.
109
                if (!this.preferencesDisabled()) {
110
                    var preferencesContainer = $(e.target).closest(SELECTORS.PREFERENCES_CONTAINER);
111
                    var preferenceElement = $(e.target).closest(SELECTORS.PREFERENCE);
112
                    var messagePreference = new MessageNotificationPreference(preferencesContainer, this.userId);
113
 
114
                    preferenceElement.addClass('loading');
115
                    messagePreference.save().always(function() {
116
                        preferenceElement.removeClass('loading');
117
                    });
118
                }
119
            }
120
        }.bind(this));
121
    };
122
 
123
    return MessagePreferences;
124
});