mobile::mobile_course_view()   F
last analyzed

Complexity

Conditions 25
Paths 39

Size

Total Lines 178

Duplication

Lines 23
Ratio 12.92 %

Importance

Changes 0
Metric Value
cc 25
nc 39
nop 1
dl 23
loc 178
rs 3.3333
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
 * Mobile output class for bigbluebuttonbn
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
 * @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
24
 */
25
26
namespace mod_bigbluebuttonbn\output;
27
28
defined('MOODLE_INTERNAL') || die();
29
30
use context_module;
31
use mod_bigbluebuttonbn\locallib\bigbluebutton;
32
33
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
34
require_once($CFG->dirroot . '/lib/grouplib.php');
35
36
/**
37
 * Mobile output class for bigbluebuttonbn
38
 *
39
 * @package    mod_bigbluebuttonbn
40
 * @copyright  2018 onwards, Blindside Networks Inc
41
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 * @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
43
 */
44
class mobile {
45
46
    /**
47
     * Returns the bigbluebuttonbn course view for the mobile app.
48
     * @param mixed $args
49
     * @return array HTML, javascript and other data.
50
     * @throws \coding_exception
51
     * @throws \moodle_exception
52
     * @throws \require_login_exception
53
     * @throws \required_capability_exception
54
     */
55
    public static function mobile_course_view($args) {
56
57
        global $OUTPUT, $SESSION;
58
59
        $args = (object) $args;
60
        $viewinstance = bigbluebuttonbn_view_validator($args->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...
61
        if (!$viewinstance) {
62
            $error = get_string('view_error_url_missing_parameters', 'bigbluebuttonbn');
63
            return(self::mobile_print_error($error));
64
        }
65
66
        $bbbsession = bigbluebutton::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...
67
68
        // Create variables for easy access.
69
        $bigbluebuttonbn = $viewinstance['bigbluebuttonbn'];
70
        $serverversion = $bbbsession['serverversion'];
71
        $cm = $bbbsession['cm'];
72
        $course = $bbbsession['course'];
73
        $context = $bbbsession['context'];
74
75
        // Check activity status.
76
        $activitystatus = \mod_bigbluebuttonbn\locallib\mobileview::get_activity_status($bbbsession);
77
        if ($activitystatus == 'not_started') {
78
            $message = get_string('view_message_conference_not_started', 'bigbluebuttonbn');
79
80
            $notstarted = array();
81
            $notstarted['starts_at'] = '';
82
            $notstarted['ends_at'] = '';
83
            if (!empty($bigbluebuttonbn->openingtime)) {
84
                $notstarted['starts_at'] = sprintf(
85
                    '%s: %s',
86
                    get_string('mod_form_field_openingtime', 'bigbluebuttonbn'),
87
                    userdate($bigbluebuttonbn->openingtime)
88
                );
89
            }
90
            if (!empty($bigbluebuttonbn->closingtime)) {
91
                $notstarted['ends_at'] = sprintf(
92
                    '%s: %s',
93
                    get_string('mod_form_field_closingtime', 'bigbluebuttonbn'),
94
                    userdate($bigbluebuttonbn->closingtime)
95
                );
96
            }
97
98
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message, $notstarted));
99
        }
100
        if ($activitystatus == 'ended') {
101
            $message = get_string('view_message_conference_has_ended', 'bigbluebuttonbn');
102
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message));
103
        }
104
105
        // Check if the BBB server is working.
106 View Code Duplication
        if (is_null($serverversion)) {
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...
107
108
            if ($bbbsession['administrator']) {
109
                $error = get_string('view_error_unable_join', 'bigbluebuttonbn');
110
            } else if ($bbbsession['moderator']) {
111
                $error = get_string('view_error_unable_join_teacher', 'bigbluebuttonbn');
112
            } else {
113
                $error = get_string('view_error_unable_join_student', 'bigbluebuttonbn');
114
            }
115
116
            return(self::mobile_print_error($error));
117
        }
118
119
        // Mark viewed by user (if required).
120
        $completion = new \completion_info($course);
121
        $completion->set_module_viewed($cm);
122
123
        // Validate if the user is in a role allowed to join.
124
        if (!has_capability('moodle/category:manage', $context) &&
125
            !has_capability('mod/bigbluebuttonbn:join', $context)) {
126
            $error = get_string('view_nojoin', 'bigbluebuttonbn');
127
            return(self::mobile_print_error($error));
128
        }
129
130
        // Initialize session variable used across views.
131
        $SESSION->bigbluebuttonbn_bbbsession = $bbbsession;
132
133
        // Logic of bbb_view for join to session.
134
        // If user is not administrator nor moderator (user is student) and waiting is required.
135
        if (!$bbbsession['administrator'] && !$bbbsession['moderator'] && $bbbsession['wait']) {
136
            $canjoin = \mod_bigbluebuttonbn\locallib\bigbluebutton::can_join_meeting($args->cmid);
137
            if (!$canjoin['can_join']) {
138
                $message = get_string('view_message_conference_wait_for_moderator', 'bigbluebuttonbn');
139
                return (self::mobile_print_notification($bigbluebuttonbn, $cm, $message));
140
            }
141
        }
142
143
        // See if the BBB session is already in progress.
