Completed
Branch v2.2-stable (d83e60)
by Jesus
02:31
created

upgrade.php ➔ xmldb_bigbluebuttonbn_upgrade()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 99
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 65
nc 48
nop 1
dl 0
loc 99
rs 5.2996
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
 * @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(dirname(__FILE__)).'/locallib.php');
30
31
/**
32
 * Performs data migrations and updates on upgrade.
33
 *
34
 * @param   integer   $oldversion
35
 * @return  boolean
36
 */
37
function xmldb_bigbluebuttonbn_upgrade($oldversion = 0) {
38
    global $DB;
39
    $dbman = $DB->get_manager();
40
    if ($oldversion < 2015080605) {
41
        // Drop field description.
42
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'description');
43
        // Change welcome, allow null.
44
        $fielddefinition = array('type' => XMLDB_TYPE_TEXT, 'precision' => null, 'unsigned' => null,
45
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => null, 'previous' => 'type');
46
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'welcome',
47
            $fielddefinition);
48
        // Change userid definition in bigbluebuttonbn_log.
49
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER, 'precision' => '10', 'unsigned' => null,
50
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => null,
51
            'previous' => 'bigbluebuttonbnid');
52
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn_log', 'userid',
53
            $fielddefinition);
54
        // No settings to migrate.
55
        // Update db version tag.
56
        upgrade_mod_savepoint(true, 2015080605, 'bigbluebuttonbn');
57
    }
58
    if ($oldversion < 2016011305) {
59
        // Define field type to be droped from bigbluebuttonbn.
60
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'type');
61
        // Rename table bigbluebuttonbn_log to bigbluebuttonbn_logs.
62
        xmldb_bigbluebuttonbn_rename_table($dbman, 'bigbluebuttonbn_log', 'bigbluebuttonbn_logs');
63
        // Rename field event to log in table bigbluebuttonbn_logs.
64
        xmldb_bigbluebuttonbn_rename_field($dbman, 'bigbluebuttonbn_logs', 'event', 'log');
65
        // No settings to migrate.
66
        // Update db version tag.
67
        upgrade_mod_savepoint(true, 2016011305, 'bigbluebuttonbn');
68
    }
69
    if ($oldversion < 2017101000) {
70
        // Drop field newwindow.
71
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'newwindow');
72
        // Add field type.
73
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER, 'precision' => '2', 'unsigned' => null,
74
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => 0, 'previous' => 'id');
75
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'type',
76
            $fielddefinition);
77
        // Add field recordings_html.
78
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER, 'precision' => '1', 'unsigned' => null,
79
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => 0, 'previous' => null);
80
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'recordings_html',
81
            $fielddefinition);
82
        // Add field recordings_deleted.
83
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER, 'precision' => '1', 'unsigned' => null,
84
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => 1, 'previous' => null);
85
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'recordings_deleted',
86
            $fielddefinition);
87
        // Add field recordings_imported.
88
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER, 'precision' => '1', 'unsigned' => null,
89
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => 0, 'previous' => null);
90
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'recordings_imported',
91
            $fielddefinition);
92
        // Drop field newwindow.
93
        xmldb_bigbluebuttonbn_drop_field($dbman, 'bigbluebuttonbn', 'tagging');
94
        // Migrate settings.
95
        unset_config('bigbluebuttonbn_recordingtagging_default', '');
96
        unset_config('bigbluebuttonbn_recordingtagging_editable', '');
97
        $cfgvalue = get_config('', 'bigbluebuttonbn_importrecordings_from_deleted_activities_enabled');
98
        set_config('bigbluebuttonbn_importrecordings_from_deleted_enabled', $cfgvalue, '');
99
        unset_config('bigbluebuttonbn_importrecordings_from_deleted_activities_enabled', '');
100
        $cfgvalue = get_config('', 'bigbluebuttonbn_moderator_default');
101
        set_config('bigbluebuttonbn_participant_moderator_default', $cfgvalue, '');
102
        unset_config('bigbluebuttonbn_moderator_default', '');
103
        // Update db version tag.
104
        upgrade_mod_savepoint(true, 2017101000, 'bigbluebuttonbn');
105
    }
