bbb_view.php ➔ bigbluebuttonbn_bbb_view_join_meeting()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 3
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * View for BigBlueButton interaction.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2010 onwards, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
24
 */
25
26
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
27
require_once(dirname(__FILE__).'/locallib.php');
28
29
global $SESSION;
30
31
$action = required_param('action', PARAM_TEXT);
32
$id = optional_param('id', 0, PARAM_INT);
33
$bn = optional_param('bn', 0, PARAM_INT);
34
$href = optional_param('href', '', PARAM_TEXT);
35
$mid = optional_param('mid', '', PARAM_TEXT);
36
$rid = optional_param('rid', '', PARAM_TEXT);
37
$rtype = optional_param('rtype', 'presentation', PARAM_TEXT);
38
$errors = optional_param('errors', '', PARAM_TEXT);
39
$timeline = optional_param('timeline', 0, PARAM_INT);
40
$index = optional_param('index', 0, PARAM_INT);
41
$group = optional_param('group', -1, PARAM_INT);
42
43
$bbbviewinstance = bigbluebuttonbn_view_validator($id, $bn);
44
if (!$bbbviewinstance) {
45
    print_error(get_string('view_error_url_missing_parameters', 'bigbluebuttonbn'));
46
}
47
48
$cm = $bbbviewinstance['cm'];
49
$course = $bbbviewinstance['course'];
50
$bigbluebuttonbn = $bbbviewinstance['bigbluebuttonbn'];
51
$context = context_module::instance($cm->id);
52
53
require_login($course, true, $cm);
54
55
$bbbsession = null;
56
if (isset($SESSION->bigbluebuttonbn_bbbsession)) {
57
    $bbbsession = $SESSION->bigbluebuttonbn_bbbsession;
58
}
59
60
if ($timeline || $index) {
61
    // Validates if the BigBlueButton server is working.
62
    $serverversion = bigbluebuttonbn_get_server_version();
63
    if (is_null($serverversion)) {
64
        if ($bbbsession['administrator']) {
65
            print_error('view_error_unable_join', 'bigbluebuttonbn',
66
                $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
67
            exit;
68
        }
69
        if ($bbbsession['moderator']) {
70
            print_error('view_error_unable_join_teacher', 'bigbluebuttonbn',
71
                $CFG->wwwroot.'/course/view.php?id='.$bigbluebuttonbn->course);
72
            exit;
73
        }
74
        print_error('view_error_unable_join_student', 'bigbluebuttonbn',
75
            $CFG->wwwroot.'/course/view.php?id='.$bigbluebuttonbn->course);
76
        exit;
77
    }
78
79
    $bbbsession = mod_bigbluebuttonbn\locallib\bigbluebutton::build_bbb_session($cm, $course, $bigbluebuttonbn);
80
81
    // Check status and set extra values.
82
    $activitystatus = bigbluebuttonbn_view_get_activity_status($bbbsession);
83 View Code Duplication
    if ($activitystatus == 'ended') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
        $bbbsession['presentation'] = bigbluebuttonbn_get_presentation_array(
85
            $bbbsession['context'], $bbbsession['bigbluebuttonbn']->presentation);
86
    } else if ($activitystatus == 'open') {
87
        $bbbsession['presentation'] = bigbluebuttonbn_get_presentation_array(
88
            $bbbsession['context'], $bbbsession['bigbluebuttonbn']->presentation, $bbbsession['bigbluebuttonbn']->id);
89
    }
90
91
    // Check group.
92
    if ($group >= 0) {
93
        $bbbsession['group'] = $group;
94
        $groupname = get_string('allparticipants');
95
        if ($bbbsession['group'] != 0) {
96
            $groupname = groups_get_group_name($bbbsession['group']);
97
        }
98
99
        // Assign group default values.
100
        $bbbsession['meetingid'] .= '['.$bbbsession['group'].']';
101
        $bbbsession['meetingname'] .= ' ('.$groupname.')';
102
    }
103
104
    // Initialize session variable used across views.
105
    $SESSION->bigbluebuttonbn_bbbsession = $bbbsession;
106
}
107
108
// Print the page header.
109
$PAGE->set_context($context);
110
$PAGE->set_url('/mod/bigbluebuttonbn/bbb_view.php', array('id' => $cm->id, 'bigbluebuttonbn' => $bigbluebuttonbn->id));
111
$PAGE->set_title(format_string($bigbluebuttonbn->name));
112
$PAGE->set_cacheable(false);
113
$PAGE->set_heading($course->fullname);
114
$PAGE->blocks->show_only_fake_blocks();
115
116
switch (strtolower($action)) {
117
    case 'logout':
118
        if (isset($errors) && $errors != '') {
119
            bigbluebuttonbn_bbb_view_errors($errors, $id);
120
            break;
121
        }
122
        if (is_null($bbbsession)) {
123
            bigbluebuttonbn_bbb_view_close_window_manually();
124
            break;
125
        }
126
        // Moodle event logger: Create an event for meeting left.
127
        bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['meeting_left'], $bigbluebuttonbn);
