Completed
Pull Request — master (#82)
by
unknown
02:57
created

mobileview   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 203
Duplicated Lines 61.58 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
B bigbluebuttonbn_view_bbbsession_set() 55 55 9
A build_url_join_session() 1 10 3
A bigbluebuttonbn_view_get_activity_status() 4 13 5
A bigbluebutton_bbb_view_create_meeting_metadata() 28 30 4
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

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