bigbluebutton::view_bbbsession_set()   F
last analyzed

Complexity

Conditions 12
Paths 768

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 768
nop 2
dl 0
loc 82
rs 2.8905
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * The mod_bigbluebuttonbn locallib/bigbluebutton.
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
namespace mod_bigbluebuttonbn\locallib;
27
28
use context_module;
29
30
defined('MOODLE_INTERNAL') || die();
31
global $CFG;
32
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
33
34
/**
35
 * Wrapper for executing http requests on a BigBlueButton server.
36
 *
37
 * @copyright 2010 onwards, Blindside Networks Inc
38
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class bigbluebutton {
41
42
    /**
43
     * Returns the right URL for the action specified.
44
     *
45
     * @param string $action
46
     * @param array  $data
47
     * @param array  $metadata
48
     * @return string
49
     */
50
    public static function action_url($action = '', $data = array(), $metadata = array()) {
51
        $baseurl = self::sanitized_url() . $action . '?';
52
        $metadata = array_combine(
53
            array_map(
54
                function($k) {
55
                    return 'meta_' . $k;
56
                }
57
                , array_keys($metadata)
58
            ),
59
            $metadata
60
        );
61
        $params = http_build_query($data + $metadata, '', '&');
62
        return $baseurl . $params . '&checksum=' . sha1($action . $params . self::sanitized_secret());
63
    }
64
65
    /**
66
     * Makes sure the url used doesn't is in the format required.
67
     *
68
     * @return string
69
     */
70
    public static function sanitized_url() {
71
        $serverurl = trim(config::get('server_url'));
72
        if (substr($serverurl, -1) == '/') {
73
            $serverurl = rtrim($serverurl, '/');
74
        }
75
        if (substr($serverurl, -4) == '/api') {
76
            $serverurl = rtrim($serverurl, '/api');
77
        }
78
        return $serverurl . '/api/';
79
    }
80
81
    /**
82
     * Makes sure the shared_secret used doesn't have trailing white characters.
83
     *
84
     * @return string
85
     */
86
    public static function sanitized_secret() {
87
        return trim(config::get('shared_secret'));
88
    }
89
90
    /**
91
     * Returns the BigBlueButton server root URL.
92
     *
93
     * @return string
94
     */
95
    public static function root() {
96
        $pserverurl = parse_url(trim(config::get('server_url')));
97
        $pserverurlport = "";
98
        if (isset($pserverurl['port'])) {
99
            $pserverurlport = ":" . $pserverurl['port'];
100
        }
101
        return $pserverurl['scheme'] . "://" . $pserverurl['host'] . $pserverurlport . "/";
102
    }
103
104
    /**
105
     * Get BBB session information from viewinstance
106
     *
107
     * @param object $viewinstance
108
     * @return mixed
109
     * @throws \coding_exception
110
     * @throws \dml_exception
111
     * @throws \moodle_exception
112
     * @throws \require_login_exception
113
     * @throws \required_capability_exception
114
     */
115
    public static function build_bbb_session_fromviewinstance($viewinstance) {
116
        $cm = $viewinstance['cm'];
117
        $course = $viewinstance['course'];
118
        $bigbluebuttonbn = $viewinstance['bigbluebuttonbn'];
119
        return self::build_bbb_session($cm, $course, $bigbluebuttonbn);
120
    }
121
122
    /**
123
     * Get BBB session from parameters
124
     *
125
     * @param \course_modinfo $cm
126
     * @param object $course
127
     * @param object $bigbluebuttonbn
128
     * @return mixed
129
     * @throws \coding_exception
130
     * @throws \dml_exception
131
     * @throws \moodle_exception
132
     * @throws \require_login_exception
133
     * @throws \required_capability_exception
134
     */
135
    public static function build_bbb_session($cm, $course, $bigbluebuttonbn) {
136
        global $CFG;
137
        $context = context_module::instance($cm->id);
138
        require_login($course->id, false, $cm, true, true);
139
        require_capability('mod/bigbluebuttonbn:join', $context);
140
141
        // Add view event.
142
        bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['view'], $bigbluebuttonbn);
143
144
        // Create array bbbsession with configuration for BBB server.
145
        $bbbsession['course'] = $course;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$bbbsession was never initialized. Although not strictly required by PHP, it is generally a good practice to add $bbbsession = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
146
        $bbbsession['coursename'] = $course->fullname;
147
        $bbbsession['cm'] = $cm;
148
        $bbbsession['bigbluebuttonbn'] = $bigbluebuttonbn;
