Completed
Pull Request — master (#42)
by Jesus
02:06
created

M.mod_bigbluebuttonbn.rooms.initJoinButton   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 22
rs 9.2
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.rooms = {
22
23
    datasource: null,
24
    bigbluebuttonbn: {},
25
    panel: null,
26
    pinginterval: null,
27
28
    /**
29
     * Initialise the broker code.
30
     *
31
     * @method init
32
     */
33
    init: function(bigbluebuttonbn) {
34
        this.datasource = new Y.DataSource.Get({
35
            source: M.cfg.wwwroot + "/mod/bigbluebuttonbn/bbb_broker.php?"
36
        });
37
        this.bigbluebuttonbn = bigbluebuttonbn;
38
        this.pinginterval = bigbluebuttonbn.ping_interval;
39
        if (this.pinginterval === 0) {
40
            this.pinginterval = 10000;
41
        }
42
        if (this.bigbluebuttonbn.profile_features.includes('all') || this.bigbluebuttonbn.profile_features.includes('showroom')) {
43
            this.initRoom();
44
        }
45
    },
46
47
    initRoom: function() {
48
        if (this.bigbluebuttonbn.activity !== 'open') {
49
            var statusBar = [M.util.get_string('view_message_conference_has_ended', 'bigbluebuttonbn')];
50
            if (this.bigbluebuttonbn.activity !== 'ended') {
51
                statusBar = [
52
                    M.util.get_string('view_message_conference_not_started', 'bigbluebuttonbn'),
53
                    this.bigbluebuttonbn.opening,
54
                    this.bigbluebuttonbn.closing
55
                  ];
56
            }
57
            Y.DOM.addHTML(Y.one('#status_bar'), this.initStatusBar(statusBar));
58
            return;
59
        }
60
        this.updateRoom();
61
    },
62
63
    updateRoom: function(f) {
64
        var forced = 'false';
65
        if (typeof f !== 'undefined' && f) {
66
            forced = 'true';
67
        }
68
        var id = this.bigbluebuttonbn.meetingid;
69
        var bnid = this.bigbluebuttonbn.bigbluebuttonbnid;
70
        this.datasource.sendRequest({
71
            request: 'action=meeting_info&id=' + id + '&bigbluebuttonbn=' + bnid + '&forced=' + forced,
72
            callback: {
73
                success: function(e) {
74
                    Y.DOM.addHTML(Y.one('#status_bar'),
75
                        M.mod_bigbluebuttonbn.rooms.initStatusBar(e.data.status.message));
76
                    Y.DOM.addHTML(Y.one('#control_panel'),
77
                        M.mod_bigbluebuttonbn.rooms.initControlPanel(e.data));
78
                    if (typeof e.data.status.can_join != 'undefined') {
79
                        Y.DOM.addHTML(Y.one('#join_button'),
80
                            M.mod_bigbluebuttonbn.rooms.initJoinButton(e.data.status));
81
                    }
82
                    if (typeof e.data.status.can_end != 'undefined' && e.data.status.can_end) {
83
                        Y.DOM.addHTML(Y.one('#end_button'),
84
                            M.mod_bigbluebuttonbn.rooms.initEndButton(e.data.status));
85
                    }
86
                    if (!e.data.status.can_join) {
87
                        M.mod_bigbluebuttonbn.rooms.waitModerator({
88
                            id: id,
89
                            bnid: bnid
90
                        });
91
                    }
92
                }
93
            }
94
        });
95
    },
96
97
    initStatusBar: function(statusMessage) {
98
        var statusBarSpan = Y.DOM.create('<span id="status_bar_span">');
99
        if (statusMessage.constructor !== Array) {
100
            Y.DOM.setText(statusBarSpan, statusMessage);
101
            return statusBarSpan;
102
        }
103
        for (var message in statusMessage) {
104
            if (!statusMessage.hasOwnProperty(message)) {
105
                continue; // Skip keys from the prototype.
106
            }
107
            var statusBarSpanSpan = Y.DOM.create('<span id="status_bar_span_span">');
108
            Y.DOM.setText(statusBarSpanSpan, statusMessage[message]);
109
            Y.DOM.addHTML(statusBarSpan, statusBarSpanSpan);
110
            Y.DOM.addHTML(statusBarSpan, Y.DOM.create('<br>'));
111
        }
112
        return statusBarSpan;
113
    },
114
115
    initControlPanel: function(data) {
116
        var controlPanelDiv = Y.DOM.create('<div>');
117
        Y.DOM.setAttribute(controlPanelDiv, 'id', 'control_panel_div');
118
        var controlPanelDivHtml = '';
119
        if (data.running) {
120
            controlPanelDivHtml += this.msgStartedAt(data.info.startTime) + ' ';
121
            controlPanelDivHtml += this.msgAttendeesIn(data.info.moderatorCount, data.info.participantCount);
122
        }
123
        Y.DOM.addHTML(controlPanelDiv, controlPanelDivHtml);
124
        return (controlPanelDiv);
125
    },
126
127
    msgStartedAt: function(startTime) {
128
        var startTimestamp = (parseInt(startTime, 10) - parseInt(startTime, 10) % 1000);
129
        var date = new Date(startTimestamp);
130
        var hours = date.getHours();
131
        var minutes = date.getMinutes();
132
        var started_at = M.util.get_string('view_message_session_started_at', 'bigbluebuttonbn');
133
        return started_at + ' <b>' + hours + ':' + (minutes < 10 ? '0' : '') + minutes + '</b>.';
134
    },
135
136
    msgModeratorsIn: function(moderators) {
137
        var msgModerators = M.util.get_string('view_message_moderators', 'bigbluebuttonbn');
138
        if (moderators == 1) {
139
            msgModerators = M.util.get_string('view_message_moderator', 'bigbluebuttonbn');
140
        }
141
        return msgModerators;
142
    },
143
144
    msgViewersIn: function(viewers) {
145
        var msgViewers = M.util.get_string('view_message_viewers', 'bigbluebuttonbn');
146
        if (viewers == 1) {
147
            msgViewers = M.util.get_string('view_message_viewer', 'bigbluebuttonbn');
148
        }
149
        return msgViewers;
150
    },
151
152
    msgAttendeesIn: function(moderators, participants) {
153
        var msgModerators, viewers, msgViewers, msg;
154
        if (!this.hasParticipants(participants)) {
155
            return M.util.get_string('view_message_session_no_users', 'bigbluebuttonbn') + '.';
156
        }
157
        msgModerators = this.msgModeratorsIn(moderators);
158
        viewers = participants - moderators;
159
        msgViewers = this.msgViewersIn(viewers);
160
        msg = M.util.get_string('view_message_session_has_users', 'bigbluebuttonbn');
161
        if (participants > 1) {
162
            return msg + ' <b>' + moderators + '</b> ' + msgModerators + ' and <b>' + viewers + '</b> ' + msgViewers + '.';
163
        }
164
        msg = M.util.get_string('view_message_session_has_user', 'bigbluebuttonbn');
165
        if (moderators > 0) {
166
            return msg + ' <b>1</b> ' + msgModerators + '.';
167
        }
168
        return msg + ' <b>1</b> ' + msgViewers + '.';
169
    },
170
171
    hasParticipants: function(participants) {
172
        return (typeof participants != 'undefined' && participants > 0);
173
    },
174
175
    initJoinButton: function(status) {
176
        var joinButtonInput = Y.DOM.create('<input>');
177
        Y.DOM.setAttribute(joinButtonInput, 'id', 'join_button_input');
178
        Y.DOM.setAttribute(joinButtonInput, 'type', 'button');
179
        Y.DOM.setAttribute(joinButtonInput, 'value', status.join_button_text);
180
        Y.DOM.setAttribute(joinButtonInput, 'class', 'btn btn-primary');
181
        var input_html = 'M.mod_bigbluebuttonbn.rooms.join(\'' + status.join_url + '\');';
182
        Y.DOM.setAttribute(joinButtonInput, 'onclick', input_html);
183
        if (!status.can_join) {
184
            // Disable join button.
185
            Y.DOM.setAttribute(joinButtonInput, 'disabled', true);
186
            var statusBarSpan = Y.one('#status_bar_span');
187
            // Create a img element.
188
            var spinning_wheel = Y.DOM.create('<img>');
189
            Y.DOM.setAttribute(spinning_wheel, 'id', 'spinning_wheel');
190
            Y.DOM.setAttribute(spinning_wheel, 'src', 'pix/i/processing16.gif');
191
            // Add the spinning wheel.
192
            Y.DOM.addHTML(statusBarSpan, '&nbsp;');
193
            Y.DOM.addHTML(statusBarSpan, spinning_wheel);
194
        }
195
        return joinButtonInput;
196
    },
197
198
    initEndButton: function(status) {
199
        var endButtonInput = Y.DOM.create('<input>');
200
        Y.DOM.setAttribute(endButtonInput, 'id', 'end_button_input');
201
        Y.DOM.setAttribute(endButtonInput, 'type', 'button');
202
        Y.DOM.setAttribute(endButtonInput, 'value', status.end_button_text);
203
        Y.DOM.setAttribute(endButtonInput, 'class', 'btn btn-secondary');
204
        if (status.can_end) {
205
            Y.DOM.setAttribute(endButtonInput, 'onclick', 'M.mod_bigbluebuttonbn.broker.endMeeting();');
206
        }
207
        return endButtonInput;
208
    },
209
210
    endMeeting: function() {
211
        Y.one('#control_panel_div').remove();
212
        Y.one('#join_button').hide();
213
        Y.one('#end_button').hide();
214
    },
215
216
    remoteUpdate: function(delay) {
217
        setTimeout(function() {
218
            M.mod_bigbluebuttonbn.rooms.cleanRoom();
219
            M.mod_bigbluebuttonbn.rooms.updateRoom(true);
220
        }, delay);
221
    },
222
223
    cleanRoom: function() {
224
        Y.one('#status_bar_span').remove();
225
        Y.one('#control_panel_div').remove();
226
        Y.one('#join_button').setContent('');
227
        Y.one('#end_button').setContent('');
228
    },
229
230
    windowClose: function() {
231
        window.onunload = function() {
232
            /** global: opener */
233
            opener.M.mod_bigbluebuttonbn.rooms.remoteUpdate(5000);
234
        };
235
        window.close();
236
    },
237
238
    waitModerator: function(payload) {
239
        this.datasource.sendRequest({
240
            request: "action=meeting_info&id=" + payload.id + "&bigbluebuttonbn=" + payload.bnid,
241
            callback: {
242
                success: function(e) {
243
                    if (e.data.running) {
244
                        M.mod_bigbluebuttonbn.rooms.cleanRoom();
245
                        M.mod_bigbluebuttonbn.rooms.updateRoom();
246
                    }
247
                    return setTimeout(((function() {
248
                        return function() {
249
                            M.mod_bigbluebuttonbn.rooms.waitModerator(payload);
250
                        };
251
                    })(this)), M.mod_bigbluebuttonbn.rooms.pinginterval);
252
                },
253
                failure: function(e) {
254
                    payload.message = e.error.message;
255
                }
256
            }
257
        });
258
    },
259
260
    join: function(joinUrl) {
261
        M.mod_bigbluebuttonbn.broker.joinRedirect(joinUrl);
262
        // Update view.
263
        setTimeout(function() {
264
            M.mod_bigbluebuttonbn.rooms.cleanRoom();
265
            M.mod_bigbluebuttonbn.rooms.updateRoom(true);
266
        }, 15000);
267
    }
268
};
269