Skip to content

Commit ce026a4

Browse files
committed
Adding ability to save to repeating instruments. Defaults to first instance, need to allow for adding new instances instead.
1 parent 1a8a655 commit ce026a4

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

DateCalculatedFieldsExternalModule.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $g
2828
echo "<pre>";
2929
print_r($metaData);
3030
echo "</pre>";*/
31+
$recordData = \Records::getData($project_id,'array',array($record),array(),array(),array($group_id));
32+
3133
echo $this->createCalcuationJava($Proj,$instrument,$record,$event_id,$repeat_instance);
3234
}
3335

@@ -103,7 +105,8 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
103105
if ($eventToPipe == $event_id && in_array($destinationField,$fieldsOnForm)) continue;
104106

105107
$eventInfo = $Proj->eventInfo[$eventToPipe];
106-
$daysOffset = "";
108+
109+
$daysOffset = "";
107110
# If we don't specify the number of days to add per event in the project, use the project's event days offset setting
108111
if ($daysAdd[$index][$destIndex] != "") {
109112
//$daysOffset = $daysAdd[$index][$destIndex] * (($eventIndex - $currentEventIndex)+1);
@@ -120,7 +123,8 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
120123
$newDate = $this->generateNewDate($postDate,$daysOrMonths,$daysOffset,$componentDate);
121124

122125
if (is_a($newDate,'DateTime')) {
123-
$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
126+
//$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
127+
$this->setSaveData($fieldsToSave,$Proj,$record,$destinationField,$newDate,$eventToPipe,$repeat_instance);
124128
}
125129

126130

@@ -145,7 +149,8 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
145149
$combinedOffset = (int)$daysOffset + (int)$startOffset;
146150
$startDate = $this->generateNewDate($postDate,$daysOrMonths,$combinedOffset,$componentDate);
147151
if (is_a($startDate,'DateTime')) {
148-
$fieldsToSave[$record][$eventToPipe][$this->getProjectSetting('event-start-date')[$index][$destIndex]] = $startDate->format($this->dateSaveFormat($Proj->metadata[$this->getProjectSetting('event-start-date')[$index][$destIndex]]['element_validation_type']));
152+
$startDateField = $this->getProjectSetting('event-start-date')[$index][$destIndex];
153+
$this->setSaveData($fieldsToSave,$Proj,$record,$startDateField,$startDate,$eventToPipe,$repeat_instance);
149154
}
150155
}
151156
}
@@ -169,7 +174,9 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
169174
$combinedOffset = (int)$daysOffset + (int)$endOffset;
170175
$endDate = $this->generateNewDate($postDate,$daysOrMonths,$combinedOffset,$componentDate);
171176
if (is_a($endDate,'DateTime')) {
172-
$fieldsToSave[$record][$eventToPipe][$this->getProjectSetting('event-end-date')[$index][$destIndex]] = $endDate->format($this->dateSaveFormat($Proj->metadata[$this->getProjectSetting('event-end-date')[$index][$destIndex]]['element_validation_type']));
177+
$endDateField = $this->getProjectSetting('event-end-date')[$index][$destIndex];
178+
//$fieldsToSave[$record][$eventToPipe][$this->getProjectSetting('event-end-date')[$index][$destIndex]] = $endDate->format($this->dateSaveFormat($Proj->metadata[$this->getProjectSetting('event-end-date')[$index][$destIndex]]['element_validation_type']));
179+
$this->setSaveData($fieldsToSave,$Proj,$record,$endDate,$endDate,$eventToPipe,$repeat_instance);
173180
}
174181

175182
}
@@ -188,7 +195,8 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
188195
//$newDate = date_add($postDate,date_interval_create_from_date_string($daysAdd[$index][$destIndex].' days'));
189196
$newDate = $this->generateNewDate($postDate,$daysOrMonths,$daysAdd[$index][$destIndex],$componentDate);
190197
if (is_a($newDate,'DateTime')) {
191-
$fieldsToSave[$record][$event_id][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
198+
//$fieldsToSave[$record][$event_id][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
199+
$this->setSaveData($fieldsToSave,$Proj,$record,$destinationField,$newDate,$event_id,$repeat_instance);
192200
}
193201
/*if (!empty($fieldsToSave)) {
194202
$output = \Records::saveData($project_id,'array',$fieldsToSave,$overwriteText);
@@ -781,4 +789,20 @@ function validateDate($date,$format='Y-m-d') {
781789
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
782790
return $d && $d->format($format) === $date;
783791
}
792+
793+
function setSaveData(&$fieldsToSave,$project,$record,$fieldName,$date,$event_id,$repeat_instance = 1) {
794+
$destFieldForm = $project->metadata[$fieldName]['form_name'];
795+
$destEventRepeats = $project->isRepeatingEvent($event_id);
796+
$destInstrumentRepeats = $project->isRepeatingForm($event_id, $destFieldForm);
797+
798+
if ($destEventRepeats) {
799+
$fieldsToSave[$record]['repeat_instances'][$event_id][][$repeat_instance][$fieldName] = $date->format($this->dateSaveFormat($project->metadata[$fieldName]['element_validation_type']));
800+
}
801+
elseif ($destInstrumentRepeats) {
802+
$fieldsToSave[$record]['repeat_instances'][$event_id][$destFieldForm][$repeat_instance][$fieldName] = $date->format($this->dateSaveFormat($project->metadata[$fieldName]['element_validation_type']));
803+
}
804+
else {
805+
$fieldsToSave[$record][$event_id][$fieldName] = $date->format($this->dateSaveFormat($project->metadata[$fieldName]['element_validation_type']));
806+
}
807+
}
784808
}

0 commit comments

Comments
 (0)