149
        self::view_bbbsession_set($context, $bbbsession);
150
151
        $serverversion = bigbluebuttonbn_get_server_version();
152
        $bbbsession['serverversion'] = (string) $serverversion;
153
154
        // Operation URLs.
155
        $bbbsession['bigbluebuttonbnURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/view.php?id=' . $cm->id;
156
        $bbbsession['logoutURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=logout&id=' . $cm->id .
157
            '&bn=' . $bbbsession['bigbluebuttonbn']->id;
158
        $bbbsession['recordingReadyURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=recording_' .
159
            'ready&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id;
160
        $bbbsession['meetingEventsURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=meeting' .
161
            '_events&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id;
162
        $bbbsession['joinURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=join&id=' . $cm->id .
163
            '&bn=' . $bbbsession['bigbluebuttonbn']->id;
164
165
        return $bbbsession;
166
    }
167
168
    /**
169
     * Build standard array with configurations required for BBB server.
170
     *
171
     * @param \context $context
172
     * @param array $bbbsession
173
     * @throws \coding_exception
174
     * @throws \dml_exception
175
     */
176
    public static function view_bbbsession_set($context, &$bbbsession) {
177
178
        global $CFG, $USER;
179
180
        $bbbsession['username'] = fullname($USER);
181
        $bbbsession['userID'] = $USER->id;
182
        $bbbsession['administrator'] = is_siteadmin($bbbsession['userID']);
183
        $participantlist = bigbluebuttonbn_get_participant_list($bbbsession['bigbluebuttonbn'], $context);
184
        $bbbsession['moderator'] = bigbluebuttonbn_is_moderator($context, $participantlist);
185
        $bbbsession['managerecordings'] = ($bbbsession['administrator']
186
            || has_capability('mod/bigbluebuttonbn:managerecordings', $context));
187
        $bbbsession['importrecordings'] = ($bbbsession['managerecordings']);
188
        $bbbsession['modPW'] = $bbbsession['bigbluebuttonbn']->moderatorpass;
189
        $bbbsession['viewerPW'] = $bbbsession['bigbluebuttonbn']->viewerpass;
190
        $bbbsession['meetingid'] = $bbbsession['bigbluebuttonbn']->meetingid.'-'.$bbbsession['course']->id.'-'.
191
            $bbbsession['bigbluebuttonbn']->id;
192
        $bbbsession['meetingname'] = $bbbsession['bigbluebuttonbn']->name;
193
        $bbbsession['meetingdescription'] = $bbbsession['bigbluebuttonbn']->intro;
194
        $bbbsession['userlimit'] = intval((int) config::get('userlimit_default'));
195
        if ((boolean) config::get('userlimit_editable')) {
196
            $bbbsession['userlimit'] = intval($bbbsession['bigbluebuttonbn']->userlimit);
197
        }
198
        $bbbsession['voicebridge'] = $bbbsession['bigbluebuttonbn']->voicebridge;
199
        if ($bbbsession['bigbluebuttonbn']->voicebridge > 0) {
200
            $bbbsession['voicebridge'] = 70000 + $bbbsession['bigbluebuttonbn']->voicebridge;
201
        }
202
        $bbbsession['wait'] = $bbbsession['bigbluebuttonbn']->wait;
203
        $bbbsession['record'] = $bbbsession['bigbluebuttonbn']->record;
204
        $bbbsession['recordallfromstart'] = $CFG->bigbluebuttonbn_recording_all_from_start_default;
205
        if ($CFG->bigbluebuttonbn_recording_all_from_start_editable) {
206
            $bbbsession['recordallfromstart'] = $bbbsession['bigbluebuttonbn']->recordallfromstart;
207
        }
208
        $bbbsession['recordhidebutton'] = $CFG->bigbluebuttonbn_recording_hide_button_default;
209
        if ($CFG->bigbluebuttonbn_recording_hide_button_editable) {
210
            $bbbsession['recordhidebutton'] = $bbbsession['bigbluebuttonbn']->recordhidebutton;
211
        }
212
        $bbbsession['welcome'] = $bbbsession['bigbluebuttonbn']->welcome;
213
        if (!isset($bbbsession['welcome']) || $bbbsession['welcome'] == '') {
214
            $bbbsession['welcome'] = get_string('mod_form_field_welcome_default', 'bigbluebuttonbn');
215
        }
216
        if ($bbbsession['bigbluebuttonbn']->record) {
217
            // Check if is enable record all from start.
218
            if ($bbbsession['recordallfromstart']) {
219
                $bbbsession['welcome'] .= '<br><br>'.get_string('bbbrecordallfromstartwarning',
220
                        'bigbluebuttonbn');
221
            } else {
222
                $bbbsession['welcome'] .= '<br><br>'.get_string('bbbrecordwarning', 'bigbluebuttonbn');
223
            }
224
        }
225
        $bbbsession['openingtime'] = $bbbsession['bigbluebuttonbn']->openingtime;
226
        $bbbsession['closingtime'] = $bbbsession['bigbluebuttonbn']->closingtime;
227
        $bbbsession['muteonstart'] = $bbbsession['bigbluebuttonbn']->muteonstart;
228
        // Lock settings.
229
        $bbbsession['disablecam'] = $bbbsession['bigbluebuttonbn']->disablecam;
230
        $bbbsession['disablemic'] = $bbbsession['bigbluebuttonbn']->disablemic;
231
        $bbbsession['disableprivatechat'] = $bbbsession['bigbluebuttonbn']->disableprivatechat;
232
        $bbbsession['disablepublicchat'] = $bbbsession['bigbluebuttonbn']->disablepublicchat;
233
        $bbbsession['disablenote'] = $bbbsession['bigbluebuttonbn']->disablenote;
234
        $bbbsession['hideuserlist'] = $bbbsession['bigbluebuttonbn']->hideuserlist;
235
        $bbbsession['lockedlayout'] = $bbbsession['bigbluebuttonbn']->lockedlayout;
236
        $bbbsession['lockonjoin'] = $bbbsession['bigbluebuttonbn']->lockonjoin;
237
        $bbbsession['lockonjoinconfigurable'] = $bbbsession['bigbluebuttonbn']->lockonjoinconfigurable;
238
        // Additional info related to the course.
239
        $bbbsession['context'] = $context;
240
        // Metadata (origin).
241
        $bbbsession['origin'] = 'Moodle';
242
        $bbbsession['originVersion'] = $CFG->release;
243
        $parsedurl = parse_url($CFG->wwwroot);
244
        $bbbsession['originServerName'] = $parsedurl['host'];
245
        $bbbsession['originServerUrl'] = $CFG->wwwroot;
246
        $bbbsession['originServerCommonName'] = '';
247
        $bbbsession['originTag'] = 'moodle-mod_bigbluebuttonbn ('.get_config('mod_bigbluebuttonbn', 'version').')';
248
        $bbbsession['bnserver'] = bigbluebuttonbn_is_bn_server();
249
        // Setting for clienttype, assign flash if not enabled, or default if not editable.
250
        $bbbsession['clienttype'] = config::get('clienttype_default');
251
        if (config::get('clienttype_editable')) {
252
            $bbbsession['clienttype'] = $bbbsession['bigbluebuttonbn']->clienttype;
253
        }
254
        if (!config::clienttype_enabled()) {
255
            $bbbsession['clienttype'] = BIGBLUEBUTTON_CLIENTTYPE_FLASH;
256
        }
257
    }
258
259
    /**
260
     * Can join meeting.
261
     *
262
     * @param int $cmid
263
     * @return array|bool[]
264
     * @throws \coding_exception
265
     * @throws \dml_exception
266
     * @throws \moodle_exception
267
     * @throws \require_login_exception
268
     * @throws \required_capability_exception
269
     */
270
    public static function can_join_meeting($cmid) {
271
        global $CFG;
272
        $canjoin = array('can_join' => false, 'message' => '');
273
274
        $viewinstance = bigbluebuttonbn_view_validator($cmid, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
275
        if ($viewinstance) {
276
            $bbbsession = self::build_bbb_session_fromviewinstance($viewinstance);
0 ignored issues
show
Documentation introduced by
$viewinstance is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
277
            if ($bbbsession) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bbbsession of type array<string,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
278
                require_once($CFG->dirroot . "/mod/bigbluebuttonbn/brokerlib.php");
279
                $info = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], false);
280
                $running = false;
281
                if ($info['returncode'] == 'SUCCESS') {
282
                    $running = ($info['running'] === 'true');
283
                }
284
                $participantcount = 0;
285
                if (isset($info['participantCount'])) {
286
                    $participantcount = $info['participantCount'];
287
                }
288
                $canjoin = bigbluebuttonbn_broker_meeting_info_can_join($bbbsession, $running, $participantcount);
289
            }
290
        }
291
        return $canjoin;
292
    }
293
}
294