Completed
Push — v2.2-stable ( 657284...8a4d51 )
by Jesus
02:11
created

mod_bigbluebuttonbn_mod_form   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 392
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 392
rs 6.96
c 0
b 0
f 0
wmc 53
lcom 1
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
B definition() 0 57 4
A data_preprocessing() 0 15 3
B validation() 0 15 8
A bigbluebuttonbn_mform_add_block_profiles() 0 8 2
A bigbluebuttonbn_mform_add_block_general() 0 23 4
B bigbluebuttonbn_mform_add_block_room_room() 0 43 5
B bigbluebuttonbn_mform_add_block_room() 0 12 9
A bigbluebuttonbn_mform_add_block_preuploads() 0 15 2
A bigbluebuttonbn_mform_add_block_participants() 0 37 1
A bigbluebuttonbn_mform_add_block_schedule() 0 13 5
A bigbluebuttonbn_mform_add_element() 0 17 4
B bigbluebuttonbn_mform_add_block_room_recordings() 0 35 6

How to fix   Complexity   

Complex Class

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-2017 Blindside Networks Inc
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 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-2017 Blindside Networks Inc
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 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
        // Validates if the BigBlueButton server is running.
48
        $serverversion = bigbluebuttonbn_get_server_version();
49
        if (is_null($serverversion)) {
50
            print_error('general_error_unable_connect', 'bigbluebuttonbn',
51
                $CFG->wwwroot.'/admin/settings.php?section=modsettingbigbluebuttonbn');
52
            return;
53
        }
54
        // Context.
55
        $bigbluebuttonbn = null;
56
        $course = null;
57
        $courseid = optional_param('course', 0, PARAM_INT);
58
        if ($courseid) {
59
            $course = get_course($courseid);
60
        }
61
        if (!$course) {
62
            $cm = get_coursemodule_from_id('bigbluebuttonbn',
63
                optional_param('update', 0, PARAM_INT), 0, false, MUST_EXIST);
64
            $course = $DB->get_record('course', array('id' => $cm->course),
65
                '*', MUST_EXIST);
66
            $bigbluebuttonbn = $DB->get_record('bigbluebuttonbn',
67
                array('id' => $cm->instance), '*', MUST_EXIST);
68
        }
69
        $context = context_course::instance($course->id);
70
        // UI configuration options.
71
        $cfg = \mod_bigbluebuttonbn\locallib\config::get_options();
72
        $mform = &$this->_form;
73
        $jsvars = array();
74
        $jsvars['instanceTypeRoomOnly'] = BIGBLUEBUTTONBN_TYPE_ROOM_ONLY;
75
        $jsvars['instanceTypeProfiles'] = bigbluebuttonbn_get_instance_type_profiles();
76
        $this->bigbluebuttonbn_mform_add_block_profiles($mform, $jsvars['instanceTypeProfiles']);
77
        // Data for participant selection.
78
        $participantlist = bigbluebuttonbn_get_participant_list($bigbluebuttonbn, $context);
79
        // Add block 'General'.
80
        $this->bigbluebuttonbn_mform_add_block_general($mform, $cfg);
81
        // Add block 'Room'.
82
        $this->bigbluebuttonbn_mform_add_block_room($mform, $cfg);
83
        // Add block 'Preuploads'.
84
        $this->bigbluebuttonbn_mform_add_block_preuploads($mform, $cfg);
85
        // Add block 'Participant List'.
86
        $this->bigbluebuttonbn_mform_add_block_participants($mform, $cfg, $participantlist);
87
        // Add block 'Schedule'.
88
        $this->bigbluebuttonbn_mform_add_block_schedule($mform, $this->current);
89
        // Add standard elements, common to all modules.
90
        $this->standard_coursemodule_elements();
91
        // Add standard buttons, common to all modules.
92
        $this->add_action_buttons();
93
        // JavaScript for locales.
94
        $PAGE->requires->strings_for_js(array_keys(bigbluebuttonbn_get_strings_for_js()), 'bigbluebuttonbn');
95
        $jsvars['participantData'] = bigbluebuttonbn_get_participant_data($context);
96
        $jsvars['participantList'] = $participantlist;
