Completed
Pull Request — master (#82)
by
unknown
03:18
created

mobileview   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 208
Duplicated Lines 60.1 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 125
loc 208
rs 9.92
c 0
b 0
f 0
wmc 31
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A bigbluebutton_bbb_view_create_meeting_data() 30 30 5
A bigbluebutton_bbb_view_create_meeting_data_record() 0 6 3
A bigbluebutton_bbb_view_create_meeting_data_duration() 0 6 2
B bigbluebuttonbn_view_bbbsession_set() 58 58 9
A build_url_join_session() 0 10 3
A bigbluebuttonbn_view_get_activity_status() 4 13 5
A bigbluebutton_bbb_view_create_meeting_metadata() 28 32 4

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
        $session['welcome'] = $session['bigbluebuttonbn']->welcome;
76
        if (!isset($session['welcome']) || $session['welcome'] == '') {
77
            $session['welcome'] = get_string('mod_form_field_welcome_default', 'bigbluebuttonbn');
78
        }
79
        if ($session['bigbluebuttonbn']->record) {
80
            $session['welcome'] .= '<br><br>'.get_string('bbbrecordwarning', 'bigbluebuttonbn');
81
        }
82
        $session['openingtime'] = $session['bigbluebuttonbn']->openingtime;
83
        $session['closingtime'] = $session['bigbluebuttonbn']->closingtime;
84
        $session['context'] = $context;
85
        $session['origin'] = 'Moodle';
86
        $session['originVersion'] = $CFG->release;
87
        $parsedurl = parse_url($CFG->wwwroot);
88
        $session['originServerName'] = $parsedurl['host'];
89
        $session['originServerUrl'] = $CFG->wwwroot;
90
        $session['originServerCommonName'] = '';
91
        $session['originTag'] = 'moodle-mod_bigbluebuttonbn ('.get_config('mod_bigbluebuttonbn', 'version').')';
92
        $session['bnserver'] = bigbluebuttonbn_is_bn_server();
93
        $session['clienttype'] = \mod_bigbluebuttonbn\locallib\config::get('clienttype_default');
94
95
        if (\mod_bigbluebuttonbn\locallib\config::get('clienttype_editable')) {
96
            $session['clienttype'] = $session['bigbluebuttonbn']->clienttype;
97
        }
98
99
        if (!\mod_bigbluebuttonbn\locallib\config::clienttype_enabled()) {
100
            $session['clienttype'] = BIGBLUEBUTTON_CLIENTTYPE_FLASH;
101
        }
102
103
        return($session);
104
    }
105
106
    /**
107
     * Build url for join to session.
108
     * This method is similar to "bigbluebutton_bbb_view_join_meeting()" in bbb_view.
109
     * @param $bbbsession
110
     * @return string
111
     */
112
    public static function build_url_join_session($bbbsession) {
113
        $password = $bbbsession['viewerPW'];
114
        if ($bbbsession['administrator'] || $bbbsession['moderator']) {
115
            $password = $bbbsession['modPW'];
116
        }
117
        $joinurl = bigbluebuttonbn_get_join_url($bbbsession['meetingid'], $bbbsession['username'],
118
            $password, $bbbsession['logoutURL'], null, $bbbsession['userID'], $bbbsession['clienttype']);
119
120
        return($joinurl);
121
    }
122
123
    /**
124
     * Return the status of an activity [open|not_started|ended].
125
     *
126
     * @param array $bbbsession
127
     * @return string
128
     */
129
    public static function bigbluebuttonbn_view_get_activity_status(&$bbbsession) {
130
        $now = time();
131 View Code Duplication
        if (!empty($bbbsession['bigbluebuttonbn']->openingtime) && $now < $bbbsession['bigbluebuttonbn']->openingtime) {
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...
132
            // The activity has not been opened.
133
            return 'not_started';
134
        }
135
        if (!empty($bbbsession['bigbluebuttonbn']->closingtime) && $now > $bbbsession['bigbluebuttonbn']->closingtime) {
136
            // The activity has been closed.
137
            return 'ended';
138
        }
139
        // The activity is open.
140
        return 'open';
141
    }
142
143
    /**
144
     * Helper for preparing metadata used while creating the meeting.
145
     *
146
     * @param  array    $bbbsession
147
     * @return array
148
     */
