Completed
Pull Request — master (#33)
by Jesus
02:27
created

upgrade.php ➔ xmldb_bigbluebuttonbn_upgrade()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 89
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 60
nc 8
nop 1
dl 0
loc 89
rs 8.38
c 0
b 0
f 0

How to fix   Long Method   

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
// 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
 * Upgrade logic.
19
 *
20
 * @author    Fred Dixon  (ffdixon [at] blindsidenetworks [dt] com)
21
 * @author    Jesus Federico  (jesus [at] blindsidenetworks [dt] com)
22
 * @copyright 2010-2017 Blindside Networks Inc
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
24
 */
25
26
defined('MOODLE_INTERNAL') || die();
27
28
function xmldb_bigbluebuttonbn_upgrade($oldversion = 0) {
29
    global $DB;
30
31
    $dbman = $DB->get_manager();
32
33
    if ($oldversion < 2015080605) {
34
        // Drop field description.
35
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'description');
36
37
        // Change welcome, allow null.
38
        $fielddefinition = array('type' => XMLDB_TYPE_TEXT,
39
                                  'precision' => null,
40
                                  'unsigned' => null,
41
                                  'notnull' => XMLDB_NOTNULL,
42
                                  'sequence' => null,
43
                                  'default' => null,
44
                                  'previous' => 'type');
45
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'welcome',
46
            $fielddefinition);
47
48
        // Change userid definition in bigbluebuttonbn_log.
49
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER,
50
                                  'precision' => '10',
51
                                  'unsigned' => XMLDB_UNSIGNED,
52
                                  'notnull' => XMLDB_NOTNULL,
53
                                  'sequence' => null,
54
                                  'default' => null,
55
                                  'previous' => 'bigbluebuttonbnid');
56
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn_log', 'userid',
57
            $fielddefinition);
58
59
        upgrade_mod_savepoint(true, 2015080605, 'bigbluebuttonbn');
60
    }
61
62
    if ($oldversion < 2016011305) {
63
        // Define field type to be droped from bigbluebuttonbn.
64
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'type');
65
66
        // Rename table bigbluebuttonbn_log to bigbluebuttonbn_logs.
67
        xmldb_bigbluebuttonbn_rename_table($dbman, 'bigbluebuttonbn_log', 'bigbluebuttonbn_logs');
68
69
        // Rename field event to log in table bigbluebuttonbn_logs.
70
        xmldb_bigbluebuttonbn_rename_field($dbman, 'bigbluebuttonbn_logs', 'event', 'log');
71
72
        upgrade_mod_savepoint(true, 2016011305, 'bigbluebuttonbn');
73
    }
74
75
    if ($oldversion < 2016080106) {
76
        // Drop field newwindow.
77
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'newwindow');
78
79
        // Add field type.
80
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER,
81
                                  'precision' => '2',
82
                                  'unsigned' => XMLDB_UNSIGNED,
83
                                  'notnull' => XMLDB_NOTNULL,
84
                                  'sequence' => null,
85
                                  'default' => 0,
86
                                  'previous' => 'id');
87
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'type',
88
            $fielddefinition);
89
90
        // Add field recordings_html.
91
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER,
92
                                  'precision' => '1',
93
                                  'unsigned' => XMLDB_UNSIGNED,
94
                                  'notnull' => XMLDB_NOTNULL,
95
                                  'sequence' => null,
96
                                  'default' => 0,
97
                                  'previous' => null);
98
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'recordings_html',
99
            $fielddefinition);
100
101
        // Add field recordings_deleted_activities.
102
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER,
103
                                  'precision' => '1',
104
                                  'unsigned' => XMLDB_UNSIGNED,
105
                                  'notnull' => XMLDB_NOTNULL,
106
                                  'sequence' => null,
107
                                  'default' => 1,
108
                                  'previous' => null);
109
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'recordings_deleted_activities',
110
            $fielddefinition);
111
112
        upgrade_mod_savepoint(true, 2016080106, 'bigbluebuttonbn');
113
    }
114
115
    return true;
116
}
117
118
function xmldb_bigbluebuttonbn_add_change_field($dbman, $tablename, $fieldname, $fielddefinition) {
119
    $table = new xmldb_table($tablename);
120
    $field = new xmldb_field($fieldname);
121
    $field->set_attributes($fielddefinition['type'],
122
                           $fielddefinition['precision'],
123
                           $fielddefinition['unsigned'],
124
                           $fielddefinition['notnull'],
125
                           $fielddefinition['sequence'],
126
                           $fielddefinition['default'],
127
                           $fielddefinition['previous']);
128
    if ($dbman->field_exists($table, $field)) {
129
        $dbman->change_field($table, $field, true, true);
130
131
        return;
132
    }
133
134
    $dbman->add_field($table, $field, true, true);
135
}
136
137 View Code Duplication
function xmldb_bigbluebuttonbn_drop_field($dbman, $tablename, $fieldname) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
138
    $table = new xmldb_table($tablename);
139
    $field = new xmldb_field($fieldname);
140
    if ($dbman->field_exists($table, $field)) {
141
        $dbman->drop_field($table, $field, true, true);
142
    }
143
}
144
145 View Code Duplication
function xmldb_bigbluebuttonbn_rename_field($dbman, $tablename, $fieldnameold, $fieldnamenew) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in 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...
146
    $table = new xmldb_table($tablename);
147
    $field = new xmldb_field($fieldnameold);
148
    if ($dbman->field_exists($table, $field)) {
149
        $dbman->rename_field($table, $field, $fieldnamenew, true, true);
150
    }
151
}
152
153
function xmldb_bigbluebuttonbn_rename_table($dbman, $tablenameold, $tablenamenew) {
154
    $table = new xmldb_table($tablenameold);
155
    if ($dbman->table_exists($table)) {
156
        $dbman->rename_table($table, $tablenamenew, true, true);
157
    }
158
}
159