mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
upgrade.php
4525 lines (3729 loc) · 203 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 http://docs.moodle.org/dev/XMLDB_editor}.
* 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 http://docs.moodle.org/dev/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 http://docs.moodle.org/dev/Data_definition_API}
* - Upgrade API: {@link http://docs.moodle.org/dev/Upgrade_API}
*
* @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.6.0 right now).
if ($oldversion < 2018120300) {
// Just in case somebody hacks upgrade scripts or env, we really can not continue.
echo("You need to upgrade to 3.6.x or higher first!\n");
exit(1);
// Note this savepoint is 100% unreachable, but needed to pass the upgrade checks.
upgrade_main_savepoint(true, 2018120300);
}
// Automatically generated Moodle v3.6.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2018120300.01) {
// Update the FB logo URL.
$oldurl = 'https://facebookbrand.com/wp-content/themes/fb-branding/prj-fb-branding/assets/images/fb-art.png';
$newurl = 'https://facebookbrand.com/wp-content/uploads/2016/05/flogo_rgb_hex-brc-site-250.png';
$updatesql = "UPDATE {oauth2_issuer}
SET image = :newimage
WHERE " . $DB->sql_compare_text('image', 100). " = :oldimage";
$params = [
'newimage' => $newurl,
'oldimage' => $oldurl
];
$DB->execute($updatesql, $params);
upgrade_main_savepoint(true, 2018120300.01);
}
if ($oldversion < 2018120300.02) {
// Set all individual conversations to enabled.
$updatesql = "UPDATE {message_conversations}
SET enabled = :enabled
WHERE type = :type";
$DB->execute($updatesql, ['enabled' => 1, 'type' => 1]);
upgrade_main_savepoint(true, 2018120300.02);
}
if ($oldversion < 2018120301.02) {
upgrade_delete_orphaned_file_records();
upgrade_main_savepoint(true, 2018120301.02);
}
if ($oldversion < 2019011500.00) {
// Define table task_log to be created.
$table = new xmldb_table('task_log');
// Adding fields to table task_log.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('type', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null);
$table->add_field('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timestart', XMLDB_TYPE_NUMBER, '20, 10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timeend', XMLDB_TYPE_NUMBER, '20, 10', null, XMLDB_NOTNULL, null, null);
$table->add_field('dbreads', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('dbwrites', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('result', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, null);
// Adding keys to table task_log.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
// Adding indexes to table task_log.
$table->add_index('classname', XMLDB_INDEX_NOTUNIQUE, ['classname']);
$table->add_index('timestart', XMLDB_INDEX_NOTUNIQUE, ['timestart']);
// Conditionally launch create table for task_log.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019011500.00);
}
if ($oldversion < 2019011501.00) {
// Define field output to be added to task_log.
$table = new xmldb_table('task_log');
$field = new xmldb_field('output', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'result');
// Conditionally launch add field output.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019011501.00);
}
if ($oldversion < 2019011801.00) {
// Define table customfield_category to be created.
$table = new xmldb_table('customfield_category');
// Adding fields to table customfield_category.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('name', XMLDB_TYPE_CHAR, '400', null, XMLDB_NOTNULL, null, null);
$table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', 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);
$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('contextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
// Adding keys to table customfield_category.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('contextid', XMLDB_KEY_FOREIGN, ['contextid'], 'context', ['id']);
// Adding indexes to table customfield_category.
$table->add_index('component_area_itemid', XMLDB_INDEX_NOTUNIQUE, ['component', 'area', 'itemid', 'sortorder']);
// Conditionally launch create table for customfield_category.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Define table customfield_field to be created.
$table = new xmldb_table('customfield_field');
// Adding fields to table customfield_field.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('shortname', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('name', XMLDB_TYPE_CHAR, '400', null, XMLDB_NOTNULL, null, null);
$table->add_field('type', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('descriptionformat', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('categoryid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('configdata', 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 customfield_field.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('categoryid', XMLDB_KEY_FOREIGN, ['categoryid'], 'customfield_category', ['id']);
// Adding indexes to table customfield_field.
$table->add_index('categoryid_sortorder', XMLDB_INDEX_NOTUNIQUE, ['categoryid', 'sortorder']);
// Conditionally launch create table for customfield_field.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Define table customfield_data to be created.
$table = new xmldb_table('customfield_data');
// Adding fields to table customfield_data.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('fieldid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('instanceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('intvalue', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('decvalue', XMLDB_TYPE_NUMBER, '10, 5', null, null, null, null);
$table->add_field('shortcharvalue', XMLDB_TYPE_CHAR, '255', null, null, null, null);
$table->add_field('charvalue', XMLDB_TYPE_CHAR, '1333', null, null, null, null);
$table->add_field('value', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
$table->add_field('valueformat', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, 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);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
// Adding keys to table customfield_data.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('fieldid', XMLDB_KEY_FOREIGN, ['fieldid'], 'customfield_field', ['id']);
$table->add_key('contextid', XMLDB_KEY_FOREIGN, ['contextid'], 'context', ['id']);
// Adding indexes to table customfield_data.
$table->add_index('instanceid-fieldid', XMLDB_INDEX_UNIQUE, ['instanceid', 'fieldid']);
$table->add_index('fieldid-intvalue', XMLDB_INDEX_NOTUNIQUE, ['fieldid', 'intvalue']);
$table->add_index('fieldid-shortcharvalue', XMLDB_INDEX_NOTUNIQUE, ['fieldid', 'shortcharvalue']);
$table->add_index('fieldid-decvalue', XMLDB_INDEX_NOTUNIQUE, ['fieldid', 'decvalue']);
// Conditionally launch create table for customfield_data.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
upgrade_main_savepoint(true, 2019011801.00);
}
if ($oldversion < 2019011801.01) {
// Delete all files that have been used in sections, which are already deleted.
$sql = "SELECT DISTINCT f.itemid as sectionid, f.contextid
FROM {files} f
LEFT JOIN {course_sections} s ON f.itemid = s.id
WHERE f.component = :component AND f.filearea = :filearea AND s.id IS NULL ";
$params = [
'component' => 'course',
'filearea' => 'section'
];
$stalefiles = $DB->get_recordset_sql($sql, $params);
$fs = get_file_storage();
foreach ($stalefiles as $stalefile) {
$fs->delete_area_files($stalefile->contextid, 'course', 'section', $stalefile->sectionid);
}
$stalefiles->close();
upgrade_main_savepoint(true, 2019011801.01);
}
if ($oldversion < 2019011801.02) {
// Add index 'useridfrom' to the table 'notifications'.
$table = new xmldb_table('notifications');
$index = new xmldb_index('useridfrom', XMLDB_INDEX_NOTUNIQUE, ['useridfrom']);
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}
upgrade_main_savepoint(true, 2019011801.02);
}
if ($oldversion < 2019011801.03) {
// Remove duplicate entries from group memberships.
// Find records with multiple userid/groupid combinations and find the highest ID.
// Later we will remove all those entries.
$sql = "
SELECT MIN(id) as minid, userid, groupid
FROM {groups_members}
GROUP BY userid, groupid
HAVING COUNT(id) > 1";
if ($duplicatedrows = $DB->get_recordset_sql($sql)) {
foreach ($duplicatedrows as $row) {
$DB->delete_records_select('groups_members',
'userid = :userid AND groupid = :groupid AND id <> :minid', (array)$row);
}
}
$duplicatedrows->close();
// Define key useridgroupid (unique) to be added to group_members.
$table = new xmldb_table('groups_members');
$key = new xmldb_key('useridgroupid', XMLDB_KEY_UNIQUE, array('userid', 'groupid'));
// Launch add key useridgroupid.
$dbman->add_key($table, $key);
// Main savepoint reached.
upgrade_main_savepoint(true, 2019011801.03);
}
if ($oldversion < 2019021500.01) {
$insights = $DB->get_record('message_providers', ['component' => 'moodle', 'name' => 'insights']);
if (!empty($insights)) {
$insights->capability = null;
$DB->update_record('message_providers', $insights);
}
upgrade_main_savepoint(true, 2019021500.01);
}
if ($oldversion < 2019021500.02) {
// Default 'off' for existing sites as this is the behaviour they had earlier.
set_config('messagingdefaultpressenter', false);
// Main savepoint reached.
upgrade_main_savepoint(true, 2019021500.02);
}
if ($oldversion < 2019030100.01) {
// Create adhoc task to delete renamed My Course search area (ID core_course-mycourse).
$record = new \stdClass();
$record->classname = '\core\task\clean_up_deleted_search_area_task';
$record->component = 'core';
// Next run time based from nextruntime computation in \core\task\manager::queue_adhoc_task().
$nextruntime = time() - 1;
$record->nextruntime = $nextruntime;
$record->customdata = json_encode('core_course-mycourse');
$DB->insert_record('task_adhoc', $record);
// Main savepoint reached.
upgrade_main_savepoint(true, 2019030100.01);
}
if ($oldversion < 2019030700.01) {
// Define field evaluationmode to be added to analytics_models_log.
$table = new xmldb_table('analytics_models_log');
$field = new xmldb_field('evaluationmode', XMLDB_TYPE_CHAR, '50', null, null, null,
null, 'version');
// Conditionally launch add field evaluationmode.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
$updatesql = "UPDATE {analytics_models_log}
SET evaluationmode = 'configuration'";
$DB->execute($updatesql, []);
// Changing nullability of field evaluationmode on table block_instances to not null.
$field = new xmldb_field('evaluationmode', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL,
null, null, 'version');
// Launch change of nullability for field evaluationmode.
$dbman->change_field_notnull($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019030700.01);
}
if ($oldversion < 2019030800.00) {
// Define table 'message_conversation_actions' to be created.
// Note - I would have preferred 'message_conversation_user_actions' but due to Oracle we can't. Boo.
$table = new xmldb_table('message_conversation_actions');
// Adding fields to table 'message_conversation_actions'.
$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('conversationid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('action', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
// Adding keys to table 'message_conversation_actions'.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
$table->add_key('conversationid', XMLDB_KEY_FOREIGN, ['conversationid'], 'message_conversations', ['id']);
// Conditionally launch create table for 'message_conversation_actions'.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019030800.00);
}
if ($oldversion < 2019030800.02) {
// Remove any conversations and their members associated with non-existent groups.
$sql = "SELECT mc.id
FROM {message_conversations} mc
LEFT JOIN {groups} g
ON mc.itemid = g.id
WHERE mc.component = :component
AND mc.itemtype = :itemtype
AND g.id is NULL";
$conversations = $DB->get_records_sql($sql, ['component' => 'core_group', 'itemtype' => 'groups']);
if ($conversations) {
$conversationids = array_keys($conversations);
$DB->delete_records_list('message_conversations', 'id', $conversationids);
$DB->delete_records_list('message_conversation_members', 'conversationid', $conversationids);
$DB->delete_records_list('message_conversation_actions', 'conversationid', $conversationids);
// Now, go through each conversation and delete any messages and related message actions.
foreach ($conversationids as $conversationid) {
if ($messages = $DB->get_records('messages', ['conversationid' => $conversationid])) {
$messageids = array_keys($messages);
// Delete the actions.
list($insql, $inparams) = $DB->get_in_or_equal($messageids);
$DB->delete_records_select('message_user_actions', "messageid $insql", $inparams);
// Delete the messages.
$DB->delete_records('messages', ['conversationid' => $conversationid]);
}
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019030800.02);
}
if ($oldversion < 2019030800.03) {
// Add missing indicators to course_dropout.
$params = [
'target' => '\core\analytics\target\course_dropout',
'trained' => 0,
'enabled' => 0,
];
$models = $DB->get_records('analytics_models', $params);
foreach ($models as $model) {
$indicators = json_decode($model->indicators);
$potentiallymissingindicators = [
'\core_course\analytics\indicator\completion_enabled',
'\core_course\analytics\indicator\potential_cognitive_depth',
'\core_course\analytics\indicator\potential_social_breadth',
'\core\analytics\indicator\any_access_after_end',
'\core\analytics\indicator\any_access_before_start',
'\core\analytics\indicator\any_write_action_in_course',
'\core\analytics\indicator\read_actions'
];
$missing = false;
foreach ($potentiallymissingindicators as $potentiallymissingindicator) {
if (!in_array($potentiallymissingindicator, $indicators)) {
// Add the missing indicator to sites upgraded before 2017072000.02.
$indicators[] = $potentiallymissingindicator;
$missing = true;
}
}
if ($missing) {
$model->indicators = json_encode($indicators);
$model->version = time();
$model->timemodified = time();
$DB->update_record('analytics_models', $model);
}
}
// Add missing indicators to no_teaching.
$params = [
'target' => '\core\analytics\target\no_teaching',
];
$models = $DB->get_records('analytics_models', $params);
foreach ($models as $model) {
$indicators = json_decode($model->indicators);
if (!in_array('\core_course\analytics\indicator\no_student', $indicators)) {
// Add the missing indicator to sites upgraded before 2017072000.02.
$indicators[] = '\core_course\analytics\indicator\no_student';
$model->indicators = json_encode($indicators);
$model->version = time();
$model->timemodified = time();
$DB->update_record('analytics_models', $model);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019030800.03);
}
if ($oldversion < 2019031500.01) {
$defaulttimesplittings = get_config('analytics', 'timesplittings');
if ($defaulttimesplittings !== false) {
set_config('defaulttimesplittingsevaluation', $defaulttimesplittings, 'analytics');
unset_config('timesplittings', 'analytics');
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019031500.01);
}
if ($oldversion < 2019032200.02) {
// The no_teaching model might have been marked as not-trained by mistake (static models are always trained).
$DB->set_field('analytics_models', 'trained', 1, ['target' => '\core\analytics\target\no_teaching']);
upgrade_main_savepoint(true, 2019032200.02);
}
if ($oldversion < 2019032900.00) {
// Define table badge_competencies to be renamed to badge_alignment.
$table = new xmldb_table('badge_competencies');
// Be careful if this step gets run twice.
if ($dbman->table_exists($table)) {
$key = new xmldb_key('competenciesbadge', XMLDB_KEY_FOREIGN, ['badgeid'], 'badge', ['id']);
// Launch drop key competenciesbadge.
$dbman->drop_key($table, $key);
$key = new xmldb_key('alignmentsbadge', XMLDB_KEY_FOREIGN, ['badgeid'], 'badge', ['id']);
// Launch add key alignmentsbadge.
$dbman->add_key($table, $key);
// Launch rename table for badge_alignment.
$dbman->rename_table($table, 'badge_alignment');
}
upgrade_main_savepoint(true, 2019032900.00);
}
if ($oldversion < 2019032900.01) {
$sql = "UPDATE {task_scheduled}
SET classname = ?
WHERE component = ?
AND classname = ?";
$DB->execute($sql, [
'\core\task\question_preview_cleanup_task',
'moodle',
'\core\task\question_cron_task'
]);
// Main savepoint reached.
upgrade_main_savepoint(true, 2019032900.01);
}
if ($oldversion < 2019040200.01) {
// Removing the themes BSB, Clean, More from core.
// If these theme wish to be retained empty this array before upgrade.
$themes = array('theme_bootstrapbase' => 'bootstrapbase',
'theme_clean' => 'clean', 'theme_more' => 'more');
foreach ($themes as $key => $theme) {
if (check_dir_exists($CFG->dirroot . '/theme/' . $theme, false)) {
// Ignore the themes that have been re-downloaded.
unset($themes[$key]);
}
}
// Check we actually have themes to remove.
if (count($themes) > 0) {
list($insql, $inparams) = $DB->get_in_or_equal($themes, SQL_PARAMS_NAMED);
// Replace the theme usage.
$DB->set_field_select('course', 'theme', 'classic', "theme $insql", $inparams);
$DB->set_field_select('course_categories', 'theme', 'classic', "theme $insql", $inparams);
$DB->set_field_select('user', 'theme', 'classic', "theme $insql", $inparams);
$DB->set_field_select('mnet_host', 'theme', 'classic', "theme $insql", $inparams);
$DB->set_field_select('cohort', 'theme', 'classic', "theme $insql", $inparams);
// Replace the theme configs.
if (in_array(get_config('core', 'theme'), $themes)) {
set_config('theme', 'classic');
}
if (in_array(get_config('core', 'thememobile'), $themes)) {
set_config('thememobile', 'classic');
}
if (in_array(get_config('core', 'themelegacy'), $themes)) {
set_config('themelegacy', 'classic');
}
if (in_array(get_config('core', 'themetablet'), $themes)) {
set_config('themetablet', 'classic');
}
// Hacky emulation of plugin uninstallation.
foreach ($themes as $key => $theme) {
unset_all_config_for_plugin($key);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019040200.01);
}
if ($oldversion < 2019040600.02) {
// Define key fileid (foreign) to be dropped form analytics_train_samples.
$table = new xmldb_table('analytics_train_samples');
$key = new xmldb_key('fileid', XMLDB_KEY_FOREIGN, ['fileid'], 'files', ['id']);
// Launch drop key fileid.
$dbman->drop_key($table, $key);
// Define field fileid to be dropped from analytics_train_samples.
$table = new xmldb_table('analytics_train_samples');
$field = new xmldb_field('fileid');
// Conditionally launch drop field fileid.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019040600.02);
}
if ($oldversion < 2019040600.04) {
// Define field and index to be added to backup_controllers.
$table = new xmldb_table('backup_controllers');
$field = new xmldb_field('progress', XMLDB_TYPE_NUMBER, '15, 14', null, XMLDB_NOTNULL, null, '0', 'timemodified');
$index = new xmldb_index('useritem_ix', XMLDB_INDEX_NOTUNIQUE, ['userid', 'itemid']);
// Conditionally launch add field progress.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Conditionally launch add index useritem_ix.
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019040600.04);
}
if ($oldversion < 2019041000.02) {
// Define field fullmessagetrust to be added to messages.
$table = new xmldb_table('messages');
$field = new xmldb_field('fullmessagetrust', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'timecreated');
// Conditionally launch add field fullmessagetrust.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019041000.02);
}
if ($oldversion < 2019041300.01) {
// Add the field 'name' to the 'analytics_models' table.
$table = new xmldb_table('analytics_models');
$field = new xmldb_field('name', XMLDB_TYPE_CHAR, '1333', null, null, null, null, 'trained');
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019041300.01);
}
if ($oldversion < 2019041800.01) {
// STEP 1. For the existing and migrated self-conversations, set the type to the new MESSAGE_CONVERSATION_TYPE_SELF, update
// the convhash and star them.
$sql = "SELECT mcm.conversationid, mcm.userid, MAX(mcm.id) as maxid
FROM {message_conversation_members} mcm
INNER JOIN {user} u ON mcm.userid = u.id
WHERE u.deleted = 0
GROUP BY mcm.conversationid, mcm.userid
HAVING COUNT(*) > 1";
$selfconversationsrs = $DB->get_recordset_sql($sql);
$maxids = [];
foreach ($selfconversationsrs as $selfconversation) {
$DB->update_record('message_conversations',
['id' => $selfconversation->conversationid,
'type' => \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
'convhash' => \core_message\helper::get_conversation_hash([$selfconversation->userid])
]
);
// Star the existing self-conversation.
$favouriterecord = new \stdClass();
$favouriterecord->component = 'core_message';
$favouriterecord->itemtype = 'message_conversations';
$favouriterecord->itemid = $selfconversation->conversationid;
$userctx = \context_user::instance($selfconversation->userid);
$favouriterecord->contextid = $userctx->id;
$favouriterecord->userid = $selfconversation->userid;
if (!$DB->record_exists('favourite', (array)$favouriterecord)) {
$favouriterecord->timecreated = time();
$favouriterecord->timemodified = $favouriterecord->timecreated;
$DB->insert_record('favourite', $favouriterecord);
}
// Set the self-conversation member with maxid to remove it later.
$maxids[] = $selfconversation->maxid;
}
$selfconversationsrs->close();
// Remove the repeated member with the higher id for all the existing self-conversations.
if (!empty($maxids)) {
list($insql, $inparams) = $DB->get_in_or_equal($maxids);
$DB->delete_records_select('message_conversation_members', "id $insql", $inparams);
}
// STEP 2. Migrate existing self-conversation relying on old message tables, setting the type to the new
// MESSAGE_CONVERSATION_TYPE_SELF and the convhash to the proper one. Star them also.
// On the messaging legacy tables, self-conversations are only present in the 'message_read' table, so we don't need to
// check the content in the 'message' table.
$sql = "SELECT mr.*
FROM {message_read} mr
INNER JOIN {user} u ON mr.useridfrom = u.id
WHERE mr.useridfrom = mr.useridto AND mr.notification = 0 AND u.deleted = 0";
$legacyselfmessagesrs = $DB->get_recordset_sql($sql);
foreach ($legacyselfmessagesrs as $message) {
// Get the self-conversation or create and star it if doesn't exist.
$conditions = [
'type' => \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
'convhash' => \core_message\helper::get_conversation_hash([$message->useridfrom])
];
$selfconversation = $DB->get_record('message_conversations', $conditions);
if (empty($selfconversation)) {
// Create the self-conversation.
$selfconversation = new \stdClass();
$selfconversation->type = \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF;
$selfconversation->convhash = \core_message\helper::get_conversation_hash([$message->useridfrom]);
$selfconversation->enabled = 1;
$selfconversation->timecreated = time();
$selfconversation->timemodified = $selfconversation->timecreated;
$selfconversation->id = $DB->insert_record('message_conversations', $selfconversation);
// Add user to this self-conversation.
$member = new \stdClass();
$member->conversationid = $selfconversation->id;
$member->userid = $message->useridfrom;
$member->timecreated = time();
$member->id = $DB->insert_record('message_conversation_members', $member);
// Star the self-conversation.
$favouriterecord = new \stdClass();
$favouriterecord->component = 'core_message';
$favouriterecord->itemtype = 'message_conversations';
$favouriterecord->itemid = $selfconversation->id;
$userctx = \context_user::instance($message->useridfrom);
$favouriterecord->contextid = $userctx->id;
$favouriterecord->userid = $message->useridfrom;
if (!$DB->record_exists('favourite', (array)$favouriterecord)) {
$favouriterecord->timecreated = time();
$favouriterecord->timemodified = $favouriterecord->timecreated;
$DB->insert_record('favourite', $favouriterecord);
}
}
// Create the object we will be inserting into the database.
$tabledata = new \stdClass();
$tabledata->useridfrom = $message->useridfrom;
$tabledata->conversationid = $selfconversation->id;
$tabledata->subject = $message->subject;
$tabledata->fullmessage = $message->fullmessage;
$tabledata->fullmessageformat = $message->fullmessageformat ?? FORMAT_MOODLE;
$tabledata->fullmessagehtml = $message->fullmessagehtml;
$tabledata->smallmessage = $message->smallmessage;
$tabledata->timecreated = $message->timecreated;
$messageid = $DB->insert_record('messages', $tabledata);
// Check if we need to mark this message as deleted (self-conversations add this information on the
// timeuserfromdeleted field.
if ($message->timeuserfromdeleted) {
$mua = new \stdClass();
$mua->userid = $message->useridfrom;
$mua->messageid = $messageid;
$mua->action = \core_message\api::MESSAGE_ACTION_DELETED;
$mua->timecreated = $message->timeuserfromdeleted;
$DB->insert_record('message_user_actions', $mua);
}
// Mark this message as read.
$mua = new \stdClass();
$mua->userid = $message->useridto;
$mua->messageid = $messageid;
$mua->action = \core_message\api::MESSAGE_ACTION_READ;
$mua->timecreated = $message->timeread;
$DB->insert_record('message_user_actions', $mua);
// The self-conversation message has been migrated. Delete the record from the legacy table as soon as possible
// to avoid migrate it twice.
$DB->delete_records('message_read', ['id' => $message->id]);
}
$legacyselfmessagesrs->close();
// Main savepoint reached.
upgrade_main_savepoint(true, 2019041800.01);
}
if ($oldversion < 2019042200.01) {
// Define table role_sortorder to be dropped.
$table = new xmldb_table('role_sortorder');
// Conditionally launch drop table for role_sortorder.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019042200.01);
}
if ($oldversion < 2019042200.02) {
// Let's update all (old core) targets to their new (core_course) locations.
$targets = [
'\core\analytics\target\course_competencies' => '\core_course\analytics\target\course_competencies',
'\core\analytics\target\course_completion' => '\core_course\analytics\target\course_completion',
'\core\analytics\target\course_dropout' => '\core_course\analytics\target\course_dropout',
'\core\analytics\target\course_gradetopass' => '\core_course\analytics\target\course_gradetopass',
'\core\analytics\target\no_teaching' => '\core_course\analytics\target\no_teaching',
];
foreach ($targets as $oldclass => $newclass) {
$DB->set_field('analytics_models', 'target', $newclass, ['target' => $oldclass]);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019042200.02);
}
if ($oldversion < 2019042300.01) {
$sql = "UPDATE {capabilities}
SET name = ?,
contextlevel = ?
WHERE name = ?";
$DB->execute($sql, ['moodle/category:viewcourselist', CONTEXT_COURSECAT, 'moodle/course:browse']);
$sql = "UPDATE {role_capabilities}
SET capability = ?
WHERE capability = ?";
$DB->execute($sql, ['moodle/category:viewcourselist', 'moodle/course:browse']);
// Main savepoint reached.
upgrade_main_savepoint(true, 2019042300.01);
}
if ($oldversion < 2019042300.03) {
// Add new customdata field to message table.
$table = new xmldb_table('message');
$field = new xmldb_field('customdata', XMLDB_TYPE_TEXT, null, null, null, null, null, 'eventtype');
// Conditionally launch add field output.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Add new customdata field to notifications and messages table.
$table = new xmldb_table('notifications');
$field = new xmldb_field('customdata', XMLDB_TYPE_TEXT, null, null, null, null, null, 'timecreated');
// Conditionally launch add field output.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$table = new xmldb_table('messages');
// Conditionally launch add field output.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019042300.03);
}
if ($oldversion < 2019042700.01) {
// Define field firstanalysis to be added to analytics_used_analysables.
$table = new xmldb_table('analytics_used_analysables');
// Declaring it as null initially (although it is NOT NULL).
$field = new xmldb_field('firstanalysis', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'analysableid');
// Conditionally launch add field firstanalysis.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
// Set existing values to the current timeanalysed value.
$recordset = $DB->get_recordset('analytics_used_analysables');
foreach ($recordset as $record) {
$record->firstanalysis = $record->timeanalysed;
$DB->update_record('analytics_used_analysables', $record);
}
$recordset->close();
// Now make the field 'NOT NULL'.
$field = new xmldb_field('firstanalysis', XMLDB_TYPE_INTEGER, '10',
null, XMLDB_NOTNULL, null, null, 'analysableid');
$dbman->change_field_notnull($table, $field);
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2019042700.01);
}
if ($oldversion < 2019050300.01) {
// Delete all stale favourite records which were left behind when a course was deleted.
$params = ['component' => 'core_message', 'itemtype' => 'message_conversations'];
$sql = "SELECT fav.id as id
FROM {favourite} fav
LEFT JOIN {context} ctx ON (ctx.id = fav.contextid)
WHERE fav.component = :component
AND fav.itemtype = :itemtype
AND ctx.id IS NULL";
if ($records = $DB->get_fieldset_sql($sql, $params)) {
// Just for safety, delete by chunks.
$chunks = array_chunk($records, 1000);
foreach ($chunks as $chunk) {
list($insql, $inparams) = $DB->get_in_or_equal($chunk);
$DB->delete_records_select('favourite', "id $insql", $inparams);
}
}
upgrade_main_savepoint(true, 2019050300.01);
}
if ($oldversion < 2019050600.00) {
// Define field apiversion to be added to badge_backpack.
$table = new xmldb_table('badge_backpack');
$field = new xmldb_field('apiversion', XMLDB_TYPE_CHAR, '12', null, XMLDB_NOTNULL, null, '1.0', 'password');
// Conditionally launch add field apiversion.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define table badge_external_backpack to be created.
$table = new xmldb_table('badge_external_backpack');
// Adding fields to table badge_external_backpack.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('backpackapiurl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('backpackweburl', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('apiversion', XMLDB_TYPE_CHAR, '12', null, XMLDB_NOTNULL, null, '1.0');
$table->add_field('sortorder', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('password', XMLDB_TYPE_CHAR, '255', null, null, null, null);
// Adding keys to table badge_external_backpack.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('backpackapiurlkey', XMLDB_KEY_UNIQUE, ['backpackapiurl']);
$table->add_key('backpackweburlkey', XMLDB_KEY_UNIQUE, ['backpackweburl']);
// Conditionally launch create table for badge_external_backpack.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
// Define field entityid to be added to badge_external.
$table = new xmldb_table('badge_external');