Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 definitions for the hvp module.
19
 *
20
 * @package    mod_hvp
21
 * @copyright  2016 Joubel AS <contact@joubel.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * Adds data for tracking when content was created and last modified.
29
 */
30
function hvp_upgrade_2016011300() {
31
    global $DB;
32
    $dbman = $DB->get_manager();
33
 
34
    $table = new xmldb_table('hvp');
35
 
36
    // Define field timecreated to be added to hvp.
37
    $timecreated = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'slug');
38
 
39
    // Conditionally launch add field timecreated.
40
    if (!$dbman->field_exists($table, $timecreated)) {
41
        $dbman->add_field($table, $timecreated);
42
    }
43
 
44
    // Define field timemodified to be added to hvp.
45
    $timemodified = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'timecreated');
46
 
47
    // Conditionally launch add field timemodified.
48
    if (!$dbman->field_exists($table, $timemodified)) {
49
        $dbman->add_field($table, $timemodified);
50
    }
51
}
52
 
53
/**
54
 * Adds table for keeping track of, and cleaning up temporary files
55
 */
56
function hvp_upgrade_2016042500() {
57
    global $DB;
58
    $dbman = $DB->get_manager();
59
 
60
    // Define table hvp_tmpfiles to be created.
61
    $table = new xmldb_table('hvp_tmpfiles');
62
 
63
    // Adding fields to table hvp_tmpfiles.
64
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
65
 
66
    // Adding keys to table hvp_tmpfiles.
67
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
68
 
69
    // Conditionally launch create table for hvp_tmpfiles.
70
    if (!$dbman->table_exists($table)) {
71
        $dbman->create_table($table);
72
    }
73
}
74
 
75
/**
76
 * Adds events table
77
 */
