mod_bigbluebuttonbn_mod_form   F
last analyzed

Complexity

Total Complexity 83

Size/Duplication

Total Lines 659
Duplicated Lines 1.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 12
loc 659
rs 1.941
c 0
b 0
f 0
wmc 83
lcom 1
cbo 1

17 Methods

Rating   Name   Duplication   Size   Complexity  
B definition() 0 64 4
A data_preprocessing() 0 28 5
B validation() 0 15 8
A add_completion_rules() 0 40 2
A completion_rule_enabled() 0 3 2
A data_postprocessing() 0 10 5
A bigbluebuttonbn_mform_add_block_profiles() 0 8 2
A bigbluebuttonbn_mform_add_block_general() 0 17 3
B bigbluebuttonbn_mform_add_block_room_room() 0 74 8
F bigbluebuttonbn_mform_add_block_locksettings() 6 94 11
B bigbluebuttonbn_mform_add_block_room_recordings() 6 46 7
B bigbluebuttonbn_mform_add_block_room() 0 12 9
A bigbluebuttonbn_mform_add_block_preuploads() 0 15 2
A bigbluebuttonbn_mform_add_block_user_role_mapping() 0 37 1
A bigbluebuttonbn_mform_add_block_clienttype() 0 25 4
A bigbluebuttonbn_mform_add_block_schedule() 0 13 5
A bigbluebuttonbn_mform_add_element() 0 17 5

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like mod_bigbluebuttonbn_mod_form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use mod_bigbluebuttonbn_mod_form, and based on these observations, apply Extract Interface, too.

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
 * Config all BigBlueButtonBN instances in this course.
19
 *
20
 * @package   mod_bigbluebuttonbn
21
 * @copyright 2010 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
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
25
 */
26
27
defined('MOODLE_INTERNAL') || die();
28
29
require_once(dirname(__FILE__).'/locallib.php');
30
require_once($CFG->dirroot.'/course/moodleform_mod.php');
31
32
/**
33
 * Moodle class for mod_form.
34
 *
35
 * @copyright 2010 onwards, Blindside Networks Inc
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class mod_bigbluebuttonbn_mod_form extends moodleform_mod {
39
40
    /**
41
     * Define (add) particular settings this activity can have.
42
     *
43
     * @return void
44
     */