128
        // Update the cache.
129
        $meetinginfo = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], BIGBLUEBUTTONBN_UPDATE_CACHE);
130
        // Check the origin page.
131
        $select = "userid = ? AND log = ?";
132
        $params = array(
133
                'userid' => $bbbsession['userID'],
134
                'log' => BIGBLUEBUTTONBN_LOG_EVENT_JOIN,
135
            );
136
        $accesses = $DB->get_records_select('bigbluebuttonbn_logs', $select, $params, 'id ASC', 'id, meta', 1);
137
        $lastaccess = end($accesses);
138
        $lastaccess = json_decode($lastaccess->meta);
139
        // If the user acceded from Timeline it should be redirected to the Dashboard.
140
        if (isset($lastaccess->origin) && $lastaccess->origin == BIGBLUEBUTTON_ORIGIN_TIMELINE) {
141
            redirect($CFG->wwwroot . '/my/');
142
        }
143
        // Close the tab or window where BBB was opened.
144
        bigbluebuttonbn_bbb_view_close_window();
145
        break;
146
    case 'join':
147
        if (is_null($bbbsession)) {
148
            print_error('view_error_unable_join', 'bigbluebuttonbn');
149
            break;
150
        }
151
        // Check the origin page.
152
        $origin = BIGBLUEBUTTON_ORIGIN_BASE;
153
        if ($timeline) {
154
            $origin = BIGBLUEBUTTON_ORIGIN_TIMELINE;
155
        } else if ($index) {
156
            $origin = BIGBLUEBUTTON_ORIGIN_INDEX;
157
        }
158
        // See if the session is in progress.
159
        if (bigbluebuttonbn_is_meeting_running($bbbsession['meetingid'])) {
160
            // Since the meeting is already running, we just join the session.
161
            bigbluebuttonbn_bbb_view_join_meeting($bbbsession, $bigbluebuttonbn, $origin);
162
            break;
163
        }
164
        // If user is not administrator nor moderator (user is steudent) and waiting is required.
165
        if (!$bbbsession['administrator'] && !$bbbsession['moderator'] && $bbbsession['wait']) {
166
            header('Location: '.$bbbsession['logoutURL']);
167
            break;
168
        }
169
        // As the meeting doesn't exist, try to create it.
170
        $response = bigbluebuttonbn_get_create_meeting_array(
171
            bigbluebuttonbn_bbb_view_create_meeting_data($bbbsession),
172
            bigbluebuttonbn_bbb_view_create_meeting_metadata($bbbsession),
173
            $bbbsession['presentation']['name'],
174
            $bbbsession['presentation']['url']
175
        );
