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

mobile::mobile_issues_view()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 8
nop 1
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
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_external;
32
require_once($CFG->dirroot . '/mod/bigbluebuttonbn/locallib.php');
33
34
/**
35
 * Mobile output class for bigbluebuttonbn
36
 *
37
 * @package    mod_bigbluebuttonbn
38
 * @copyright  2018 onwards, Blindside Networks Inc
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 * @author     Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
41
 */
42
class mobile {
43
44
    /**
45
     * Returns the bigbluebuttonbn course view for the mobile app.
46
     * @param $args
47
     * @return array HTML, javascript and other data.
48
     * @throws \coding_exception
49
     * @throws \moodle_exception
50
     * @throws \require_login_exception
51
     * @throws \required_capability_exception
52
     */
53
    public static function mobile_course_view($args) {
54
55
        global $OUTPUT, $SESSION, $CFG;
56
57
        $args = (object) $args;
58
        $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...
59
        if (!$viewinstance) {
60
            $error = get_string('view_error_url_missing_parameters', 'bigbluebuttonbn');
61
            return(self::mobile_print_error($error));
62
        }
63
64
        $cm = $viewinstance['cm'];
65
        $course = $viewinstance['course'];
66
        $bigbluebuttonbn = $viewinstance['bigbluebuttonbn'];
67
        $context = context_module::instance($cm->id);
68
69
        require_login($course->id, false , $cm, true, true);
70
        require_capability('mod/bigbluebuttonbn:join', $context);
71
72
        // Add view event.
73
        bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['view'], $bigbluebuttonbn);
74
75
        // Create array bbbsession with configuration for BBB server.
76
        $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...
77
        $bbbsession['coursename'] = $course->fullname;
78
        $bbbsession['cm'] = $cm;
79
        $bbbsession['bigbluebuttonbn'] = $bigbluebuttonbn;
80
        $bbbsession = \mod_bigbluebuttonbn\locallib\mobileview::bigbluebuttonbn_view_bbbsession_set($context, $bbbsession);
81
82
        // Check activity status.
83
        $activitystatus = \mod_bigbluebuttonbn\locallib\mobileview::bigbluebuttonbn_view_get_activity_status($bbbsession);
84
        if ($activitystatus == 'not_started') {
85
            $message = get_string('view_message_conference_not_started', 'bigbluebuttonbn');
86
87
            $notstarted = array();
88
            $notstarted['starts_at'] = '';
89
            $notstarted['ends_at'] = '';
90
            if (!empty($bigbluebuttonbn->openingtime)) {
91
                $notstarted['starts_at'] = sprintf(
92
                    '%s: %s',
93
                    get_string('mod_form_field_openingtime', 'bigbluebuttonbn'),
94
                    userdate($bigbluebuttonbn->openingtime)
95
                );
96
            }
97
            if (!empty($bigbluebuttonbn->closingtime)) {
98
                $notstarted['ends_at'] = sprintf(
99
                    '%s: %s',
100
                    get_string('mod_form_field_closingtime', 'bigbluebuttonbn'),
101
                    userdate($bigbluebuttonbn->closingtime)
102
                );
103
            }
104
105
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message, $notstarted));
106
        }
107
        if ($activitystatus == 'ended') {
108
            $message = get_string('view_message_conference_has_ended', 'bigbluebuttonbn');
109
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message));
110
        }
111
112
        // Check if the BBB server is working.
113
        $serverversion = bigbluebuttonbn_get_server_version();
114
        $bbbsession['serverversion'] = (string) $serverversion;
115 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...
116
117
            if ($bbbsession['administrator']) {
118
                $error = get_string('view_error_unable_join', 'bigbluebuttonbn');
119
            } else if ($bbbsession['moderator']) {
120
                $error = get_string('view_error_unable_join_teacher', 'bigbluebuttonbn');
121
            } else {
122
                $error = get_string('view_error_unable_join_student', 'bigbluebuttonbn');
123
            }
124
125
            return(self::mobile_print_error($error));
126
        }
127
128
        // Mark viewed by user (if required).
129
        $completion = new \completion_info($course);
130
        $completion->set_module_viewed($cm);
131
132
        // Validate if the user is in a role allowed to join.
133
        if (!has_capability('moodle/category:manage', $context) &&
134
            !has_capability('mod/bigbluebuttonbn:join', $context)) {
135
            $error = get_string('view_nojoin', 'bigbluebuttonbn');
136
            return(self::mobile_print_error($error));
137
        }
138
139
        // Operation URLs.