45
    public function definition() {
46
        global $CFG, $DB, $OUTPUT, $PAGE;
47
        $mform = &$this->_form;
48
49
        // Validates if the BigBlueButton server is running.
50
        $serverversion = bigbluebuttonbn_get_server_version();
51
        if (is_null($serverversion)) {
52
            print_error('general_error_unable_connect', 'bigbluebuttonbn',
53
                $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
54
            return;
55
        }
56
        // Context.
57
        $bigbluebuttonbn = null;
58
        $course = get_course($this->current->course);
59
        if ($this->current->id) {
60
            $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', array('id' => $this->current->id), '*', MUST_EXIST);
61
        }
62
        $context = context_course::instance($course->id);
63
        // UI configuration options.
64
        $cfg = \mod_bigbluebuttonbn\locallib\config::get_options();
65
66
        $jsvars = array();
67
        // Get only those that are allowed.
68
        $createroom = has_capability('mod/bigbluebuttonbn:meeting', $context);
69
        $createrecording = has_capability('mod/bigbluebuttonbn:recording', $context);
70
        $jsvars['instanceTypeProfiles'] = bigbluebuttonbn_get_instance_type_profiles_create_allowed(
71
            $createroom, $createrecording);
72
        $jsvars['instanceTypeDefault'] = array_keys($jsvars['instanceTypeProfiles'])[0];
73
        // If none is allowed, fail and return.
74
        if (empty($jsvars['instanceTypeProfiles'])) {
75
            print_error('general_error_not_allowed_to_create_instances)', 'bigbluebuttonbn',
76
                $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
77
            return;
78
        }
79
        $this->bigbluebuttonbn_mform_add_block_profiles($mform, $jsvars['instanceTypeProfiles']);
80
        // Data for participant selection.
81
        $participantlist = bigbluebuttonbn_get_participant_list($bigbluebuttonbn, $context);
82
        // Add block 'General'.
83
        $this->bigbluebuttonbn_mform_add_block_general($mform, $cfg);
84
        // Add block 'Room'.
85
        $this->bigbluebuttonbn_mform_add_block_room($mform, $cfg);
86
        // Add block 'Lock'.
87
        $this->bigbluebuttonbn_mform_add_block_locksettings($mform, $cfg);
88
        // Add block 'Preuploads'.
89
        $this->bigbluebuttonbn_mform_add_block_preuploads($mform, $cfg);
90
        // Add block 'Participant List'.
91
        $this->bigbluebuttonbn_mform_add_block_user_role_mapping($mform, $participantlist);
92
        // Add block 'Schedule'.
93
        $this->bigbluebuttonbn_mform_add_block_schedule($mform, $this->current);
94
        // Add block 'client Type'.
95
        $this->bigbluebuttonbn_mform_add_block_clienttype($mform, $cfg);
96
        // Add standard elements, common to all modules.
97
        $this->standard_coursemodule_elements();
98
        // Add standard buttons, common to all modules.
99
        $this->add_action_buttons();
100
        // JavaScript for locales.
101
        $PAGE->requires->strings_for_js(array_keys(bigbluebuttonbn_get_strings_for_js()), 'bigbluebuttonbn');
102
        $jsvars['participantData'] = bigbluebuttonbn_get_participant_data($context, $bigbluebuttonbn);
103
        $jsvars['participantList'] = $participantlist;
104
        $jsvars['iconsEnabled'] = (boolean)$cfg['recording_icons_enabled'];
105
        $jsvars['pixIconDelete'] = (string)$OUTPUT->pix_icon('t/delete', get_string('delete'), 'moodle');
106
        $PAGE->requires->yui_module('moodle-mod_bigbluebuttonbn-modform',
107
            'M.mod_bigbluebuttonbn.modform.init', array($jsvars));
108
    }
109
110
    /**
111
     * Prepare the attachment for being stored.
112
     *
113
     * @param array $defaultvalues
114
     * @return void
115
     */
116
    public function data_preprocessing(&$defaultvalues) {
117
        parent::data_preprocessing($defaultvalues);
118
119
        // Completion: tick by default if completion attendance settings is set to 1 or more.
120
        $defaultvalues['completionattendanceenabled'] = 0;
121
        if (!empty($defaultvalues['completionattendance'])) {
122
            $defaultvalues['completionattendanceenabled'] = 1;
123
        }
124
        // Check if we are Editing an existing instance.
125
        if ($this->current->instance) {
126
            // Pre-uploaded presentation: copy existing files into draft area.
127
            try {
128
                $draftitemid = file_get_submitted_draft_itemid('presentation');
129
                file_prepare_draft_area($draftitemid, $this->context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
130
                    array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1, 'mainfile' => true)
131
                );
132
                $defaultvalues['presentation'] = $draftitemid;
133
            } catch (Exception $e) {
134
                debugging('Presentation could not be loaded: '.$e->getMessage(), DEBUG_DEVELOPER);
135
                return;
136
            }
137
            // Completion: tick if completion attendance settings is set to 1 or more.
138
            $defaultvalues['completionattendanceenabled'] = 0;
139
            if (!empty($this->current->completionattendance)) {
140
                $defaultvalues['completionattendanceenabled'] = 1;
141
            }
142
        }
143
    }
144
145
    /**
146
     * Validates the data processed by the form.
147
     *
148
     * @param array $data
149
     * @param array $files
150
     * @return void
151
     */
152
    public function validation($data, $files) {
153
        $errors = parent::validation($data, $files);
154
        if (isset($data['openingtime']) && isset($data['closingtime'])) {
155
            if ($data['openingtime'] != 0 && $data['closingtime'] != 0 &&
156
                $data['closingtime'] < $data['openingtime']) {
157
                $errors['closingtime'] = get_string('bbbduetimeoverstartingtime', 'bigbluebuttonbn');
158
            }
159
        }
160
        if (isset($data['voicebridge'])) {
161
            if (!bigbluebuttonbn_voicebridge_unique($data['instance'], $data['voicebridge'])) {
162
                $errors['voicebridge'] = get_string('mod_form_field_voicebridge_notunique_error', 'bigbluebuttonbn');
163
            }
164
        }
165
        return $errors;
166
    }
167
168
    /**
169
     * Add elements for setting the custom completion rules.
170
     *
171
     * @category completion
172
     * @return array List of added element names, or names of wrapping group elements.
173
     */
174
    public function add_completion_rules() {
175
        $mform = $this->_form;
176
        if (!(boolean)\mod_bigbluebuttonbn\locallib\config::get('meetingevents_enabled')) {
177
            return [];
178
        }
179
180
        // Elements for completion by Attendance.
181
        $attendance['grouplabel'] = get_string('completionattendancegroup', 'bigbluebuttonbn');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attendance was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attendance = 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...
182
        $attendance['rulelabel'] = get_string('completionattendance', 'bigbluebuttonbn');
183
        $attendance['group'] = [
184
            $mform->createElement('advcheckbox', 'completionattendanceenabled', '', $attendance['rulelabel'] . '&nbsp;'),
185
            $mform->createElement('text', 'completionattendance', '', ['size' => 3]),
186
            $mform->createElement('static', 'completionattendanceunit', ' ', get_string('minutes', 'bigbluebuttonbn'))
187
        ];
188
        $mform->setType('completionattendance', PARAM_INT);
189
        $mform->addGroup($attendance['group'], 'completionattendancegroup', $attendance['grouplabel'], [' '], false);
190
        $mform->addHelpButton('completionattendancegroup', 'completionattendancegroup', 'bigbluebuttonbn');
191
        $mform->disabledIf('completionattendancegroup', 'completionview', 'notchecked');
192
        $mform->disabledIf('completionattendance', 'completionattendanceenabled', 'notchecked');
193
194
        // Elements for completion by Engagement.
195
        $engagement['grouplabel'] = get_string('completionengagementgroup', 'bigbluebuttonbn');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$engagement was never initialized. Although not strictly required by PHP, it is generally a good practice to add $engagement = 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...
196
        $engagement['chatlabel'] = get_string('completionengagementchats', 'bigbluebuttonbn');
197
        $engagement['talklabel'] = get_string('completionengagementtalks', 'bigbluebuttonbn');
198
        $engagement['raisehand'] = get_string('completionengagementraisehand', 'bigbluebuttonbn');
199
        $engagement['pollvotes'] = get_string('completionengagementpollvotes', 'bigbluebuttonbn');
200
        $engagement['emojis'] = get_string('completionengagementemojis', 'bigbluebuttonbn');
201
        $engagement['group'] = [
202
            $mform->createElement('advcheckbox', 'completionengagementchats', '', $engagement['chatlabel'] . '&nbsp;&nbsp;'),
203
            $mform->createElement('advcheckbox', 'completionengagementtalks', '', $engagement['talklabel'] . '&nbsp;&nbsp;'),
204
            $mform->createElement('advcheckbox', 'completionengagementraisehand', '', $engagement['raisehand'] . '&nbsp;&nbsp;'),
205
            $mform->createElement('advcheckbox', 'completionengagementpollvotes', '', $engagement['pollvotes'] . '&nbsp;&nbsp;'),
206
            $mform->createElement('advcheckbox', 'completionengagementemojis', '', $engagement['emojis'] . '&nbsp;&nbsp;'),
207
        ];
208
        $mform->addGroup($engagement['group'], 'completionengagementgroup', $engagement['grouplabel'], [' '], false);
209
        $mform->addHelpButton('completionengagementgroup', 'completionengagementgroup', 'bigbluebuttonbn');
210
        $mform->disabledIf('completionengagementgroup', 'completionview', 'notchecked');
211
212
        return ['completionattendancegroup', 'completionengagementgroup'];
213
    }
214
215
    /**
216
     * Called during validation to see whether some module-specific completion rules are selected.
217
     *
218
     * @param array $data Input data not yet validated.
219
     * @return bool True if one or more rules is enabled, false if none are.
220
     */
221
    public function completion_rule_enabled($data) {
222
        return (!empty($data['completionattendanceenabled']) && $data['completionattendance'] != 0);
223
    }
224
225
    /**
226
     * Allows module to modify the data returned by form get_data().
227
     * This method is also called in the bulk activity completion form.
228
     *
229
     * Only available on moodleform_mod.
230
     *
231
     * @param stdClass $data the form data to be modified.
232
     */
233
    public function data_postprocessing($data) {
234
        parent::data_postprocessing($data);
235
        // Turn off completion settings if the checkboxes aren't ticked.
236
        if (!empty($data->completionunlocked)) {
237
            $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
238
            if (empty($data->completionattendanceenabled) || !$autocompletion) {
239
                $data->completionattendance = 0;
240
            }
241
        }
242
    }
243
244
245
    /**
246
     * Function for showing the block for selecting profiles.
247
     *
248
     * @param object $mform
249
     * @param array $profiles
250
     * @return void
251
     */
252
    private function bigbluebuttonbn_mform_add_block_profiles(&$mform, $profiles) {
253
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::recordings_enabled()) {
254
            $mform->addElement('select', 'type', get_string('mod_form_field_instanceprofiles', 'bigbluebuttonbn'),
255
                bigbluebuttonbn_get_instance_profiles_array($profiles),
256
                array('onchange' => 'M.mod_bigbluebuttonbn.modform.updateInstanceTypeProfile(this);'));
257
            $mform->addHelpButton('type', 'mod_form_field_instanceprofiles', 'bigbluebuttonbn');
258
        }
259
    }
