Completed
Push — master ( 12ecea...43b232 )
by Jesus
03:32
created

index.php ➔ displayBigBlueButtonRooms()   D

Complexity

Conditions 19
Paths 35

Size

Total Lines 101
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 19
eloc 59
c 3
b 1
f 1
nc 35
nop 6
dl 0
loc 101
rs 4.764

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
/**
3
 * View all BigBlueButton instances in this course.
4
 * 
5
 * @package   mod_bigbluebuttonbn
6
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
7
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
8
 * @copyright 2010-2015 Blindside Networks Inc.
9
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
10
 */
11
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
12
require_once(dirname(__FILE__).'/locallib.php');
13
14
$id = required_param('id', PARAM_INT);      // Course Module ID, or
15
$a  = optional_param('a', 0, PARAM_INT);    // bigbluebuttonbn instance ID
16
$g  = optional_param('g', 0, PARAM_INT);    // group instance ID
17
18
if ($id) {
19
    $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
20
} else {
21
    print_error('You must specify a course_module ID or an instance ID');
22
}
23
24
require_login($course, true);
25
26
$context = bigbluebuttonbn_get_context_course($course->id);
27
28
/// Print the header
29
$PAGE->set_url('/mod/bigbluebuttonbn/index.php', array('id'=>$id));
30
$PAGE->set_title(get_string('modulename', 'bigbluebuttonbn'));
31
$PAGE->set_heading($course->fullname);
32
$PAGE->set_cacheable(false);
33
$PAGE->set_pagelayout('incourse');
34
35
//$navigation = build_navigation(array('name' => get_string('modulename', 'bigbluebuttonbn'), 'link' => '', 'type' => 'activity'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
$PAGE->navbar->add(get_string('modulename', 'bigbluebuttonbn'), "index.php?id=$course->id");
37
38
/// Get all the appropriate data
39
if (! $bigbluebuttonbns = get_all_instances_in_course('bigbluebuttonbn', $course)) {
40
    notice('There are no instances of bigbluebuttonbn', "../../course/view.php?id=$course->id");
41
}
42
43
/// Print the list of instances
44
$timenow            = time();
45
$strweek            = get_string('week');
46
$strtopic           = get_string('topic');
47
$heading_name       = get_string('index_heading_name', 'bigbluebuttonbn' );
48
$heading_group      = get_string('index_heading_group', 'bigbluebuttonbn' );
49
$heading_users      = get_string('index_heading_users', 'bigbluebuttonbn');
50
$heading_viewer     = get_string('index_heading_viewer', 'bigbluebuttonbn');
51
$heading_moderator  = get_string('index_heading_moderator', 'bigbluebuttonbn' );
52
$heading_actions    = get_string('index_heading_actions', 'bigbluebuttonbn' );
53
$heading_recording  = get_string('index_heading_recording', 'bigbluebuttonbn' );
54
55
$table = new html_table();
56
57
$table->head  = array ($strweek, $heading_name, $heading_group, $heading_users, $heading_viewer, $heading_moderator, $heading_recording, $heading_actions );
58
$table->align = array ('center', 'left', 'center', 'center', 'center',  'center', 'center' );
59
60
$endpoint = bigbluebuttonbn_get_cfg_server_url();
61
$shared_secret = bigbluebuttonbn_get_cfg_shared_secret();
62
$logoutURL = $CFG->wwwroot;
63
64
$submit = optional_param('submit', '', PARAM_TEXT);
65
if ($submit === 'end') {
66
    //
67
    // A request to end the meeting
68
    //
69
    if (! $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $a), '*', MUST_EXIST) ) {
70
        print_error("BigBlueButton ID $a is incorrect");
71
    }
72
    $course = $DB->get_record('course', array('id' => $bigbluebuttonbn->course), '*', MUST_EXIST);
73
    $cm = get_coursemodule_from_instance('bigbluebuttonbn', $bigbluebuttonbn->id, $course->id, false, MUST_EXIST);
74
75
    //User roles
