Skip to content

Commit

Permalink
MDL-82220 backup: ensure correct group/ing custom fields are included.
Browse files Browse the repository at this point in the history
We should include only the custom field values that belong to those
groups and/or groupings that are being backed up.
  • Loading branch information
paulholden committed Aug 16, 2024
1 parent 6cd5507 commit 27b384e
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1424,24 +1424,37 @@ protected function define_structure() {
FROM {groups} g
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
WHERE bi.backupid = ?
AND bi.itemname = 'groupfinal'", array(backup::VAR_BACKUPID));
AND bi.itemname = 'groupfinal'",
[backup_helper::is_sqlparam($this->get_backupid())]
);

$grouping->set_source_sql("
SELECT g.*
FROM {groupings} g
JOIN {backup_ids_temp} bi ON g.id = bi.itemid
WHERE bi.backupid = ?
AND bi.itemname = 'groupingfinal'", array(backup::VAR_BACKUPID));
AND bi.itemname = 'groupingfinal'",
[backup_helper::is_sqlparam($this->get_backupid())]
);

$groupinggroup->set_source_table('groupings_groups', array('groupingid' => backup::VAR_PARENTID));

// This only happens if we are including users.
if ($userinfo) {
$member->set_source_table('groups_members', array('groupid' => backup::VAR_PARENTID));
}

$courseid = $this->task->get_courseid();
$groupcustomfield->set_source_array($this->get_group_custom_fields_for_backup($courseid));
$groupingcustomfield->set_source_array($this->get_grouping_custom_fields_for_backup($courseid));
$groupcustomfieldarray = $this->get_group_custom_fields_for_backup(
$group->get_source_sql(),
[$this->get_backupid()]
);
$groupcustomfield->set_source_array($groupcustomfieldarray);

$groupingcustomfieldarray = $this->get_grouping_custom_fields_for_backup(
$grouping->get_source_sql(),
[$this->get_backupid()]
);
$groupingcustomfield->set_source_array($groupingcustomfieldarray);
}

// Define id annotations (as final)
Expand All @@ -1460,14 +1473,16 @@ protected function define_structure() {

/**
* Get custom fields array for group
* @param int $courseid
*
* @param string $groupsourcesql
* @param array $groupsourceparams
* @return array
*/
protected function get_group_custom_fields_for_backup(int $courseid): array {
protected function get_group_custom_fields_for_backup(string $groupsourcesql, array $groupsourceparams): array {
global $DB;
$handler = \core_group\customfield\group_handler::create();
$fieldsforbackup = [];
if ($groups = $DB->get_records('groups', ['courseid' => $courseid], '', 'id')) {
if ($groups = $DB->get_records_sql($groupsourcesql, $groupsourceparams)) {
foreach ($groups as $group) {
$fieldsforbackup = array_merge($fieldsforbackup, $handler->get_instance_data_for_backup($group->id));
}
Expand All @@ -1477,14 +1492,16 @@ protected function get_group_custom_fields_for_backup(int $courseid): array {

/**
* Get custom fields array for grouping
* @param int $courseid
*
* @param string $groupingsourcesql
* @param array $groupingsourceparams
* @return array
*/
protected function get_grouping_custom_fields_for_backup(int $courseid): array {
protected function get_grouping_custom_fields_for_backup(string $groupingsourcesql, array $groupingsourceparams): array {
global $DB;
$handler = \core_group\customfield\grouping_handler::create();
$fieldsforbackup = [];
if ($groupings = $DB->get_records('groupings', ['courseid' => $courseid], '', 'id')) {
if ($groupings = $DB->get_records_sql($groupingsourcesql, $groupingsourceparams)) {
foreach ($groupings as $grouping) {
$fieldsforbackup = array_merge($fieldsforbackup, $handler->get_instance_data_for_backup($grouping->id));
}
Expand Down

0 comments on commit 27b384e

Please sign in to comment.