260
261
    /**
262
     * Function for showing the block for general settings.
263
     *
264
     * @param object $mform
265
     * @param array $cfg
266
     * @return void
267
     */
268
    private function bigbluebuttonbn_mform_add_block_general(&$mform, $cfg) {
269
        global $CFG;
270
        $mform->addElement('header', 'general', get_string('mod_form_block_general', 'bigbluebuttonbn'));
271
        $mform->addElement('text', 'name', get_string('mod_form_field_name', 'bigbluebuttonbn'),
272
            'maxlength="64" size="32"');
273
        $mform->setType('name', empty($CFG->formatstringstriptags) ? PARAM_CLEANHTML : PARAM_TEXT);
274
        $mform->addRule('name', null, 'required', null, 'client');
275
        $this->standard_intro_elements(get_string('mod_form_field_intro', 'bigbluebuttonbn'));
276
        $mform->setAdvanced('introeditor');
277
        $mform->setAdvanced('showdescription');
278
        if ($cfg['sendnotifications_enabled']) {
279
            $field = ['type' => 'checkbox', 'name' => 'notification', 'data_type' => PARAM_INT,
280
                'description_key' => 'mod_form_field_notification'];
281
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
282
                $field['description_key'], 0);
283
        }
284
    }
285
286
    /**
287
     * Function for showing details of the room settings for the room.
288
     *
289
     * @param object $mform
290
     * @param array $cfg
291
     * @return void
292
     */
