Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for event instances #56

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 3 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)" and is not first instance.
*/
function isRepeatEntireEvent() {
// The first instance of Repeating events are treated differently from events where $_GET['instance'] > 1.
// For the first instance, we look at previous event. For $_GET['instance'] > 1, we look at the current event.
// Because $GLOBALS['isRepeatingEvent'] is true regardless of the instance of the event, we also check for the current instance.
if ($_GET['instance'] > 1 && $GLOBALS['isRepeatingEvent']) {
return true;
}
return false;
}

/**
* Returns true if the event is a "Repeat Instrument (repeat independently of each other)".
*/
function isRepeatInstrument() {
return $GLOBALS['isRepeatingForm'];
Copy link
Contributor

@ChemiKyle ChemiKyle Aug 5, 2021

Choose a reason for hiding this comment

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

@mbentz-uf This causes APF to fail to populate fields for an event that is after a repeating form.

image

If I revert this change, event 4 populates from the latest instance of event 3. A key bit of information is that I have event 4 set to "Repeat instrument".

}

/**
* @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