176
        if (empty($response)) {
177
            // The server is unreachable.
178
            if ($bbbsession['administrator']) {
179
                print_error('view_error_unable_join', 'bigbluebuttonbn',
180
                    $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
181
                break;
182
            }
183
            if ($bbbsession['moderator']) {
184
                print_error('view_error_unable_join_teacher', 'bigbluebuttonbn',
185
                    $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
186
                break;
187
            }
188
            print_error('view_error_unable_join_student', 'bigbluebuttonbn',
189
                $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
190
            break;
191
        }
192
        if ($response['returncode'] == 'FAILED') {
193
            // The meeting was not created.
194
            if (!$printerrorkey) {
195
                print_error($response['message'], 'bigbluebuttonbn');
196
                break;
197
            }
198
            $printerrorkey = bigbluebuttonbn_get_error_key($response['messageKey'], 'view_error_create');
199
            print_error($printerrorkey, 'bigbluebuttonbn');
200
            break;
201
        }
202
        if ($response['hasBeenForciblyEnded'] == 'true') {
203
            print_error(get_string('index_error_forciblyended', 'bigbluebuttonbn'));
204
            break;
205
        }
206
        // Moodle event logger: Create an event for meeting created.
207
        bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['meeting_create'], $bigbluebuttonbn);
208
        // Internal logger: Insert a record with the meeting created.
209
        $overrides = array('meetingid' => $bbbsession['meetingid']);
210
        $meta = '{"record":'.($bbbsession['record'] ? 'true' : 'false').'}';
211
        bigbluebuttonbn_log($bbbsession['bigbluebuttonbn'], BIGBLUEBUTTONBN_LOG_EVENT_CREATE, $overrides, $meta);
212
        // Since the meeting is already running, we just join the session.
213
        bigbluebuttonbn_bbb_view_join_meeting($bbbsession, $bigbluebuttonbn, $origin);
214
        break;
215
    case 'play':
216
        $href = bigbluebuttonbn_bbb_view_playback_href($href, $mid, $rid, $rtype);
217
        // Moodle event logger: Create an event for meeting left.
218
        bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['recording_play'], $bigbluebuttonbn,
219
            ['other' => $rid]);
220
        // Internal logger: Instert a record with the playback played.
221
        $overrides = array('meetingid' => $bbbsession['meetingid']);
222
        bigbluebuttonbn_log($bbbsession['bigbluebuttonbn'], BIGBLUEBUTTONBN_LOG_EVENT_PLAYED, $overrides);
223
        // Execute the redirect.
224
        header('Location: '.urldecode($href));
225
        break;
226
    default:
227
        bigbluebuttonbn_bbb_view_close_window();
228
}
229
230
/**
231
 * Helper for getting the playback url that corresponds to an specific type.
232
 *
233
 * @param  string   $href
234
 * @param  string   $mid
235
 * @param  string   $rid
236
 * @param  string   $rtype
237
 * @return string
238
 */
239
function bigbluebuttonbn_bbb_view_playback_href($href, $mid, $rid, $rtype) {
240
    if ($href != '' || $mid == '' || $rid == '') {
241
        return $href;
242
    }
243
    $recordings = bigbluebuttonbn_get_recordings_array($mid, $rid);
244
    if (empty($recordings)) {
245
        return '';
246
    }
247
    return bigbluebuttonbn_bbb_view_playback_href_lookup($recordings[$rid]['playbacks'], $rtype);
248
}
249
250
/**
251
 * Helper for looking up playback url in the recording playback array.
252
 *
253
 * @param  array    $playbacks
254
 * @param  string   $type
255
 * @return string
256
 */
257
function bigbluebuttonbn_bbb_view_playback_href_lookup($playbacks, $type) {
258
    foreach ($playbacks as $playback) {
259
        if ($playback['type'] == $type) {
260
            return $playback['url'];
261
        }
262
    }
263
    return '';
264
}
265
266
/**
267
 * Helper for closing the tab or window when the user lefts the meeting.
268
 *
269
 * @return string
270
 */
271
function bigbluebuttonbn_bbb_view_close_window() {
272
    global $OUTPUT, $PAGE;
273
    echo $OUTPUT->header();
274
    $PAGE->requires->yui_module('moodle-mod_bigbluebuttonbn-rooms', 'M.mod_bigbluebuttonbn.rooms.windowClose');
275
    echo $OUTPUT->footer();
276
}
277
278
/**
279
 * Helper for showing a message when the tab or window can not be closed.
280
 *
281
 * @return string
282
 */