293
    private function bigbluebuttonbn_mform_add_block_room_room(&$mform, $cfg) {
294
        $field = ['type' => 'textarea', 'name' => 'welcome', 'data_type' => PARAM_CLEANHTML,
295
            'description_key' => 'mod_form_field_welcome'];
296
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
297
            $field['description_key'], '', ['wrap' => 'virtual', 'rows' => 5, 'cols' => '60']);
298
        $field = ['type' => 'hidden', 'name' => 'voicebridge', 'data_type' => PARAM_INT,
299
            'description_key' => null];
300
        if ($cfg['voicebridge_editable']) {
301
            $field['type'] = 'text';
302
            $field['data_type'] = PARAM_TEXT;
303
            $field['description_key'] = 'mod_form_field_voicebridge';
304
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
305
                $field['description_key'], 0, ['maxlength' => 4, 'size' => 6],
306
                ['message' => get_string('mod_form_field_voicebridge_format_error', 'bigbluebuttonbn'),
307
                 'type' => 'numeric', 'rule' => '####', 'validator' => 'server']
308
              );
309
        } else {
310
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
311
                $field['description_key'], 0, ['maxlength' => 4, 'size' => 6]);
312
        }
313
        $field = ['type' => 'hidden', 'name' => 'wait', 'data_type' => PARAM_INT, 'description_key' => null];
314
        if ($cfg['waitformoderator_editable']) {
315
            $field['type'] = 'checkbox';
316
            $field['description_key'] = 'mod_form_field_wait';
317
        }
318
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
319
            $field['description_key'], $cfg['waitformoderator_default']);
320
        $field = ['type' => 'hidden', 'name' => 'userlimit', 'data_type' => PARAM_INT, 'description_key' => null];
321
        if ($cfg['userlimit_editable']) {
322
            $field['type'] = 'text';
323
            $field['data_type'] = PARAM_TEXT;
324
            $field['description_key'] = 'mod_form_field_userlimit';
325
        }
326
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
327
            $field['description_key'], $cfg['userlimit_default']);
328
        $field = ['type' => 'hidden', 'name' => 'record', 'data_type' => PARAM_INT, 'description_key' => null];
329
        if ($cfg['recording_editable']) {
330
            $field['type'] = 'checkbox';
331
            $field['description_key'] = 'mod_form_field_record';
332
        }
333
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
334
            $field['description_key'], $cfg['recording_default']);
335
336
        // Record all from start and hide button.
337
        $field = ['type' => 'hidden', 'name' => 'recordallfromstart', 'data_type' => PARAM_INT, 'description_key' => null];