149 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...
150
151
        global $USER;
152
        // Create standard metadata.
153
        $metadatabbb = [
154
            'bbb-origin' => $bbbsession['origin'],
155
            'bbb-origin-version' => $bbbsession['originVersion'],
156
            'bbb-origin-server-name' => $bbbsession['originServerName'],
157
            'bbb-origin-server-common-name' => $bbbsession['originServerCommonName'],
158
            'bbb-origin-tag' => $bbbsession['originTag'],
159
            'bbb-context' => $bbbsession['course']->fullname,
160
            'bbb-recording-name' => bigbluebuttonbn_html2text($bbbsession['meetingname'], 64),
161
            'bbb-recording-description' => bigbluebuttonbn_html2text($bbbsession['meetingdescription'], 64),
162
            'bbb-recording-tags' => bigbluebuttonbn_get_tags($bbbsession['cm']->id), // Same as $id.
163
        ];
164
        // Check recording status.
165
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('recordingstatus_enabled')) {
166
            $metadatabbb["bn-recording-status"] = json_encode(
167
                array(
168
                    'email' => array('"' . fullname($USER) . '" <' . $USER->email . '>'),
169
                    'context' => $bbbsession['bigbluebuttonbnURL']
170
                )
171
            );
172
        }
173
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('recordingready_enabled')) {
174
            $metadatabbb['bn-recording-ready-url'] = $bbbsession['recordingReadyURL'];
175
        }
176
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('meetingevents_enabled')) {
177
            $metadatabbb['bn-meeting-events-url'] = $bbbsession['meetingEventsURL'];
178
        }
179
        return $metadatabbb;
180
    }
181
182
    /**
183
     * Helper to prepare data used for create meeting.
184
     * @param $bbbsession
185
     * @return array
186
     * @throws \coding_exception
187
     */
188 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...
189
        $data = ['meetingID' => $bbbsession['meetingid'],
190
            'name' => bigbluebuttonbn_html2text($bbbsession['meetingname'], 64),
191
            'attendeePW' => $bbbsession['viewerPW'],
192
            'moderatorPW' => $bbbsession['modPW'],
193
            'logoutURL' => $bbbsession['logoutURL'],
194
        ];
195
        $data['record'] = self::bigbluebutton_bbb_view_create_meeting_data_record($bbbsession['record']);
196
        $data['welcome'] = trim($bbbsession['welcome']);
197
        // Set the duration for the meeting.
198
        $durationtime = self::bigbluebutton_bbb_view_create_meeting_data_duration($bbbsession['bigbluebuttonbn']->closingtime);
199
        if ($durationtime > 0) {
200
            $data['duration'] = $durationtime;
201
            $data['welcome'] .= '<br><br>';
202
            $data['welcome'] .= str_replace(
203
                '%duration%',
204
                (string) $durationtime,
205
                get_string('bbbdurationwarning', 'bigbluebuttonbn')
206
            );
207
        }
208
        $voicebridge = intval($bbbsession['voicebridge']);
209
        if ($voicebridge > 0 && $voicebridge < 79999) {
210
            $data['voiceBridge'] = $voicebridge;
211
        }
212
        $maxparticipants = intval($bbbsession['userlimit']);
213
        if ($maxparticipants > 0) {
214
            $data['maxParticipants'] = $maxparticipants;
215
        }
216
        return $data;
217
    }
218
219
    /**
220
     * Helper for returning the flag to know if the meeting is recorded.
221
     *
222
     * @param  boolean    $record
223
     * @return string
224
     */
225
    public static function bigbluebutton_bbb_view_create_meeting_data_record($record) {
226
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::recordings_enabled() && $record) {
227
            return 'true';
228
        }
229
        return 'false';
230
    }
231
232
    /**
233
     * Helper for returning the duration expected for the meeting.
234
     *
235
     * @param  string    $closingtime
236
     * @return integer
237
     */
238
    public static function bigbluebutton_bbb_view_create_meeting_data_duration($closingtime) {
239
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::get('scheduled_duration_enabled')) {
240
            return bigbluebuttonbn_get_duration($closingtime);
241
        }
242
        return 0;
243
    }
244
}
245