@@ -53,7 +53,7 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
53
53
# Determines whether we want to override data that already exists in a record
54
54
$ overwriteText = ($ this ->getProjectSetting ('source-overwrite ' )[$ index ] == "1 " ? "overwrite " : "normal " );
55
55
# 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 ]) ) {
57
57
foreach ($ destinationFields [$ index ] as $ destIndex => $ destinationField ) {
58
58
$ Locking = new Locking ();
59
59
$ 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
108
108
$fieldsToSave[$record][$eventToPipe][$destinationField] = $newDate->format($this->getDateFormat($Proj->metadata[$destinationField]['element_validation_type'],'php'));*/
109
109
}
110
110
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
- }*/
143
111
$ newDate = $ this ->generateNewDate ($ postDate ,$ daysOrMonths ,$ daysOffset ,$ componentDate );
144
- try {
112
+
113
+ if (is_a ($ newDate ,'DateTime ' )) {
145
114
$ fieldsToSave [$ record ][$ eventToPipe ][$ destinationField ] = $ newDate ->format ($ this ->dateSaveFormat ($ Proj ->metadata [$ destinationField ]['element_validation_type ' ]));
146
115
}
147
- catch (\Exception $ e ) {
148
- throw new \Exception ("An invalid date was returned, giving the following error: " .$ e ->getMessage ());
149
- }
116
+
150
117
151
118
# Make sure whether we need to pipe into a "Start Date" date range field
152
119
if ($ this ->getProjectSetting ('event-start-date ' )[$ index ][$ destIndex ] != "" ) {
@@ -168,11 +135,8 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
168
135
//$startDate = date_add($startDate, date_interval_create_from_date_string($startOffset . ' days'));
169
136
$ combinedOffset = (int )$ daysOffset + (int )$ startOffset ;
170
137
$ startDate = $ this ->generateNewDate ($ postDate ,$ daysOrMonths ,$ combinedOffset ,$ componentDate );
171
- try {
138
+ if ( is_a ( $ startDate , ' DateTime ' )) {
172
139
$ 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 ());
176
140
}
177
141
}
178
142
}
@@ -195,12 +159,10 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
195
159
//$endDate = date_add($endDate, date_interval_create_from_date_string((int)$endOffset . ' days'));
196
160
$ combinedOffset = (int )$ daysOffset + (int )$ endOffset ;
197
161
$ endDate = $ this ->generateNewDate ($ postDate ,$ daysOrMonths ,$ combinedOffset ,$ componentDate );
198
- try {
162
+ if ( is_a ( $ endDate , ' DateTime ' )) {
199
163
$ 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 ' ]));
200
164
}
201
- catch (\Exception $ e ) {
202
- throw new \Exception ("An invalid date was returned, giving the following error: " .$ e ->getMessage ());
203
- }
165
+
204
166
}
205
167
}
206
168
/*if (!empty($fieldsToSave)) {
@@ -216,7 +178,9 @@ function redcap_save_record($project_id, $record, $instrument, $event_id, $group
216
178
$ 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 ' ));
217
179
//$newDate = date_add($postDate,date_interval_create_from_date_string($daysAdd[$index][$destIndex].' days'));
218
180
$ 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
+ }
220
184
/*if (!empty($fieldsToSave)) {
221
185
$output = \Records::saveData($project_id,'array',$fieldsToSave,$overwriteText);
222
186
}*/
0 commit comments