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

upgrade.php ➔ xmldb_bigbluebuttonbn_upgrade()   F

Complexity

Conditions 44
Paths > 20000

Size

Total Lines 268
Code Lines 145

Duplication

Lines 21
Ratio 7.84 %

Importance

Changes 18
Bugs 4 Features 2
Metric Value
cc 44
eloc 145
c 18
b 4
f 2
nc 716679900
nop 1
dl 21
loc 268
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Upgrade logic.
5
 *
6
 * @package   mod_bigbluebuttonbn
7
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
8
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
9
 * @copyright 2010-2015 Blindside Networks Inc.
10
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
11
 */
12
13
function xmldb_bigbluebuttonbn_upgrade($oldversion=0) {
14
15
    global $CFG, $THEME, $DB;
16
    $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
17
    
18
    $result = true;
19
20
    if ($result && $oldversion < 2012040200) {
21
        // Define field intro to be droped from bigbluebuttonbn
22
        $table = new xmldb_table('bigbluebuttonbn');
23
        $field = new xmldb_field('intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null,'name');
24
25
        // Drop field intro
26
        if ($dbman->field_exists($table, $field)) {
27
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
28
        }
29
30
        // Define field introformat to be droped from bigbluebuttonbn
31
        $table = new xmldb_table('bigbluebuttonbn');
32
        $field = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
33
34
        // Drop field introformat
35
        if ($dbman->field_exists($table, $field)) {
36
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
37
        }
38
39
        // Once we reach this point, we can store the new version and consider the module
40
        // upgraded to the version 2012040200 so the next time this block is skipped
41
        upgrade_mod_savepoint(true, 2012040200, 'bigbluebuttonbn');
42
    }
43
    
44
    if ($result && $oldversion < 2012062705) {
45
46
        // Define table bigbluebuttonbn_logs to be created
47
        $table = new xmldb_table('bigbluebuttonbn_logs');
48
49
        // Adding fields to table bigbluebuttonbn_logs
50
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
51
        $table->add_field('meetingid', XMLDB_TYPE_CHAR, '256', null, XMLDB_NOTNULL, null, null);
52
        $table->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
53
        $table->add_field('bigbluebuttonbnid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
54
        $table->add_field('record', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
55
        $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
56
        $table->add_field('event', XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
57
58
        // Adding keys to table bigbluebuttonbn_logs
59
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
60
61
        // Conditionally launch create table for bigbluebuttonbn_logs
62
        if (!$dbman->table_exists($table)) {
63
            $dbman->create_table($table);
64
        }
65
66
        // bigbluebuttonbn savepoint reached
67
        upgrade_mod_savepoint(true, 2012062705, 'bigbluebuttonbn');
68
    }
69
70
    if ($result && $oldversion < 2012100100) {
71
72
        $table = new xmldb_table('bigbluebuttonbn');
73
        $field = new xmldb_field('welcome');
74
        $field->set_attributes(XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, null, null, 'type');
75
76
        $dbman->change_field_type($table, $field, $continue=true, $feedback=true);
77
78
        upgrade_mod_savepoint(true, 2012100100, 'bigbluebuttonbn');
79
    }
80
81
    if ($result && $oldversion < 2014050100) {
82
83
        $table = new xmldb_table('bigbluebuttonbn');
84
        $field = new xmldb_field('allmoderators');
85
        $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
86
87
        if (!$dbman->field_exists($table, $field)) {
88
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
89
        }
90
91
        upgrade_mod_savepoint(true, 2014050100, 'bigbluebuttonbn');
92
    }
93
94 View Code Duplication
    if ($result && $oldversion < 2014070420) {
1 ignored issue
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
96
        $table = new xmldb_table('bigbluebuttonbn');
97
        $field = new xmldb_field('participants', XMLDB_TYPE_TEXT, 'medium', null, null, null, null);
98
99
        if (!$dbman->field_exists($table, $field)) {
100
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
101
        }
102
103
        upgrade_mod_savepoint(true, 2014070420, 'bigbluebuttonbn');
104
    }
105
106 View Code Duplication
    if ($result && $oldversion < 2014101004) {
1 ignored issue
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...
107
108
        $table = new xmldb_table('bigbluebuttonbn');
109
        $field = new xmldb_field('participants');
110
        $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null);
111
112
        $dbman->change_field_type($table, $field, $continue=true, $feedback=true);
113
114
        upgrade_mod_savepoint(true, 2014101004, 'bigbluebuttonbn');
115
    }
116
117
    if ($result && $oldversion < 2015063000) {
118
        // Update the bigbluebuttonbn table
119
        $table = new xmldb_table('bigbluebuttonbn');
120
        //// Drop field timeduration
121
        $field = new xmldb_field('timeduration');
122
        if( $dbman->field_exists($table, $field) ) {
123
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
124
        }
125
        //// Drop field allmoderators
126
        $field = new xmldb_field('allmoderators');
127
        if( $dbman->field_exists($table, $field) ) {
128
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
129
        }
130
        //// Add field intro
131
        $field = new xmldb_field('intro');
132
        $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'name');
133
        if( !$dbman->field_exists($table, $field) ) {
134
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
135
        }
136
        //// Add field introformat
137
        $field = new xmldb_field('introformat');
138
        $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 1, 'intro');
139
        if( !$dbman->field_exists($table, $field) ) {
140
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
141
        }
142
        //// Add field tagging
143
        $field = new xmldb_field('tagging');
144
        $field->set_attributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'record');
145
        if( !$dbman->field_exists($table, $field) ) {
146
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
147
        }
148
        //// Add field presentation
149
        $field = new xmldb_field('presentation');
150
        $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'timemodified');
151
        if( !$dbman->field_exists($table, $field) ) {
152
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
153
        }
154
        //// Add field type
155
        $field = new xmldb_field('type');
156
        $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'course');