106
    if ($oldversion < 2017101009) {
107
        // Add field recordings_preview.
108
        $fielddefinition = array('type' => XMLDB_TYPE_INTEGER, 'precision' => '1', 'unsigned' => null,
109
            'notnull' => XMLDB_NOTNULL, 'sequence' => null, 'default' => 0, 'previous' => null);
110
        xmldb_bigbluebuttonbn_add_change_field($dbman, 'bigbluebuttonbn', 'recordings_preview',
111
            $fielddefinition);
112
        upgrade_mod_savepoint(true, 2017101009, 'bigbluebuttonbn');
113
    }
114
    if ($oldversion < 2017101010) {
115
        // Fix for CONTRIB-7221.
116
        if ($oldversion == 2017101003) {
117
            // A bug intorduced in 2017101003 causes new instances to be created without BBB passwords.
118
            // A workaround was put in place in version 2017101004 that was relabeled to 2017101005.
119
            // However, as the code was relocated to upgrade.php in version 2017101010, a new issue came up.
120
            // There is now a timeout error when the plugin is upgraded in large Moodle sites.
121
            // The script should only be considered when migrating from this version.
122
            $sql  = "SELECT * FROM {bigbluebuttonbn} ";
123
            $sql .= "WHERE moderatorpass = ? OR viewerpass = ?";
124
            $instances = $DB->get_records_sql($sql, array('', ''));
125
            foreach ($instances as $instance) {
126
                $instance->moderatorpass = bigbluebuttonbn_random_password(12);
127
                $instance->viewerpass = bigbluebuttonbn_random_password(12, $instance->moderatorpass);
128
                // Store passwords in the database.
129
                $DB->update_record('bigbluebuttonbn', $instance);
130
            }
131
        }
132
        upgrade_mod_savepoint(true, 2017101010, 'bigbluebuttonbn');
133
    }
134
    return true;
135
}
136
137
/**
138
 * Generic helper function for adding or changing a field in a table.
139
 *
140
 * @param   object    $dbman
141
 * @param   string    $tablename
142
 * @param   string    $fieldname
143
 * @param   array     $fielddefinition
144
 */
145
function xmldb_bigbluebuttonbn_add_change_field($dbman, $tablename, $fieldname, $fielddefinition) {
146
    $table = new xmldb_table($tablename);
147
    $field = new xmldb_field($fieldname);
148
    $field->set_attributes($fielddefinition['type'], $fielddefinition['precision'], $fielddefinition['unsigned'],
149
        $fielddefinition['notnull'], $fielddefinition['sequence'], $fielddefinition['default'],
150
        $fielddefinition['previous']);
151
    if ($dbman->field_exists($table, $field)) {
152
        $dbman->change_field_type($table, $field, true, true);
153
        $dbman->change_field_precision($table, $field, true, true);
154
        $dbman->change_field_notnull($table, $field, true, true);
155
        $dbman->change_field_default($table, $field, true, true);
156
        return;
157
    }
158
    $dbman->add_field($table, $field, true, true);
159
}
160
161
/**
162
 * Generic helper function for dropping a field from a table.
163
 *
164
 * @param   object    $dbman
165
 * @param   string    $tablename
166
 * @param   string    $fieldname
167
 */
168 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...
169
    $table = new xmldb_table($tablename);
170
    $field = new xmldb_field($fieldname);
171
    if ($dbman->field_exists($table, $field)) {
172
        $dbman->drop_field($table, $field, true, true);
173
    }
174
}
175
176
/**
177
 * Generic helper function for renaming a field in a table.
178
 *
179
 * @param   object    $dbman
180
 * @param   string    $tablename
181
 * @param   string    $fieldnameold
182
 * @param   string    $fieldnamenew
183
 */
184 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...
185
    $table = new xmldb_table($tablename);
186
    $field = new xmldb_field($fieldnameold);
187
    if ($dbman->field_exists($table, $field)) {
188
        $dbman->rename_field($table, $field, $fieldnamenew, true, true);
189
    }
190
}
191
192
/**
193
 * Generic helper function for renaming a table.
194
 *
195
 * @param   object    $dbman
196
 * @param   string    $tablenameold
197
 * @param   string    $tablenamenew
198
 */
199
function xmldb_bigbluebuttonbn_rename_table($dbman, $tablenameold, $tablenamenew) {
200
    $table = new xmldb_table($tablenameold);
201
    if ($dbman->table_exists($table)) {
202
        $dbman->rename_table($table, $tablenamenew, true, true);
203
    }
204
}
205