Completed
Pull Request — master (#47)
by Jesus
02:19
created

M.mod_bigbluebuttonbn.modform.isFeatureEnabled   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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
/** global: M */
17
/** global: Y */
18
19
M.mod_bigbluebuttonbn = M.mod_bigbluebuttonbn || {};
20
21
M.mod_bigbluebuttonbn.modform = {
22
23
    bigbluebuttonbn: {},
24
    strings: {},
25
26
    /**
27
     * Initialise the broker code.
28
     *
29
     * @method init
30
     * @param {object} bigbluebuttonbn
31
     */
32
    init: function(bigbluebuttonbn) {
33
        this.bigbluebuttonbn = bigbluebuttonbn;
34
        this.strings = {
35
            as: M.util.get_string('mod_form_field_participant_list_text_as', 'bigbluebuttonbn'),
36
            viewer: M.util.get_string('mod_form_field_participant_bbb_role_viewer', 'bigbluebuttonbn'),
37
            moderator: M.util.get_string('mod_form_field_participant_bbb_role_moderator', 'bigbluebuttonbn'),
38
            remove: M.util.get_string('mod_form_field_participant_list_action_remove', 'bigbluebuttonbn')
39
        };
40
        this.updateInstanceTypeProfile();
41
        this.participantListInit();
42
    },
43
44
    updateInstanceTypeProfile: function() {
45
        var selectedType, profileType;
46
        selectedType = Y.one('#id_type');
47
        profileType = this.bigbluebuttonbn.instance_type_room_only;
48
        if (selectedType !== null) {
49
            profileType = selectedType.get('value');
50
        }
51
        this.applyInstanceTypeProfile(profileType);
52
    },
53
54
    applyInstanceTypeProfile: function(profileType) {
55
        var showAll = this.isFeatureEnabled(profileType, 'all');
56
        // Show room settings validation.
57
        this.showFieldset('id_room', showAll ||
58
                          this.isFeatureEnabled(profileType, 'showroom'));
59
        this.showInput('id_record', showAll ||
60
                       this.isFeatureEnabled(profileType, 'showrecordings'));
61
        // Show recordings settings validation.
62
        this.showFieldset('id_recordings', showAll ||
63
                          this.isFeatureEnabled(profileType, 'showrecordings'));
64
        // Show recordings imported settings validation.
65
        this.showInput('id_recordings_imported', showAll ||
66
                       this.isFeatureEnabled(profileType, 'showrecordings'));
67
        // Preuploadpresentation feature validation.
68
        this.showFieldset('id_preuploadpresentation', showAll ||
69
                          this.isFeatureEnabled(profileType, 'preuploadpresentation'));
70
        // Participants feature validation.
71
        this.showFieldset('id_permissions', showAll ||
72
                          this.isFeatureEnabled(profileType, 'permissions'));
73
        // Schedule feature validation.
74
        this.showFieldset('id_schedule', showAll ||
75
                          this.isFeatureEnabled(profileType, 'schedule'));
76
        // Common module settings validation.
77
        this.showFieldset('id_modstandardelshdr', showAll ||
78
                          this.isFeatureEnabled(profileType, 'modstandardelshdr'));
79
        // Restrict access validation.
80
        this.showFieldset('id_availabilityconditionsheader', showAll ||
81
                          this.isFeatureEnabled(profileType, 'availabilityconditionsheader'));
82
        // Tags validation.
83
        this.showFieldset('id_tagshdr', showAll || this.isFeatureEnabled(profileType, 'tagshdr'));
84
        // Competencies validation.
85
        this.showFieldset('id_competenciessection', showAll ||
86
                          this.isFeatureEnabled(profileType, 'competenciessection'));
87
    },
88
89
    isFeatureEnabled: function(profileType, feature) {
90
        var features = this.bigbluebuttonbn.instance_type_profiles[profileType].features;
91
        return(features.indexOf(feature) != -1);
92
    },
93
94
    showFieldset: function(id, show) {
95
        // Show room settings validation.
96
        var fieldset = Y.DOM.byId(id);
97
        if (!fieldset) {
98
            return;
99
        }
100
        if (show) {
101
            Y.DOM.setStyle(fieldset, 'display', 'block');
102
            return;
103
        }
104
        Y.DOM.setStyle(fieldset, 'display', 'none');
105
    },
106
107
    showInput: function(id, show) {
108
        // Show room settings validation.
109
        var inputset = Y.DOM.byId(id);
110
        if (!inputset) {
111
            return;
112
        }
113
        var node = Y.one(inputset).ancestor('div').ancestor('div');
114
        if (show) {
115
            node.setStyle('display', 'block');
116
            return;
117
        }
118
        node.setStyle('display', 'none');
119
    },
120
121
    participantSelectionSet: function() {
122
        this.selectClear('bigbluebuttonbn_participant_selection');
123
        var type = document.getElementById('bigbluebuttonbn_participant_selection_type');
124
        for (var i = 0; i < type.options.length; i++) {
125
            if (type.options[i].selected) {
126
                var options = this.bigbluebuttonbn.participant_data[type.options[i].value].children;
127
                for (var option in options) {
128
                    if (options.hasOwnProperty(option)) {
129
                        this.selectAddOption(
130
                            'bigbluebuttonbn_participant_selection', options[option].name, options[option].id
131
                        );
132
                    }
133
                }
134
                if (type.options[i].value === 'all') {
135
                    this.selectAddOption('bigbluebuttonbn_participant_selection',
136
                        '---------------', 'all');
137
                    this.selectDisable('bigbluebuttonbn_participant_selection');
138
                } else {
139
                    this.selectEnable('bigbluebuttonbn_participant_selection');
140
                }
141
            }
142
        }
143
    },
144
145
    participantListInit: function() {
146
        var selectionTypeValue, selectionValue, selectionRole, participantSelectionTypes;
147
        for (var i = 0; i < this.bigbluebuttonbn.participant_list.length; i++) {
148
            selectionTypeValue = this.bigbluebuttonbn.participant_list[i].selectiontype;
149
            selectionValue = this.bigbluebuttonbn.participant_list[i].selectionid;
150
            selectionRole = this.bigbluebuttonbn.participant_list[i].role;
151
            participantSelectionTypes = this.bigbluebuttonbn.participant_data[selectionTypeValue];
152
            if (selectionTypeValue != 'all' && typeof participantSelectionTypes.children[selectionValue] == 'undefined') {
153
                // Remove from memory.
154
                this.participantRemoveFromMemory(selectionTypeValue, selectionValue);
155
                continue;
156
            }
157
            // Add it to the form.
158
            this.participantAddToForm(selectionTypeValue, selectionValue, selectionRole);
159
        }
160
        // Update in the form.
161
        this.participantListUpdate();
162
    },
163
164
    participantListUpdate: function() {
165
        var participantList = document.getElementsByName('participants')[0];
166
        participantList.value = JSON.stringify(this.bigbluebuttonbn.participant_list).replace(/"/g, '&quot;');
167
    },
168
169
    participantRemove: function(selectionTypeValue, selectionValue) {
170
        // Remove from memory.
171
        this.participantRemoveFromMemory(selectionTypeValue, selectionValue);
172
173
        // Remove from the form.
174
        this.participantRemoveFromForm(selectionTypeValue, selectionValue);
175
176
        // Update in the form.
177
        this.participantListUpdate();
178
    },
179
180
    participantRemoveFromMemory: function(selectionTypeValue, selectionValue) {
181
        var selectionid = (selectionValue === '' ? null : selectionValue);
182
        for (var i = 0; i < this.bigbluebuttonbn.participant_list.length; i++) {
183
            if (this.bigbluebuttonbn.participant_list[i].selectiontype == selectionTypeValue &&
184
                this.bigbluebuttonbn.participant_list[i].selectionid == selectionid) {
185
                this.bigbluebuttonbn.participant_list.splice(i, 1);
186
            }
187
        }
188
    },
189
190
    participantRemoveFromForm: function(selectionTypeValue, selectionValue) {
191
        var id = 'participant_list_tr_' + selectionTypeValue + '-' + selectionValue;
192
        var participantListTable = document.getElementById('participant_list_table');
193
        for (var i = 0; i < participantListTable.rows.length; i++) {
194
            if (participantListTable.rows[i].id == id) {
195
                participantListTable.deleteRow(i);
196
            }
197
        }
198
    },
199
200
    participantAdd: function() {
201
        var selectionType = document.getElementById('bigbluebuttonbn_participant_selection_type');
202
        var selection = document.getElementById('bigbluebuttonbn_participant_selection');
203
        // Lookup to see if it has been added already.
204
        for (var i = 0; i < this.bigbluebuttonbn.participant_list.length; i++) {
205
            if (this.bigbluebuttonbn.participant_list[i].selectiontype == selectionType.value &&
206
                this.bigbluebuttonbn.participant_list[i].selectionid == selection.value) {
207
                return;
208
            }
209
        }
210
        // Add it to memory.
211
        this.participantAddToMemory(selectionType.value, selection.value);
212
        // Add it to the form.
213
        this.participantAddToForm(selectionType.value, selection.value, 'viewer');
214
        // Update in the form.
215
        this.participantListUpdate();
216
    },
217
218
    participantAddToMemory: function(selectionTypeValue, selectionValue) {
219
        this.bigbluebuttonbn.participant_list.push({
220
            "selectiontype": selectionTypeValue,
221
            "selectionid": selectionValue,
222
            "role": "viewer"
223
        });
224
    },
225
226
    participantAddToForm: function(selectionTypeValue, selectionValue, selectionRole) {
227
        var listTable, innerHTML, selectedHtml, removeHtml, removeClass, bbbRoles, i, row, cell0, cell1, cell2, cell3;
228
        listTable = document.getElementById('participant_list_table');
229
        row = listTable.insertRow(listTable.rows.length);
230
        row.id = "participant_list_tr_" + selectionTypeValue + "-" + selectionValue;
231
        cell0 = row.insertCell(0);
232
        cell0.width = "125px";
233
        cell0.innerHTML = '<b><i>' + this.bigbluebuttonbn.participant_data[selectionTypeValue].name;
234
        cell0.innerHTML += (selectionTypeValue !== 'all' ? ':&nbsp;' : '') + '</i></b>';
235
        cell1 = row.insertCell(1);
236
        cell1.innerHTML = '';
237
        if (selectionTypeValue !== 'all') {
238
            cell1.innerHTML = this.bigbluebuttonbn.participant_data[selectionTypeValue].children[selectionValue].name;
239
        }
240
        innerHTML = '&nbsp;<i>' + this.strings.as + '</i>&nbsp;';
241
        innerHTML += '<select id="participant_list_role_' + selectionTypeValue + '-' + selectionValue + '"';
242
        innerHTML += ' onchange="M.mod_bigbluebuttonbn.modform.participantListRoleUpdate(\'';
243
        innerHTML += selectionTypeValue + '\', \'' + selectionValue;
244
        innerHTML += '\'); return 0;" class="select custom-select">';
245
        bbbRoles = ['viewer', 'moderator'];
246
        for (i = 0; i < bbbRoles.length; i++) {
247
            selectedHtml = '';
248
            if (bbbRoles[i] === selectionRole) {
249
                selectedHtml = ' selected="selected"';
250
            }
251
            innerHTML += '<option value="' + bbbRoles[i] + '"' + selectedHtml + '>' + this.strings[bbbRoles[i]] + '</option>';
252
        }
253
        innerHTML += '</select>';
254
        cell2 = row.insertCell(2);
255
        cell2.innerHTML = innerHTML;
256
        cell3 = row.insertCell(3);
257
        cell3.width = "20px";
258
        removeHtml = this.strings.remove;
259
        removeClass = "btn btn-secondary btn-sm";
260
        if (this.bigbluebuttonbn.icons_enabled) {
261
            removeHtml = this.bigbluebuttonbn.pix_icon_delete;
262
            removeClass = "btn btn-link";
263
        }
264
        innerHTML = '<a class="' + removeClass + '" onclick="M.mod_bigbluebuttonbn.modform.participantRemove(\'';
265
        innerHTML += selectionTypeValue + '\', \'' + selectionValue;
266
        innerHTML += '\'); return 0;" title="' + this.strings.remove + '">' + removeHtml + '</a>';
267
        cell3.innerHTML = innerHTML;
268
    },
269
270
    participantListRoleUpdate: function(type, id) {
271
        // Update in memory.
272
        var participantListRoleSelection = document.getElementById('participant_list_role_' + type + '-' + id);
273
        for (var i = 0; i < this.bigbluebuttonbn.participant_list.length; i++) {
274
            if (this.bigbluebuttonbn.participant_list[i].selectiontype == type &&
275
                this.bigbluebuttonbn.participant_list[i].selectionid == (id === '' ? null : id)) {
276
                this.bigbluebuttonbn.participant_list[i].role = participantListRoleSelection.value;
277
            }
278
        }
279
280
        // Update in the form.
281
        this.participantListUpdate();
282
    },
283
284
    selectClear: function(id) {
285
        var select = document.getElementById(id);
286
        while (select.length > 0) {
287
            select.remove(select.length - 1);
288
        }
289
    },
290
291
    selectEnable: function(id) {
292
        var select = document.getElementById(id);
293
        select.disabled = false;
294
    },
295
296
    selectDisable: function(id) {
297
        var select = document.getElementById(id);
298
        select.disabled = true;
299
    },
300
301
    selectAddOption: function(id, text, value) {
302
        var select = document.getElementById(id);
303
        var option = document.createElement('option');
304
        option.text = text;
305
        option.value = value;
306
        select.add(option, option.length);
307
    }
308
};
309