338
        if ($cfg['recording_all_from_start_editable']) {
339
            $field['type'] = 'checkbox';
340
            $field['description_key'] = 'mod_form_field_recordallfromstart';
341
        }
342
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
343
            $field['description_key'], $cfg['recording_all_from_start_default']);
344
345
        $field = ['type' => 'hidden', 'name' => 'recordhidebutton', 'data_type' => PARAM_INT, 'description_key' => null];
346
        if ($cfg['recording_hide_button_editable']) {
347
            $field['type'] = 'checkbox';
348
            $field['description_key'] = 'mod_form_field_recordhidebutton';
349
        }
350
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
351
            $field['description_key'], $cfg['recording_hide_button_default']);
352
353
        $mform->disabledIf('recordallfromstart', 'record', $condition = 'notchecked', $value = '0');
354
        $mform->disabledIf('recordhidebutton', 'record', $condition = 'notchecked', $value = '0');
355
        $mform->disabledIf('recordhidebutton', 'recordallfromstart', $condition = 'notchecked', $value = '0');
356
        // End Record all from start and hide button.
357
358
        $field = ['type' => 'hidden', 'name' => 'muteonstart', 'data_type' => PARAM_INT, 'description_key' => null];
359
        if ($cfg['muteonstart_editable']) {
360
            $field['type'] = 'checkbox';
361
            $field['description_key'] = 'mod_form_field_muteonstart';
362
        }
363
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
364
            $field['description_key'], $cfg['muteonstart_default']);
365
366
    }
367
368
    /**
369
     * Function for showing details of the lock settings for the room.
370
     *
371
     * @param object $mform
372
     * @param array $cfg
373
     * @return void
374
     */
375
    private function bigbluebuttonbn_mform_add_block_locksettings(&$mform, $cfg) {
376
        $mform->addElement('header', 'lock', get_string('mod_form_locksettings', 'bigbluebuttonbn'));
377
378
        $locksettings = false;
379
380
        $field = ['type' => 'hidden', 'name' => 'disablecam', 'data_type' => PARAM_INT, 'description_key' => null];
381
        if ($cfg['disablecam_editable']) {
382
            $field['type'] = 'checkbox';
383
            $field['description_key'] = 'mod_form_field_disablecam';
384
            $locksettings = true;
385
        }
386
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
387
            $field['description_key'], $cfg['disablecam_default']);
388
389
        $field = ['type' => 'hidden', 'name' => 'disablemic', 'data_type' => PARAM_INT, 'description_key' => null];
390
        if ($cfg['disablemic_editable']) {
391
            $field['type'] = 'checkbox';
392
            $field['description_key'] = 'mod_form_field_disablemic';
393
            $locksettings = true;
394
        }
395
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
396
            $field['description_key'], $cfg['disablemic_default']);
397
398
        $field = ['type' => 'hidden', 'name' => 'disableprivatechat', 'data_type' => PARAM_INT, 'description_key' => null];
399
        if ($cfg['disableprivatechat_editable']) {
400
            $field['type'] = 'checkbox';
401
            $field['description_key'] = 'mod_form_field_disableprivatechat';
402
            $locksettings = true;
403
        }
404
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
405
            $field['description_key'], $cfg['disableprivatechat_default']);
406
407
        $field = ['type' => 'hidden', 'name' => 'disablepublicchat', 'data_type' => PARAM_INT, 'description_key' => null];
408
        if ($cfg['disablepublicchat_editable']) {
409
            $field['type'] = 'checkbox';
410
            $field['description_key'] = 'mod_form_field_disablepublicchat';
411
            $locksettings = true;
412
        }
413
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
414
            $field['description_key'], $cfg['disablepublicchat_default']);
415
416
        $field = ['type' => 'hidden', 'name' => 'disablenote', 'data_type' => PARAM_INT, 'description_key' => null];
417
        if ($cfg['disablenote_editable']) {
418
            $field['type'] = 'checkbox';
419
            $field['description_key'] = 'mod_form_field_disablenote';
420
            $locksettings = true;
421
        }
422
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
423
            $field['description_key'], $cfg['disablenote_default']);
424
425
        $field = ['type' => 'hidden', 'name' => 'hideuserlist', 'data_type' => PARAM_INT, 'description_key' => null];
426
        if ($cfg['hideuserlist_editable']) {
427
            $field['type'] = 'checkbox';
428
            $field['description_key'] = 'mod_form_field_hideuserlist';
429
            $locksettings = true;
430
        }
