forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgradelib.php
1296 lines (1146 loc) · 50.9 KB
/
upgradelib.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/>.
/**
* Upgrade helper functions
*
* This file is used for special upgrade functions - for example groups and gradebook.
* These functions must use SQL and database related functions only- no other Moodle API,
* because it might depend on db structures that are not yet present during upgrade.
* (Do not use functions from accesslib.php, grades classes or group functions at all!)
*
* @package core_install
* @category upgrade
* @copyright 2007 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Returns all non-view and non-temp tables with sane names.
* Prints list of non-supported tables using $OUTPUT->notification()
*
* @return array
*/
function upgrade_mysql_get_supported_tables() {
global $OUTPUT, $DB;
$tables = array();
$patprefix = str_replace('_', '\\_', $DB->get_prefix());
$pregprefix = preg_quote($DB->get_prefix(), '/');
$sql = "SHOW FULL TABLES LIKE '$patprefix%'";
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $record) {
$record = array_change_key_case((array)$record, CASE_LOWER);
$type = $record['table_type'];
unset($record['table_type']);
$fullname = array_shift($record);
if ($pregprefix === '') {
$name = $fullname;
} else {
$count = null;
$name = preg_replace("/^$pregprefix/", '', $fullname, -1, $count);
if ($count !== 1) {
continue;
}
}
if (!preg_match("/^[a-z][a-z0-9_]*$/", $name)) {
echo $OUTPUT->notification("Database table with invalid name '$fullname' detected, skipping.", 'notifyproblem');
continue;
}
if ($type === 'VIEW') {
echo $OUTPUT->notification("Unsupported database table view '$fullname' detected, skipping.", 'notifyproblem');
continue;
}
$tables[$name] = $name;
}
$rs->close();
return $tables;
}
/**
* Using data for a single course-module that has groupmembersonly enabled,
* returns the new availability value that incorporates the correct
* groupmembersonly option.
*
* Included as a function so that it can be shared between upgrade and restore,
* and unit-tested.
*
* @param int $groupingid Grouping id for the course-module (0 if none)
* @param string $availability Availability JSON data for the module (null if none)
* @return string New value for availability for the module
*/
function upgrade_group_members_only($groupingid, $availability) {
// Work out the new JSON object representing this option.
if ($groupingid) {
// Require specific grouping.
$condition = (object)array('type' => 'grouping', 'id' => (int)$groupingid);
} else {
// No grouping specified, so require membership of any group.
$condition = (object)array('type' => 'group');
}
if (is_null($availability)) {
// If there are no conditions using the new API then just set it.
$tree = (object)array('op' => '&', 'c' => array($condition), 'showc' => array(false));
} else {
// There are existing conditions.
$tree = json_decode($availability);
switch ($tree->op) {
case '&' :
// For & conditions we can just add this one.
$tree->c[] = $condition;
$tree->showc[] = false;
break;
case '!|' :
// For 'not or' conditions we can add this one
// but negated.
$tree->c[] = (object)array('op' => '!&', 'c' => array($condition));
$tree->showc[] = false;
break;
default:
// For the other two (OR and NOT AND) we have to add
// an extra level to the tree.
$tree = (object)array('op' => '&', 'c' => array($tree, $condition),
'showc' => array($tree->show, false));
// Inner trees do not have a show option, so remove it.
unset($tree->c[0]->show);
break;
}
}
return json_encode($tree);
}
/**
* Marks all courses with changes in extra credit weight calculation
*
* Used during upgrade and in course restore process
*
* This upgrade script is needed because we changed the algorithm for calculating the automatic weights of extra
* credit items and want to prevent changes in the existing student grades.
*
* @param int $onlycourseid
*/
function upgrade_extra_credit_weightoverride($onlycourseid = 0) {
global $DB;
// Find all courses that have categories in Natural aggregation method where there is at least one extra credit
// item and at least one item with overridden weight.
$courses = $DB->get_fieldset_sql(
"SELECT DISTINCT gc.courseid
FROM {grade_categories} gc
INNER JOIN {grade_items} gi ON gc.id = gi.categoryid AND gi.weightoverride = :weightoverriden
INNER JOIN {grade_items} gie ON gc.id = gie.categoryid AND gie.aggregationcoef = :extracredit
WHERE gc.aggregation = :naturalaggmethod" . ($onlycourseid ? " AND gc.courseid = :onlycourseid" : ''),
array('naturalaggmethod' => 13,
'weightoverriden' => 1,
'extracredit' => 1,
'onlycourseid' => $onlycourseid,
)
);
foreach ($courses as $courseid) {
$gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
if (!$gradebookfreeze) {
set_config('gradebook_calculations_freeze_' . $courseid, 20150619);
}
}
}
/**
* Marks all courses that require calculated grade items be updated.
*
* Used during upgrade and in course restore process.
*
* This upgrade script is needed because the calculated grade items were stuck with a maximum of 100 and could be changed.
* This flags the courses that are affected and the grade book is frozen to retain grade integrity.
*
* @param int $courseid Specify a course ID to run this script on just one course.
*/
function upgrade_calculated_grade_items($courseid = null) {
global $DB, $CFG;
$affectedcourses = array();
$possiblecourseids = array();
$params = array();
$singlecoursesql = '';
if (isset($courseid)) {
$singlecoursesql = "AND ns.id = :courseid";
$params['courseid'] = $courseid;
}
$siteminmaxtouse = 1;
if (isset($CFG->grade_minmaxtouse)) {
$siteminmaxtouse = $CFG->grade_minmaxtouse;
}
$courseidsql = "SELECT ns.id
FROM (
SELECT c.id, coalesce(" . $DB->sql_compare_text('gs.value') . ", :siteminmax) AS gradevalue
FROM {course} c
LEFT JOIN {grade_settings} gs
ON c.id = gs.courseid
AND ((gs.name = 'minmaxtouse' AND " . $DB->sql_compare_text('gs.value') . " = '2'))
) ns
WHERE " . $DB->sql_compare_text('ns.gradevalue') . " = '2' $singlecoursesql";
$params['siteminmax'] = $siteminmaxtouse;
$courses = $DB->get_records_sql($courseidsql, $params);
foreach ($courses as $course) {
$possiblecourseids[$course->id] = $course->id;
}
if (!empty($possiblecourseids)) {
list($sql, $params) = $DB->get_in_or_equal($possiblecourseids);
// A calculated grade item grade min != 0 and grade max != 100 and the course setting is set to
// "Initial min and max grades".
$coursesql = "SELECT DISTINCT courseid
FROM {grade_items}
WHERE calculation IS NOT NULL
AND itemtype = 'manual'
AND (grademax <> 100 OR grademin <> 0)
AND courseid $sql";
$affectedcourses = $DB->get_records_sql($coursesql, $params);
}
// Check for second type of affected courses.
// If we already have the courseid parameter set in the affectedcourses then there is no need to run through this section.
if (!isset($courseid) || !in_array($courseid, $affectedcourses)) {
$singlecoursesql = '';
$params = array();
if (isset($courseid)) {
$singlecoursesql = "AND courseid = :courseid";
$params['courseid'] = $courseid;
}
$nestedsql = "SELECT id
FROM {grade_items}
WHERE itemtype = 'category'
AND calculation IS NOT NULL $singlecoursesql";
$calculatedgradecategories = $DB->get_records_sql($nestedsql, $params);
$categoryids = array();
foreach ($calculatedgradecategories as $key => $gradecategory) {
$categoryids[$key] = $gradecategory->id;
}
if (!empty($categoryids)) {
list($sql, $params) = $DB->get_in_or_equal($categoryids);
// A category with a calculation where the raw grade min and the raw grade max don't match the grade min and grade max
// for the category.
$coursesql = "SELECT DISTINCT gi.courseid
FROM {grade_grades} gg, {grade_items} gi
WHERE gi.id = gg.itemid
AND (gg.rawgrademax <> gi.grademax OR gg.rawgrademin <> gi.grademin)
AND gi.id $sql";
$additionalcourses = $DB->get_records_sql($coursesql, $params);
foreach ($additionalcourses as $key => $additionalcourse) {
if (!array_key_exists($key, $affectedcourses)) {
$affectedcourses[$key] = $additionalcourse;
}
}
}
}
foreach ($affectedcourses as $affectedcourseid) {
if (isset($CFG->upgrade_calculatedgradeitemsonlyregrade) && !($courseid)) {
$DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $affectedcourseid->courseid));
} else {
// Check to see if the gradebook freeze is already in affect.
$gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $affectedcourseid->courseid);
if (!$gradebookfreeze) {
set_config('gradebook_calculations_freeze_' . $affectedcourseid->courseid, 20150627);
}
}
}
}
/**
* This function creates a default separated/connected scale
* so there's something in the database. The locations of
* strings and files is a bit odd, but this is because we
* need to maintain backward compatibility with many different
* existing language translations and older sites.
*
* @global object
* @return void
*/
function make_default_scale() {
global $DB;
$defaultscale = new stdClass();
$defaultscale->courseid = 0;
$defaultscale->userid = 0;
$defaultscale->name = get_string('separateandconnected');
$defaultscale->description = get_string('separateandconnectedinfo');
$defaultscale->scale = get_string('postrating1', 'forum').','.
get_string('postrating2', 'forum').','.
get_string('postrating3', 'forum');
$defaultscale->timemodified = time();
$defaultscale->id = $DB->insert_record('scale', $defaultscale);
return $defaultscale;
}
/**
* Create another default scale.
*
* @param int $oldversion
* @return bool always true
*/
function make_competence_scale() {
global $DB;
$defaultscale = new stdClass();
$defaultscale->courseid = 0;
$defaultscale->userid = 0;
$defaultscale->name = get_string('defaultcompetencescale');
$defaultscale->description = get_string('defaultcompetencescaledesc');
$defaultscale->scale = get_string('defaultcompetencescalenotproficient').','.
get_string('defaultcompetencescaleproficient');
$defaultscale->timemodified = time();
$defaultscale->id = $DB->insert_record('scale', $defaultscale);
return $defaultscale;
}
/**
* Marks all courses that require rounded grade items be updated.
*
* Used during upgrade and in course restore process.
*
* This upgrade script is needed because it has been decided that if a grade is rounded up, and it will changed a letter
* grade or satisfy a course completion grade criteria, then it should be set as so, and the letter will be awarded and or
* the course completion grade will be awarded.
*
* @param int $courseid Specify a course ID to run this script on just one course.
*/
function upgrade_course_letter_boundary($courseid = null) {
global $DB, $CFG;
$coursesql = '';
$params = array('contextlevel' => CONTEXT_COURSE);
if (!empty($courseid)) {
$coursesql = 'AND c.id = :courseid';
$params['courseid'] = $courseid;
}
// Check to see if the system letter boundaries are borked.
$systemcontext = context_system::instance();
$systemneedsfreeze = upgrade_letter_boundary_needs_freeze($systemcontext);
// Check the setting for showing the letter grade in a column (default is false).
$usergradelettercolumnsetting = 0;
if (isset($CFG->grade_report_user_showlettergrade)) {
$usergradelettercolumnsetting = (int)$CFG->grade_report_user_showlettergrade;
}
$lettercolumnsql = '';
if ($usergradelettercolumnsetting) {
// The system default is to show a column with letters (and the course uses the defaults).
$lettercolumnsql = '(gss.value is NULL OR ' . $DB->sql_compare_text('gss.value') . ' <> \'0\')';
} else {
// The course displays a column with letters.
$lettercolumnsql = $DB->sql_compare_text('gss.value') . ' = \'1\'';
}
// 3, 13, 23, 31, and 32 are the grade display types that incorporate showing letters. See lib/grade/constants/php.
$systemusesletters = (int) (isset($CFG->grade_displaytype) && in_array($CFG->grade_displaytype, array(3, 13, 23, 31, 32)));
$systemletters = $systemusesletters || $usergradelettercolumnsetting;
$contextselect = context_helper::get_preload_record_columns_sql('ctx');
if ($systemletters && $systemneedsfreeze) {
// Select courses with no grade setting for display and a grade item that is using the default display,
// but have not altered the course letter boundary configuration. These courses are definitely affected.
$sql = "SELECT DISTINCT c.id AS courseid
FROM {course} c
JOIN {grade_items} gi ON c.id = gi.courseid
JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
LEFT JOIN {grade_settings} gs ON gs.courseid = c.id AND gs.name = 'displaytype'
LEFT JOIN {grade_settings} gss ON gss.courseid = c.id AND gss.name = 'report_user_showlettergrade'
LEFT JOIN {grade_letters} gl ON gl.contextid = ctx.id
WHERE gi.display = 0
AND ((gs.value is NULL)
AND ($lettercolumnsql))
AND gl.id is NULL $coursesql";
$affectedcourseids = $DB->get_recordset_sql($sql, $params);
foreach ($affectedcourseids as $courseid) {
set_config('gradebook_calculations_freeze_' . $courseid->courseid, 20160518);
}
$affectedcourseids->close();
}
// If the system letter boundary is okay proceed to check grade item and course grade display settings.
$sql = "SELECT DISTINCT c.id AS courseid, $contextselect
FROM {course} c
JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
JOIN {grade_items} gi ON c.id = gi.courseid
LEFT JOIN {grade_settings} gs ON c.id = gs.courseid AND gs.name = 'displaytype'
LEFT JOIN {grade_settings} gss ON gss.courseid = c.id AND gss.name = 'report_user_showlettergrade'
WHERE
(
-- A grade item is using letters
(gi.display IN (3, 13, 23, 31, 32))
-- OR the course is using letters
OR (" . $DB->sql_compare_text('gs.value') . " IN ('3', '13', '23', '31', '32')
-- OR the course using the system default which is letters
OR (gs.value IS NULL AND $systemusesletters = 1)
)
OR ($lettercolumnsql)
)
-- AND the course matches
$coursesql";
$potentialcourses = $DB->get_recordset_sql($sql, $params);
foreach ($potentialcourses as $value) {
$gradebookfreeze = 'gradebook_calculations_freeze_' . $value->courseid;
// Check also if this course id has already been frozen.
// If we already have this course ID then move on to the next record.
if (!property_exists($CFG, $gradebookfreeze)) {
// Check for 57 letter grade issue.
context_helper::preload_from_record($value);
$coursecontext = context_course::instance($value->courseid);
if (upgrade_letter_boundary_needs_freeze($coursecontext)) {
// We have a course with a possible score standardisation problem. Flag for freeze.
// Flag this course as being frozen.
set_config('gradebook_calculations_freeze_' . $value->courseid, 20160518);
}
}
}
$potentialcourses->close();
}
/**
* Checks the letter boundary of the provided context to see if it needs freezing.
* Each letter boundary is tested to see if receiving that boundary number will
* result in achieving the cosponsoring letter.
*
* @param object $context Context object
* @return bool if the letter boundary for this context should be frozen.
*/
function upgrade_letter_boundary_needs_freeze($context) {
global $DB;
$contexts = $context->get_parent_context_ids();
array_unshift($contexts, $context->id);
foreach ($contexts as $ctxid) {
$letters = $DB->get_records_menu('grade_letters', array('contextid' => $ctxid), 'lowerboundary DESC',
'lowerboundary, letter');
if (!empty($letters)) {
foreach ($letters as $boundary => $notused) {
$standardisedboundary = upgrade_standardise_score($boundary, 0, 100, 0, 100);
if ($standardisedboundary < $boundary) {
return true;
}
}
// We found letters but we have no boundary problem.
return false;
}
}
return false;
}
/**
* Given a float value situated between a source minimum and a source maximum, converts it to the
* corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
* for the formula :-)
*
* @param float $rawgrade
* @param float $sourcemin
* @param float $sourcemax
* @param float $targetmin
* @param float $targetmax
* @return float Converted value
*/
function upgrade_standardise_score($rawgrade, $sourcemin, $sourcemax, $targetmin, $targetmax) {
if (is_null($rawgrade)) {
return null;
}
if ($sourcemax == $sourcemin or $targetmin == $targetmax) {
// Prevent division by 0.
return $targetmax;
}
$factor = ($rawgrade - $sourcemin) / ($sourcemax - $sourcemin);
$diff = $targetmax - $targetmin;
$standardisedvalue = $factor * $diff + $targetmin;
return $standardisedvalue;
}
/**
* Provides a way to check and update a serialized string that uses the deprecated object class.
*
* @param string $serializeddata Serialized string which may contain the now deprecated object.
* @return array Returns an array where the first variable is a bool with a status of whether the initial data was changed
* or not. The second variable is the said data.
*/
function upgrade_fix_serialized_objects($serializeddata) {
$updated = false;
if (strpos($serializeddata, ":6:\"object") !== false) {
$serializeddata = str_replace(":6:\"object", ":8:\"stdClass", $serializeddata);
$updated = true;
}
return [$updated, $serializeddata];
}
/**
* Deletes file records which have their repository deleted.
*
*/
function upgrade_delete_orphaned_file_records() {
global $DB;
$sql = "SELECT f.id, f.contextid, f.component, f.filearea, f.itemid, fr.id AS referencefileid
FROM {files} f
JOIN {files_reference} fr ON f.referencefileid = fr.id
LEFT JOIN {repository_instances} ri ON fr.repositoryid = ri.id
WHERE ri.id IS NULL";
$deletedfiles = $DB->get_recordset_sql($sql);
$deletedfileids = array();
$fs = get_file_storage();
foreach ($deletedfiles as $deletedfile) {
$fs->delete_area_files($deletedfile->contextid, $deletedfile->component, $deletedfile->filearea, $deletedfile->itemid);
$deletedfileids[] = $deletedfile->referencefileid;
}
$deletedfiles->close();
$DB->delete_records_list('files_reference', 'id', $deletedfileids);
}
/**
* Updates the existing prediction actions in the database according to the new suggested actions.
* @return null
*/
function upgrade_rename_prediction_actions_useful_incorrectly_flagged() {
global $DB;
// The update depends on the analyser class used by each model so we need to iterate through the models in the system.
$modelids = $DB->get_records_sql("SELECT DISTINCT am.id, am.target
FROM {analytics_models} am
JOIN {analytics_predictions} ap ON ap.modelid = am.id
JOIN {analytics_prediction_actions} apa ON ap.id = apa.predictionid");
foreach ($modelids as $model) {
$targetname = $model->target;
if (!class_exists($targetname)) {
// The plugin may not be available.
continue;
}
$target = new $targetname();
$analyserclass = $target->get_analyser_class();
if (!class_exists($analyserclass)) {
// The plugin may not be available.
continue;
}
if ($analyserclass::one_sample_per_analysable()) {
// From 'fixed' to 'useful'.
$params = ['oldaction' => 'fixed', 'newaction' => 'useful'];
} else {
// From 'notuseful' to 'incorrectlyflagged'.
$params = ['oldaction' => 'notuseful', 'newaction' => 'incorrectlyflagged'];
}
$subsql = "SELECT id FROM {analytics_predictions} WHERE modelid = :modelid";
$updatesql = "UPDATE {analytics_prediction_actions}
SET actionname = :newaction
WHERE predictionid IN ($subsql) AND actionname = :oldaction";
$DB->execute($updatesql, $params + ['modelid' => $model->id]);
}
}
/**
* Convert the site settings for the 'hub' component in the config_plugins table.
*
* @param stdClass $hubconfig Settings loaded for the 'hub' component.
* @param string $huburl The URL of the hub to use as the valid one in case of conflict.
* @return stdClass List of new settings to be applied (including null values to be unset).
*/
function upgrade_convert_hub_config_site_param_names(stdClass $hubconfig, string $huburl): stdClass {
$cleanhuburl = clean_param($huburl, PARAM_ALPHANUMEXT);
$converted = [];
foreach ($hubconfig as $oldname => $value) {
if (preg_match('/^site_([a-z]+)([A-Za-z0-9_-]*)/', $oldname, $matches)) {
$newname = 'site_'.$matches[1];
if ($oldname === $newname) {
// There is an existing value with the new naming convention already.
$converted[$newname] = $value;
} else if (!array_key_exists($newname, $converted)) {
// Add the value under a new name and mark the original to be unset.
$converted[$newname] = $value;
$converted[$oldname] = null;
} else if ($matches[2] === '_'.$cleanhuburl) {
// The new name already exists, overwrite only if coming from the valid hub.
$converted[$newname] = $value;
$converted[$oldname] = null;
} else {
// Just unset the old value.
$converted[$oldname] = null;
}
} else {
// Not a hub-specific site setting, just keep it.
$converted[$oldname] = $value;
}
}
return (object) $converted;
}
/**
* Fix the incorrect default values inserted into analytics contextids field.
*/
function upgrade_analytics_fix_contextids_defaults() {
global $DB;
$select = $DB->sql_compare_text('contextids') . ' = :zero OR ' . $DB->sql_compare_text('contextids') . ' = :null';
$params = ['zero' => '0', 'null' => 'null'];
$DB->execute("UPDATE {analytics_models} set contextids = null WHERE " . $select, $params);
}
/**
* Upgrade core licenses shipped with Moodle.
*/
function upgrade_core_licenses() {
global $CFG, $DB;
$corelicenses = [];
$license = new stdClass();
$license->shortname = 'unknown';
$license->fullname = 'Licence not specified';
$license->source = '';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'allrightsreserved';
$license->fullname = 'All rights reserved';
$license->source = 'https://en.wikipedia.org/wiki/All_rights_reserved';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'public';
$license->fullname = 'Public domain';
$license->source = 'https://en.wikipedia.org/wiki/Public_domain';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'cc';
$license->fullname = 'Creative Commons';
$license->source = 'https://creativecommons.org/licenses/by/3.0/';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'cc-nd';
$license->fullname = 'Creative Commons - NoDerivs';
$license->source = 'https://creativecommons.org/licenses/by-nd/3.0/';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'cc-nc-nd';
$license->fullname = 'Creative Commons - No Commercial NoDerivs';
$license->source = 'https://creativecommons.org/licenses/by-nc-nd/3.0/';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'cc-nc';
$license->fullname = 'Creative Commons - No Commercial';
$license->source = 'https://creativecommons.org/licenses/by-nc/3.0/';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'cc-nc-sa';
$license->fullname = 'Creative Commons - No Commercial ShareAlike';
$license->source = 'https://creativecommons.org/licenses/by-nc-sa/3.0/';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
$license = new stdClass();
$license->shortname = 'cc-sa';
$license->fullname = 'Creative Commons - ShareAlike';
$license->source = 'https://creativecommons.org/licenses/by-sa/3.0/';
$license->enabled = 1;
$license->version = '2010033100';
$license->custom = 0;
$corelicenses[] = $license;
foreach ($corelicenses as $corelicense) {
// Check for current license to maintain idempotence.
$currentlicense = $DB->get_record('license', ['shortname' => $corelicense->shortname]);
if (!empty($currentlicense)) {
$corelicense->id = $currentlicense->id;
// Remember if the license was enabled before upgrade.
$corelicense->enabled = $currentlicense->enabled;
$DB->update_record('license', $corelicense);
} else if (!isset($CFG->upgraderunning) || during_initial_install()) {
// Only install missing core licenses if not upgrading or during initial install.
$DB->insert_record('license', $corelicense);
}
}
// Add sortorder to all licenses.
$licenses = $DB->get_records('license');
$sortorder = 1;
foreach ($licenses as $license) {
$license->sortorder = $sortorder++;
$DB->update_record('license', $license);
}
// Set the license config values, used by file repository for rendering licenses at front end.
$activelicenses = $DB->get_records_menu('license', ['enabled' => 1], 'id', 'id, shortname');
set_config('licenses', implode(',', $activelicenses));
$sitedefaultlicense = get_config('', 'sitedefaultlicense');
if (empty($sitedefaultlicense) || !in_array($sitedefaultlicense, $activelicenses)) {
set_config('sitedefaultlicense', reset($activelicenses));
}
}
/**
* Detects if the site may need to get the calendar events fixed or no. With optional output.
*
* @param bool $output true if the function must output information, false if not.
* @return bool true if the site needs to run the fixes, false if not.
*/
function upgrade_calendar_site_status(bool $output = true): bool {
global $DB;
// List of upgrade steps where the bug happened.
$badsteps = [
'3.9.5' => '2020061504.08',
'3.10.2' => '2020110901.09',
'3.11dev' => '2021022600.02',
'4.0dev' => '2021052500.65',
];
// List of upgrade steps that ran the fixer.
$fixsteps = [
'3.9.6+' => '2020061506.05',
'3.10.3+' => '2020110903.05',
'3.11dev' => '2021042100.02',
'4.0dev' => '2021052500.85',
];
$targetsteps = array_merge(array_values($badsteps), array_values( $fixsteps));
list($insql, $inparams) = $DB->get_in_or_equal($targetsteps);
$foundsteps = $DB->get_fieldset_sql("
SELECT DISTINCT version
FROM {upgrade_log}
WHERE plugin = 'core'
AND version " . $insql . "
ORDER BY version", $inparams);
// Analyse the found steps, to decide if the site needs upgrading or no.
$badfound = false;
$fixfound = false;
foreach ($foundsteps as $foundstep) {
$badfound = $badfound ?: array_search($foundstep, $badsteps, true);
$fixfound = $fixfound ?: array_search($foundstep, $fixsteps, true);
}
$needsfix = $badfound && !$fixfound;
// Let's output some textual information if required to.
if ($output) {
mtrace("");
if ($badfound) {
mtrace("This site has executed the problematic upgrade step {$badsteps[$badfound]} present in {$badfound}.");
} else {
mtrace("Problematic upgrade steps were NOT found, site should be safe.");
}
if ($fixfound) {
mtrace("This site has executed the fix upgrade step {$fixsteps[$fixfound]} present in {$fixfound}.");
} else {
mtrace("Fix upgrade steps were NOT found.");
}
mtrace("");
if ($needsfix) {
mtrace("This site NEEDS to run the calendar events fix!");
mtrace('');
mtrace("You can use this CLI tool or upgrade to a version of Moodle that includes");
mtrace("the fix and will be executed as part of the normal upgrade procedure.");
mtrace("The following versions or up are known candidates to upgrade to:");
foreach ($fixsteps as $key => $value) {
mtrace(" - {$key}: {$value}");
}
mtrace("");
}
}
return $needsfix;
}
/**
* Detects the calendar events needing to be fixed. With optional output.
*
* @param bool $output true if the function must output information, false if not.
* @return stdClass[] an array of event types (as keys) with total and bad counters, plus sql to retrieve them.
*/
function upgrade_calendar_events_status(bool $output = true): array {
global $DB;
// Calculate the list of standard (core) activity plugins.
$plugins = core_plugin_manager::standard_plugins_list('mod');
$coremodules = "modulename IN ('" . implode("', '", $plugins) . "')";
// Some query parts go here.
$brokenevents = "(userid = 0 AND (eventtype <> 'user' OR priority <> 0))"; // From the original bad upgrade step.
$standardevents = "(eventtype IN ('site', 'category', 'course', 'group', 'user') AND subscriptionid IS NULL)";
$subscriptionevents = "(subscriptionid IS NOT NULL)";
$overrideevents = "({$coremodules} AND priority IS NOT NULL)";
$actionevents = "({$coremodules} AND instance > 0 and priority IS NULL)";
$otherevents = "(NOT ({$standardevents} OR {$subscriptionevents} OR {$overrideevents} OR {$actionevents}))";
// Detailed query template.
$detailstemplate = "
SELECT ##group## AS groupname, COUNT(1) AS count
FROM {event}
WHERE ##groupconditions##
GROUP BY ##group##";
// Count total and potentially broken events.
$total = $DB->count_records_select('event', '');
$totalbadsql = $brokenevents;
$totalbad = $DB->count_records_select('event', $totalbadsql);
// Standard events.
$standard = $DB->count_records_select('event', $standardevents);
$standardbadsql = "{$brokenevents} AND {$standardevents}";
$standardbad = $DB->count_records_select('event', $standardbadsql);
$standarddetails = $DB->get_records_sql(
str_replace(
['##group##', '##groupconditions##'],
['eventtype', $standardbadsql],
$detailstemplate
)
);
array_walk($standarddetails, function (&$rec) {
$rec = $rec->groupname . ': ' . $rec->count;
});
$standarddetails = $standarddetails ? '(' . implode(', ', $standarddetails) . ')' : '- all good!';
// Subscription events.
$subscription = $DB->count_records_select('event', $subscriptionevents);
$subscriptionbadsql = "{$brokenevents} AND {$subscriptionevents}";
$subscriptionbad = $DB->count_records_select('event', $subscriptionbadsql);
$subscriptiondetails = $DB->get_records_sql(
str_replace(
['##group##', '##groupconditions##'],
['eventtype', $subscriptionbadsql],
$detailstemplate
)
);
array_walk($subscriptiondetails, function (&$rec) {
$rec = $rec->groupname . ': ' . $rec->count;
});
$subscriptiondetails = $subscriptiondetails ? '(' . implode(', ', $subscriptiondetails) . ')' : '- all good!';
// Override events.
$override = $DB->count_records_select('event', $overrideevents);
$overridebadsql = "{$brokenevents} AND {$overrideevents}";
$overridebad = $DB->count_records_select('event', $overridebadsql);
$overridedetails = $DB->get_records_sql(
str_replace(
['##group##', '##groupconditions##'],
['modulename', $overridebadsql],
$detailstemplate
)
);
array_walk($overridedetails, function (&$rec) {
$rec = $rec->groupname . ': ' . $rec->count;
});
$overridedetails = $overridedetails ? '(' . implode(', ', $overridedetails) . ')' : '- all good!';
// Action events.
$action = $DB->count_records_select('event', $actionevents);
$actionbadsql = "{$brokenevents} AND {$actionevents}";
$actionbad = $DB->count_records_select('event', $actionbadsql);
$actiondetails = $DB->get_records_sql(
str_replace(
['##group##', '##groupconditions##'],
['modulename', $actionbadsql],
$detailstemplate
)
);
array_walk($actiondetails, function (&$rec) {
$rec = $rec->groupname . ': ' . $rec->count;
});
$actiondetails = $actiondetails ? '(' . implode(', ', $actiondetails) . ')' : '- all good!';
// Other events.
$other = $DB->count_records_select('event', $otherevents);
$otherbadsql = "{$brokenevents} AND {$otherevents}";
$otherbad = $DB->count_records_select('event', $otherbadsql);
$otherdetails = $DB->get_records_sql(
str_replace(
['##group##', '##groupconditions##'],
['COALESCE(component, modulename)', $otherbadsql],
$detailstemplate
)
);
array_walk($otherdetails, function (&$rec) {
$rec = ($rec->groupname ?: 'unknown') . ': ' . $rec->count;
});
$otherdetails = $otherdetails ? '(' . implode(', ', $otherdetails) . ')' : '- all good!';
// Let's output some textual information if required to.
if ($output) {
mtrace("");
mtrace("Totals: {$total} / {$totalbad} (total / wrong)");
mtrace(" - standards events: {$standard} / {$standardbad} {$standarddetails}");
mtrace(" - subscription events: {$subscription} / {$subscriptionbad} {$subscriptiondetails}");
mtrace(" - override events: {$override} / {$overridebad} {$overridedetails}");
mtrace(" - action events: {$action} / {$actionbad} {$actiondetails}");
mtrace(" - other events: {$other} / {$otherbad} {$otherdetails}");
mtrace("");
}
return [
'total' => (object)['count' => $total, 'bad' => $totalbad, 'sql' => $totalbadsql],
'standard' => (object)['count' => $standard, 'bad' => $standardbad, 'sql' => $standardbadsql],
'subscription' => (object)['count' => $subscription, 'bad' => $subscriptionbad, 'sql' => $subscriptionbadsql],
'override' => (object)['count' => $override, 'bad' => $overridebad, 'sql' => $overridebadsql],
'action' => (object)['count' => $action, 'bad' => $actionbad, 'sql' => $actionbadsql],
'other' => (object)['count' => $other, 'bad' => $otherbad, 'sql' => $otherbadsql],
];
}
/**
* Detects the calendar events needing to be fixed. With optional output.
*
* @param stdClass[] an array of event types (as keys) with total and bad counters, plus sql to retrieve them.
* @param bool $output true if the function must output information, false if not.
* @param int $maxseconds Number of seconds the function will run as max, with zero meaning no limit.
* @return bool true if the function has not finished fixing everything, false if it has finished.
*/
function upgrade_calendar_events_fix_remaining(array $info, bool $output = true, int $maxseconds = 0): bool {
global $DB;
upgrade_calendar_events_mtrace('', $output);
// Initial preparations.
$starttime = time();
$endtime = $maxseconds ? ($starttime + $maxseconds) : 0;
// No bad events, or all bad events are "other" events, finished.
if ($info['total']->bad == 0 || $info['total']->bad == $info['other']->bad) {
return false;
}
// Let's fix overriden events first (they are the ones performing worse with the missing userid).
if ($info['override']->bad != 0) {
if (upgrade_calendar_override_events_fix($info['override'], $output, $endtime)) {
return true; // Not finished yet.
}
}
// Let's fix the subscription events (like standard ones, but with the event_subscriptions table).
if ($info['subscription']->bad != 0) {
if (upgrade_calendar_subscription_events_fix($info['subscription'], $output, $endtime)) {
return true; // Not finished yet.
}
}
// Let's fix the standard events (site, category, course, group).
if ($info['standard']->bad != 0) {
if (upgrade_calendar_standard_events_fix($info['standard'], $output, $endtime)) {
return true; // Not finished yet.
}