forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.php
3612 lines (3003 loc) · 161 KB
/
upgrade.php
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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.
//
// 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file keeps track of upgrades to Moodle.
*
* Sometimes, changes between versions involve
* alterations to database structures and other
* major things that may break installations.
*
* The upgrade function in this file will attempt
* to perform all the necessary actions to upgrade
* your older installation to the current version.
*
* If there's something it cannot do itself, it
* will tell you what you need to do.
*
* The commands in here will all be database-neutral,
* using the methods of database_manager class
*
* Please do not forget to use upgrade_set_timeout()
* before any action that may take longer time to finish.
*
* @package core_install
* @category upgrade
* @copyright 2006 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Main upgrade tasks to be executed on Moodle version bump
*
* This function is automatically executed after one bump in the Moodle core
* version is detected. It's in charge of performing the required tasks
* to raise core from the previous version to the next one.
*
* It's a collection of ordered blocks of code, named "upgrade steps",
* each one performing one isolated (from the rest of steps) task. Usually
* tasks involve creating new DB objects or performing manipulation of the
* information for cleanup/fixup purposes.
*
* Each upgrade step has a fixed structure, that can be summarised as follows:
*
* if ($oldversion < XXXXXXXXXX.XX) {
* // Explanation of the update step, linking to issue in the Tracker if necessary
* upgrade_set_timeout(XX); // Optional for big tasks
* // Code to execute goes here, usually the XMLDB Editor will
* // help you here. See {@link https://moodledev.io/general/development/tools/xmldb}.
* upgrade_main_savepoint(true, XXXXXXXXXX.XX);
* }
*
* All plugins within Moodle (modules, blocks, reports...) support the existence of
* their own upgrade.php file, using the "Frankenstyle" component name as
* defined at {@link https://moodledev.io/general/development/policies/codingstyle/frankenstyle}, for example:
* - {@link xmldb_page_upgrade($oldversion)}. (modules don't require the plugintype ("mod_") to be used.
* - {@link xmldb_auth_manual_upgrade($oldversion)}.
* - {@link xmldb_workshopform_accumulative_upgrade($oldversion)}.
* - ....
*
* In order to keep the contents of this file reduced, it's allowed to create some helper
* functions to be used here in the {@link upgradelib.php} file at the same directory. Note
* that such a file must be manually included from upgrade.php, and there are some restrictions
* about what can be used within it.
*
* For more information, take a look to the documentation available:
* - Data definition API: {@link https://moodledev.io/docs/apis/core/dml/ddl}
* - Upgrade API: {@link https://moodledev.io/docs/guides/upgrade}
*
* @param int $oldversion
* @return bool always true
*/
function xmldb_main_upgrade($oldversion) {
global $CFG, $DB;
require_once($CFG->libdir.'/db/upgradelib.php'); // Core Upgrade-related functions.
$dbman = $DB->get_manager(); // Loads ddl manager and xmldb classes.
// Always keep this upgrade step with version being the minimum
// allowed version to upgrade from (v3.11.8 right now).
if ($oldversion < 2021051708) {
// Just in case somebody hacks upgrade scripts or env, we really can not continue.
echo("You need to upgrade to 3.11.8 or higher first!\n");
exit(1);
// Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
upgrade_main_savepoint(true, 2021051708);
}
if ($oldversion < 2021052500.01) {
// Delete all user evidence files from users that have been deleted.
$sql = "SELECT DISTINCT f.*
FROM {files} f
LEFT JOIN {context} c ON f.contextid = c.id
WHERE f.component = :component
AND f.filearea = :filearea
AND c.id IS NULL";
$stalefiles = $DB->get_records_sql($sql, ['component' => 'core_competency', 'filearea' => 'userevidence']);
$fs = get_file_storage();
foreach ($stalefiles as $stalefile) {
$fs->get_file_instance($stalefile)->delete();
}
upgrade_main_savepoint(true, 2021052500.01);
}
if ($oldversion < 2021052500.02) {
// Define field timecreated to be added to task_adhoc.
$table = new xmldb_table('task_adhoc');
$field = new xmldb_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'blocking');
// Conditionally launch add field timecreated.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.02);
}
if ($oldversion < 2021052500.04) {
// Define field metadatasettings to be added to h5p_libraries.
$table = new xmldb_table('h5p_libraries');
$field = new xmldb_field('metadatasettings', XMLDB_TYPE_TEXT, null, null, null, null, null, 'coreminor');
// Conditionally launch add field metadatasettings.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Get installed library files that have no metadata settings value.
$params = [
'component' => 'core_h5p',
'filearea' => 'libraries',
'filename' => 'library.json',
];
$sql = "SELECT l.id, f.id as fileid
FROM {files} f
LEFT JOIN {h5p_libraries} l ON f.itemid = l.id
WHERE f.component = :component
AND f.filearea = :filearea
AND f.filename = :filename";
$libraries = $DB->get_records_sql($sql, $params);
// Update metadatasettings field when the attribute is present in the library.json file.
$fs = get_file_storage();
foreach ($libraries as $library) {
$jsonfile = $fs->get_file_by_id($library->fileid);
$jsoncontent = json_decode($jsonfile->get_content());
if (isset($jsoncontent->metadataSettings)) {
unset($library->fileid);
$library->metadatasettings = json_encode($jsoncontent->metadataSettings);
$DB->update_record('h5p_libraries', $library);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.04);
}
if ($oldversion < 2021052500.05) {
// Define fields to be added to task_scheduled.
$table = new xmldb_table('task_scheduled');
$field = new xmldb_field('timestarted', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'disabled');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('hostname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timestarted');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('pid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'hostname');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define fields to be added to task_adhoc.
$table = new xmldb_table('task_adhoc');
$field = new xmldb_field('timestarted', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'blocking');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('hostname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'timestarted');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('pid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'hostname');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define fields to be added to task_log.
$table = new xmldb_table('task_log');
$field = new xmldb_field('hostname', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'output');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('pid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'hostname');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.05);
}
if ($oldversion < 2021052500.06) {
// Define table to store virus infected details.
$table = new xmldb_table('infected_files');
// Adding fields to table infected_files.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('filename', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('quarantinedfile', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('reason', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
// Adding keys to table infected_files.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
// Conditionally launch create table for infected_files.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
upgrade_main_savepoint(true, 2021052500.06);
}
if ($oldversion < 2021052500.13) {
// Remove all the files with component='core_h5p' and filearea='editor' because they won't be used anymore.
$fs = get_file_storage();
$syscontext = context_system::instance();
$fs->delete_area_files($syscontext->id, 'core_h5p', 'editor');
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.13);
}
if ($oldversion < 2021052500.15) {
// Copy From id captures the id of the source course when a new course originates from a restore
// of another course on the same site.
$table = new xmldb_table('course');
$field = new xmldb_field('originalcourseid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.15);
}
if ($oldversion < 2021052500.19) {
// Define table oauth2_refresh_token to be created.
$table = new xmldb_table('oauth2_refresh_token');
// Adding fields to table oauth2_refresh_token.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('issuerid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('token', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('scopehash', XMLDB_TYPE_CHAR, 40, null, XMLDB_NOTNULL, null, null);
// Adding keys to table oauth2_refresh_token.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('issueridkey', XMLDB_KEY_FOREIGN, ['issuerid'], 'oauth2_issuer', ['id']);
$table->add_key('useridkey', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
// Adding indexes to table oauth2_refresh_token.
$table->add_index('userid-issuerid-scopehash', XMLDB_INDEX_UNIQUE, array('userid', 'issuerid', 'scopehash'));
// Conditionally launch create table for oauth2_refresh_token.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.19);
}
if ($oldversion < 2021052500.20) {
// Define index modulename-instance-eventtype (not unique) to be added to event.
$table = new xmldb_table('event');
$index = new xmldb_index('modulename-instance-eventtype', XMLDB_INDEX_NOTUNIQUE, ['modulename', 'instance', 'eventtype']);
// Conditionally launch add index modulename-instance-eventtype.
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}
// Define index modulename-instance (not unique) to be dropped form event.
$table = new xmldb_table('event');
$index = new xmldb_index('modulename-instance', XMLDB_INDEX_NOTUNIQUE, ['modulename', 'instance']);
// Conditionally launch drop index modulename-instance.
if ($dbman->index_exists($table, $index)) {
$dbman->drop_index($table, $index);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.20);
}
if ($oldversion < 2021052500.24) {
// Define fields tutorial and example to be added to h5p_libraries.
$table = new xmldb_table('h5p_libraries');
// Add tutorial field.
$field = new xmldb_field('tutorial', XMLDB_TYPE_TEXT, null, null, null, null, null, 'metadatasettings');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Add example field.
$field = new xmldb_field('example', XMLDB_TYPE_TEXT, null, null, null, null, null, 'tutorial');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.24);
}
if ($oldversion < 2021052500.26) {
// Delete orphaned course_modules_completion rows; these were not deleted properly
// by remove_course_contents function.
$DB->delete_records_select('course_modules_completion', "
NOT EXISTS (
SELECT 1
FROM {course_modules} cm
WHERE cm.id = {course_modules_completion}.coursemoduleid
)");
upgrade_main_savepoint(true, 2021052500.26);
}
if ($oldversion < 2021052500.27) {
// Script to fix incorrect records of "hidden" field in existing grade items.
$sql = "SELECT cm.instance, cm.course
FROM {course_modules} cm
JOIN {modules} m ON m.id = cm.module
WHERE m.name = :module AND cm.visible = :visible";
$hidequizlist = $DB->get_recordset_sql($sql, ['module' => 'quiz', 'visible' => 0]);
foreach ($hidequizlist as $hidequiz) {
$params = [
'itemmodule' => 'quiz',
'courseid' => $hidequiz->course,
'iteminstance' => $hidequiz->instance,
];
$DB->set_field('grade_items', 'hidden', 1, $params);
}
$hidequizlist->close();
upgrade_main_savepoint(true, 2021052500.27);
}
if ($oldversion < 2021052500.29) {
// Get the current guest user which is also set as 'deleted'.
$guestuser = $DB->get_record('user', ['id' => $CFG->siteguest, 'deleted' => 1]);
// If there is a deleted guest user, reset the user to not be deleted and make sure the related
// user context exists.
if ($guestuser) {
$guestuser->deleted = 0;
$DB->update_record('user', $guestuser);
// Get the guest user context.
$guestusercontext = $DB->get_record('context',
['contextlevel' => CONTEXT_USER, 'instanceid' => $guestuser->id]);
// If the guest user context does not exist, create it.
if (!$guestusercontext) {
$record = new stdClass();
$record->contextlevel = CONTEXT_USER;
$record->instanceid = $guestuser->id;
$record->depth = 0;
// The path is not known before insert.
$record->path = null;
$record->locked = 0;
$record->id = $DB->insert_record('context', $record);
// Update the path.
$record->path = '/' . SYSCONTEXTID . '/' . $record->id;
$record->depth = substr_count($record->path, '/');
$DB->update_record('context', $record);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.29);
}
if ($oldversion < 2021052500.30) {
// Reset analytics model output dir if it's the default value.
$modeloutputdir = get_config('analytics', 'modeloutputdir');
if (strcasecmp($modeloutputdir, $CFG->dataroot . DIRECTORY_SEPARATOR . 'models') == 0) {
set_config('modeloutputdir', '', 'analytics');
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.30);
}
if ($oldversion < 2021052500.32) {
// Define field downloadcontent to be added to course.
$table = new xmldb_table('course');
$field = new xmldb_field('downloadcontent', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'visibleold');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.32);
}
if ($oldversion < 2021052500.33) {
$table = new xmldb_table('badge_backpack');
// There is no key_exists, so test the equivalent index.
$oldindex = new xmldb_index('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']);
if (!$dbman->index_exists($table, $oldindex)) {
// All external backpack providers/hosts are now exclusively stored in badge_external_backpack.
// All credentials are stored in badge_backpack and are unique per user, backpack.
$uniquekey = new xmldb_key('backpackcredentials', XMLDB_KEY_UNIQUE, ['userid', 'externalbackpackid']);
$dbman->add_key($table, $uniquekey);
}
// Drop the password field as this is moved to badge_backpack.
$table = new xmldb_table('badge_external_backpack');
$field = new xmldb_field('password', XMLDB_TYPE_CHAR, '50');
if ($dbman->field_exists($table, $field)) {
// If there is a current backpack set then copy it across to the new structure.
if ($CFG->badges_defaultissuercontact) {
// Get the currently used site backpacks.
$records = $DB->get_records_select('badge_external_backpack', "password IS NOT NULL AND password != ''");
$backpack = [
'userid' => '0',
'email' => $CFG->badges_defaultissuercontact,
'backpackuid' => -1
];
// Create records corresponding to the site backpacks.
foreach ($records as $record) {
$backpack['password'] = $record->password;
$backpack['externalbackpackid'] = $record->id;
$DB->insert_record('badge_backpack', (object) $backpack);
}
}
$dbman->drop_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.33);
}
if ($oldversion < 2021052500.36) {
// Define table payment_accounts to be created.
$table = new xmldb_table('payment_accounts');
// Adding fields to table payment_accounts.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('archived', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
// Adding keys to table payment_accounts.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
// Conditionally launch create table for payment_accounts.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Define table payment_gateways to be created.
$table = new xmldb_table('payment_gateways');
// Adding fields to table payment_gateways.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('accountid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('gateway', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1');
$table->add_field('config', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
// Adding keys to table payment_gateways.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('accountid', XMLDB_KEY_FOREIGN, ['accountid'], 'payment_accounts', ['id']);
// Conditionally launch create table for payment_gateways.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Define table payments to be created.
$table = new xmldb_table('payments');
// Adding fields to table payments.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('paymentarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
$table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('amount', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
$table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, XMLDB_NOTNULL, null, null);
$table->add_field('accountid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('gateway', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
// Adding keys to table payments.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
$table->add_key('accountid', XMLDB_KEY_FOREIGN, ['accountid'], 'payment_accounts', ['id']);
// Adding indexes to table payments.
$table->add_index('gateway', XMLDB_INDEX_NOTUNIQUE, ['gateway']);
$table->add_index('component-paymentarea-itemid', XMLDB_INDEX_NOTUNIQUE, ['component', 'paymentarea', 'itemid']);
// Conditionally launch create table for payments.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.36);
}
if ($oldversion < 2021052500.42) {
// Get all lessons that are set with a completion criteria of 'requires grade' but with no grade type set.
$sql = "SELECT cm.id
FROM {course_modules} cm
JOIN {lesson} l ON l.id = cm.instance
JOIN {modules} m ON m.id = cm.module
WHERE m.name = :name AND cm.completiongradeitemnumber IS NOT NULL AND l.grade = :grade";
do {
if ($invalidconfigrations = $DB->get_records_sql($sql, ['name' => 'lesson', 'grade' => 0], 0, 1000)) {
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($invalidconfigrations), SQL_PARAMS_NAMED);
$DB->set_field_select('course_modules', 'completiongradeitemnumber', null, "id $insql", $inparams);
}
} while ($invalidconfigrations);
upgrade_main_savepoint(true, 2021052500.42);
}
if ($oldversion < 2021052500.55) {
$DB->delete_records_select('event', "eventtype = 'category' AND categoryid = 0 AND userid <> 0");
upgrade_main_savepoint(true, 2021052500.55);
}
if ($oldversion < 2021052500.59) {
// Define field visibility to be added to contentbank_content.
$table = new xmldb_table('contentbank_content');
$field = new xmldb_field('visibility', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '1', 'contextid');
// Conditionally launch add field visibility.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.59);
}
if ($oldversion < 2021052500.60) {
// We are going to remove the field 'hidepicture' from the groups
// so we need to remove the pictures from those groups. But we prevent
// the execution twice because this could be executed again when upgrading
// to different versions.
if ($dbman->field_exists('groups', 'hidepicture')) {
$sql = "SELECT g.id, g.courseid, ctx.id AS contextid
FROM {groups} g
JOIN {context} ctx
ON ctx.instanceid = g.courseid
AND ctx.contextlevel = :contextlevel
WHERE g.hidepicture = 1";
// Selecting all the groups that have hide picture enabled, and organising them by context.
$groupctx = [];
$records = $DB->get_recordset_sql($sql, ['contextlevel' => CONTEXT_COURSE]);
foreach ($records as $record) {
if (!isset($groupctx[$record->contextid])) {
$groupctx[$record->contextid] = [];
}
$groupctx[$record->contextid][] = $record->id;
}
$records->close();
// Deleting the group files.
$fs = get_file_storage();
foreach ($groupctx as $contextid => $groupids) {
list($in, $inparams) = $DB->get_in_or_equal($groupids, SQL_PARAMS_NAMED);
$fs->delete_area_files_select($contextid, 'group', 'icon', $in, $inparams);
}
// Updating the database to remove picture from all those groups.
$sql = "UPDATE {groups} SET picture = :pic WHERE hidepicture = :hide";
$DB->execute($sql, ['pic' => 0, 'hide' => 1]);
}
// Define field hidepicture to be dropped from groups.
$table = new xmldb_table('groups');
$field = new xmldb_field('hidepicture');
// Conditionally launch drop field hidepicture.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.60);
}
if ($oldversion < 2021052500.64) {
// Get all the external backpacks and update the sortorder column, to avoid repeated/wrong values. As sortorder was not
// used since now, the id column will be the criteria to follow for re-ordering them with a valid value.
$i = 1;
$records = $DB->get_records('badge_external_backpack', null, 'id ASC');
foreach ($records as $record) {
$record->sortorder = $i++;
$DB->update_record('badge_external_backpack', $record);
}
upgrade_main_savepoint(true, 2021052500.64);
}
if ($oldversion < 2021052500.67) {
// The $CFG->badges_site_backpack setting has been removed because it's not required anymore. From now, the default backpack
// will be the one with lower sortorder value.
unset_config('badges_site_backpack');
upgrade_main_savepoint(true, 2021052500.67);
}
if ($oldversion < 2021052500.69) {
// Define field type to be added to oauth2_issuer.
$table = new xmldb_table('oauth2_issuer');
$field = new xmldb_field('servicetype', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'requireconfirmation');
// Conditionally launch add field type.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Set existing values to the proper servicetype value.
// It's not critical if the servicetype column doesn't contain the proper value for Google, Microsoft, Facebook or
// Nextcloud services because, for now, this value is used for services using different discovery method.
// However, let's try to upgrade it using the default value for the baseurl or image. If any of these default values
// have been changed, the servicetype column will remain NULL.
$recordset = $DB->get_recordset('oauth2_issuer');
foreach ($recordset as $record) {
if ($record->baseurl == 'https://accounts.google.com/') {
$record->servicetype = 'google';
$DB->update_record('oauth2_issuer', $record);
} else if ($record->image == 'https://www.microsoft.com/favicon.ico') {
$record->servicetype = 'microsoft';
$DB->update_record('oauth2_issuer', $record);
} else if ($record->image == 'https://facebookbrand.com/wp-content/uploads/2016/05/flogo_rgb_hex-brc-site-250.png') {
$record->servicetype = 'facebook';
$DB->update_record('oauth2_issuer', $record);
} else if ($record->image == 'https://nextcloud.com/wp-content/themes/next/assets/img/common/favicon.png?x16328') {
$record->servicetype = 'nextcloud';
$DB->update_record('oauth2_issuer', $record);
}
}
$recordset->close();
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.69);
}
if ($oldversion < 2021052500.74) {
// Define field 'showactivitydates' to be added to course table.
$table = new xmldb_table('course');
$field = new xmldb_field('showactivitydates', XMLDB_TYPE_INTEGER, '1', null,
XMLDB_NOTNULL, null, '0', 'originalcourseid');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.74);
}
if ($oldversion < 2021052500.75) {
// Define field 'showcompletionconditions' to be added to course.
$table = new xmldb_table('course');
$field = new xmldb_field('showcompletionconditions', XMLDB_TYPE_INTEGER, '1', null,
XMLDB_NOTNULL, null, '1', 'completionnotify');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.75);
}
if ($oldversion < 2021052500.78) {
// Define field enabled to be added to h5p_libraries.
$table = new xmldb_table('h5p_libraries');
$field = new xmldb_field('enabled', XMLDB_TYPE_INTEGER, '1', null, null, null, '1', 'example');
// Conditionally launch add field enabled.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.78);
}
if ($oldversion < 2021052500.83) {
// Define field loginpagename to be added to oauth2_issuer.
$table = new xmldb_table('oauth2_issuer');
$field = new xmldb_field('loginpagename', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'servicetype');
// Conditionally launch add field loginpagename.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.83);
}
if ($oldversion < 2021052500.84) {
require_once($CFG->dirroot . '/user/profile/field/social/upgradelib.php');
$table = new xmldb_table('user');
$tablecolumns = ['icq', 'skype', 'aim', 'yahoo', 'msn', 'url'];
foreach ($tablecolumns as $column) {
$field = new xmldb_field($column);
if ($dbman->field_exists($table, $field)) {
user_profile_social_moveto_profilefield($column);
$dbman->drop_field($table, $field);
}
}
// Update all module availability if it relies on the old user fields.
user_profile_social_update_module_availability();
// Remove field mapping for oauth2.
$DB->delete_records('oauth2_user_field_mapping', array('internalfield' => 'url'));
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.84);
}
if ($oldversion < 2021052500.85) {
require_once($CFG->libdir . '/db/upgradelib.php');
// Check if this site has executed the problematic upgrade steps.
$needsfixing = upgrade_calendar_site_status(false);
// Only queue the task if this site has been affected by the problematic upgrade step.
if ($needsfixing) {
// Create adhoc task to search and recover orphaned calendar events.
$record = new \stdClass();
$record->classname = '\core\task\calendar_fix_orphaned_events';
// Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
$nextruntime = time() - 1;
$record->nextruntime = $nextruntime;
$DB->insert_record('task_adhoc', $record);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.85);
}
if ($oldversion < 2021052500.87) {
// Changing the default of field showcompletionconditions on table course to 0.
$table = new xmldb_table('course');
$field = new xmldb_field('showcompletionconditions', XMLDB_TYPE_INTEGER, '1', null, null, null, null, 'showactivitydates');
// Launch change of nullability for field showcompletionconditions.
$dbman->change_field_notnull($table, $field);
// Launch change of default for field showcompletionconditions.
$dbman->change_field_default($table, $field);
// Set showcompletionconditions to null for courses which don't track completion.
$sql = "UPDATE {course}
SET showcompletionconditions = null
WHERE enablecompletion <> 1";
$DB->execute($sql);
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.87);
}
if ($oldversion < 2021052500.90) {
// Remove usemodchooser user preference for every user.
$DB->delete_records('user_preferences', ['name' => 'usemodchooser']);
// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.90);
}
if ($oldversion < 2021060200.00) {
// Define index name (not unique) to be added to user_preferences.
$table = new xmldb_table('user_preferences');
$index = new xmldb_index('name', XMLDB_INDEX_NOTUNIQUE, ['name']);
// Conditionally launch add index name.
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021060200.00);
}
if ($oldversion < 2021060900.00) {
// Update the externalfield to be larger.
$table = new xmldb_table('oauth2_user_field_mapping');
$field = new xmldb_field('externalfield', XMLDB_TYPE_CHAR, '500', null, XMLDB_NOTNULL, false, null, 'issuerid');
$dbman->change_field_type($table, $field);
// Main savepoint reached.
upgrade_main_savepoint(true, 2021060900.00);
}
if ($oldversion < 2021072800.01) {
// Define table reportbuilder_report to be created.
$table = new xmldb_table('reportbuilder_report');
// Adding fields to table reportbuilder_report.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('source', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('type', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('area', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('usercreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('usermodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
// Adding keys to table reportbuilder_report.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('usercreated', XMLDB_KEY_FOREIGN, ['usercreated'], 'user', ['id']);
$table->add_key('usermodified', XMLDB_KEY_FOREIGN, ['usermodified'], 'user', ['id']);
$table->add_key('contextid', XMLDB_KEY_FOREIGN, ['contextid'], 'context', ['id']);
// Conditionally launch create table for reportbuilder_report.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021072800.01);
}
if ($oldversion < 2021090200.01) {
// Remove qformat_webct (unless it has manually been added back).
if (!file_exists($CFG->dirroot . '/question/format/webct/format.php')) {
unset_all_config_for_plugin('qformat_webct');
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021090200.01);
}
if ($oldversion < 2021091100.01) {
// If message_jabber is no longer present, remove it.
if (!file_exists($CFG->dirroot . '/message/output/jabber/message_output_jabber.php')) {
// Remove Jabber from the notification plugins list.
$DB->delete_records('message_processors', ['name' => 'jabber']);
// Remove user preference settings.
$DB->delete_records('user_preferences', ['name' => 'message_processor_jabber_jabberid']);
$sql = 'SELECT *
FROM {user_preferences} up
WHERE ' . $DB->sql_like('up.name', ':name', false, false) . ' AND ' .
$DB->sql_like('up.value', ':value', false, false);
$params = [
'name' => 'message_provider_%',
'value' => '%jabber%',
];
$jabbersettings = $DB->get_recordset_sql($sql, $params);
foreach ($jabbersettings as $jabbersetting) {
// Remove 'jabber' from the value.
$jabbersetting->value = implode(',', array_diff(explode(',', $jabbersetting->value), ['jabber']));
$DB->update_record('user_preferences', $jabbersetting);
}
$jabbersettings->close();
// Clean config settings.
unset_config('jabberhost');
unset_config('jabberserver');
unset_config('jabberusername');
unset_config('jabberpassword');
unset_config('jabberport');
// Remove default notification preferences.
$like = $DB->sql_like('name', '?', true, true, false, '|');
$params = [$DB->sql_like_escape('jabber_provider_', '|') . '%'];
$DB->delete_records_select('config_plugins', $like, $params);
// Clean config config settings.
unset_all_config_for_plugin('message_jabber');
}
upgrade_main_savepoint(true, 2021091100.01);
}
if ($oldversion < 2021091100.02) {
// Set the description field to HTML format for the Default course category.
$category = $DB->get_record('course_categories', ['id' => 1]);
if (!empty($category) && $category->descriptionformat == FORMAT_MOODLE) {
// Format should be changed only if it's still set to FORMAT_MOODLE.
if (!is_null($category->description)) {
// If description is not empty, format the content to HTML.
$category->description = format_text($category->description, FORMAT_MOODLE);
}
$category->descriptionformat = FORMAT_HTML;
$DB->update_record('course_categories', $category);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2021091100.02);
}
if ($oldversion < 2021091700.01) {
// Default 'off' for existing sites as this is the behaviour they had earlier.
set_config('enroladminnewcourse', false);
// Main savepoint reached.
upgrade_main_savepoint(true, 2021091700.01);
}
if ($oldversion < 2021091700.02) {
// If portfolio_picasa is no longer present, remove it.
if (!file_exists($CFG->dirroot . '/portfolio/picasa/version.php')) {
$instance = $DB->get_record('portfolio_instance', ['plugin' => 'picasa']);
if (!empty($instance)) {
// Remove all records from portfolio_instance_config.
$DB->delete_records('portfolio_instance_config', ['instance' => $instance->id]);
// Remove all records from portfolio_instance_user.
$DB->delete_records('portfolio_instance_user', ['instance' => $instance->id]);
// Remove all records from portfolio_log.
$DB->delete_records('portfolio_log', ['portfolio' => $instance->id]);
// Remove all records from portfolio_tempdata.
$DB->delete_records('portfolio_tempdata', ['instance' => $instance->id]);
// Remove the record from the portfolio_instance table.
$DB->delete_records('portfolio_instance', ['id' => $instance->id]);
}
// Clean config.
unset_all_config_for_plugin('portfolio_picasa');
}
upgrade_main_savepoint(true, 2021091700.02);
}
if ($oldversion < 2021091700.03) {
// If repository_picasa is no longer present, remove it.
if (!file_exists($CFG->dirroot . '/repository/picasa/version.php')) {