97
        $jsvars['iconsEnabled'] = (boolean)$cfg['recording_icons_enabled'];
98
        $jsvars['pixIconDelete'] = (string)$OUTPUT->pix_icon('t/delete', get_string('delete'), 'moodle');
99
        $PAGE->requires->yui_module('moodle-mod_bigbluebuttonbn-modform',
100
            'M.mod_bigbluebuttonbn.modform.init', array($jsvars));
101
    }
102
103
    /**
104
     * Prepare the attachment for being stored.
105
     *
106
     * @param array $defaultvalues
107
     * @return void
108
     */
109
    public function data_preprocessing(&$defaultvalues) {
110
        if ($this->current->instance) {
111
            // Editing existing instance - copy existing files into draft area.
112
            try {
113
                $draftitemid = file_get_submitted_draft_itemid('presentation');
114
                file_prepare_draft_area($draftitemid, $this->context->id, 'mod_bigbluebuttonbn', 'presentation', 0,
115
                    array('subdirs' => 0, 'maxbytes' => 0, 'maxfiles' => 1, 'mainfile' => true)
116
                );
117
                $defaultvalues['presentation'] = $draftitemid;
118
            } catch (Exception $e) {
119
                debugging('Presentation could not be loaded: '.$e->getMessage(), DEBUG_DEVELOPER);
120
                return;
121
            }
122
        }
123
    }
124
125
    /**
126
     * Validates the data processed by the form.
127
     *
128
     * @param array $data
129
     * @param array $files
130
     * @return void
131
     */
132
    public function validation($data, $files) {
133
        $errors = parent::validation($data, $files);
134
        if (isset($data['openingtime']) && isset($data['closingtime'])) {
135
            if ($data['openingtime'] != 0 && $data['closingtime'] != 0 &&
136
                $data['closingtime'] < $data['openingtime']) {
137
                $errors['closingtime'] = get_string('bbbduetimeoverstartingtime', 'bigbluebuttonbn');
138
            }
139
        }
140
        if (isset($data['voicebridge'])) {
141
            if (!bigbluebuttonbn_voicebridge_unique($data['instance'], $data['voicebridge'])) {
142
                $errors['voicebridge'] = get_string('mod_form_field_voicebridge_notunique_error', 'bigbluebuttonbn');
143
            }
144
        }
145
        return $errors;
146
    }
147
148
    /**
149
     * Function for showing the block for selecting profiles.
150
     *
151
     * @param object $mform
152
     * @param array $profiles
153
     * @return void
154
     */
155
    private function bigbluebuttonbn_mform_add_block_profiles(&$mform, $profiles) {
156
        if ((boolean)\mod_bigbluebuttonbn\locallib\config::recordings_enabled()) {
157
            $mform->addElement('select', 'type', get_string('mod_form_field_instanceprofiles', 'bigbluebuttonbn'),
158
                bigbluebuttonbn_get_instance_profiles_array($profiles),
159
                array('onchange' => 'M.mod_bigbluebuttonbn.modform.updateInstanceTypeProfile(this);'));
160
            $mform->addHelpButton('type', 'mod_form_field_instanceprofiles', 'bigbluebuttonbn');
161
        }
162
    }
163
164
    /**
165
     * Function for showing the block for general settings.
166
     *
167
     * @param object $mform
168
     * @param array $cfg
169
     * @return void
170
     */
171
    private function bigbluebuttonbn_mform_add_block_general(&$mform, $cfg) {
172
        global $CFG;
173
        $mform->addElement('header', 'general', get_string('mod_form_block_general', 'bigbluebuttonbn'));
174
        $mform->addElement('text', 'name', get_string('mod_form_field_name', 'bigbluebuttonbn'),
175
            'maxlength="64" size="32"');
176
        $mform->setType('name', empty($CFG->formatstringstriptags) ? PARAM_CLEANHTML : PARAM_TEXT);
177
        $mform->addRule('name', null, 'required', null, 'client');
178
        if ($cfg['version_major'] < '2015051100') {
179
            // This is valid before v2.9.
180
            $this->add_intro_editor(false, get_string('mod_form_field_intro', 'bigbluebuttonbn'));
181
        } else {
182
            // This is valid after v2.9.
183
            $this->standard_intro_elements(get_string('mod_form_field_intro', 'bigbluebuttonbn'));
184
        }
185
        $mform->setAdvanced('introeditor');
186
        $mform->setAdvanced('showdescription');
187
        if ($cfg['sendnotifications_enabled']) {
188
            $field = ['type' => 'checkbox', 'name' => 'notification', 'data_type' => PARAM_INT,
189
                'description_key' => 'mod_form_field_notification'];
190
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
191
                $field['description_key'], 0);
192
        }
