Skip to content

Commit 5a1933e

Browse files
author
mbentz
committed
Fix bug breaking 'Repeat Entire Event'
1 parent 5fd13de commit 5a1933e

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

ExternalModule.php

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@
1919
class ExternalModule extends AbstractExternalModule {
2020
private $survey_APF_fields = [];
2121

22+
/**
23+
* Returns true if the event is either a "Repeat Entire Event" or "Repeat Instrument" repeating event.
24+
*/
25+
function isRepeatEvent() {
26+
$val = ($this->isRepeatEntireEvent() || $this->isRepeatInstrument());
27+
return $val;
28+
}
29+
30+
/**
31+
* Returns true if the event is a "Repeat Entire Event (repeat all instruments together)".
32+
*/
33+
function isRepeatEntireEvent() {
34+
if ($_GET['instance'] > 1 && isset($_GET['oldinstance'])) {
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
/**
41+
* Returns true if the event is a "Repeat Instrument (repeat independently of each other)".
42+
*/
43+
function isRepeatInstrument() {
44+
if ($_GET['instance'] > 1 && !isset($_GET['oldinstance'])) {
45+
return true;
46+
}
47+
return false;
48+
}
49+
2250
/**
2351
* @inheritdoc
2452
*/
@@ -145,6 +173,7 @@ function setDefaultValues() {
145173

146174
$default_value = '';
147175

176+
$form_name = "";
148177
// Looping over @DEFAULT_<N> and @DEFAULT-FROM-PREVIOUS-EVENT_<N>
149178
// action tags.
150179
foreach ($action_tags as $action_tag) {
@@ -163,16 +192,28 @@ function setDefaultValues() {
163192
$source_form = $Proj->metadata[$source_field]['form_name'];
164193

165194
// Getting previous event ID.
195+
// For non repeating events the previous event is the closest event prior to the current event
196+
// For repeating events, (Repeat Entire Event & Repeat Instrument) the previous event is the current event
166197
foreach ($events as $event) {
167-
// Event instances share the same event id for every instance.
168-
// To support event instances we only break on the first instance, and allow execution for all future instances i.e., $_GET['instance'] greater than 1.
169-
// Event instances are enabled under: Project Setup > Enable optional modules and customizations > Repeatable instruments and events > 'Repeat instruments (repeat independently of each other)`
170-
if ($event == $_GET['event_id'] && $_GET['instance'] == 1) {
198+
// Only break for non repeating events.
199+
// Non repeating events should not get data from current event, whereas, repeating events with instances depend on data from the current event i.e., $_GET['event_id']
200+
if ($event == $_GET['event_id'] && !$this->isRepeatEvent()) {
171201
break;
172202
}
173203

174-
if (in_array($source_form, $Proj->eventsForms[$event])) {
204+
if (in_array($source_form, $Proj->eventsForms[$event]) && !$this->isRepeatEvent()) {
175205
$prev_event = $event;
206+
$form_name = "";
207+
} elseif (in_array($source_form, $Proj->eventsForms[$event]) && $this->isRepeatEntireEvent()) {
208+
$prev_event = $_GET['event_id'];
209+
$form_name = "";
210+
break;
211+
} elseif (in_array($source_form, $Proj->eventsForms[$event]) && $this->isRepeatInstrument()) {
212+
// Repeat instruments behave differently from 'Repeat Entire Event' and non repeating events.
213+
// Repeat instruments require the $Proj->metadata[$field_name]['form_name'] when looking up the data from the event
214+
$prev_event = $_GET['event_id'];
215+
$form_name = $source_form;
216+
break;
176217
}
177218
}
178219

@@ -186,9 +227,9 @@ function setDefaultValues() {
186227
$prev_event_field_value = $data[$prev_event][$source_field];
187228
if (isset($prev_event_field_value) && !empty($prev_event_field_value)) {
188229
$default_value = $prev_event_field_value;
189-
} elseif ($data['repeat_instances'][$prev_event][$source_form]) {
230+
} elseif (isset($data['repeat_instances'][$prev_event][$form_name])) {
190231
// Handling repeat events by using the most recent instance of the previous event to source values
191-
$most_recent_instance = array_slice($data['repeat_instances'][$prev_event][$source_form], -1)[0];
232+
$most_recent_instance = array_slice($data['repeat_instances'][$prev_event][$form_name], -1)[0];
192233
$default_value = $most_recent_instance[$source_field];
193234
}
194235

0 commit comments

Comments
 (0)