431
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
432
            $field['description_key'], $cfg['hideuserlist_default']);
433
434
        $field = ['type' => 'hidden', 'name' => 'lockedlayout', 'data_type' => PARAM_INT, 'description_key' => null];
435
        if ($cfg['lockedlayout_editable']) {
436
            $field['type'] = 'checkbox';
437
            $field['description_key'] = 'mod_form_field_lockedlayout';
438
            $locksettings = true;
439
        }
440
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
441
            $field['description_key'], $cfg['lockedlayout_default']);
442
443
        $field = ['type' => 'hidden', 'name' => 'lockonjoin', 'data_type' => PARAM_INT, 'description_key' => null];
444
        if ($cfg['lockonjoin_editable']) {
445
            $field['type'] = 'checkbox';
446
            $field['description_key'] = 'mod_form_field_lockonjoin';
447
            $locksettings = true;
448
        }
449
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
450
            $field['description_key'], $cfg['lockonjoin_default']);
451
452
        $field = ['type' => 'hidden', 'name' => 'lockonjoinconfigurable', 'data_type' => PARAM_INT, 'description_key' => null];
453
        if ($cfg['lockonjoinconfigurable_editable']) {
454
            $field['type'] = 'checkbox';
455
            $field['description_key'] = 'mod_form_field_lockonjoinconfigurable';
456
            $locksettings = true;
457
        }
458
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
459
            $field['description_key'], $cfg['lockonjoinconfigurable_default']);
460
461
        // Output message if no settings.
462 View Code Duplication
        if (!$locksettings) {
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...
463
            $field = ['type' => 'static', 'name' => 'no_locksettings',
464
                'defaultvalue' => get_string('mod_form_field_nosettings', 'bigbluebuttonbn')];
465
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], null, null,
466
                $field['defaultvalue']);
467
        }
468
    }
469
470
    /**
471
     * Function for showing details of the recording settings for the room.
472
     *
473
     * @param object $mform
474
     * @param array $cfg
475
     * @return void
476
     */
477
    private function bigbluebuttonbn_mform_add_block_room_recordings(&$mform, $cfg) {
478
        $recordingsettings = false;
479
        $field = ['type' => 'hidden', 'name' => 'recordings_html', 'data_type' => PARAM_INT,
480
                  'description_key' => null];
481
        if ($cfg['recordings_html_editable']) {
482
            $field['type'] = 'checkbox';
483
            $field['description_key'] = 'mod_form_field_recordings_html';
484
            $recordingsettings = true;
485
        }
486
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
487
            $field['description_key'], $cfg['recordings_html_default']);
488
        $field = ['type' => 'hidden', 'name' => 'recordings_deleted', 'data_type' => PARAM_INT,
489
                  'description_key' => null];
490
        if ($cfg['recordings_deleted_editable']) {
491
            $field['type'] = 'checkbox';
492
            $field['description_key'] = 'mod_form_field_recordings_deleted';
493
            $recordingsettings = true;
494
        }
495
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
496
            $field['description_key'], $cfg['recordings_deleted_default']);
497
        $field = ['type' => 'hidden', 'name' => 'recordings_imported', 'data_type' => PARAM_INT,
498
                  'description_key' => null];
499
        if ($cfg['importrecordings_enabled'] && $cfg['recordings_imported_editable']) {
500
            $field['type'] = 'checkbox';
501
            $field['description_key'] = 'mod_form_field_recordings_imported';
502
            $recordingsettings = true;
503
        }
504
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
505
            $field['description_key'], $cfg['recordings_imported_default']);
506
        $field = ['type' => 'hidden', 'name' => 'recordings_preview', 'data_type' => PARAM_INT,
507
                  'description_key' => null];
508
        if ($cfg['recordings_preview_editable']) {
509
            $field['type'] = 'checkbox';
510
            $field['description_key'] = 'mod_form_field_recordings_preview';
511
            $recordingsettings = true;
512
        }
513
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
514
            $field['description_key'], $cfg['recordings_preview_default']);
515
516 View Code Duplication
        if (!$recordingsettings) {
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...
517
            $field = ['type' => 'static', 'name' => 'no_recordings',
518
                'defaultvalue' => get_string('mod_form_field_nosettings', 'bigbluebuttonbn')];
519
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], null, null,
520
                $field['defaultvalue']);
521
        }
522
    }
