Proyectos de Subversion Moodle

Rev

Rev 4 | Ir a la última revisión | | 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() {
5 ariadna 28
        var $enableReactions = $('#id_config_enable_point_views');
29
        var $enableDifficultyTracks = $('#id_config_enable_difficultytracks');
4 ariadna 30
 
31
        var updateElementsVisibility = function() {
32
            var reactionsEnabled = $enableReactions.val() > 0;
33
            var difficultyTracksEnabled = $enableDifficultyTracks.val() > 0;
34
 
5 ariadna 35
            $('#id_activities_header').toggle(reactionsEnabled || difficultyTracksEnabled);
4 ariadna 36
 
5 ariadna 37
            $('.reactions, #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
 
109
    return {
110
        init: function(envconf, trackcolors) {
111
 
112
            manageElementsVisibility();
113
 
114
            manageEnableDisableButtons();
115
 
5 ariadna 116
            // Difficulty track change.
117
            $('.moduletrackselect select').change(function() {
118
                $('#track_' + $(this).data('id')).css({
119
                    'background-color': trackcolors[$(this).val()] // Change track color.
120
                });
121
            }).change(); // Update track colors once on page load.
4 ariadna 122
 
123
            // Custom emoji deletion.
124
            buttonWithAjaxCall(
125
                    $('#delete_custom_pix'),
126
                    'deleteemojiconfirmation',
127
                    'delete_custom_pix',
128
                    {
129
                        contextid: envconf.contextid,
130
                        courseid: envconf.courseid,
131
                        draftitemid: $('input[name="config_point_views_pix"]').val()
132
                    },
133
                    function() {
134
                        $('.pix-preview[data-source="custom"], #delete_custom_pix').remove(); // Remove emoji preview and button.
135
                        // Refresh draft area files.
136
                        // # For an unknown reason, the following instruction with jQuery does not work
137
                        // # (or at least does not trigger the expected listener).
5 ariadna 138
                        document.querySelector('#fitem_id_config_point_views_pix .fp-path-folder-name').click();
4 ariadna 139
                    }
140
            );
141
 
142
            // Update current emoji on emoji change.
143
            $('[name=config_pixselect]').change(function() {
144
                var newsource = $(this).val();
145
                $('img.currentpix').each(function() {
146
                    var $img = $('img[data-source="' + newsource + '"][data-reaction="' + $(this).data('reaction') + '"]');
147
                    if ($img.length == 1 && $img.attr('src') > '') {
148
                        $(this).attr('src', $img.attr('src'));
149
                    }
150
                });
151
            });
152
 
153
            // Course reactions cleanup.
154
            buttonWithAjaxCall(
155
                    $('#cleanup_reactions'),
156
                    'cleanupreactionsconfirmation',
157
                    'update_db',
158
                    {
159
                        func: 'cleanup',
160
                        courseid: envconf.courseid
161
                    },
162
                    function() {
163
                        notification.alert(M.util.get_string('info', 'moodle'),
164
                                M.util.get_string('reactionscleanedupsuccessfully', 'block_point_view'),
165
                                M.util.get_string('ok', 'moodle'));
166
                    }
167
            );
168
 
169
            // Course reactions reset.
170
            buttonWithAjaxCall(
171
                    $('#reset_reactions'),
172
                    'resetreactionsconfirmation',
173
                    'update_db',
174
                    {
175
                        func: 'reset',
176
                        courseid: envconf.courseid
177
                    },
178
                    function() {
179
                        notification.alert(M.util.get_string('info', 'moodle'),
180
                                M.util.get_string('reactionsresetsuccessfully', 'block_point_view'),
181
                                M.util.get_string('ok', 'moodle'));
182
                    }
183
            );
184
 
185
            // Module reactions reset.
186
            $('[data-role=reset_module').each(function() {
187
                var cmid = $(this).data('cmid');
188
                buttonWithAjaxCall(
189
                        $(this),
190
                        'resetreactionsonmoduleconfirmation',
191
                        'update_db',
192
                        {
193
                            func: 'reset',
194
                            courseid: envconf.courseid,
195
                            cmid: cmid
196
                        },
197
                        function() {
198
                            notification.alert(M.util.get_string('info', 'moodle'),
199
                                    M.util.get_string('reactionsresetsuccessfully', 'block_point_view'),
200
                                    M.util.get_string('ok', 'moodle'));
201
                        }
202
                );
203
            });
5 ariadna 204
        }
4 ariadna 205
    };
206
});