157
        if( !$dbman->field_exists($table, $field) ) {
158
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
159
        }
160
        //// Rename field timeavailable
161
        $field = new xmldb_field('timeavailable');
162
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
163
        if( $dbman->field_exists($table, $field) ) {
164
            $dbman->rename_field($table, $field, 'openingtime', $continue=true, $feedback=true);
165
        }
166
        //// Rename field timedue
167
        $field = new xmldb_field('timedue');
168
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
169
        if( $dbman->field_exists($table, $field) ) {
170
            $dbman->rename_field($table, $field, 'closingtime', $continue=true, $feedback=true);
171
        }
172
        //// Add field timecreated
173
        $field = new xmldb_field('timecreated');
174
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'closingtime');
175
        if( !$dbman->field_exists($table, $field) ) {
176
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
177
        }
178
        //// Add field userlimit
179
        $field = new xmldb_field('userlimit');
180
        $field->set_attributes(XMLDB_TYPE_INTEGER, '3', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
181
        if (!$dbman->field_exists($table, $field)) {
182
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
183
        }
184
185
        // Update the bigbluebuttonbn_logs table
186
        $table = new xmldb_table('bigbluebuttonbn_logs');
187
        //// Add field userid
188
        $field = new xmldb_field('userid');
189
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'bigbluebuttonbnid');
190
        if( !$dbman->field_exists($table, $field) ) {
191
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
192
        }
193
        //// Add field meta
194
        $field = new xmldb_field('meta');
195
        $field->set_attributes(XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'event');
196
        if( !$dbman->field_exists($table, $field) ) {
197
            $dbman->add_field($table, $field, $continue=true, $feedback=true);
198
        }
199
        //// Drop field recording
200
        $field = new xmldb_field('record');
201
        if( $dbman->field_exists($table, $field) ) {
202
            //// Migrate data in field recording to new format in meta
203
            $meta = new \stdClass();
204
205
            // Record => true.
206
            $meta->record = true;
207
            $DB->set_field('bigbluebuttonbn_logs', 'meta', json_encode($meta), array('event' => 'Create', 'record' => 1));
208
209
            // Record => false.
210
            $meta->record = false;
211
            $DB->set_field('bigbluebuttonbn_logs', 'meta', json_encode($meta), array('event' => 'Create', 'record' => 0));
212
213
            // Drop field recording
214
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
215
        }
216
217
        upgrade_mod_savepoint(true, 2015063000, 'bigbluebuttonbn');
218
    }
219
220
    if ($result && $oldversion < 2015080605) {
221
        // Update the bigbluebuttonbn table
222
        $table = new xmldb_table('bigbluebuttonbn');
223
        //// Drop field description
224
        $field = new xmldb_field('description');
225
        if( $dbman->field_exists($table, $field) ) {
226
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
227
        }
228
        //// Change welcome, allow null
229
        $field = new xmldb_field('welcome');
230
        $field->set_attributes(XMLDB_TYPE_TEXT, null, null, null, null, null, null, null, 'type');
231
        if( $dbman->field_exists($table, $field) ) {
232
            $dbman->change_field_notnull($table, $field, $continue=true, $feedback=true);
233
        }
234
235
        // Update the bigbluebuttonbn_logs table
236
        $table = new xmldb_table('bigbluebuttonbn_logs');
237
        //// Change welcome, allow null
238
        $field = new xmldb_field('userid');
239
        //$field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'bigbluebuttonbnid');
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
240
        $field->set_attributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'bigbluebuttonbnid');
241
        if( $dbman->field_exists($table, $field) ) {
242
            $dbman->change_field_notnull($table, $field, $continue=true, $feedback=true);
243
        }
244
245
        upgrade_mod_savepoint(true, 2015080605, 'bigbluebuttonbn');
246
    }
247
248
    if ( $result && $oldversion < 2016011305 ) {
249
        // Update the bigbluebuttonbn table
250
        $table = new xmldb_table('bigbluebuttonbn');
251
252
        // Define field type to be droped from bigbluebuttonbn
253
        $field = new xmldb_field('type');
254
        $field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0, 'course');
255
        if ( $dbman->field_exists($table, $field) ) {
256
            $dbman->drop_field($table, $field, $continue=true, $feedback=true);
257
        }
258
259
        // Make sure bigbluebuttonbn_logs table exists
260
        $table = new xmldb_table('bigbluebuttonbn_log');
261
        // Conditionally launch rename table for bigbluebuttonbn_logs
262
        if ($dbman->table_exists($table)) {
263
            $dbman->rename_table($table, 'bigbluebuttonbn_logs', $continue=true, $feedback=true);
264
        }
265
266
        // Update the bigbluebuttonbn_logs table
267
        $table = new xmldb_table('bigbluebuttonbn_logs');
268
269
        // Define field 'event' to be renamed
270
        $field = new xmldb_field('event');
271
        $field->set_attributes(XMLDB_TYPE_CHAR, '32', null, XMLDB_NOTNULL, null, null);
272
        if ( $dbman->field_exists($table, $field) ) {
273
            $dbman->rename_field($table, $field, 'log', $continue=true, $feedback=true);
274
        }
275
276
        upgrade_mod_savepoint(true, 2016011305, 'bigbluebuttonbn');
277
    }
278
279
    return $result;
280
}
281