523
524
    /**
525
     * Function for showing the block for room settings.
526
     *
527
     * @param object $mform
528
     * @param array $cfg
529
     * @return void
530
     */
531
    private function bigbluebuttonbn_mform_add_block_room(&$mform, $cfg) {
532
        if ($cfg['voicebridge_editable'] || $cfg['waitformoderator_editable'] ||
533
            $cfg['userlimit_editable'] || $cfg['recording_editable']) {
534
            $mform->addElement('header', 'room', get_string('mod_form_block_room', 'bigbluebuttonbn'));
535
            $this->bigbluebuttonbn_mform_add_block_room_room($mform, $cfg);
536
        }
537
        if ($cfg['recordings_html_editable'] || $cfg['recordings_deleted_editable'] ||
538
            $cfg['recordings_imported_editable'] || $cfg['recordings_preview_editable']) {
539
            $mform->addElement('header', 'recordings', get_string('mod_form_block_recordings', 'bigbluebuttonbn'));
540
            $this->bigbluebuttonbn_mform_add_block_room_recordings($mform, $cfg);
541
        }
542
    }
543
544
    /**
545
     * Function for showing the block for preuploaded presentation.
546
     *
547
     * @param object $mform
548
     * @param array $cfg
549
     * @return void
550
     */
551
    private function bigbluebuttonbn_mform_add_block_preuploads(&$mform, $cfg) {
552
        if ($cfg['preuploadpresentation_enabled']) {
553
            $mform->addElement('header', 'preuploadpresentation',
554
                get_string('mod_form_block_presentation', 'bigbluebuttonbn'));
555
            $mform->setExpanded('preuploadpresentation');
556
            $filemanageroptions = array();
557
            $filemanageroptions['accepted_types'] = '*';
558
            $filemanageroptions['maxbytes'] = 0;
559
            $filemanageroptions['subdirs'] = 0;
560
            $filemanageroptions['maxfiles'] = 1;
561
            $filemanageroptions['mainfile'] = true;
562
            $mform->addElement('filemanager', 'presentation', get_string('selectfiles'),
563
                null, $filemanageroptions);
564
        }
565
    }
566
567
    /**
568
     * Function for showing the block for setting participant roles.
569
     *
570
     * @param object $mform
571
     * @param string $participantlist
572
     * @return void
573
     */
574
    private function bigbluebuttonbn_mform_add_block_user_role_mapping(&$mform, $participantlist) {
575
        $participantselection = bigbluebuttonbn_get_participant_selection_data();
576
        $mform->addElement('header', 'permissions', get_string('mod_form_block_participants', 'bigbluebuttonbn'));
577
        $mform->setExpanded('permissions');
578
        $mform->addElement('hidden', 'participants', json_encode($participantlist));
579
        $mform->setType('participants', PARAM_TEXT);
580
        // Render elements for participant selection.
581
        $htmlselectiontype = html_writer::select($participantselection['type_options'],
582
            'bigbluebuttonbn_participant_selection_type', $participantselection['type_selected'], array(),
583
            array('id' => 'bigbluebuttonbn_participant_selection_type',
584
                  'onchange' => 'M.mod_bigbluebuttonbn.modform.participantSelectionSet(); return 0;'));
585
        $htmlselectionoptions = html_writer::select($participantselection['options'], 'bigbluebuttonbn_participant_selection',
586
            $participantselection['selected'], array(),
587
            array('id' => 'bigbluebuttonbn_participant_selection', 'disabled' => 'disabled'));
588
        $htmlselectioninput = html_writer::tag('input', '', array('id' => 'id_addselectionid',
589
            'type' => 'button', 'class' => 'btn btn-secondary',
590
            'value' => get_string('mod_form_field_participant_list_action_add', 'bigbluebuttonbn'),
591
            'onclick' => 'M.mod_bigbluebuttonbn.modform.participantAdd(); return 0;'
592
          ));
593
        $htmladdparticipant = html_writer::tag('div',
594
            $htmlselectiontype . '&nbsp;&nbsp;' . $htmlselectionoptions . '&nbsp;&nbsp;' . $htmlselectioninput, null);
595
        $mform->addElement('html', "\n\n");
596
        $mform->addElement('static', 'static_add_participant',
597
            get_string('mod_form_field_participant_add', 'bigbluebuttonbn'), $htmladdparticipant);
598
        $mform->addElement('html', "\n\n");
599
        // Declare the table.
600
        $htmltable = new html_table();
601
        $htmltable->align = array('left', 'left', 'left', 'left');
602
        $htmltable->id = 'participant_list_table';
603
        $htmltable->data = array(array());
604
        // Render elements for participant list.
605
        $htmlparticipantlist = html_writer::table($htmltable);
606
        $mform->addElement('html', "\n\n");
607
        $mform->addElement('static', 'static_participant_list',
608
            get_string('mod_form_field_participant_list', 'bigbluebuttonbn'), $htmlparticipantlist);
609
        $mform->addElement('html', "\n\n");
610
    }
