Completed
Push — master ( 980cba...3f169a )
by Jesus
03:01
created

mod_bigbluebuttonbn_mod_form::validation()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 9
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 17
rs 7.7777
1
<?php
2
/**
3
 * Config all BigBlueButtonBN 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
12
defined('MOODLE_INTERNAL') || die();
13
14
require_once(dirname(__FILE__).'/locallib.php');
15
require_once($CFG->dirroot.'/course/moodleform_mod.php');
16
17
class mod_bigbluebuttonbn_mod_form extends moodleform_mod {
18
19
    function definition() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
        global $CFG, $DB, $USER, $BIGBLUEBUTTONBN_CFG;
21
22
        $course_id = optional_param('course', 0, PARAM_INT); // course ID, or
23
        $course_module_id = optional_param('update', 0, PARAM_INT); // course_module ID, or
24
        if ($course_id) {
25
            $course = $DB->get_record('course', array('id' => $course_id), '*', MUST_EXIST);
26
            $bigbluebuttonbn = null;
27
        } else if ($course_module_id) {
28
            $cm = get_coursemodule_from_id('bigbluebuttonbn', $course_module_id, 0, false, MUST_EXIST);
29
            $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
30
            $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $cm->instance), '*', MUST_EXIST);
31
        }
32
33
        $context = bigbluebuttonbn_get_context_course($course->id);
0 ignored issues
show
Bug introduced by
The variable $course does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
34
35
        //BigBlueButton server data
36
        $endpoint = bigbluebuttonbn_get_cfg_server_url();
37
38
        //UI configuration options
39
        $voicebridge_editable = bigbluebuttonbn_get_cfg_voicebridge_editable();
40
        $recording_default = bigbluebuttonbn_get_cfg_recording_default();
41
        $recording_editable = bigbluebuttonbn_get_cfg_recording_editable();
42
        $recording_tagging_default = bigbluebuttonbn_get_cfg_recording_tagging_default();
43
        $recording_tagging_editable = bigbluebuttonbn_get_cfg_recording_tagging_editable();
44
        $waitformoderator_default = bigbluebuttonbn_get_cfg_waitformoderator_default();
45
        $waitformoderator_editable = bigbluebuttonbn_get_cfg_waitformoderator_editable();
46
        $userlimit_default = bigbluebuttonbn_get_cfg_userlimit_default();
47
        $userlimit_editable = bigbluebuttonbn_get_cfg_userlimit_editable();
48
        $preuploadpresentation_enabled = bigbluebuttonbn_get_cfg_preuploadpresentation_enabled();
49
        $sendnotifications_enabled = bigbluebuttonbn_get_cfg_sendnotifications_enabled(); 
50
51
        //Validates if the BigBlueButton server is running 
52
        $serverVersion = bigbluebuttonbn_getServerVersion($endpoint);
53
        if ( !isset($serverVersion) ) {
54
            print_error( 'general_error_unable_connect', 'bigbluebuttonbn', $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn' );
55
        }
56
57
        $mform =& $this->_form;
58
        $current_activity =& $this->current;
59
60
        //-------------------------------------------------------------------------------
61
        // First block starts here
62
        //-------------------------------------------------------------------------------
63
        $mform->addElement('header', 'general', get_string('mod_form_block_general', 'bigbluebuttonbn'));
64
65
        $mform->addElement('text', 'name', get_string('mod_form_field_name','bigbluebuttonbn'), 'maxlength="64" size="32"');
66
        $mform->setType('name', PARAM_TEXT);
67
        $mform->addRule('name', null, 'required', null, 'client');
68
69
        $version_major = bigbluebuttonbn_get_moodle_version_major();
70
        if ( $version_major < '2015051100' ) {
71
            //This is valid before v2.9
72
            $this->add_intro_editor(false, get_string('mod_form_field_intro', 'bigbluebuttonbn'));
73
        } else {
74
            //This is valid after v2.9
75
            $this->standard_intro_elements(get_string('mod_form_field_intro', 'bigbluebuttonbn'));
76
        }
77
        $mform->setAdvanced('introeditor');
78
        $mform->setAdvanced('showdescription');
79
80
        $mform->addElement('textarea', 'welcome', get_string('mod_form_field_welcome','bigbluebuttonbn'), 'wrap="virtual" rows="5" cols="60"');
81
        $mform->addHelpButton('welcome', 'mod_form_field_welcome', 'bigbluebuttonbn');
82
        $mform->setType('welcome', PARAM_TEXT);
83
        $mform->setAdvanced('welcome');
84
85
        if ( $voicebridge_editable ) {
86
            $mform->addElement('text', 'voicebridge', get_string('mod_form_field_voicebridge','bigbluebuttonbn'), array('maxlength'=>4, 'size'=>6));
87
            $mform->addRule('voicebridge', get_string('mod_form_field_voicebridge_format_error', 'bigbluebuttonbn'), 'numeric', '####', 'server');
88
            $mform->setDefault( 'voicebridge', 0 );
89
            $mform->addHelpButton('voicebridge', 'mod_form_field_voicebridge', 'bigbluebuttonbn');
90
            $mform->setAdvanced('voicebridge');
91
        }
92
        $mform->setType('voicebridge', PARAM_INT);
93
94 View Code Duplication
        if ( $waitformoderator_editable ) {
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...
95
            $mform->addElement('checkbox', 'wait', get_string('mod_form_field_wait', 'bigbluebuttonbn'));
96
            $mform->addHelpButton('wait', 'mod_form_field_wait', 'bigbluebuttonbn');
97
            $mform->setDefault( 'wait', $waitformoderator_default );
98
            $mform->setAdvanced('wait');
99
        } else {
100
            $mform->addElement('hidden', 'wait', $waitformoderator_default );
101
        }
102
        $mform->setType('wait', PARAM_INT);
103
104 View Code Duplication
        if ( $userlimit_editable ) {
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
            $mform->addElement('text', 'userlimit', get_string('mod_form_field_userlimit','bigbluebuttonbn'), 'maxlength="3" size="5"' );
106
            $mform->addHelpButton('userlimit', 'mod_form_field_userlimit', 'bigbluebuttonbn');
107
            $mform->setDefault( 'userlimit', $userlimit_default );
108
        } else {
109
            $mform->addElement('hidden', 'userlimit', $userlimit_default );
110
        }
111
        $mform->setType('userlimit', PARAM_TEXT);
112
113
        if ( floatval($serverVersion) >= 0.8 ) {
114 View Code Duplication
            if ( $recording_editable ) {
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...
115
                $mform->addElement('checkbox', 'record', get_string('mod_form_field_record', 'bigbluebuttonbn'));
116
                $mform->setDefault( 'record', $recording_default );
117
                $mform->setAdvanced('record');
118
            } else {
119
                $mform->addElement('hidden', 'record', $recording_default);
120
            }
121
            $mform->setType('record', PARAM_INT);
122
123 View Code Duplication
            if ( $recording_tagging_editable ) {
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...
124
                $mform->addElement('checkbox', 'tagging', get_string('mod_form_field_recordingtagging', 'bigbluebuttonbn'));
125
                $mform->setDefault('tagging', $recording_tagging_default);
126
                $mform->setAdvanced('tagging');
127
            } else {
128
                $mform->addElement('hidden', 'tagging', $recording_tagging_default );
129
            }
130
            $mform->setType('tagging', PARAM_INT);
131
        }
132
133
        if ( $sendnotifications_enabled ) {
134
            $mform->addElement('checkbox', 'notification', get_string('mod_form_field_notification', 'bigbluebuttonbn'));
135
            if ($this->current->instance) {
136
                $mform->addHelpButton('notification', 'mod_form_field_notification', 'bigbluebuttonbn');
137
            } else {
138
                $mform->addHelpButton('notification', 'mod_form_field_notification', 'bigbluebuttonbn');
139
            }
140
            $mform->setDefault('notification', 0);
141
        }
142
        $mform->setType('notification', PARAM_INT);
143
        //-------------------------------------------------------------------------------
144
        // First block ends here
145
        //-------------------------------------------------------------------------------
146
147
148
        //-------------------------------------------------------------------------------
149
        // Second block starts here
150
        //-------------------------------------------------------------------------------
151
        if ( $preuploadpresentation_enabled ) {
152
            $mform->addElement('header', 'preupload', get_string('mod_form_block_presentation', 'bigbluebuttonbn'));
153
            $mform->setExpanded('preupload');
154
155
            $filemanager_options = array();
156
            $filemanager_options['accepted_types'] = '*';
157
            $filemanager_options['maxbytes'] = 0;
158
            $filemanager_options['subdirs'] = 0;
159
            $filemanager_options['maxfiles'] = 1;
160
            $filemanager_options['mainfile'] = true;
161
162
            $mform->addElement('filemanager', 'presentation', get_string('selectfiles'), null, $filemanager_options);
163
        }
164
        //-------------------------------------------------------------------------------
165
        // Second block ends here
166
        //-------------------------------------------------------------------------------
167
168
169
        //-------------------------------------------------------------------------------
170
        // Third block starts here
171
        //-------------------------------------------------------------------------------
172
        $mform->addElement('header', 'permission', get_string('mod_form_block_participants', 'bigbluebuttonbn'));
173
174
        // Data required for "Add participant" and initial "Participant list" setup
175
        $roles = bigbluebuttonbn_get_roles();
176
        $users = bigbluebuttonbn_get_users($context);
177
178
        $participant_list = bigbluebuttonbn_get_participant_list($bigbluebuttonbn, $context);
0 ignored issues
show
Bug introduced by
The variable $bigbluebuttonbn does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
179
        $mform->addElement('hidden', 'participants', json_encode($participant_list));
180
        $mform->setType('participants', PARAM_TEXT);
181
182
        $html_participant_selection = ''.
183
             '<div id="fitem_bigbluebuttonbn_participant_selection" class="fitem fitem_fselect">'."\n".
184
             '  <div class="fitemtitle">'."\n".
185
             '    <label for="bigbluebuttonbn_participant_selectiontype">'.get_string('mod_form_field_participant_add', 'bigbluebuttonbn').' </label>'."\n".
186
             '  </div>'."\n".
187
             '  <div class="felement fselect">'."\n".
188
             '    <select id="bigbluebuttonbn_participant_selection_type" onchange="bigbluebuttonbn_participant_selection_set(); return 0;">'."\n".
189
             '      <option value="all" selected="selected">'.get_string('mod_form_field_participant_list_type_all', 'bigbluebuttonbn').'</option>'."\n".
190
             '      <option value="role">'.get_string('mod_form_field_participant_list_type_role', 'bigbluebuttonbn').'</option>'."\n".
191
             '      <option value="user">'.get_string('mod_form_field_participant_list_type_user', 'bigbluebuttonbn').'</option>'."\n".
192
             '    </select>'."\n".
193
             '    &nbsp;&nbsp;'."\n".
194
             '    <select id="bigbluebuttonbn_participant_selection" disabled="disabled">'."\n".
195
             '      <option value="all" selected="selected">---------------</option>'."\n".
196
             '    </select>'."\n".
197
             '    &nbsp;&nbsp;'."\n".
198
             '    <input value="'.get_string('mod_form_field_participant_list_action_add', 'bigbluebuttonbn').'" type="button" id="id_addselectionid" onclick="bigbluebuttonbn_participant_add(); return 0;" />'."\n".
199
             '  </div>'."\n".
200
             '</div>'."\n".
201
             '<div id="fitem_bigbluebuttonbn_participant_list" class="fitem">'."\n".
202
             '  <div class="fitemtitle">'."\n".
203
             '    <label for="bigbluebuttonbn_participant_list">'.get_string('mod_form_field_participant_list', 'bigbluebuttonbn').' </label>'."\n".
204
             '  </div>'."\n".
205
             '  <div class="felement fselect">'."\n".
206
             '    <table id="participant_list_table">'."\n";
207
208
        // Add participant list
209
        foreach($participant_list as $participant){
210
            $participant_selectionid = '';
211
            $participant_selectiontype = $participant['selectiontype'];
212
            if( $participant_selectiontype == 'all') {
213
                $participant_selectiontype = '<b><i>'.get_string('mod_form_field_participant_list_type_'.$participant_selectiontype, 'bigbluebuttonbn').'</i></b>';
214
            } else {
215
                if ( $participant_selectiontype == 'role') {
216
                    $participant_selectionid = bigbluebuttonbn_get_role_name($participant['selectionid']);
217
                } else {
218
                    foreach($users as $user){
219
                        if( $user->id == $participant['selectionid']) {
220
                            $participant_selectionid = $user->firstname.' '.$user->lastname;
221
                            break;
222
                        }
223
                    }
224
                }
225
                $participant_selectiontype = '<b><i>'.get_string('mod_form_field_participant_list_type_'.$participant_selectiontype, 'bigbluebuttonbn').':</i></b>&nbsp;';
226
            }
227
228
            $html_participant_selection .= ''.
229
                '      <tr id="participant_list_tr_'.$participant['selectiontype'].'-'.$participant['selectionid'].'">'."\n".
230
                '        <td width="20px"><a onclick="bigbluebuttonbn_participant_remove(\''.$participant['selectiontype'].'\', \''.$participant['selectionid'].'\'); return 0;" title="'.get_string('mod_form_field_participant_list_action_remove', 'bigbluebuttonbn').'">x</a></td>'."\n".
231
                '        <td width="125px">'.$participant_selectiontype.'</td>'."\n".
232
                '        <td>'.$participant_selectionid.'</td>'."\n".
233
                '        <td><i>&nbsp;'.get_string('mod_form_field_participant_list_text_as', 'bigbluebuttonbn').'&nbsp;</i>'."\n".
234
                '          <select id="participant_list_role_'.$participant['selectiontype'].'-'.$participant['selectionid'].'" onchange="bigbluebuttonbn_participant_list_role_update(\''.$participant['selectiontype'].'\', \''.$participant['selectionid'].'\'); return 0;">'."\n".
235
                '            <option value="'.BIGBLUEBUTTONBN_ROLE_VIEWER.'" '.($participant['role'] == BIGBLUEBUTTONBN_ROLE_VIEWER? 'selected="selected" ': '').'>'.get_string('mod_form_field_participant_bbb_role_'.BIGBLUEBUTTONBN_ROLE_VIEWER, 'bigbluebuttonbn').'</option>'."\n".
236
                '            <option value="'.BIGBLUEBUTTONBN_ROLE_MODERATOR.'" '.($participant['role'] == BIGBLUEBUTTONBN_ROLE_MODERATOR? 'selected="selected" ': '').'>'.get_string('mod_form_field_participant_bbb_role_'.BIGBLUEBUTTONBN_ROLE_MODERATOR, 'bigbluebuttonbn').'</option><select>'."\n".
237
                '        </td>'."\n".
238
                '      </tr>'."\n";
239
        }
240
241
        $html_participant_selection .= ''.
242
             '    </table>'."\n".
243
             '  </div>'."\n".
244
             '</div>'."\n".
245
             '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/bigbluebuttonbn/mod_form.js">'."\n".
246
             '</script>'."\n";
247
248
        $mform->addElement('html', $html_participant_selection);
249
250
        // Add data
251
        $mform->addElement('html', '<script type="text/javascript">var bigbluebuttonbn_participant_selection = {"all": [], "role": '.json_encode($roles).', "user": '.bigbluebuttonbn_get_users_json($users).'}; </script>');
252
        $mform->addElement('html', '<script type="text/javascript">var bigbluebuttonbn_participant_list = '.json_encode($participant_list).'; </script>');
253
        $bigbluebuttonbn_strings = Array( "as" => get_string('mod_form_field_participant_list_text_as', 'bigbluebuttonbn'),
254
                                          "viewer" => get_string('mod_form_field_participant_bbb_role_viewer', 'bigbluebuttonbn'),
255
                                          "moderator" => get_string('mod_form_field_participant_bbb_role_moderator', 'bigbluebuttonbn'),
256
                                          "remove" => get_string('mod_form_field_participant_list_action_remove', 'bigbluebuttonbn'),
257
                                    );
258
        $mform->addElement('html', '<script type="text/javascript">var bigbluebuttonbn_strings = '.json_encode($bigbluebuttonbn_strings).'; </script>');
259
        //-------------------------------------------------------------------------------
260
        // Third block ends here
261
        //-------------------------------------------------------------------------------
262
263
264
        //-------------------------------------------------------------------------------
265
        // Fourth block starts here
266
        //-------------------------------------------------------------------------------
267
        $mform->addElement('header', 'schedule', get_string('mod_form_block_schedule', 'bigbluebuttonbn'));
268
        if( isset($current_activity->openingtime) && $current_activity->openingtime != 0 || isset($current_activity->closingtime) && $current_activity->closingtime != 0 )
269
            $mform->setExpanded('schedule');
270
271
        $mform->addElement('date_time_selector', 'openingtime', get_string('mod_form_field_openingtime', 'bigbluebuttonbn'), array('optional' => true));
272
        $mform->setDefault('openingtime', 0);
273
        $mform->addElement('date_time_selector', 'closingtime', get_string('mod_form_field_closingtime', 'bigbluebuttonbn'), array('optional' => true));
274
        $mform->setDefault('closingtime', 0);
275
        //-------------------------------------------------------------------------------
276
        // Fourth block ends here
277
        //-------------------------------------------------------------------------------
278
279
280
        //-------------------------------------------------------------------------------
281
        // add standard elements, common to all modules
282
        $this->standard_coursemodule_elements();
283
284
        //-------------------------------------------------------------------------------
285
        // add standard buttons, common to all modules
286
        $this->add_action_buttons();
287
    }
288
289
    function data_preprocessing(&$default_values) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
290
        if ($this->current->instance) {
291
            // Editing existing instance - copy existing files into draft area.
292
            try {
293
                $draftitemid = file_get_submitted_draft_itemid('presentation');
294
                file_prepare_draft_area($draftitemid, $this->context->id, 'mod_bigbluebuttonbn', 'presentation', 0, array('subdirs'=>0, 'maxbytes' => 0, 'maxfiles' => 1, 'mainfile' => true));
295
                $default_values['presentation'] = $draftitemid;
296
            } catch (Exception $e){
297
                error_log("Presentation could not be loaded: ".$e->getMessage());
298
                return NULL;
299
            }
300
        }
301
    }
302
303
    function validation($data, $files) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
304
        $errors = parent::validation($data, $files);
305
306
        if ( isset($data['openingtime']) && isset($data['closingtime']) ) {
307
            if ( $data['openingtime'] != 0 && $data['closingtime'] != 0 && $data['closingtime'] < $data['openingtime']) {
308
                $errors['closingtime'] = get_string('bbbduetimeoverstartingtime', 'bigbluebuttonbn');
309
            }
310
        }
311
        
312
        if ( isset($data['voicebridge']) ) {
313
            if ( !bigbluebuttonbn_voicebridge_unique($data['voicebridge'], $data['instance'])) {
314
                $errors['voicebridge'] = get_string('mod_form_field_voicebridge_notunique_error', 'bigbluebuttonbn');
315
            }
316
        }
317
318
        return $errors;
319
    }
320
}