Skip to content

Commit 4790a32

Browse files
committed
New method of validating a date before adding it to list of fields to save.
1 parent 75d2a50 commit 4790a32

File tree

1 file changed

+10
-46
lines changed

1 file changed

+10
-46
lines changed

DateCalculatedFieldsExternalModule.php

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
5353
# Determines whether we want to override data that already exists in a record
5454
$overwriteText = ($this->getProjectSetting('source-overwrite')[$index] == "1" ? "overwrite" : "normal");
5555
# Make sure that the field that we're piping was submitted on the record save
56-
if (in_array($fieldName,array_keys($_POST)) && $_POST[$fieldName] != "") {
56+
if (in_array($fieldName,array_keys($_POST)) && $_POST[$fieldName] != "" && $this->validateDate($_POST[$fieldName])) {
5757
foreach ($destinationFields[$index] as $destIndex => $destinationField) {
5858
$Locking = new Locking();
5959
$Locking->findLocked($Proj, $record, array($destinationField), ($longitudinal ? $events : $Proj->firstEventId));
@@ -108,45 +108,12 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
108108
$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->getDateFormat($Proj->metadata[$destinationField]['element_validation_type'],'php'));*/
109109
}
110110

111-
/*if ($daysOrMonths == "months") {
112-
$newMonth = $componentDate['month'] + $daysOffset;
113-
$newYear = $componentDate['year'];
114-
$newDay = $componentDate['day'];
115-
while ($newMonth > 12) {
116-
$newMonth -= 12;
117-
$newYear += 1;
118-
}
119-
if ($newMonth == 2) {
120-
if ($newYear % 4 == 0) {
121-
if ($newDay > 29) {
122-
$newDay = 29;
123-
}
124-
}
125-
else {
126-
if ($newDay > 28) {
127-
$newDay = 28;
128-
}
129-
}
130-
}
131-
elseif ($newDay > self::daysPerMonth[$newMonth]) {
132-
$newDay = self::daysPerMonth[$newMonth];
133-
}
134-
$newDate = new \DateTime($newYear."-".$newMonth."-".$newDay." ".$componentDate['hour'].":".$componentDate['minute'].":".$componentDate['second']);
135-
$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
136-
}
137-
else {
138-
//if ($currentEvent) {
139-
$newDate = date_add($postDate, date_interval_create_from_date_string($daysOffset . ' days'));
140-
$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
141-
//}
142-
}*/
143111
$newDate = $this->generateNewDate($postDate,$daysOrMonths,$daysOffset,$componentDate);
144-
try {
112+
113+
if (is_a($newDate,'DateTime')) {
145114
$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
146115
}
147-
catch (\Exception $e) {
148-
throw new \Exception("An invalid date was returned, giving the following error: ".$e->getMessage());
149-
}
116+
150117

151118
# Make sure whether we need to pipe into a "Start Date" date range field
152119
if ($this->getProjectSetting('event-start-date')[$index][$destIndex] != "") {
@@ -168,11 +135,8 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
168135
//$startDate = date_add($startDate, date_interval_create_from_date_string($startOffset . ' days'));
169136
$combinedOffset = (int)$daysOffset + (int)$startOffset;
170137
$startDate = $this->generateNewDate($postDate,$daysOrMonths,$combinedOffset,$componentDate);
171-
try {
138+
if (is_a($startDate,'DateTime')) {
172139
$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']));
173-
}
174-
catch (\Exception $e) {
175-
throw new \Exception("An invalid date was returned, giving the following error: ".$e->getMessage());
176140
}
177141
}
178142
}
@@ -195,12 +159,10 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
195159
//$endDate = date_add($endDate, date_interval_create_from_date_string((int)$endOffset . ' days'));
196160
$combinedOffset = (int)$daysOffset + (int)$endOffset;
197161
$endDate = $this->generateNewDate($postDate,$daysOrMonths,$combinedOffset,$componentDate);
198-
try {
162+
if (is_a($endDate,'DateTime')) {
199163
$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']));
200164
}
201-
catch (\Exception $e) {
202-
throw new \Exception("An invalid date was returned, giving the following error: ".$e->getMessage());
203-
}
165+
204166
}
205167
}
206168
/*if (!empty($fieldsToSave)) {
@@ -216,7 +178,9 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
216178
$componentDate = array('year'=>$postDate->format("Y"),'month'=>$postDate->format("m"),'day'=>$postDate->format('d'),'hour'=>$postDate->format('H'),'minute'=>$postDate->format('i'),'second'=>$postDate->format('s'));
217179
//$newDate = date_add($postDate,date_interval_create_from_date_string($daysAdd[$index][$destIndex].' days'));
218180
$newDate = $this->generateNewDate($postDate,$daysOrMonths,$daysAdd[$index][$destIndex],$componentDate);
219-
$fieldsToSave[$record][$event_id][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
181+
if (is_a($newDate,'DateTime')) {
182+
$fieldsToSave[$record][$event_id][$destinationField] = $newDate->format($this->dateSaveFormat($Proj->metadata[$destinationField]['element_validation_type']));
183+
}
220184
/*if (!empty($fieldsToSave)) {
221185
$output = \Records::saveData($project_id,'array',$fieldsToSave,$overwriteText);
222186
}*/

0 commit comments

Comments
 (0)