611
612
    /**
613
     * Function for showing the client type
614
     *
615
     * @param object $mform
616
     * @param object $cfg
617
     * @return void
618
     */
619
    private function bigbluebuttonbn_mform_add_block_clienttype(&$mform, &$cfg) {
620
        // Validates if clienttype capability is enabled.
621
        if (!$cfg['clienttype_enabled']) {
622
            return;
623
        }
624
        // Validates if the html5client is supported by the BigBlueButton Server.
625
        if (!bigbluebuttonbn_has_html5_client()) {
626
            return;
627
        }
628
        $field = ['type' => 'hidden', 'name' => 'clienttype', 'data_type' => PARAM_INT,
629
            'description_key' => null];
630
        if ($cfg['clienttype_editable']) {
631
            $field['type'] = 'select';
632
            $field['data_type'] = PARAM_TEXT;
633
            $field['description_key'] = 'mod_form_field_block_clienttype';
634
             $choices = array(BIGBLUEBUTTON_CLIENTTYPE_FLASH => get_string('mod_form_block_clienttype_flash', 'bigbluebuttonbn'),
635
                             BIGBLUEBUTTON_CLIENTTYPE_HTML5 => get_string('mod_form_block_clienttype_html5', 'bigbluebuttonbn'));
636
             $mform->addElement('header', 'clienttypeselection', get_string('mod_form_block_clienttype', 'bigbluebuttonbn'));
637
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
638
                                    $field['description_key'], $cfg['clienttype_default'], $choices);
639
            return;
640
        }
641
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
642
                                null, $cfg['clienttype_default']);
643
    }
644
645
    /**
646
     * Function for showing the block for integration with the calendar.
647
     *
648
     * @param object $mform
649
     * @param object $activity
650
     * @return void
651
     */
652
    private function bigbluebuttonbn_mform_add_block_schedule(&$mform, &$activity) {
653
        $mform->addElement('header', 'schedule', get_string('mod_form_block_schedule', 'bigbluebuttonbn'));
654
        if (isset($activity->openingtime) && $activity->openingtime != 0 ||
655
            isset($activity->closingtime) && $activity->closingtime != 0) {
656
            $mform->setExpanded('schedule');
657
        }
658
        $mform->addElement('date_time_selector', 'openingtime',
659
            get_string('mod_form_field_openingtime', 'bigbluebuttonbn'), array('optional' => true));
660
        $mform->setDefault('openingtime', 0);
661
        $mform->addElement('date_time_selector', 'closingtime',
662
            get_string('mod_form_field_closingtime', 'bigbluebuttonbn'), array('optional' => true));
663
        $mform->setDefault('closingtime', 0);
664
    }
665
666
    /**
667
     * Function for showing an element.
668
     *
669
     * @param object $mform
670
     * @param string $type
671
     * @param string $name
672
     * @param string $datatype
673
     * @param string $descriptionkey
674
     * @param string $defaultvalue
675
     * @param array $options
676
     * @param string $rule
677
     * @return void
678
     */
679
    private function bigbluebuttonbn_mform_add_element(&$mform, $type, $name, $datatype,
680
            $descriptionkey, $defaultvalue = null, $options = null, $rule = null) {
681
        if ($type === 'hidden' || $type === 'static') {
682
            $mform->addElement($type, $name, $defaultvalue);
683
            $mform->setType($name, $datatype);
684
            return;
685
        }
686
        $mform->addElement($type, $name, get_string($descriptionkey, 'bigbluebuttonbn'), $options);
687
        if (get_string_manager()->string_exists($descriptionkey.'_help', 'bigbluebuttonbn')) {
688
            $mform->addHelpButton($name, $descriptionkey, 'bigbluebuttonbn');
689
        }
690
        if (!empty($rule)) {
691
            $mform->addRule($name, $rule['message'], $rule['type'], $rule['rule'], $rule['validator']);
692
        }
693
        $mform->setDefault($name, $defaultvalue);
694
        $mform->setType($name, $datatype);
695
    }
696
}
697