Proyectos de Subversion Moodle

Rev

Rev 5 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4 ariadna 1
// This file is part of Moodle - https://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
 * Defines the behavior of the configuration page of a Point of View block.
18
 * @copyright  2020 Quentin Fombaron, 2021 Astor Bizard
19
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20
 */
21
define(['jquery', 'core/ajax', 'core/notification'], function($, ajax, notification) {
22
 
23
    /**
24
     * Manage updating of visibility state of elements related to Reactions and Difficulty tracks,
25
     * depending on whether they are enabled or not.
26
     */
27
    function manageElementsVisibility() {
6 ariadna 28
        var $enableReactions = $('[name="config_enable_point_views"]');
29
        var $enableDifficultyTracks = $('[name="config_enable_difficultytracks"]');
4 ariadna 30
 
31
        var updateElementsVisibility = function() {
32
            var reactionsEnabled = $enableReactions.val() > 0;
33
            var difficultyTracksEnabled = $enableDifficultyTracks.val() > 0;
34
 
6 ariadna 35
            $('fieldset[id^=id_activities_header]').toggle(reactionsEnabled || difficultyTracksEnabled);
4 ariadna 36
 
6 ariadna 37
            $('.reactions, fieldset[id^=id_images_header]').toggle(reactionsEnabled);
4 ariadna 38
            $('.difficultytracks').toggle(difficultyTracksEnabled);
39
        };
40
 
41
        updateElementsVisibility();
42
 
43
        $enableReactions.change(updateElementsVisibility);
44
        $enableDifficultyTracks.change(updateElementsVisibility);
45
    }
46
 
47
    /**
48
     * Manage Enable/Disable buttons for sections and module types.
49
     */
50
    function manageEnableDisableButtons() {
51
 
52
        // Update of Enable/Disable buttons state for a section or module type,
53
        // depending on checkboxes state for that section or module type.
54
        var updateEnableDisableButtonsFor = function(sectionOrType) {
55
            var $checkboxes = $('.cb' + sectionOrType + ':checkbox');
56
            var nBoxesChecked = $checkboxes.filter(':checked').length;
57
            $('#enableall' + sectionOrType).toggleClass('active', nBoxesChecked === $checkboxes.length);
58
            $('#disableall' + sectionOrType).toggleClass('active', nBoxesChecked === 0);
59
        };
60
 
61
        $('.enablemodulereactions').change(function() {
62
            updateEnableDisableButtonsFor($(this).data('type')); // Update Enable/Disable buttons state for module type.
63
            updateEnableDisableButtonsFor($(this).data('section')); // Update Enable/Disable buttons state for section.
64
        }).click(function() {
65
            $('.enablemodulereactions.highlighted').removeClass('highlighted');
66
        });
67
 
68
        $('.enable-disable button').each(function() {
69
            var sectionOrType = $(this).data('type') || $(this).data('section');
70
 
71
            updateEnableDisableButtonsFor(sectionOrType); // Update Enable/Disable buttons state on page load.
72
 
73
            $(this).click(function() {
74
                $('.enablemodulereactions.highlighted').removeClass('highlighted');
75
                $('.cb' + sectionOrType + ':checkbox')
76
                .prop('checked', $(this).data('enable')) // Update all corresponding checkboxes.
77
                .addClass('highlighted')
78
                .change(); // Trigger a change to update Enable/Disable buttons state accordingly.
79
            });
80
        });
81
    }
82
 
83
    /**
84
     * Adds a listener to a button click with a confirm dialog and ajax call.
85
     * @param {jQuery} $button The button to add the listener to.
86
     * @param {String} message Confirmation message, a string component of block_point_view.
87
     * @param {String} ajaxmethod Ajax method to be called.
88
     * @param {Object} ajaxargs Arguments to be passed to the ajax call.
89
     * @param {Function} callback A function called after ajax call completed successfully.
90
     */
91
    function buttonWithAjaxCall($button, message, ajaxmethod, ajaxargs, callback) {
92
        $button.click(function(e) {
93
            M.util.show_confirm_dialog(e, {
94
                message: M.util.get_string(message, 'block_point_view'),
95
                callback: function() {
96
                    ajax.call([
97
                        {
98
                            methodname: 'block_point_view_' + ajaxmethod,
99
                            args: ajaxargs,
100
                            done: callback,
101
                            fail: notification.exception
102
                        }
103
                    ]);
104
                }
105
            });
106
        });
107
    }
108
 
6 ariadna 109
    /**
110
     * Setup change listener to update track color responsively when it is changed from select.
111
     * @param {Object} trackcolors Mapping of trackname => CSS color.
112
     */
113
    function setupDifficultyTrackChange(trackcolors) {
114
        // Difficulty track change.
115
        $('.moduletrackselect select').change(function() {
116
            $('#track_' + $(this).data('id')).css({
117
                'background-color': trackcolors[$(this).val()] // Change track color.
118
            });
119
        }).change(); // Update track colors once on page load.
120
    }
121
 
4 ariadna 122
    return {
123
        init: function(envconf, trackcolors) {
124
 
125
            manageElementsVisibility();
126
 
127
            manageEnableDisableButtons();
128
 
6 ariadna 129
            setupDifficultyTrackChange(trackcolors);
4 ariadna 130
 
131
            // Custom emoji deletion.
132
            buttonWithAjaxCall(
133
                    $('#delete_custom_pix'),
134
                    'deleteemojiconfirmation',
135
                    'delete_custom_pix',
136
                    {
137
                        contextid: envconf.contextid,
138
                        courseid: envconf.courseid,
139
                        draftitemid: $('input[name="config_point_views_pix"]').val()
140
                    },
141
                    function() {
142
                        $('.pix-preview[data-source="custom"], #delete_custom_pix').remove(); // Remove emoji preview and button.
143
                        // Refresh draft area files.
144
                        // # For an unknown reason, the following instruction with jQuery does not work
145
                        // # (or at least does not trigger the expected listener).
6 ariadna 146
                        document.querySelector('[id^=fitem_id_config_point_views_pix] .fp-path-folder-name').click();
4 ariadna 147
                    }
148
            );
149
 
150
            // Update current emoji on emoji change.
151
            $('[name=config_pixselect]').change(function() {
152
                var newsource = $(this).val();
153
                $('img.currentpix').each(function() {
154
                    var $img = $('img[data-source="' + newsource + '"][data-reaction="' + $(this).data('reaction') + '"]');
155
                    if ($img.length == 1 && $img.attr('src') > '') {
156
                        $(this).attr('src', $img.attr('src'));
157
                    }
158
                });
159
            });
160
 
161
            // Course reactions cleanup.
162
            buttonWithAjaxCall(
163
                    $('#cleanup_reactions'),
164
                    'cleanupreactionsconfirmation',
165
                    'update_db',
166
                    {
167
                        func: 'cleanup',
168
                        courseid: envconf.courseid
169
                    },
170
                    function() {
171
                        notification.alert(M.util.get_string('info', 'moodle'),
172
                                M.util.get_string('reactionscleanedupsuccessfully', 'block_point_view'),
173
                                M.util.get_string('ok', 'moodle'));
174
                    }
175
            );
176
 
177
            // Course reactions reset.
178
            buttonWithAjaxCall(
179
                    $('#reset_reactions'),
180
                    'resetreactionsconfirmation',
181
                    'update_db',
182
                    {
183
                        func: 'reset',
184
                        courseid: envconf.courseid
185
                    },
186
                    function() {
187
                        notification.alert(M.util.get_string('info', 'moodle'),
188
                                M.util.get_string('reactionsresetsuccessfully', 'block_point_view'),
189
                                M.util.get_string('ok', 'moodle'));
190
                    }
191
            );
192
 
193
            // Module reactions reset.
194
            $('[data-role=reset_module').each(function() {
195
                var cmid = $(this).data('cmid');
196
                buttonWithAjaxCall(
197
                        $(this),
198
                        'resetreactionsonmoduleconfirmation',
199
                        'update_db',
200
                        {
201
                            func: 'reset',
202
                            courseid: envconf.courseid,
203
                            cmid: cmid
204
                        },
205
                        function() {
206
                            notification.alert(M.util.get_string('info', 'moodle'),
207
                                    M.util.get_string('reactionsresetsuccessfully', 'block_point_view'),
208
                                    M.util.get_string('ok', 'moodle'));
209
                        }
210
                );
211
            });
6 ariadna 212
        },
213
 
214
        setupDifficultyTrackChange: setupDifficultyTrackChange
4 ariadna 215
    };
216
});