283
function bigbluebuttonbn_bbb_view_close_window_manually() {
284
    echo get_string('view_message_tab_close', 'bigbluebuttonbn');
285
}
286
287
/**
288
 * Helper for preparing data used for creating the meeting.
289
 *
290
 * @param  array    $bbbsession
291
 * @return object
292
 */
293
function bigbluebuttonbn_bbb_view_create_meeting_data(&$bbbsession) {
294
    $data = ['meetingID' => $bbbsession['meetingid'],
295
              'name' => bigbluebuttonbn_html2text($bbbsession['meetingname'], 64),
296
              'attendeePW' => $bbbsession['viewerPW'],
297
              'moderatorPW' => $bbbsession['modPW'],
298
              'logoutURL' => $bbbsession['logoutURL'],
299
            ];
300
    $data['record'] = bigbluebuttonbn_bbb_view_create_meeting_data_record($bbbsession['record']);
301
    // Check if auto_start_record is enable.
302 View Code Duplication
    if ($data['record'] == 'true' && $bbbsession['recordallfromstart']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
        $data['autoStartRecording'] = 'true';
304
        // Check if hide_record_button is enable.
305
        if ($bbbsession['recordhidebutton']) {
306
            $data['allowStartStopRecording'] = 'false';
307
        }
308
    }
309
310
    $data['welcome'] = trim($bbbsession['welcome']);
311
    // Set the duration for the meeting.
312
    $durationtime = bigbluebuttonbn_bbb_view_create_meeting_data_duration($bbbsession['bigbluebuttonbn']->closingtime);
313 View Code Duplication
    if ($durationtime > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
314
        $data['duration'] = $durationtime;
315
        $data['welcome'] .= '<br><br>';
316
        $data['welcome'] .= str_replace(
317
            '%duration%',
318
            (string) $durationtime,
319
            get_string('bbbdurationwarning', 'bigbluebuttonbn')
320
          );
321
    }
322
    $voicebridge = intval($bbbsession['voicebridge']);
323
    if ($voicebridge > 0 && $voicebridge < 79999) {
324
        $data['voiceBridge'] = $voicebridge;
325
    }
326
    $maxparticipants = intval($bbbsession['userlimit']);
327
    if ($maxparticipants > 0) {
328
        $data['maxParticipants'] = $maxparticipants;
329
    }
330
    if ($bbbsession['muteonstart']) {
331
        $data['muteOnStart'] = 'true';
332
    }
333
    // Lock settings.
334
    if ($bbbsession['disablecam']) {
335
        $data['lockSettingsDisableCam'] = 'true';
336
    }
337
    if ($bbbsession['disablemic']) {
338
        $data['lockSettingsDisableMic'] = 'true';
339
    }
340
    if ($bbbsession['disableprivatechat']) {
341
        $data['lockSettingsDisablePrivateChat'] = 'true';
342
    }
343
    if ($bbbsession['disablepublicchat']) {
344
        $data['lockSettingsDisablePublicChat'] = 'true';
345
    }
346
    if ($bbbsession['disablenote']) {
347
        $data['lockSettingsDisableNote'] = 'true';
348
    }
349
    if ($bbbsession['hideuserlist']) {
350
        $data['lockSettingsHideUserList'] = 'true';
351
    }
352
    if ($bbbsession['lockedlayout']) {
353
        $data['lockSettingsLockedLayout'] = 'true';
354
    }
355
    if ($bbbsession['lockonjoin']) {
356
        $data['lockSettingsLockOnJoin'] = 'false';
357
    }
358
    if ($bbbsession['lockonjoinconfigurable']) {
359
        $data['lockSettingsLockOnJoinConfigurable'] = 'true';
360
    }
361
    return $data;
362
}
363
364
/**
365
 * Helper for returning the flag to know if the meeting is recorded.
366
 *
367
 * @param  boolean    $record
368
 * @return string
369
 */