193
    }
194
195
    /**
196
     * Function for showing details of the room settings for the room.
197
     *
198
     * @param object $mform
199
     * @param array $cfg
200
     * @return void
201
     */
202
    private function bigbluebuttonbn_mform_add_block_room_room(&$mform, $cfg) {
203
        $field = ['type' => 'textarea', 'name' => 'welcome', 'data_type' => PARAM_TEXT,
204
            'description_key' => 'mod_form_field_welcome'];
205
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
206
            $field['description_key'], '', ['wrap' => 'virtual', 'rows' => 5, 'cols' => '60']);
207
        $field = ['type' => 'hidden', 'name' => 'voicebridge', 'data_type' => PARAM_INT,
208
            'description_key' => null];
209
        if ($cfg['voicebridge_editable']) {
210
            $field['type'] = 'text';
211
            $field['data_type'] = PARAM_TEXT;
212
            $field['description_key'] = 'mod_form_field_voicebridge';
213
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
214
                $field['description_key'], 0, ['maxlength' => 4, 'size' => 6],
215
                ['message' => get_string('mod_form_field_voicebridge_format_error', 'bigbluebuttonbn'),
216
                 'type' => 'numeric', 'rule' => '####', 'validator' => 'server']
217
              );
218
        } else {
219
            $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
220
                $field['description_key'], 0, ['maxlength' => 4, 'size' => 6]);
221
        }
222
        $field = ['type' => 'hidden', 'name' => 'wait', 'data_type' => PARAM_INT, 'description_key' => null];
223
        if ($cfg['waitformoderator_editable']) {
224
            $field['type'] = 'checkbox';
225
            $field['description_key'] = 'mod_form_field_wait';
226
        }
227
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
228
            $field['description_key'], $cfg['waitformoderator_default']);
229
        $field = ['type' => 'hidden', 'name' => 'userlimit', 'data_type' => PARAM_INT, 'description_key' => null];
230
        if ($cfg['userlimit_editable']) {
231
            $field['type'] = 'text';
232
            $field['data_type'] = PARAM_TEXT;
233
            $field['description_key'] = 'mod_form_field_userlimit';
234
        }
235
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
236
            $field['description_key'], $cfg['userlimit_default']);
237
        $field = ['type' => 'hidden', 'name' => 'record', 'data_type' => PARAM_INT, 'description_key' => null];
238
        if ($cfg['recording_editable']) {
239
            $field['type'] = 'checkbox';
240
            $field['description_key'] = 'mod_form_field_record';
241
        }
242
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
243
            $field['description_key'], $cfg['recording_default']);
244
    }
245
246
    /**
247
     * Function for showing details of the recording settings for the room.
248
     *
249
     * @param object $mform
250
     * @param array $cfg
251
     * @return void
252
     */
253
    private function bigbluebuttonbn_mform_add_block_room_recordings(&$mform, $cfg) {
254
        $field = ['type' => 'hidden', 'name' => 'recordings_html', 'data_type' => PARAM_INT,
255
                  'description_key' => null];
256
        if ($cfg['recordings_html_editable']) {
257
            $field['type'] = 'checkbox';
258
            $field['description_key'] = 'mod_form_field_recordings_html';
259
        }
260
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
261
            $field['description_key'], $cfg['recordings_html_default']);
262
        $field = ['type' => 'hidden', 'name' => 'recordings_deleted', 'data_type' => PARAM_INT,
263
                  'description_key' => null];
264
        if ($cfg['recordings_deleted_editable']) {
265
            $field['type'] = 'checkbox';
266
            $field['description_key'] = 'mod_form_field_recordings_deleted';
267
        }
268
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
269
            $field['description_key'], $cfg['recordings_deleted_default']);