76 View Code Duplication
    if( $bigbluebuttonbn->participants == null || $bigbluebuttonbn->participants == "" || $bigbluebuttonbn->participants == "[]" ){
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...
77
        //The room that is being used comes from a previous version
78
        $moderator = has_capability('mod/bigbluebuttonbn:moderate', $context);
79
    } else {
80
        $moderator = bigbluebuttonbn_is_moderator($USER->id, get_user_roles($context, $USER->id, true), $bigbluebuttonbn->participants);
81
    }
82
    $administrator = has_capability('moodle/category:manage', $context);
83
84
    if( $moderator || $administrator ) {
85
        bigbluebuttonbn_event_log(BIGBLUEBUTTON_EVENT_MEETING_ENDED, $bigbluebuttonbn, $context, $cm);
86
87
        echo get_string('index_ending', 'bigbluebuttonbn');
88
89
        $meetingID = $bigbluebuttonbn->meetingid.'-'.$course->id.'-'.$bigbluebuttonbn->id;
90
        $modPW = $bigbluebuttonbn->moderatorpass;
91
        if( $g != '0'  ) {
92
            $getArray = bigbluebuttonbn_wrap_xml_load_file( bigbluebuttonbn_getEndMeetingURL( $meetingID.'['.$g.']', $modPW, $endpoint, $shared_secret ) );
93
        } else {
94
            $getArray = bigbluebuttonbn_wrap_xml_load_file(bigbluebuttonbn_getEndMeetingURL( $meetingID, $modPW, $endpoint, $shared_secret ));
95
        }
96
	   redirect('index.php?id='.$id);
97
    }
98
}
99
100
foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
101
    $cm = get_coursemodule_from_id('bigbluebuttonbn', $bigbluebuttonbn->coursemodule, 0, false, MUST_EXIST);
102
103
    //User roles
104 View Code Duplication
    if( $bigbluebuttonbn->participants == null || $bigbluebuttonbn->participants == "" || $bigbluebuttonbn->participants == "[]" ){
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...
105
        //The room that is being used comes from a previous version
106
        $moderator = has_capability('mod/bigbluebuttonbn:moderate', $context);
107
    } else {
108
        $moderator = bigbluebuttonbn_is_moderator($USER->id, get_user_roles($context, $USER->id, true), $bigbluebuttonbn->participants);
109
    }
110
    $administrator = has_capability('moodle/category:manage', $context);
111
112
    if ( groups_get_activity_groupmode($cm) > 0 ){
113
        $table->data[] = displayBigBlueButtonRooms($endpoint, $shared_secret, ($administrator || $moderator), $course, $bigbluebuttonbn, (object) array('id'=>0, 'name'=>get_string('allparticipants')));
114
        $groups = groups_get_activity_allowed_groups($cm);
115
        if( isset($groups)) {
116
            foreach( $groups as $group){
117
                $table->data[] = displayBigBlueButtonRooms($endpoint, $shared_secret, ($administrator || $moderator), $course, $bigbluebuttonbn, $group);
118
            }
119
        }
120
    } else {
121
        $table->data[] = displayBigBlueButtonRooms($endpoint, $shared_secret, ($administrator || $moderator), $course, $bigbluebuttonbn);
122
    }
