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
 * JavaScript for the add_random_form class.
18
 *
19
 * @module    mod_quiz/add_random_form
20
 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
21
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(
24
    [
25
        'jquery',
26
        'mod_quiz/random_question_form_preview'
27
    ],
28
    function(
29
        $,
30
        RandomQuestionFormPreview
31
    ) {
32
 
33
    // Wait 2 seconds before reloading the question set just in case
34
    // the user is still changing the criteria.
35
    var RELOAD_DELAY = 2000;
36
    var SELECTORS = {
37
        PREVIEW_CONTAINER: '[data-region="random-question-preview-container"]',
38
        CATEGORY_FORM_ELEMENT: '[name="category"]',
39
        SUBCATEGORY_FORM_ELEMENT: '[name="includesubcategories"]',
40
        TAG_IDS_FORM_ELEMENT: '[name="fromtags[]"]'
41
    };
42
 
43
    /**
44
     * Get the selected category value from the form.
45
     *
46
     * @param {jquery} form The form element.
47
     * @return {string} The category value.
48
     */
49
    var getCategorySelectValue = function(form) {
50
        return form.find(SELECTORS.CATEGORY_FORM_ELEMENT).val();
51
    };
52
 
53
    /**
54
     * Get the category id from the form.
55
     *
56
     * @param {jquery} form The form element.
57
     * @return {string} The category id.
58
     */
59
    var getCategoryId = function(form) {
60
        // The value string is the category id and category context id joined
61
        // by a comma.
62
        var valueString = getCategorySelectValue(form);
63
        // Split the two ids.
64
        var values = valueString.split(',');
65
        // Return just the category id.
66
        return values[0];
67
    };
68
 
69
    /**
70
     * Check if a top level category is selected in the form.
71
     *
72
     * @param {jquery} form The form element.
73
     * @param {string[]} topCategories List of top category values (matching the select box values)
74
     * @return {bool}
75
     */
76
    var isTopLevelCategorySelected = function(form, topCategories) {
77
        var selectedValue = getCategorySelectValue(form);
78
        return (topCategories.indexOf(selectedValue) > -1);
79
    };
80
 
81
    /**
82
     * Check if the form indicates we should include include subcategories in
83
     * the filter.
84
     *
85
     * @param {jquery} form The form element.
86
     * @param {string[]} topCategories List of top category values (matching the select box values)
87
     * @return {bool}
88
     */
89
    var shouldIncludeSubcategories = function(form, topCategories) {
90
        if (isTopLevelCategorySelected(form, topCategories)) {
91
            return true;
92
        } else {
93
            return form.find(SELECTORS.SUBCATEGORY_FORM_ELEMENT).is(':checked');
94
        }
95
    };
96
 
97
    /**
98
     * Get the tag ids for the selected tags in the form.
99
     *
100
     * @param {jquery} form The form element.
101
     * @return {string[]} The tag ids.
102
     */
103
    var getTagIds = function(form) {
104
        var values = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT).val();
105
        return values.map(function(value) {
106
            // The tag element value is the tag id and tag name joined
107
            // by a comma. So we need to split them to get the tag id.
108
            var parts = value.split(',');
109
            return parts[0];
110
        });
111
    };
112
 
113
    /**
114
     * Reload the preview section with a new set of filters.
115
     *
116
     * @param {jquery} form The form element.
117
     * @param {int} contextId The current context id.
118
     * @param {string[]} topCategories List of top category values (matching the select box values)
119
     */
120
    var reloadQuestionPreview = function(form, contextId, topCategories) {
121
        var previewContainer = form.find(SELECTORS.PREVIEW_CONTAINER);
122
        RandomQuestionFormPreview.reload(
123
            previewContainer,
124
            getCategoryId(form),
125
            shouldIncludeSubcategories(form, topCategories),
126
            getTagIds(form),
127
            contextId
128
        );
129
    };
130
 
131
    /**
132
     * Is this an element we're interested in listening to changes on.
133
     *
134
     * @param {jquery} element The element to check.
135
     * @return {bool}
136
     */
137
    var isInterestingElement = function(element) {
138
        if (element.closest(SELECTORS.CATEGORY_FORM_ELEMENT).length > 0) {
139
            return true;
140
        }
141
 
142
        if (element.closest(SELECTORS.SUBCATEGORY_FORM_ELEMENT).length > 0) {
143
            return true;
144
        }
145
 
146
        if (element.closest(SELECTORS.TAG_IDS_FORM_ELEMENT).length > 0) {
147
            return true;
148
        }
149
 
150
        return false;
151
    };
152
 
153
    /**
154
     * Listen for changes to any of the interesting elements and reload the form
155
     * preview with the new filter values if they are changed.
156
     *
157
     * The reload is delayed for a small amount of time (see RELOAD_DELAY) in case
158
     * the user is actively editing the form. This allows us to avoid having to
159
     * send multiple requests to the server on each change.
160
     *
161
     * Instead we can just send a single request when the user appears to have
162
     * finished editing the form.
163
     *
164
     * @param {jquery} form The form element.
165
     * @param {int} contextId The current context id.
166
     * @param {string[]} topCategories List of top category values (matching the select box values)
167
     */
168
    var addEventListeners = function(form, contextId, topCategories) {
169
        var reloadTimerId = null;
170
        const tagsFilter = form.find(SELECTORS.TAG_IDS_FORM_ELEMENT);
171
 
172
        form.add(tagsFilter).on('change', function(e) {
173
            // Only reload the preview when elements that will change the result
174
            // are modified.
175
            if (!isInterestingElement($(e.target))) {
176
                return;
177
            }
178
 
179
            // Show the loading icon to let the user know that the preview
180
            // will be updated after their actions.
181
            RandomQuestionFormPreview.showLoadingIcon(form);
182
 
183
            if (reloadTimerId) {
184
                // Reset the timer each time the form is modified.
185
                clearTimeout(reloadTimerId);
186
            }
187
 
188
            // Don't immediately reload the question preview section just
189
            // in case the user is still modifying the form. We don't want to
190
            // spam reload requests.
191
            reloadTimerId = setTimeout(function() {
192
                reloadQuestionPreview(form, contextId, topCategories);
193
            }, RELOAD_DELAY);
194
        });
195
    };
196
 
197
    /**
198
     * Trigger the first load of the preview section and then listen for modifications
199
     * to the form to reload the preview with new filter values.
200
     *
201
     * @param {jquery} formId The form element id.
202
     * @param {int} contextId The current context id.
203
     * @param {string[]} topCategories List of top category values (matching the select box values)
204
     * @param {bool} isTagsEnabled Whether tags feature is enabled or not.
205
     */
206
    var init = function(formId, contextId, topCategories, isTagsEnabled) {
207
         if (isTagsEnabled == true) {
208
             var form = $('#' + formId);
209
             reloadQuestionPreview(form, contextId, topCategories, isTagsEnabled);
210
             addEventListeners(form, contextId, topCategories, isTagsEnabled);
211
         }
212
    };
213
 
214
    return {
215
        init: init
216
    };
217
});