Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions ExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@
class ExternalModule extends AbstractExternalModule {
private $survey_APF_fields = [];

/**
* Returns true if the event is either a "Repeat Entire Event" or "Repeat Instrument" repeating event.
*/
function isRepeatEvent() {
$val = ($this->isRepeatEntireEvent() || $this->isRepeatInstrument());
return $val;
}

/**
* Returns true if the event is a "Repeat Entire Event (repeat all instruments together)".
*/
function isRepeatEntireEvent() {
if ($_GET['instance'] > 1 && isset($_GET['oldinstance'])) {
Copy link
Contributor

@ChemiKyle ChemiKyle Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there, but still having some inconsistency. When creating the second instance of a repeat event (repeat entire event), the fields don't autopopulate with the prior event. I think this line is the cause

When I create the 2nd instance of an event on a new record there is no oldinstance param, so this condition doesn't fire. The third instance (and probably onward) works as expected.


To recreate

  1. Start a new record, fill in values for events 1 and 2.
  2. Observe the initial instance of event 3 will autopopulate with event 2 info.
  3. Create a new instance of event 3, observe that data does not autopopulate
  • Fill in some info here, save and create a 3rd instance; observe that the 3rd instance does autopopulate from the 2nd.

return true;
}
return false;
}

/**
* Returns true if the event is a "Repeat Instrument (repeat independently of each other)".
*/
function isRepeatInstrument() {
if ($_GET['instance'] > 1 && !isset($_GET['oldinstance'])) {
return true;
}
return false;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -145,6 +173,7 @@ function setDefaultValues() {

$default_value = '';

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

// Getting previous event ID.
// For non repeating events the previous event is the closest event prior to the current event
// For repeating events, (Repeat Entire Event & Repeat Instrument) the previous event is the current event
foreach ($events as $event) {
if ($event == $_GET['event_id']) {
// Only break for non repeating events.
// 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']
if ($event == $_GET['event_id'] && !$this->isRepeatEvent()) {
break;
}

if (in_array($source_form, $Proj->eventsForms[$event])) {
if (in_array($source_form, $Proj->eventsForms[$event]) && !$this->isRepeatEvent()) {
$prev_event = $event;
$form_name = "";
} elseif (in_array($source_form, $Proj->eventsForms[$event]) && $this->isRepeatEntireEvent()) {
$prev_event = $_GET['event_id'];
$form_name = "";
break;
} elseif (in_array($source_form, $Proj->eventsForms[$event]) && $this->isRepeatInstrument()) {
// Repeat instruments behave differently from 'Repeat Entire Event' and non repeating events.
// Repeat instruments require the $Proj->metadata[$field_name]['form_name'] when looking up the data from the event
$prev_event = $_GET['event_id'];
$form_name = $source_form;
break;
}
}

Expand All @@ -178,11 +222,14 @@ function setDefaultValues() {
}

// Getting previous event value.
if (isset($data[$prev_event][$source_field])) {
$default_value = $data[$prev_event][$source_field];
} elseif (isset($data['repeat_instances'][$prev_event][""])) {
// isset returns true for event instances when $prev_event_field_value is equal to an empty string ("").
// An additional check to verify the value is not empty is required.
$prev_event_field_value = $data[$prev_event][$source_field];
if (isset($prev_event_field_value) && !empty($prev_event_field_value)) {
$default_value = $prev_event_field_value;
} elseif (isset($data['repeat_instances'][$prev_event][$form_name])) {
// Handling repeat events by using the most recent instance of the previous event to source values
$most_recent_instance = array_slice($data['repeat_instances'][$prev_event][""], -1)[0];
$most_recent_instance = array_slice($data['repeat_instances'][$prev_event][$form_name], -1)[0];
$default_value = $most_recent_instance[$source_field];
}

Expand Down