Completed
Pull Request — master (#115)
by
unknown
02:00
created

mobileview   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 238
Duplicated Lines 68.49 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 163
loc 238
rs 9.28
c 0
b 0
f 0
wmc 39
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
F bigbluebuttonbn_view_bbbsession_set() 76 76 12
A build_url_join_session() 0 10 3
A bigbluebuttonbn_view_get_activity_status() 13 13 5
A bigbluebutton_bbb_view_create_meeting_metadata() 32 32 4
B bigbluebutton_bbb_view_create_meeting_data() 42 42 10
A bigbluebutton_bbb_view_create_meeting_data_record() 0 6 3
A bigbluebutton_bbb_view_create_meeting_data_duration() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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/mobileview.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2018 onwards, Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
25
namespace mod_bigbluebuttonbn\locallib;
26
27
defined('MOODLE_INTERNAL') || die();
28
29
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
30
31
/**
32
 * Methods used to render view BBB in mobile.
33
 *
34
 * @copyright 2018 onwards, Blindside Networks Inc
35
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class mobileview {
38
39
    /**
40
     * Return standard array with configurations required for BBB server.
41
     * @param $context
42
     * @param $session
43
     * @return mixed
44
     * @throws \coding_exception
45
     * @throws \dml_exception
46
     */
47 View Code Duplication
    public static function bigbluebuttonbn_view_bbbsession_set($context, &$session) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
48
49
        global $CFG, $USER;
50
51
        $session['username'] = fullname($USER);
52
        $session['userID'] = $USER->id;
53
        $session['administrator'] = is_siteadmin($session['userID']);
54
        $participantlist = bigbluebuttonbn_get_participant_list($session['bigbluebuttonbn'], $context);
55
        $session['moderator'] = bigbluebuttonbn_is_moderator($context, $participantlist);
56
        $session['managerecordings'] = ($session['administrator']
57
            || has_capability('mod/bigbluebuttonbn:managerecordings', $context));
58
        $session['importrecordings'] = ($session['managerecordings']);
59
        $session['modPW'] = $session['bigbluebuttonbn']->moderatorpass;
60
        $session['viewerPW'] = $session['bigbluebuttonbn']->viewerpass;
61
        $session['meetingid'] = $session['bigbluebuttonbn']->meetingid.'-'.$session['course']->id.'-'.
62
            $session['bigbluebuttonbn']->id;
63
        $session['meetingname'] = $session['bigbluebuttonbn']->name;
64
        $session['meetingdescription'] = $session['bigbluebuttonbn']->intro;
65
        $session['userlimit'] = intval((int)\mod_bigbluebuttonbn\locallib\config::get('userlimit_default'));
66
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('userlimit_editable')) {
67
            $session['userlimit'] = intval($session['bigbluebuttonbn']->userlimit);
68
        }
69
        $session['voicebridge'] = $session['bigbluebuttonbn']->voicebridge;
70
        if ($session['bigbluebuttonbn']->voicebridge > 0) {
71
            $session['voicebridge'] = 70000 + $session['bigbluebuttonbn']->voicebridge;
72
        }
73
        $session['wait'] = $session['bigbluebuttonbn']->wait;
74
        $session['record'] = $session['bigbluebuttonbn']->record;
75
76
        $session['recordallfromstart'] = $CFG->bigbluebuttonbn_recording_all_from_start_default;
77
        if ($CFG->bigbluebuttonbn_recording_all_from_start_editable) {
78
            $session['recordallfromstart'] = $session['bigbluebuttonbn']->recordallfromstart;
79
        }
80
81
        $session['recordhidebutton'] = $CFG->bigbluebuttonbn_recording_hide_button_default;
82
        if ($CFG->bigbluebuttonbn_recording_hide_button_editable) {
83
            $session['recordhidebutton'] = $session['bigbluebuttonbn']->recordhidebutton;
84
        }
85
86
        $session['welcome'] = $session['bigbluebuttonbn']->welcome;
87
        if (!isset($session['welcome']) || $session['welcome'] == '') {
88
            $session['welcome'] = get_string('mod_form_field_welcome_default', 'bigbluebuttonbn');
89
        }