270
        $field = ['type' => 'hidden', 'name' => 'recordings_imported', 'data_type' => PARAM_INT,
271
                  'description_key' => null];
272
        if ($cfg['importrecordings_enabled'] && $cfg['recordings_imported_editable']) {
273
            $field['type'] = 'checkbox';
274
            $field['description_key'] = 'mod_form_field_recordings_imported';
275
        }
276
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
277
            $field['description_key'], $cfg['recordings_imported_default']);
278
        $field = ['type' => 'hidden', 'name' => 'recordings_preview', 'data_type' => PARAM_INT,
279
                  'description_key' => null];
280
        if ($cfg['recordings_preview_editable']) {
281
            $field['type'] = 'checkbox';
282
            $field['description_key'] = 'mod_form_field_recordings_preview';
283
        }
284
        $this->bigbluebuttonbn_mform_add_element($mform, $field['type'], $field['name'], $field['data_type'],
285
            $field['description_key'], $cfg['recordings_preview_default']);
286
287
    }
288
289
    /**
290
     * Function for showing the block for room settings.
291
     *
292
     * @param object $mform
293
     * @param array $cfg
294
     * @return void
295
     */
296
    private function bigbluebuttonbn_mform_add_block_room(&$mform, $cfg) {
297
        if ($cfg['voicebridge_editable'] || $cfg['waitformoderator_editable'] ||
298
            $cfg['userlimit_editable'] || $cfg['recording_editable']) {
299
            $mform->addElement('header', 'room', get_string('mod_form_block_room', 'bigbluebuttonbn'));
300
            $this->bigbluebuttonbn_mform_add_block_room_room($mform, $cfg);
301
        }
302
        if ($cfg['recordings_html_editable'] || $cfg['recordings_deleted_editable'] ||
303
            $cfg['recordings_imported_editable'] || $cfg['recordings_preview_editable']) {
304
            $mform->addElement('header', 'recordings', get_string('mod_form_block_recordings', 'bigbluebuttonbn'));
305
            $this->bigbluebuttonbn_mform_add_block_room_recordings($mform, $cfg);
306
        }
307
    }
308
309
    /**
310
     * Function for showing the block for preuploaded presentation.
311
     *
312
     * @param object $mform
313
     * @param array $cfg
314
     * @return void
315
     */
316
    private function bigbluebuttonbn_mform_add_block_preuploads(&$mform, $cfg) {
317
        if ($cfg['preuploadpresentation_enabled']) {
318
            $mform->addElement('header', 'preuploadpresentation',
319
                get_string('mod_form_block_presentation', 'bigbluebuttonbn'));
320
            $mform->setExpanded('preuploadpresentation');
321
            $filemanageroptions = array();
322
            $filemanageroptions['accepted_types'] = '*';
323
            $filemanageroptions['maxbytes'] = 0;
324
            $filemanageroptions['subdirs'] = 0;
325
            $filemanageroptions['maxfiles'] = 1;
326
            $filemanageroptions['mainfile'] = true;
327
            $mform->addElement('filemanager', 'presentation', get_string('selectfiles'),
328
                null, $filemanageroptions);
329
        }
330
    }
331
332
    /**
333
     * Function for showing the block for setting participant roles.
334
     *
335
     * @param object $mform
336
     * @param array $cfg
337
     * @param string $participantlist
338
     * @return void
339
     */
