Skip to content

Commit

Permalink
Support repeating field in source as record id in destination
Browse files Browse the repository at this point in the history
  • Loading branch information
lsgs committed Jul 21, 2022
1 parent 9b8ae67 commit be87897
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions CopyDataOnSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function redcap_save_record($project_id, $record=null, $instrument, $event_id, $
));

$destProj = new \Project($destProjectId);
$destRecord = $sourceProjectData[$record][$event_id][$recIdField];
$destRecord = $this->getDestinationRecordId($record, $event_id, $repeat_instance, $recIdField, $sourceProjectData);
if (empty($destEventName)) {
$destEventId = $destProj->firstEventId;
} else {
Expand Down Expand Up @@ -156,7 +156,8 @@ function redcap_save_record($project_id, $record=null, $instrument, $event_id, $
$detail = "Copy from: record=$record, event=$event_id, instrument=$instrument, instance=$repeat_instance";
$detail .= " \nCopy to: project_id=$destProjectId, record=$destRecord";

if (count($saveResult['errors'])>0) {
if ((is_array($saveResult['errors']) && count($saveResult['errors'])>0) ||
(!is_array($saveResult['errors']) && !empty($saveResult['errors'])) ) {
$title .= ": COPY FAILED ";
$detail .= " \n".print_r($saveResult['errors'], true);
\REDCap::logEvent($title, $detail, '', $record, $event_id);
Expand All @@ -166,4 +167,27 @@ function redcap_save_record($project_id, $record=null, $instrument, $event_id, $
}
}
}

/**
* setDestinationRecordId
* Get the record id to use in the destination from the source data - supports using field from repeating form/event
* @param string $record
* @param string $event_id
* @param string $instance
* @param string $recIdField
* @param string $sourceProjectData
* @return string
* @since 1.1.0
*/
protected function getDestinationRecordId($record, $event_id, $instance, $recIdField, $sourceProjectData) {
global $Proj;
if ($Proj->isRepeatingEvent($event_id)) {
$destRecordId = $sourceProjectData[$record]['repeat_instances'][$event_id][''][$instance][$recIdField];
} else if ($Proj->isRepeatingForm($event_id, $Proj->metadata[$recIdField]['form_name'])) {
$destRecordId = $sourceProjectData[$record]['repeat_instances'][$event_id][$Proj->metadata[$recIdField]['form_name']][$instance][$recIdField];
} else {
$destRecordId = $sourceProjectData[$record][$event_id][$recIdField];
}
return $destRecordId;
}
}

0 comments on commit be87897

Please sign in to comment.