140
        $bbbsession['bigbluebuttonbnURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/view.php?id=' . $bbbsession['cm']->id;
141
        $bbbsession['logoutURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=logout&id='.$args->cmid .
142
            '&bn=' . $bbbsession['bigbluebuttonbn']->id;
143
        $bbbsession['recordingReadyURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=recording_' .
144
            'ready&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id;
145
        $bbbsession['meetingEventsURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_broker.php?action=meeting' .
146
            '_events&bigbluebuttonbn=' . $bbbsession['bigbluebuttonbn']->id;
147
        $bbbsession['joinURL'] = $CFG->wwwroot . '/mod/bigbluebuttonbn/bbb_view.php?action=join&id=' . $args->cmid .
148
            '&bn=' . $bbbsession['bigbluebuttonbn']->id;
149
150
        // Initialize session variable used across views.
151
        $SESSION->bigbluebuttonbn_bbbsession = $bbbsession;
152
153
        // Logic of bbb_view for join to session.
154
        // If user is not administrator nor moderator (user is student) and waiting is required.
155
        if (!$bbbsession['administrator'] && !$bbbsession['moderator'] && $bbbsession['wait']) {
156
            $message = get_string('view_message_conference_wait_for_moderator', 'bigbluebuttonbn');
157
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message));
158
        }
159
160
        // See if the BBB session is already in progress.
161
        if (!bigbluebuttonbn_is_meeting_running($bbbsession['meetingid'])) {
162
163
            // The meeting doesnt exist in BBB server, must be created.
164
            $response = bigbluebuttonbn_get_create_meeting_array(
165
                \mod_bigbluebuttonbn\locallib\mobileview::bigbluebutton_bbb_view_create_meeting_data($bbbsession),
166
                \mod_bigbluebuttonbn\locallib\mobileview::bigbluebutton_bbb_view_create_meeting_metadata($bbbsession),
167
                $bbbsession['presentation']['name'],
168
                $bbbsession['presentation']['url']
169
            );
170
171 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...
172
                // The BBB server is failing.
173
                if ($bbbsession['administrator']) {
174
                    $e = get_string('view_error_unable_join', 'bigbluebuttonbn');
175
                } else if ($bbbsession['moderator']) {
176
                    $e = get_string('view_error_unable_join_teacher', 'bigbluebuttonbn');
177
                } else {
178
                    $e = get_string('view_error_unable_join_student', 'bigbluebuttonbn');
179
                }
180
                return(self::mobile_print_error($e));
181
            }
182
            if ($response['returncode'] == 'FAILED') {
183
                // The meeting could not be created.
184
                $errorkey = bigbluebuttonbn_get_error_key($response['messageKey'],  'view_error_create');
185
                $e = get_string($errorkey, 'bigbluebuttonbn');
186
                return(self::mobile_print_error($e));
187
            }
188
            if ($response['hasBeenForciblyEnded'] == 'true') {
189
                $e = get_string('index_error_forciblyended', 'bigbluebuttonbn');
190
                return(self::mobile_print_error($e));
191
            }
192
193
            // Event meeting created.
194
            bigbluebuttonbn_event_log(\mod_bigbluebuttonbn\event\events::$events['meeting_create'], $bigbluebuttonbn);
195
            // Insert a record that meeting was created.
196
            $overrides = array('meetingid' => $bbbsession['meetingid']);
197
            $meta = '{"record":'.($bbbsession['record'] ? 'true' : 'false').'}';
198
            bigbluebuttonbn_log($bbbsession['bigbluebuttonbn'], BIGBLUEBUTTONBN_LOG_EVENT_CREATE, $overrides, $meta);
199
        }
200
201
        // It is part of 'bigbluebutton_bbb_view_join_meeting' in bbb_view.
202
        // Update the cache.
203
        $meetinginfo = bigbluebuttonbn_get_meeting_info($bbbsession['meetingid'], BIGBLUEBUTTONBN_UPDATE_CACHE);
204
        if ($bbbsession['userlimit'] > 0 && intval($meetinginfo['participantCount']) >= $bbbsession['userlimit']) {
205
            // No more users allowed to join.
206
            $message = get_string('view_error_userlimit_reached', 'bigbluebuttonbn');
207
            return(self::mobile_print_notification($bigbluebuttonbn, $cm, $message));
208
        }
209
210
        // Build final url to BBB.
211
        $urltojoin = \mod_bigbluebuttonbn\locallib\mobileview::build_url_join_session($bbbsession);
212
213
        $data = array(
214
            'bigbluebuttonbn' => $bigbluebuttonbn,
215
            'bbbsession' => (object) $bbbsession,
216
            'urltojoin' => $urltojoin,
217
            'cmid' => $cm->id,
218
            'courseid' => $args->courseid
219
        );
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' => '',
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 $bigbluebuttonbn
263
     * @param $cm
264
     * @param  string $message Message to display.
265
     * @param  array $notstarted Extra messages for not started session.
266
     *
267
     * @return array       HTML, javascript and otherdata
268
     */
269
    protected static function mobile_print_notification($bigbluebuttonbn, $cm, $message, $notstarted = array()) {
270
271
        global $OUTPUT;
272
        $data = array(
273
            'bigbluebuttonbn' => $bigbluebuttonbn,
274
            'cmid' => $cm->id,
275
            'message' => $message,
276
            'not_started' => $notstarted
277
        );
278
279
        return array(
280
            'templates' => array(
281
                array(
282
                    'id' => 'main',
283
                    'html' => $OUTPUT->render_from_template('mod_bigbluebuttonbn/mobile_view_notification', $data),
284
                ),
285
            ),
286
            'javascript' => '',
287
            'otherdata' => '',
288
            'files' => ''
289
        );
290
    }
291
}
292