340
    private function bigbluebuttonbn_mform_add_block_participants(&$mform, $cfg, $participantlist) {
341
        $participantselection = bigbluebuttonbn_get_participant_selection_data();
342
        $mform->addElement('header', 'permissions', get_string('mod_form_block_participants', 'bigbluebuttonbn'));
343
        $mform->setExpanded('permissions');
344
        $mform->addElement('hidden', 'participants', json_encode($participantlist));
345
        $mform->setType('participants', PARAM_TEXT);
346
        // Render elements for participant selection.
347
        $htmlselectiontype = html_writer::select($participantselection['type_options'],
348
            'bigbluebuttonbn_participant_selection_type', $participantselection['type_selected'], array(),
349
            array('id' => 'bigbluebuttonbn_participant_selection_type',
350
                  'onchange' => 'M.mod_bigbluebuttonbn.modform.participantSelectionSet(); return 0;'));
351
        $htmlselectionoptions = html_writer::select($participantselection['options'], 'bigbluebuttonbn_participant_selection',
352
            $participantselection['selected'], array(),
353
            array('id' => 'bigbluebuttonbn_participant_selection', 'disabled' => 'disabled'));
354
        $htmlselectioninput = html_writer::tag('input', '', array('id' => 'id_addselectionid',
355
            'type' => 'button', 'class' => 'btn btn-secondary',
356
            'value' => get_string('mod_form_field_participant_list_action_add', 'bigbluebuttonbn'),
357
            'onclick' => 'M.mod_bigbluebuttonbn.modform.participantAdd(); return 0;'
358
          ));
359
        $htmladdparticipant = html_writer::tag('div',
360
            $htmlselectiontype . '&nbsp;&nbsp;' . $htmlselectionoptions . '&nbsp;&nbsp;' . $htmlselectioninput, null);
361
        $mform->addElement('html', "\n\n");
362
        $mform->addElement('static', 'static_add_participant',
363
            get_string('mod_form_field_participant_add', 'bigbluebuttonbn'), $htmladdparticipant);
364
        $mform->addElement('html', "\n\n");
365
        // Declare the table.
366
        $htmltable = new html_table();
367
        $htmltable->align = array('left', 'left', 'left', 'left');
368
        $htmltable->id = 'participant_list_table';
369
        $htmltable->data = array(array());
370
        // Render elements for participant list.
371
        $htmlparticipantlist = html_writer::table($htmltable);
372
        $mform->addElement('html', "\n\n");
373
        $mform->addElement('static', 'static_participant_list',
374
            get_string('mod_form_field_participant_list', 'bigbluebuttonbn'), $htmlparticipantlist);
375
        $mform->addElement('html', "\n\n");
376
    }
377
378
    /**
379
     * Function for showing the block for integration with the calendar.
380
     *
381
     * @param object $mform
382
     * @param object $activity
383
     * @return void
384
     */
385
    private function bigbluebuttonbn_mform_add_block_schedule(&$mform, &$activity) {
386
        $mform->addElement('header', 'schedule', get_string('mod_form_block_schedule', 'bigbluebuttonbn'));
387
        if (isset($activity->openingtime) && $activity->openingtime != 0 ||
388
            isset($activity->closingtime) && $activity->closingtime != 0) {
389
            $mform->setExpanded('schedule');
390
        }
391
        $mform->addElement('date_time_selector', 'openingtime',
392
            get_string('mod_form_field_openingtime', 'bigbluebuttonbn'), array('optional' => true));
393
        $mform->setDefault('openingtime', 0);
394
        $mform->addElement('date_time_selector', 'closingtime',
395
            get_string('mod_form_field_closingtime', 'bigbluebuttonbn'), array('optional' => true));
396
        $mform->setDefault('closingtime', 0);
397
    }
398
399
    /**
400
     * Function for showing an element.
401
     *
402
     * @param object $mform
403
     * @param string $type
404
     * @param string $name
405
     * @param string $datatype
406
     * @param string $descriptionkey
407
     * @param string $defaultvalue
408
     * @param array $options
409
     * @param string $rule
410
     * @return void
411
     */
412
    private function bigbluebuttonbn_mform_add_element(&$mform, $type, $name, $datatype,
413
            $descriptionkey, $defaultvalue = null, $options = null, $rule = null) {
414
        if ($type === 'hidden') {
415
            $mform->addElement($type, $name, $defaultvalue);
416
            $mform->setType($name, $datatype);
417
            return;
418
        }
419
        $mform->addElement($type, $name, get_string($descriptionkey, 'bigbluebuttonbn'), $options);
420
        if (get_string_manager()->string_exists($descriptionkey.'_help', 'bigbluebuttonbn')) {
421
            $mform->addHelpButton($name, $descriptionkey, 'bigbluebuttonbn');
422
        }
423
        if (!empty($rule)) {
424
            $mform->addRule($name, $rule['message'], $rule['type'], $rule['rule'], $rule['validator']);
425
        }
426
        $mform->setDefault($name, $defaultvalue);
427
        $mform->setType($name, $datatype);
428
    }
429
}
430