370
function bigbluebuttonbn_bbb_view_create_meeting_data_record($record) {
371
    if ((boolean)\mod_bigbluebuttonbn\locallib\config::recordings_enabled() && $record) {
372
        return 'true';
373
    }
374
    return 'false';
375
}
376
377
/**
378
 * Helper for returning the duration expected for the meeting.
379
 *
380
 * @param  string    $closingtime
381
 * @return integer
382
 */
383
function bigbluebuttonbn_bbb_view_create_meeting_data_duration($closingtime) {
384
    if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('scheduled_duration_enabled')) {
385
        return bigbluebuttonbn_get_duration($closingtime);
386
    }
387
    return 0;
388
}
389
390
/**
391
 * Helper for preparing metadata used while creating the meeting.
392
 *
393
 * @param  array    $bbbsession
394
 * @return array
395
 */
396
function bigbluebuttonbn_bbb_view_create_meeting_metadata(&$bbbsession) {
397
    return bigbluebuttonbn_create_meeting_metadata($bbbsession);
398
}
399
400
/**
401
 * Helper for preparing data used while joining the meeting.
402
 *
403
 * @param array    $bbbsession
404
 * @param object   $bigbluebuttonbn
405
 * @param integer  $origin
406
 */
407
function bigbluebuttonbn_bbb_view_join_meeting($bbbsession, $bigbluebuttonbn, $origin = 0) {
408
    // Update the cache.
409
    $meetinginfo = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], BIGBLUEBUTTONBN_UPDATE_CACHE);
410
    if ($bbbsession['userlimit'] > 0 && intval($meetinginfo['participantCount']) >= $bbbsession['userlimit']) {
411
        // No more users allowed to join.
412
        header('Location: '.$bbbsession['logoutURL']);
413
        return;
414
    }
415
    // Build the URL.
416
    $password = $bbbsession['viewerPW'];
417
    if ($bbbsession['administrator'] || $bbbsession['moderator']) {
418
        $password = $bbbsession['modPW'];
419
    }
420
    $bbbsession['createtime'] = $meetinginfo['createTime'];
421
    $joinurl = bigbluebuttonbn_get_join_url($bbbsession['meetingid'], $bbbsession['username'],
422
        $password, $bbbsession['logoutURL'], null, $bbbsession['userID'], $bbbsession['clienttype'], $bbbsession['createtime']);
423
    // Moodle event logger: Create an event for meeting joined.
424
    bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['meeting_join'], $bigbluebuttonbn);
425
    // Internal logger: Instert a record with the meeting created.
426
    $overrides = array('meetingid' => $bbbsession['meetingid']);
427
    $meta = '{"origin":'.$origin.'}';
428
    bigbluebuttonbn_log($bbbsession['bigbluebuttonbn'], BIGBLUEBUTTONBN_LOG_EVENT_JOIN, $overrides, $meta);
429
    // Before executing the redirect, increment the number of participants.
430
    bigbluebuttonbn_participant_joined($bbbsession['meetingid'],
431
        ($bbbsession['administrator'] || $bbbsession['moderator']));
432
    // Execute the redirect.
433
    header('Location: '.$joinurl);
434
}
435
436
/**
437
 * Helper for showinf error messages if any.
438
 *
439
 * @param  string   $serrors
440
 * @param  string   $id
441
 * @return string
442
 */
443
function bigbluebuttonbn_bbb_view_errors($serrors, $id) {
444
    global $CFG, $OUTPUT;
445
    $errors = (array) json_decode(urldecode($serrors));
446
    $msgerrors = '';
447
    foreach ($errors as $error) {
448
        $msgerrors .= html_writer::tag('p', $error->{'message'}, array('class' => 'alert alert-danger'))."\n";
449
    }
450
    echo $OUTPUT->header();
451
    print_error('view_error_bigbluebutton', 'bigbluebuttonbn',
452
        $CFG->wwwroot.'/mod/bigbluebuttonbn/view.php?id='.$id, $msgerrors, $serrors);
453
    echo $OUTPUT->footer();
454
}
455