@@ -552,9 +552,12 @@ protected function define_structure() {
552
552
FROM {course_format_options}
553
553
WHERE courseid = ? AND sectionid = 0 ' , [ backup::VAR_PARENTID ]);
554
554
555
- $ handler = core_course \customfield \course_handler::create ();
556
- $ fieldsforbackup = $ handler ->get_instance_data_for_backup ($ this ->task ->get_courseid ());
557
- $ customfield ->set_source_array ($ fieldsforbackup );
555
+ // Custom fields.
556
+ if ($ this ->get_setting_value ('customfield ' )) {
557
+ $ handler = core_course \customfield \course_handler::create ();
558
+ $ fieldsforbackup = $ handler ->get_instance_data_for_backup ($ this ->task ->get_courseid ());
559
+ $ customfield ->set_source_array ($ fieldsforbackup );
560
+ }
558
561
559
562
// Some annotations
560
563
@@ -1403,24 +1406,40 @@ protected function define_structure() {
1403
1406
FROM {groups} g
1404
1407
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
1405
1408
WHERE bi.backupid = ?
1406
- AND bi.itemname = 'groupfinal' " , array (backup::VAR_BACKUPID ));
1409
+ AND bi.itemname = 'groupfinal' " ,
1410
+ [backup_helper::is_sqlparam ($ this ->get_backupid ())]
1411
+ );
1407
1412
1408
1413
$ grouping ->set_source_sql ("
1409
1414
SELECT g.*
1410
1415
FROM {groupings} g
1411
1416
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
1412
1417
WHERE bi.backupid = ?
1413
- AND bi.itemname = 'groupingfinal' " , array (backup::VAR_BACKUPID ));
1418
+ AND bi.itemname = 'groupingfinal' " ,
1419
+ [backup_helper::is_sqlparam ($ this ->get_backupid ())]
1420
+ );
1421
+
1414
1422
$ groupinggroup ->set_source_table ('groupings_groups ' , array ('groupingid ' => backup::VAR_PARENTID ));
1415
1423
1416
1424
// This only happens if we are including users.
1417
1425
if ($ userinfo ) {
1418
1426
$ member ->set_source_table ('groups_members ' , array ('groupid ' => backup::VAR_PARENTID ));
1419
1427
}
1420
1428
1421
- $ courseid = $ this ->task ->get_courseid ();
1422
- $ groupcustomfield ->set_source_array ($ this ->get_group_custom_fields_for_backup ($ courseid ));
1423
- $ groupingcustomfield ->set_source_array ($ this ->get_grouping_custom_fields_for_backup ($ courseid ));
1429
+ // Custom fields.
1430
+ if ($ this ->get_setting_value ('customfield ' )) {
1431
+ $ groupcustomfieldarray = $ this ->get_group_custom_fields_for_backup (
1432
+ $ group ->get_source_sql (),
1433
+ [$ this ->get_backupid ()]
1434
+ );
1435
+ $ groupcustomfield ->set_source_array ($ groupcustomfieldarray );
1436
+
1437
+ $ groupingcustomfieldarray = $ this ->get_grouping_custom_fields_for_backup (
1438
+ $ grouping ->get_source_sql (),
1439
+ [$ this ->get_backupid ()]
1440
+ );
1441
+ $ groupingcustomfield ->set_source_array ($ groupingcustomfieldarray );
1442
+ }
1424
1443
}
1425
1444
1426
1445
// Define id annotations (as final)
@@ -1439,14 +1458,16 @@ protected function define_structure() {
1439
1458
1440
1459
/**
1441
1460
* Get custom fields array for group
1442
- * @param int $courseid
1461
+ *
1462
+ * @param string $groupsourcesql
1463
+ * @param array $groupsourceparams
1443
1464
* @return array
1444
1465
*/
1445
- protected function get_group_custom_fields_for_backup (int $ courseid ): array {
1466
+ protected function get_group_custom_fields_for_backup (string $ groupsourcesql , array $ groupsourceparams ): array {
1446
1467
global $ DB ;
1447
1468
$ handler = \core_group \customfield \group_handler::create ();
1448
1469
$ fieldsforbackup = [];
1449
- if ($ groups = $ DB ->get_records ( ' groups ' , [ ' courseid ' => $ courseid ], '' , ' id ' )) {
1470
+ if ($ groups = $ DB ->get_records_sql ( $ groupsourcesql , $ groupsourceparams )) {
1450
1471
foreach ($ groups as $ group ) {
1451
1472
$ fieldsforbackup = array_merge ($ fieldsforbackup , $ handler ->get_instance_data_for_backup ($ group ->id ));
1452
1473
}
@@ -1456,14 +1477,16 @@ protected function get_group_custom_fields_for_backup(int $courseid): array {
1456
1477
1457
1478
/**
1458
1479
* Get custom fields array for grouping
1459
- * @param int $courseid
1480
+ *
1481
+ * @param string $groupingsourcesql
1482
+ * @param array $groupingsourceparams
1460
1483
* @return array
1461
1484
*/
1462
- protected function get_grouping_custom_fields_for_backup (int $ courseid ): array {
1485
+ protected function get_grouping_custom_fields_for_backup (string $ groupingsourcesql , array $ groupingsourceparams ): array {
1463
1486
global $ DB ;
1464
1487
$ handler = \core_group \customfield \grouping_handler::create ();
1465
1488
$ fieldsforbackup = [];
1466
- if ($ groupings = $ DB ->get_records ( ' groupings ' , [ ' courseid ' => $ courseid ], '' , ' id ' )) {
1489
+ if ($ groupings = $ DB ->get_records_sql ( $ groupingsourcesql , $ groupingsourceparams )) {
1467
1490
foreach ($ groupings as $ grouping ) {
1468
1491
$ fieldsforbackup = array_merge ($ fieldsforbackup , $ handler ->get_instance_data_for_backup ($ grouping ->id ));
1469
1492
}
0 commit comments