144
        if (!bigbluebuttonbn_is_meeting_running($bbbsession['meetingid'])) {
145
146
            // The meeting doesnt exist in BBB server, must be created.
147
            $response = bigbluebuttonbn_get_create_meeting_array(
148
                \mod_bigbluebuttonbn\locallib\mobileview::create_meeting_data($bbbsession),
149
                \mod_bigbluebuttonbn\locallib\mobileview::create_meeting_metadata($bbbsession),
150
                $bbbsession['presentation']['name'],
151
                $bbbsession['presentation']['url']
152
            );
153
154 View Code Duplication
            if (empty($response)) {
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...
155
                // The BBB server is failing.
156
                if ($bbbsession['administrator']) {
157
                    $e = get_string('view_error_unable_join', 'bigbluebuttonbn');
158
                } else if ($bbbsession['moderator']) {
159
                    $e = get_string('view_error_unable_join_teacher', 'bigbluebuttonbn');
160
                } else {
161
                    $e = get_string('view_error_unable_join_student', 'bigbluebuttonbn');
162
                }
163
                return(self::mobile_print_error($e));
164
            }
165
            if ($response['returncode'] == 'FAILED') {
166
                // The meeting could not be created.
167
                $errorkey = bigbluebuttonbn_get_error_key($response['messageKey'],  'view_error_create');
168
                $e = get_string($errorkey, 'bigbluebuttonbn');
169
                return(self::mobile_print_error($e));
170
            }
171
            if ($response['hasBeenForciblyEnded'] == 'true') {
172
                $e = get_string('index_error_forciblyended', 'bigbluebuttonbn');
173
                return(self::mobile_print_error($e));
174
            }
175
176
            // Event meeting created.
177
            bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['meeting_create'], $bigbluebuttonbn);
178
            // Insert a record that meeting was created.
179
            $overrides = array('meetingid' => $bbbsession['meetingid']);
180
            $meta = '{"record":'.($bbbsession['record'] ? 'true' : 'false').'}';
181
            bigbluebuttonbn_log($bbbsession['bigbluebuttonbn'], BIGBLUEBUTTONBN_LOG_EVENT_CREATE, $overrides, $meta);
182
        }
183
184
        // It is part of 'bigbluebuttonbn_bbb_view_join_meeting' in bbb_view.
185
        // Update the cache.
186
        $meetinginfo = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], BIGBLUEBUTTONBN_UPDATE_CACHE);
187
        if ($bbbsession['userlimit'] > 0 && intval($meetinginfo['participantCount']) >= $bbbsession['userlimit']) {
188
            // No more users allowed to join.
189
            $message = get_string('view_error_userlimit_reached', 'bigbluebuttonbn');
190
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message));
191
        }
192
193
        // Build final url to BBB.
194
        $bbbsession['createtime'] = $meetinginfo['createTime'];
195
        $urltojoin = \mod_bigbluebuttonbn\locallib\mobileview::build_url_join_session($bbbsession);
196
197
        // Check groups access and show message.
198
        $msjgroup = array();
199
        $groupmode = groups_get_activity_groupmode($bbbsession['cm']);
200
        if ($groupmode != NOGROUPS) {
201
            $msjgroup = array("message" => get_string('view_mobile_message_groups_not_supported',
202
                'bigbluebuttonbn'));
203
        }
204
205
        $data = array(
206
            'bigbluebuttonbn' => $bigbluebuttonbn,
207
            'bbbsession' => (object) $bbbsession,
208
            'msjgroup' => $msjgroup,
209
            'urltojoin' => $urltojoin,
210
            'cmid' => $cm->id,
211
            'courseid' => $course->id
212
        );
213
214
        // We want to show a notification when user excedded 45 seconds without click button.
215
        $jstimecreatedmeeting = 'setTimeout(function(){
216
        document.getElementById("bigbluebuttonbn-mobile-notifications").style.display = "block";
217
        document.getElementById("bigbluebuttonbn-mobile-join").disabled = true;
218
        document.getElementById("bigbluebuttonbn-mobile-meetingready").style.display = "none";
219
        }, 45000);';
220
221
        return array(
222
            'templates' => array(
223
                array(
224
                    'id' => 'main',
225
                    'html' => $OUTPUT->render_from_template('mod_bigbluebuttonbn/mobile_view_page', $data),
226
                ),
227
            ),
228
            'javascript' => $jstimecreatedmeeting,
229
            'otherdata' => '',
230
            'files' => ''
231
        );
232
    }
233
234
    /**
235
     * Returns the view for errors.
236
     * @param string $error Error to display.
237
     *
238
     * @return array       HTML, javascript and otherdata
239
     */
240
    protected static function mobile_print_error($error) {
241
242
        global $OUTPUT;
243
        $data = array(
244
            'error' => $error
245
        );
246
247
        return array(
248
            'templates' => array(
249
                array(
250
                    'id' => 'main',
251
                    'html' => $OUTPUT->render_from_template('mod_bigbluebuttonbn/mobile_view_error', $data),
252
                ),
253
            ),
254
            'javascript' => '',
255
            'otherdata' => '',
256
            'files' => ''
257
        );
258
    }
259
260
    /**
261
     * Returns the view for messages.
262
     * @param object $bigbluebuttonbn
263
     * @param stdClass $cm
264
     * @param string $message Message to display.
265
     * @param array $notstarted Extra messages for not started session.
266
     * @return array HTML, javascript and otherdata
267
     */
268
    protected static function mobile_print_notification($bigbluebuttonbn, $cm, $message, $notstarted = array()) {
269
270
        global $OUTPUT, $CFG;
271
        $data = array(
272
            'bigbluebuttonbn' => $bigbluebuttonbn,
273
            'cmid' => $cm->id,
274
            'message' => $message,
275
            'not_started' => $notstarted
276
        );
277
278
        return array(
279
            'templates' => array(
280
                array(
281
                    'id' => 'main',
282
                    'html' => $OUTPUT->render_from_template('mod_bigbluebuttonbn/mobile_view_notification', $data),
283
                ),
284
            ),
285
            'javascript' => file_get_contents($CFG->dirroot . '/mod/bigbluebuttonbn/mobile.notification.js'),
286
            'otherdata' => '',
287
            'files' => ''
288
        );
289
    }
290
}
291