90
        if ($session['bigbluebuttonbn']->record) {
91
            // Check if is enable record all from start.
92
            if ($session['recordallfromstart']) {
93
                $session['welcome'] .= '<br><br>'.get_string('bbbrecordallfromstartwarning',
94
                        'bigbluebuttonbn');
95
            } else {
96
                $session['welcome'] .= '<br><br>'.get_string('bbbrecordwarning', 'bigbluebuttonbn');
97
            }
98
        }
99
        $session['openingtime'] = $session['bigbluebuttonbn']->openingtime;
100
        $session['closingtime'] = $session['bigbluebuttonbn']->closingtime;
101
        $session['muteonstart'] = $session['bigbluebuttonbn']->muteonstart;
102
        $session['context'] = $context;
103
        $session['origin'] = 'Moodle';
104
        $session['originVersion'] = $CFG->release;
105
        $parsedurl = parse_url($CFG->wwwroot);
106
        $session['originServerName'] = $parsedurl['host'];
107
        $session['originServerUrl'] = $CFG->wwwroot;
108
        $session['originServerCommonName'] = '';
109
        $session['originTag'] = 'moodle-mod_bigbluebuttonbn ('.get_config('mod_bigbluebuttonbn', 'version').')';
110
        $session['bnserver'] = bigbluebuttonbn_is_bn_server();
111
        $session['clienttype'] = \mod_bigbluebuttonbn\locallib\config::get('clienttype_default');
112
113
        if (\mod_bigbluebuttonbn\locallib\config::get('clienttype_editable')) {
114
            $session['clienttype'] = $session['bigbluebuttonbn']->clienttype;
115
        }
116
117
        if (!\mod_bigbluebuttonbn\locallib\config::clienttype_enabled()) {
118
            $session['clienttype'] = BIGBLUEBUTTON_CLIENTTYPE_FLASH;
119
        }
120
121
        return($session);
122
    }
123
124
    /**
125
     * Build url for join to session.
126
     * This method is similar to "bigbluebutton_bbb_view_join_meeting()" in bbb_view.
127
     * @param $bbbsession
128
     * @return string
129
     */
130
    public static function build_url_join_session($bbbsession) {
131
        $password = $bbbsession['viewerPW'];
132
        if ($bbbsession['administrator'] || $bbbsession['moderator']) {
133
            $password = $bbbsession['modPW'];
134
        }
135
        $joinurl = bigbluebuttonbn_get_join_url($bbbsession['meetingid'], $bbbsession['username'],
136
            $password, $bbbsession['logoutURL'], null, $bbbsession['userID'], $bbbsession['clienttype']);
137
138
        return($joinurl);
139
    }
140
141
    /**
142
     * Return the status of an activity [open|not_started|ended].
143
     *
144
     * @param array $bbbsession
145
     * @return string
146
     */
147 View Code Duplication
    public static function bigbluebuttonbn_view_get_activity_status(&$bbbsession) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
148
        $now = time();
149
        if (!empty($bbbsession['bigbluebuttonbn']->openingtime) && $now < $bbbsession['bigbluebuttonbn']->openingtime) {
150
            // The activity has not been opened.
151
            return 'not_started';
152
        }
153
        if (!empty($bbbsession['bigbluebuttonbn']->closingtime) && $now > $bbbsession['bigbluebuttonbn']->closingtime) {
154
            // The activity has been closed.
155
            return 'ended';
156
        }
157
        // The activity is open.
158
        return 'open';
159
    }
160
161
    /**
162
     * Helper for preparing metadata used while creating the meeting.
163
     *
164
     * @param  array    $bbbsession
165
     * @return array
166
     */
167 View Code Duplication
    public static function bigbluebutton_bbb_view_create_meeting_metadata(&$bbbsession) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
168
169
        global $USER;
170
        // Create standard metadata.
171
        $metadatabbb = [
172
            'bbb-origin' => $bbbsession['origin'],
173
            'bbb-origin-version' => $bbbsession['originVersion'],
174
            'bbb-origin-server-name' => $bbbsession['originServerName'],
175
            'bbb-origin-server-common-name' => $bbbsession['originServerCommonName'],
176
            'bbb-origin-tag' => $bbbsession['originTag'],
177
            'bbb-context' => $bbbsession['course']->fullname,
178
            'bbb-recording-name' => bigbluebuttonbn_html2text($bbbsession['meetingname'], 64),
179
            'bbb-recording-description' => bigbluebuttonbn_html2text($bbbsession['meetingdescription'], 64),
180
            'bbb-recording-tags' => bigbluebuttonbn_get_tags($bbbsession['cm']->id), // Same as $id.
181
        ];
