upgrade.php 16.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
// This file is part of VPL for Moodle - http://vpl.dis.ulpgc.es/
//
// VPL for Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VPL for Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with VPL for Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 *
 * @package mod_vpl
 * @copyright 2012 Juan Carlos Rodríguez-del-Pino
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author Juan Carlos Rodríguez-del-Pino <jcrodriguez@dis.ulpgc.es>
 */
defined( 'MOODLE_INTERNAL' ) || die();
function xmldb_vpl_upgrade($oldversion = 0) {
    global $CFG, $DB;

    $dbman = $DB->get_manager();
    if ($oldversion < 2012060112) {

        // Define field intro to be added to vpl.
        $table = new xmldb_table( 'vpl' );

        $field = new xmldb_field( 'visiblefrom' );

        // Conditionally launch drop field intro.
        if ($dbman->field_exists( $table, $field )) {
            $dbman->drop_field( $table, $field );
        }

        $field = new xmldb_field( 'availablefrom', XMLDB_TYPE_INTEGER, '10'
                , XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'shortdescription' );

        // Launch rename field startdate.
        if ($dbman->field_exists( $table, $field )) {
            $dbman->rename_field( $table, $field, 'startdate' );
        }

        $field = new xmldb_field( 'intro', XMLDB_TYPE_TEXT, 'medium', null, null, null, null, 'shortdescription' );
        // Conditionally launch add field intro.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }
        $field = new xmldb_field( 'introformat', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0', 'intro' );
        // Conditionally launch add field introformat.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }

        $field = new xmldb_field( 'worktype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'jailservers' );

        // Conditionally launch add field worktype.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }

        $field = new xmldb_field( 'worktype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'jailservers' );

        // Conditionally launch add field worktype.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }

        $field = new xmldb_field( 'emailteachers', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'worktype' );

        // Conditionally launch add field emailteachers.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }

        // Define field mailed to be added to vpl_submissions.
        $table = new xmldb_table( 'vpl_submissions' );
        $field = new xmldb_field( 'mailed', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'grade' );

        // Conditionally launch add field mailed.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }
        $field = new xmldb_field( 'highlight', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'mailed' );

        // Conditionally launch add field highlight.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }

        $table = new xmldb_table( 'vpl_jailservers' );
        $field = new xmldb_field( 'nrequests', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'laststrerror' );

        // Conditionally launch add field nrequests.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }
        rename( $CFG->dataroot . '/vpl_data', $CFG->dataroot . '/vpl_data_old' );
        mkdir( $CFG->dataroot . '/vpl_data' );
        // Load all vpl ids.
        $vpls = $DB->get_records( 'vpl', null, '', 'id' );
        $vplbar = new progress_bar( 'migratingvpl', 500, true );
        $vplbarpos = 1;
        $vplbartotal = count( $vpls );
        foreach ($vpls as $vpl) {
            $vplbar->update( $vplbarpos, $vplbartotal, "Migrating VPL instances $vplbarpos/$vplbartotal" );
            $vplbarpos ++;
            $id = $vpl->id;
            // Load full vpl instance.
            $vpl = $DB->get_record( 'vpl', array (
                    'id' => $id
            ) );
            $oldpath = $CFG->dataroot . '/vpl_data_old/' . $vpl->course . '/' . $id . '/config';
            $newpath = $CFG->dataroot . '/vpl_data/' . $id;
            if (file_exists( $oldpath )) {
                rename( $oldpath, $newpath );
            }
            $fullpath = $newpath . '/fulldescription.html';
            if (file_exists( $fullpath )) {
                $vpl->intro = file_get_contents( $fullpath );
                unlink( $fullpath );
            } else {
                $vpl->intro = '';
            }
            $vpl->shortdescription = strip_tags( $vpl->shortdescription );
            $vpl->introformat = 1;
            $DB->update_record( 'vpl', $vpl );
            $subs = $DB->get_records( 'vpl_submissions', array (
                    'vpl' => $id
            ), '', 'id,userid' );
            upgrade_set_timeout( 300 + count( $subs ) / 10 );
            $oldbasepath = $CFG->dataroot . '/vpl_data_old/' . $vpl->course . '/' . $id . '/usersdata';
            $newbasepath = $CFG->dataroot . '/vpl_data/' . $id . '/usersdata';
            @mkdir( $newbasepath, $CFG->directorypermissions, true );
            foreach ($subs as $sub) {
                $oldpath = $oldbasepath . '/' . $sub->userid . '/' . $sub->id;
                $newpath = $newbasepath . '/' . $sub->userid;
                @mkdir( $newpath, $CFG->directorypermissions, true );
                $newpath .= '/' . $sub->id;
                if (file_exists( $oldpath )) {
                    rename( $oldpath, $newpath );
                }
                $olddir = $newpath . '/submitedfiles';
                $oldfile = $newpath . '/submitedfilelist.txt';
                $newdir = $newpath . '/submittedfiles';
                $newfile = $newpath . '/submittedfiles.lst';
                if (file_exists( $olddir )) {
                    rename( $olddir, $newdir );
                }
                if (file_exists( $oldfile )) {
                    rename( $oldfile, $newfile );
                }
            }
        }
        // VPL savepoint reached.
        upgrade_mod_savepoint( true, 2012060112, 'vpl' );
    }
    if ($oldversion < 2012100212) {

        $table = new xmldb_table( 'vpl_jailservers' );
        $field = new xmldb_field( 'nbusy', XMLDB_TYPE_INTEGER, '10', ! XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'nrequests' );

        // Conditionally launch add field nbusy.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }

        // VPL savepoint reached.
        upgrade_mod_savepoint( true, 2012100212, 'vpl' );
    }
    if ($oldversion < 2013111512) {
        // Define table vpl_running_processes to be created.
        $table = new xmldb_table( 'vpl_running_processes' );

        // Adding fields to table vpl_running_processes.
        $table->add_field( 'id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null );
        $table->add_field( 'userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null );
        $table->add_field( 'vpl', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null );
        $table->add_field( 'server', XMLDB_TYPE_CHAR, '256', null, XMLDB_NOTNULL, null, null );
        $table->add_field( 'start_time', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null );
        $table->add_field( 'adminticket', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null );

        // Adding keys to table vpl_running_processes.
        $table->add_key( 'primary', XMLDB_KEY_PRIMARY, array (
                'id'
        ) );

        // Adding indexes to table vpl_running_processes.
        $table->add_index( 'userid_id', XMLDB_INDEX_UNIQUE, array (
                'userid',
                'id'
        ) );

        // Conditionally launch create table for vpl_running_processes.
        if (! $dbman->table_exists( $table )) {
            $dbman->create_table( $table );
        }

        // VPL savepoint reached.
        upgrade_mod_savepoint( true, 2013111512, 'vpl' );
    }

    $vpl33 = 2017112412;
    if ($oldversion < $vpl33) {
        // Define field nevaluations to be added to vpl_submissions.
        $table = new xmldb_table('vpl_submissions');
        $field = new xmldb_field('nevaluations', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'highlight');

        // Conditionally launch add field nevaluations.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('groupid', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'nevaluations');

        // Conditionally launch add field groupid.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Define field nevaluations to be added to vpl_submissions.
        $table = new xmldb_table('vpl');
        $field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'emailteachers');
        // Conditionally launch add field nevaluations.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('freeevaluations', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'timemodified');
        // Conditionally launch add field nevaluations.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('reductionbyevaluation', XMLDB_TYPE_CHAR, '10', null, null, null, '0', 'freeevaluations');
        // Conditionally launch add field groupid.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }
        $DB->delete_records('vpl_jailservers');
        $table = new xmldb_table('vpl_jailservers');
        $key = new xmldb_key('servers_key', XMLDB_KEY_UNIQUE, array('server'));
        // Launch drop key servers_key.
        $dbman->drop_key($table, $key);

        $field = new xmldb_field('serverhash', XMLDB_TYPE_INTEGER, '20', null, null, null, '0', 'nbusy');
        // Conditionally launch add field serverhash.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $index = new xmldb_index('serverhash_idx', XMLDB_INDEX_NOTUNIQUE, array('serverhash'));
        // Conditionally launch add index serverhash_idx.
        if (!$dbman->index_exists($table, $index)) {
            $dbman->add_index($table, $index);
        }
        // Vpl savepoint reached.
        upgrade_mod_savepoint(true, $vpl33, 'vpl');
    }
    $vpl331 = 2017121312;
    if ($oldversion < $vpl331) {
        // Define field sebrequired to be added to vpl.
        $table = new xmldb_table('vpl');
        $field = new xmldb_field('sebrequired', XMLDB_TYPE_INTEGER, '2', null, null, null, '0', 'reductionbyevaluation');

        // Conditionally launch add field sebrequired.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('sebkeys', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'sebrequired');

        // Conditionally launch add field sebkeys.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('runscript', XMLDB_TYPE_CHAR, '63', null, null, null, null, 'sebkeys');

        // Conditionally launch add field runscript.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('debugscript', XMLDB_TYPE_CHAR, '63', null, null, null, null, 'runscript');

        // Conditionally launch add field debugscript.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        $field = new xmldb_field('worktype', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'jailservers');

        // Conditionally launch add field worktype.
        if (!$dbman->field_exists($table, $field)) {
            $dbman->add_field($table, $field);
        }

        // Vpl savepoint reached.
        upgrade_mod_savepoint(true, $vpl331, 'vpl');
    }
    if ($oldversion < 2018011314) {
        // Define table vpl  to be created.
        $table = new xmldb_table( 'vpl' );

        // Adding fields to table vpl.
        $field = new xmldb_field( 'usableasbase', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, null, null, '0', 'variationtitle' );

        // Conditionally launch add field worktype.
        if (! $dbman->field_exists( $table, $field )) {
            $dbman->add_field( $table, $field );
        }
        $rs = $DB->get_recordset_select('vpl','basedon > 0',null,null,'basedon');
        foreach ($rs as $record){
            $DB->update_record('vpl', array('id' => $record->basedon,'usableasbase' => 1), $bulk = false);
        }
        $rs->close();
        // VPL savepoint reached.
        upgrade_mod_savepoint( true, 2018011314, 'vpl' );
    }
326
    if ($oldversion < 2021061600) {
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

        // Define table vpl_overrides to be created.
        $table = new xmldb_table('vpl_overrides');

        // Adding fields to table vpl_overrides.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('vpl', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('startdate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('duedate', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('freeevaluations', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('reductionbyevaluation', XMLDB_TYPE_CHAR, '10', null, null, null, null);

        // Adding keys to table vpl_overrides.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

342
343
344
        // Adding indexes to table vpl_assigned_overrides.
        $table->add_index('vpl', XMLDB_INDEX_NOTUNIQUE, ['vpl']);

345
346
347
348
349
        // Conditionally launch create table for vpl_overrides.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
        // Define table vpl_assigned_overrides to be created.
        $table = new xmldb_table('vpl_assigned_overrides');

        // Adding fields to table vpl_assigned_overrides.
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
        $table->add_field('vpl', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('groupid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
        $table->add_field('override', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);

        // Adding keys to table vpl_assigned_overrides.
        $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

        // Adding indexes to table vpl_assigned_overrides.
        $table->add_index('vpl', XMLDB_INDEX_NOTUNIQUE, ['vpl']);
        $table->add_index('override', XMLDB_INDEX_NOTUNIQUE, ['override']);

        // Conditionally launch create table for vpl_assigned_overrides.
        if (!$dbman->table_exists($table)) {
            $dbman->create_table($table);
        }

        // Vpl savepoint reached.
        upgrade_mod_savepoint(true, 2021061600, 'vpl');
    }
375
376
    return true;
}