123
}
124
125
echo $OUTPUT->header();
126
127
echo $OUTPUT->heading(get_string('index_heading', 'bigbluebuttonbn'));
128
echo html_writer::table($table);
129
130
echo $OUTPUT->footer();
131
132
/// Functions
133
function displayBigBlueButtonRooms($endpoint, $shared_secret, $moderator, $course, $bigbluebuttonbn, $groupObj = null ){
134
    $joinURL = null;
0 ignored issues
show
Unused Code introduced by
$joinURL is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
    $group = "-";
136
    $users = "-";
137
    $running = "-";
0 ignored issues
show
Unused Code introduced by
$running is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
    $actions = "-";
139
    $viewerList = "-";
140
    $moderatorList = "-";
141
    $recording = "-";
142
143
    if ( !$bigbluebuttonbn->visible ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
144
        // Nothing to do
145
    } else {
146
        $modPW = $bigbluebuttonbn->moderatorpass;
147
        $attPW = $bigbluebuttonbn->viewerpass;
0 ignored issues
show
Unused Code introduced by
$attPW is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
148
149
        $meetingID = $bigbluebuttonbn->meetingid.'-'.$course->id.'-'.$bigbluebuttonbn->id;
150
        //
151
        // Output Users in the meeting
152
        //
153
        if( $groupObj == null ){
154
            $meetingInfo = bigbluebuttonbn_getMeetingInfoArray( $meetingID, $modPW, $endpoint, $shared_secret );
155
            $joinURL = '<a href="view.php?id='.$bigbluebuttonbn->coursemodule.'">'.format_string($bigbluebuttonbn->name).'</a>';
156
        } else {
157
            $meetingInfo = bigbluebuttonbn_getMeetingInfoArray( $meetingID.'['.$groupObj->id.']', $modPW, $endpoint, $shared_secret );
158
            $joinURL = '<a href="view.php?id='.$bigbluebuttonbn->coursemodule.'&group='.$groupObj->id.'">'.format_string($bigbluebuttonbn->name).'</a>';
159
            $group = $groupObj->name;
160
        }
161
        
162
        if (!$meetingInfo) {
163
            //
164
            // The server was unreachable
165
            //
166
            print_error( get_string( 'index_error_unable_display', 'bigbluebuttonbn' ));
167
            return;
168
        }
169
170
        if (isset($meetingInfo['messageKey'])) {
171
            //
172
            // There was an error returned
173
            //
174
            if ($meetingInfo['messageKey'] == "checksumError") {
175
                print_error( get_string( 'index_error_checksum', 'bigbluebuttonbn' ));
176
                return;
177
            }
178
179
            if ($meetingInfo['messageKey'] == "notFound" || $meetingInfo['messageKey'] == "invalidMeetingId") {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
180
                //
181
                // The meeting does not exist yet on the BigBlueButton server.  This is OK.
182
                //
183
            } else {
184
                //
185
                // There was an error
186
                //
187
                $users = $meetingInfo['messageKey'].": ".$meetingInfo['message'];
188
            }
189
        } else {
190
            //
191
            // The meeting info was returned
192
            //
193
            if ($meetingInfo['running'] == 'true') {
194
                if ( $moderator ) {
195
                    if( $groupObj == null ) {
196
                        $actions = '<form name="form1" method="post" action=""><INPUT type="hidden" name="id" value="'.$course->id.'"><INPUT type="hidden" name="a" value="'.$bigbluebuttonbn->id.'"><INPUT type="submit" name="submit" value="end" onclick="return confirm(\''. get_string('index_confirm_end', 'bigbluebuttonbn' ).'\')"></form>';
197
                    } else {
198
                        $actions = '<form name="form1" method="post" action=""><INPUT type="hidden" name="id" value="'.$course->id.'"><INPUT type="hidden" name="a" value="'.$bigbluebuttonbn->id.'"><INPUT type="hidden" name="g" value="'.$groupObj->id.'"><INPUT type="submit" name="submit" value="end" onclick="return confirm(\''. get_string('index_confirm_end', 'bigbluebuttonbn' ).'\')"></form>';
199
                    }
200
                }
201
                if ( isset($meetingInfo['recording']) && $meetingInfo['recording'] == 'true' ){ // if it has been set when meeting created, set the variable on/off
202
                    $recording = get_string('index_enabled', 'bigbluebuttonbn' );
203
                }
204
                 
205
                $xml = $meetingInfo['attendees'];
206
                if (count( $xml ) && count( $xml->attendee ) ) {
207
                    $users = count( $xml->attendee );
208
                    $viewer_count = 0;
209
                    $moderator_count = 0;
210
                    foreach ( $xml->attendee as $attendee ) {
211
                        if ($attendee->role == "MODERATOR" ) {
212
                            if ( $viewer_count++ > 0 ) {
213
                                $moderatorList .= ", ";
214
                            } else {
215
                                $moderatorList = "";
216
                            }
217
                            $moderatorList .= $attendee->fullName;
218
                        } else {
219
                            if ( $moderator_count++ > 0 ) {
220
                                $viewerList .= ", ";
221
                            } else {
222
                                $viewerList = "";
223
                            }
224
                            $viewerList .= $attendee->fullName;
225
                        }
226
                    }
227
                }
228
            }
229
        }
230
231
        return array ($bigbluebuttonbn->section, $joinURL, $group, $users, $viewerList, $moderatorList, $recording, $actions );
232
    }
233
}