182
        // Check recording status.
183
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('recordingstatus_enabled')) {
184
            $metadatabbb["bn-recording-status"] = json_encode(
185
                array(
186
                    'email' => array('"' . fullname($USER) . '" <' . $USER->email . '>'),
187
                    'context' => $bbbsession['bigbluebuttonbnURL']
188
                )
189
            );
190
        }
191
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('recordingready_enabled')) {
192
            $metadatabbb['bn-recording-ready-url'] = $bbbsession['recordingReadyURL'];
193
        }
194
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('meetingevents_enabled')) {
195
            $metadatabbb['bn-meeting-events-url'] = $bbbsession['meetingEventsURL'];
196
        }
197
        return $metadatabbb;
198
    }
199
200
    /**
201
     * Helper to prepare data used for create meeting.
202
     * @param $bbbsession
203
     * @return array
204
     * @throws \coding_exception
205
     */
206 View Code Duplication
    public static function bigbluebutton_bbb_view_create_meeting_data(&$bbbsession) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
207
        $data = ['meetingID' => $bbbsession['meetingid'],
208
            'name' => bigbluebuttonbn_html2text($bbbsession['meetingname'], 64),
209
            'attendeePW' => $bbbsession['viewerPW'],
210
            'moderatorPW' => $bbbsession['modPW'],
211
            'logoutURL' => $bbbsession['logoutURL'],
212
        ];
213
        $data['record'] = self::bigbluebutton_bbb_view_create_meeting_data_record($bbbsession['record']);
214
        // Check if auto_start_record is enable.
215
        if ($data['record'] == 'true' && $bbbsession['recordallfromstart']) {
216
            $data['autoStartRecording'] = 'true';
217
            // Check if hide_record_button is enable.
218
            if ($bbbsession['recordallfromstart'] && $bbbsession['recordhidebutton']) {
219
                $data['allowStartStopRecording'] = 'false';
220
            }
221
        }
222
223
        $data['welcome'] = trim($bbbsession['welcome']);
224
        // Set the duration for the meeting.
225
        $durationtime = self::bigbluebutton_bbb_view_create_meeting_data_duration($bbbsession['bigbluebuttonbn']->closingtime);
226
        if ($durationtime > 0) {
227
            $data['duration'] = $durationtime;
228
            $data['welcome'] .= '<br><br>';
229
            $data['welcome'] .= str_replace(
230
                '%duration%',
231
                (string) $durationtime,
232
                get_string('bbbdurationwarning', 'bigbluebuttonbn')
233
            );
234
        }
235
        $voicebridge = intval($bbbsession['voicebridge']);
236
        if ($voicebridge > 0 && $voicebridge < 79999) {
237
            $data['voiceBridge'] = $voicebridge;
238
        }
239
        $maxparticipants = intval($bbbsession['userlimit']);
240
        if ($maxparticipants > 0) {
241
            $data['maxParticipants'] = $maxparticipants;
242
        }
243
        if ($bbbsession['muteonstart']) {
244
            $data['muteOnStart'] = 'true';
245
        }
246
        return $data;
247
    }
248
249
    /**
250
     * Helper for returning the flag to know if the meeting is recorded.
251
     *
252
     * @param  boolean    $record
253
     * @return string
254
     */
255
    public static function bigbluebutton_bbb_view_create_meeting_data_record($record) {
256
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::recordings_enabled() && $record) {
257
            return 'true';
258
        }
259
        return 'false';
260
    }
261
262
    /**
263
     * Helper for returning the duration expected for the meeting.
264
     *
265
     * @param  string    $closingtime
266
     * @return integer
267
     */
268
    public static function bigbluebutton_bbb_view_create_meeting_data_duration($closingtime) {
269
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('scheduled_duration_enabled')) {
270
            return bigbluebuttonbn_get_duration($closingtime);
271
        }
272
        return 0;
273
    }
274
}
275