78
function hvp_upgrade_2016050600() {
79
    global $DB;
80
    $dbman = $DB->get_manager();
81
 
82
    // Define table hvp_events to be created.
83
    $table = new xmldb_table('hvp_events');
84
 
85
    // Adding fields to table hvp_events.
86
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
87
    $table->add_field('user_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
88
    $table->add_field('created_at', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
89
    $table->add_field('type', XMLDB_TYPE_CHAR, '63', null, XMLDB_NOTNULL, null, null);
90
    $table->add_field('sub_type', XMLDB_TYPE_CHAR, '63', null, XMLDB_NOTNULL, null, null);
91
    $table->add_field('content_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
92
    $table->add_field('content_title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
93
    $table->add_field('library_name', XMLDB_TYPE_CHAR, '127', null, XMLDB_NOTNULL, null, null);
94
    $table->add_field('library_version', XMLDB_TYPE_CHAR, '31', null, XMLDB_NOTNULL, null, null);
95
 
96
    // Adding keys to table hvp_events.
97
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
98
 
99
    // Conditionally launch create table for hvp_events.
100
    if (!$dbman->table_exists($table)) {
101
        $dbman->create_table($table);
102
    }
103
 
104
    // Define table hvp_counters to be created.
105
    $table = new xmldb_table('hvp_counters');
106
 
107
    // Adding fields to table hvp_counters.
108
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
109
    $table->add_field('type', XMLDB_TYPE_CHAR, '63', null, XMLDB_NOTNULL, null, null);
110
    $table->add_field('library_name', XMLDB_TYPE_CHAR, '127', null, XMLDB_NOTNULL, null, null);
111
    $table->add_field('library_version', XMLDB_TYPE_CHAR, '31', null, XMLDB_NOTNULL, null, null);
112
    $table->add_field('num', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
113
 
114
    // Adding keys to table hvp_counters.
115
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
116
 
117
    // Adding indexes to table hvp_counters.
118
    $table->add_index('realkey', XMLDB_INDEX_NOTUNIQUE, [
119
        'type',
120
        'library_name',
121
        'library_version',
122
    ]);
123
 
124
    // Conditionally launch create table for hvp_counters.
125
    if (!$dbman->table_exists($table)) {
126
        $dbman->create_table($table);
127
    }
128
}
129
 
130
/**
131
 * Adds intro and introformat to hvp table
132
 */
133
function hvp_upgrade_2016051000() {
134
    global $DB;
135
    $dbman = $DB->get_manager();
136
 
137
    $table = new xmldb_table('hvp');
138
 
139
    // Define field intro to be added to hvp.
140
    $intro = new xmldb_field('intro', XMLDB_TYPE_TEXT, null, null, null, null, null, 'name');
141
 
142
    // Add field intro if not defined already.
143
    if (!$dbman->field_exists($table, $intro)) {
144
        $dbman->add_field($table, $intro);
145
    }
146
 
147
    // Define field introformat to be added to hvp.
148
    $introformat = new xmldb_field('introformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0', 'intro');
149
 
150
    // Add field introformat if not defined already.
151
    if (!$dbman->field_exists($table, $introformat)) {
152
        $dbman->add_field($table, $introformat);
153
    }
154
}
155
 
156
/**
157
 * Changes context of activity files to enable backup an restore.
158
 */
159
function hvp_upgrade_2016110100() {
160
    global $DB;
161
 
162
    // Change context of activity files from COURSE to MODULE.
163
    $filearea  = 'content';
164
    $component = 'mod_hvp';
165
 
166
    // Find activity ID and correct context ID.
167
    $hvpsresult = $DB->get_records_sql(
168
        "SELECT f.id AS fileid, f.itemid, c.id, f.filepath, f.filename, f.pathnamehash
169
                   FROM {files} f
170
                   JOIN {course_modules} cm ON f.itemid = cm.instance
171
                   JOIN {modules} md ON md.id = cm.module
172
                   JOIN {context} c ON c.instanceid = cm.id
173
                  WHERE md.name = 'hvp'
174
                    AND f.filearea = 'content'
175
                    AND c.contextlevel = " . CONTEXT_MODULE
176
    );
177
 
178
    foreach ($hvpsresult as $hvp) {
179
        // Need to re-hash pathname after changing context.
180
        $pathnamehash = file_storage::get_pathname_hash($hvp->id,
181
            $component,
182
            $filearea,
183
            $hvp->itemid,
184
            $hvp->filepath,
185
            $hvp->filename
186
        );
187
 
188
        // Double check that hash doesn't exist (avoid duplicate entries).
189
        if (!$DB->get_field_sql("SELECT contextid FROM {files} WHERE pathnamehash = '{$pathnamehash}'")) {
190
            // Update context ID and pathname hash for files.
191
            $DB->execute("
192
                  UPDATE {files}
193
                  SET contextid = {$hvp->id},
194
                      pathnamehash = '{$pathnamehash}'
195
                  WHERE pathnamehash = '{$hvp->pathnamehash}'"
196
            );
197
        }
198
    }
199
}
200
 
201
/**
202
 * Notifies about breaking changes to H5P content type styling
203
 */
204
function hvp_upgrade_2016122800() {
205
    // @codingStandardsIgnoreLine
206
    \mod_hvp\framework::messages('info', '<span style="font-weight: bold;">Upgrade your H5P content types!</span> Old content types will still work, but the authoring tool will look and feel much better if you <a href="https://h5p.org/update-all-content-types">upgrade the content types</a>.');
207
    \mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info'));
208
}
209
 
210
/**
211
 * Adds content type cache to enable the content type hub
212
 */
213
function hvp_upgrade_2017040500() {
214
    global $DB;
215
    $dbman = $DB->get_manager();
216
 
217
    // Add content type cache database.
218
    $table = new xmldb_table('hvp_libraries_hub_cache');
219
 
220
    // Adding fields to table hvp_libraries_hub_cache.
221
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
222
    $table->add_field('machine_name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
223
    $table->add_field('major_version', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
224
    $table->add_field('minor_version', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
225
    $table->add_field('patch_version', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
226
    $table->add_field('h5p_major_version', XMLDB_TYPE_INTEGER, '4', null, null, null, null);
227
    $table->add_field('h5p_minor_version', XMLDB_TYPE_INTEGER, '4', null, null, null, null);
228
    $table->add_field('title', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
229
    $table->add_field('summary', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
230
    $table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
231
    $table->add_field('icon', XMLDB_TYPE_CHAR, '511', null, XMLDB_NOTNULL, null, null);
232
    $table->add_field('created_at', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
233
    $table->add_field('updated_at', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
234
    $table->add_field('is_recommended', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
235
    $table->add_field('popularity', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
236
    $table->add_field('screenshots', XMLDB_TYPE_TEXT, null, null, null, null, null);
237
    $table->add_field('license', XMLDB_TYPE_TEXT, null, null, null, null, null);
238
    $table->add_field('example', XMLDB_TYPE_CHAR, '511', null, XMLDB_NOTNULL, null, null);
239
    $table->add_field('tutorial', XMLDB_TYPE_CHAR, '511', null, null, null, null);
240
    $table->add_field('keywords', XMLDB_TYPE_TEXT, null, null, null, null, null);
241
    $table->add_field('categories', XMLDB_TYPE_TEXT, null, null, null, null, null);
242
    $table->add_field('owner', XMLDB_TYPE_CHAR, '511', null, null, null, null);
243
 
244
    // Adding keys to table hvp_libraries_hub_cache.
245
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
246
 
247
    // Conditionally create table for hvp_libraries_hub_cache.
248
    if (!$dbman->table_exists($table)) {
249
        $dbman->create_table($table);
250
    }
251
 
252
    // Update the content type cache.
253
    $core = \mod_hvp\framework::instance();
254
    $core->updateContentTypeCache();
255
 
256
    // Print messages.
257
    \mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info'));
258
    \mod_hvp\framework::printMessages('error', \mod_hvp\framework::messages('error'));
259
 
260
    // Add has_icon to libraries folder.
261
    $table = new xmldb_table('hvp_libraries');
262
 
263
    // Define field has_icon to be added to hvp_libraries.
264
    $hasicon = new xmldb_field('has_icon', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
265
 
266
    // Add field has_icon if it does not exist.
267
    if (!$dbman->field_exists($table, $hasicon)) {
268
        $dbman->add_field($table, $hasicon);
269
    }
270
 
271
    // Display hub communication info.
272
    if (!get_config('mod_hvp', 'external_communication')) {
273
        // @codingStandardsIgnoreLine
274
        \mod_hvp\framework::messages('info', 'H5P now fetches content types directly from the H5P Hub. In order to do this, the H5P plugin will communicate with H5P.org once per day to fetch information about new and updated content types. It will send in anonymous data to the hub about H5P usage. You may disable the data contribution and/or the H5P Hub in the H5P settings.');
275
        \mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info'));
276
    }
277
 
278
    // Enable hub and delete old communication variable.
279
    set_config('hub_is_enabled', true, 'mod_hvp');
280
    unset_config('hub_is_enabled', 'mod_hvp');
281
}
282
 
283
/**
284
 * Adds xAPI results table to enable reporting
285
 */
286
function hvp_upgrade_2017050900() {
287
    global $DB;
288
    $dbman = $DB->get_manager();
289
 
290
    // Add report rendering.
291
    $table = new xmldb_table('hvp_xapi_results');
292
 
293
    // Add fields.
294
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
295
    $table->add_field('content_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
296
    $table->add_field('user_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
297
    $table->add_field('parent_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
298
    $table->add_field('interaction_type', XMLDB_TYPE_CHAR, '127', null, XMLDB_NOTNULL, null, null);
299
    $table->add_field('description', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
300
    $table->add_field('correct_responses_pattern', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
301
    $table->add_field('response', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
302
    $table->add_field('additionals', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
303
 
304
    // Add keys and index.
305
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
306
    $table->add_index('result', XMLDB_INDEX_UNIQUE, [
307
        'id',
308
        'content_id',
309
        'user_id',
310
    ]);
311
 
312
    // Create table if it does not exist.
313
    if (!$dbman->table_exists($table)) {
314
        $dbman->create_table($table);
315
    }
316
}
317
 
318
/**
319
 * Adds raw score and max score to xapi results table
320
 */
321
function hvp_upgrade_2017060900() {
322
    global $DB;
323
    $dbman = $DB->get_manager();
324
 
325
    // Add score to report rendering.
326
    $table = new xmldb_table('hvp_xapi_results');
327
 
328
    if ($dbman->table_exists($table)) {
329
        // Raw score field.
330
        $scorefield = new xmldb_field('raw_score', XMLDB_TYPE_INTEGER, '6');
331
        if (!$dbman->field_exists($table, $scorefield)) {
332
            $dbman->add_field($table, $scorefield);
333
        }
334
 
335
        // Max score field.
336
        $maxscorefield = new xmldb_field('max_score', XMLDB_TYPE_INTEGER, '6');
337
        if (!$dbman->field_exists($table, $maxscorefield)) {
338
            $dbman->add_field($table, $maxscorefield);
339
        }
340
    }
341
}
342
 
343
function hvp_upgrade_2018090300() {
344
    global $DB;
345
    $dbman = $DB->get_manager();
346
 
347
    $table = new xmldb_table('hvp');
348
 
349
    // Remove old, unused metadata fields.
350
    if ($dbman->field_exists($table, 'author')) {
351
        $dbman->drop_field($table, new xmldb_field('author'));
352
    }
353
 
354
    if ($dbman->field_exists($table, 'license')) {
355
        $dbman->drop_field($table, new xmldb_field('license'));
356
    }
357
 
358
    if ($dbman->field_exists($table, 'meta_keywords')) {
359
        $dbman->drop_field($table, new xmldb_field('meta_keywords'));
360
    }
361
 
362
    if ($dbman->field_exists($table, 'meta_description')) {
363
        $dbman->drop_field($table, new xmldb_field('meta_description'));
364
    }
365
 
366
    // Create new metadata fields.
367
    if (!$dbman->field_exists($table, 'authors')) {
368
        $dbman->add_field($table,
369
            new xmldb_field('authors', XMLDB_TYPE_TEXT, null, null, null, null, null)
370
        );
371
    }
372
 
373
    if (!$dbman->field_exists($table, 'source')) {
374
        $dbman->add_field($table,
375
            new xmldb_field('source', XMLDB_TYPE_CHAR, '255', null, null, null, null)
376
        );
377
    }
378
 
379
    if (!$dbman->field_exists($table, 'year_from')) {
380
        $dbman->add_field($table,
381
            new xmldb_field('year_from', XMLDB_TYPE_INTEGER, '4', null, null, null, null)
382
        );
383
    }
384
 
385
    if (!$dbman->field_exists($table, 'year_to')) {
386
        $dbman->add_field($table,
387
            new xmldb_field('year_to', XMLDB_TYPE_INTEGER, '4', null, null, null, null)
388
        );
389
    }
390
 
391
    if (!$dbman->field_exists($table, 'license')) {
392
        $dbman->add_field($table,
393
            new xmldb_field('license', XMLDB_TYPE_CHAR, '63', null, null, null, null)
394
        );
395
    }
396
 
397
    if (!$dbman->field_exists($table, 'license_version')) {
398
        $dbman->add_field($table,
399
            new xmldb_field('license_version', XMLDB_TYPE_CHAR, '15', null, null, null, null)
400
        );
401
    }
402
 
403
    if (!$dbman->field_exists($table, 'changes')) {
404
        $dbman->add_field($table,
405
            new xmldb_field('changes', XMLDB_TYPE_TEXT, null, null, null, null, null)
406
        );
407
    }
408
 
409
    if (!$dbman->field_exists($table, 'license_extras')) {
410
        $dbman->add_field($table,
411
            new xmldb_field('license_extras', XMLDB_TYPE_TEXT, null, null, null, null, null)
412
        );
413
    }
414
 
415
    if (!$dbman->field_exists($table, 'author_comments')) {
416
        $dbman->add_field($table,
417
            new xmldb_field('author_comments', XMLDB_TYPE_TEXT, null, null, null, null, null)
418
        );
419
    }
420
 
421
    // Add new libraries fields.
422
    $table = new xmldb_table('hvp_libraries');
423
    if (!$dbman->field_exists($table, 'add_to')) {
424
        $dbman->add_field($table,
425
            new xmldb_field('add_to', XMLDB_TYPE_TEXT, null, null, null, null, null)
426
        );
427
    }
428
 
429
    if (!$dbman->field_exists($table, 'metadata_settings')) {
430
        $dbman->add_field($table,
431
            new xmldb_field('metadata_settings', XMLDB_TYPE_TEXT, null, null, null, null, null)
432
        );
433
    }
434
}
435
 
436
 
437
/**
438
 * Adds authentication table
439
 *
440
 * @throws ddl_exception
441
 */
442
function hvp_upgrade_2019022600() {
443
    global $DB;
444
    $dbman = $DB->get_manager();
445
 
446
    // Add auth table.
447
    $table = new xmldb_table('hvp_auth');
448
 
449
    // Add fields.
450
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
451
    $table->add_field('user_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
452
    $table->add_field('created_at', XMLDB_TYPE_INTEGER, '11', null, XMLDB_NOTNULL, null, null);
453
    $table->add_field('secret', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null);
454
 
455
    // Add keys and index.
456
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
457
    $table->add_index('user_id', XMLDB_INDEX_UNIQUE, ['user_id']);
458
 
459
    // Create table if it does not exist.
460
    if (!$dbman->table_exists($table)) {
461
        $dbman->create_table($table);
462
    }
463
}
464
 
465
/**
466
 * Add default language to content
467
 *
468
 * @throws ddl_exception
469
 * @throws ddl_table_missing_exception
470
 */
471
function hvp_upgrade_2019030700() {
472
    global $DB;
473
    $dbman = $DB->get_manager();
474
 
475
    $table = new xmldb_table('hvp');
476
 
477
    if (!$dbman->field_exists($table, 'default_language')) {
478
        $dbman->add_field($table,
479
            new xmldb_field('default_language', XMLDB_TYPE_CHAR, '32', null, null, null, null)
480
        );
481
    }
482
}
483
 
484
function hvp_upgrade_2020080400() {
485
    global $DB;
486
    $dbman = $DB->get_manager();
487
    // Define field completionscorerequired to be added to hvp.
488
    $table = new xmldb_table('hvp');
489
    // Conditionally launch add field completionscorerequired.
490
    if (!$dbman->field_exists($table, 'completionpass')) {
491
        $dbman->add_field(
492
            $table,
493
            new xmldb_field('completionpass', XMLDB_TYPE_INTEGER, '1', null, null, null, 0, 'timemodified')
494
        );
495
    }
496
}
497
 
498
function hvp_upgrade_2020080401() {
499
    global $DB;
500
    $dbman = $DB->get_manager();
501
 
502
    // Changing nullability of field completionpass on table hvp to not null.
503
    $table = new xmldb_table('hvp');
504
    $field = new xmldb_field('completionpass', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'timemodified');
505
 
506
    // Launch change of nullability for field completionpass.
507
    $dbman->change_field_notnull($table, $field);
508
}
509
 
510
function hvp_upgrade_2020082800() {
511
    global $DB;
512
    $dbman = $DB->get_manager();
513
 
514
    $table = new xmldb_table('hvp');
515
 
516
    if (!$dbman->field_exists($table, 'a11y_title')) {
517
        $dbman->add_field($table,
518
            new xmldb_field('a11y_title', XMLDB_TYPE_CHAR, '255', null, null, null, null)
519
        );
520
    }
521
}
522
 
523
/**
524
 * Drop old unused unique index, add nonunique index.
525
 */
526
function hvp_upgrade_2020091500() {
527
    global $DB;
528
    $dbman = $DB->get_manager();
529
    $table = new xmldb_table('hvp_xapi_results');
530
    $index = new xmldb_index('results', XMLDB_INDEX_NOTUNIQUE, ['content_id', 'user_id']);
531
    if (!$dbman->index_exists($table, $index)) {
532
        $dbman->add_index($table, $index);
533
    }
534
 
535
    $oldindex = new xmldb_index('result', XMLDB_INDEX_UNIQUE, ['id', 'content_id', 'user_id']);
536
    $dbman->drop_index($table, $oldindex);
537
}
538
 
539
function hvp_upgrade_2020112600() {
540
    global $DB;
541
    $dbman = $DB->get_manager();
542
 
543
    // Add Content Hub fields to main content table.
544
    $table = new xmldb_table('hvp');
545
    if (!$dbman->field_exists($table, 'shared')) {
546
        $dbman->add_field($table, new xmldb_field('shared', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, 0, 'completionpass'));
547
    }
548
    if (!$dbman->field_exists($table, 'synced')) {
549
        $dbman->add_field($table, new xmldb_field('synced', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'shared'));
550
    }
551
    if (!$dbman->field_exists($table, 'hub_id')) {
552
        $dbman->add_field($table, new xmldb_field('hub_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'synced'));
553
    }
554
 
555
    // Create table for caching content hub metadata.
556
    $table = new xmldb_table('hvp_content_hub_cache');
557
    $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
558
    $table->add_field('language', XMLDB_TYPE_CHAR, '31', null, XMLDB_NOTNULL, null, null);
559
    $table->add_field('json', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
560
    $table->add_field('last_checked', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
561
 
562
    $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
563
    $table->add_index('language', XMLDB_INDEX_UNIQUE, ['language']);
564
 
565
    if (!$dbman->table_exists($table)) {
566
        $dbman->create_table($table);
567
    }
568
}
569
 
570
/**
571
 * Hvp module upgrade function.
572
 *
573
 * @param string $oldversion The version we are upgrading from
574
 *
575
 * @return bool Success
576
 */
577
function xmldb_hvp_upgrade($oldversion) {
578
    $upgrades = [
579
        2016011300,
580
        2016042500,
581
        2016050600,
582
        2016051000,
583
        2016110100,
584
        2016122800,
585
        2017040500,
586
        2017050900,
587
        2017060900,
588
        2018090300,
589
        2019022600,
590
        2019030700,
591
        2020080400,
592
        2020080401,
593
        2020082800,
594
        2020091500,
595
        2020112600,
596
    ];
597
 
598
    foreach ($upgrades as $version) {
599
        if ($oldversion < $version) {
600
            call_user_func("hvp_upgrade_{$version}");
601
            upgrade_mod_savepoint(true, $version, 'hvp');
602
        }
603
    }
604
 
605
    return true;
606
}