Skip to content

Commit

Permalink
Simplify logic and fix auto populate bug on form deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
mbentz committed Aug 11, 2021
1 parent ba01745 commit 79eeb8c
Showing 1 changed file with 53 additions and 31 deletions.
84 changes: 53 additions & 31 deletions ExternalModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,48 @@
class ExternalModule extends AbstractExternalModule {
private $survey_APF_fields = [];

function isFirstRepeatEventOrForm() {
$val = ($this->isFirstRepeatEvent() || $this->isFirstRepeatForm());
return $val;
}

function isNthRepeatEventOrForm() {
$val = ($this->isNthRepeatEvent() || $this->isNthRepeatForm());
return $val;
}

/**
* Returns true if the event is either a "Repeat Entire Event" or "Repeat Instrument" repeating event.
* Returns true if the event is a "Repeat Entire Event (repeat all instruments together)" and is the first instance.
*/
function isRepeatEvent() {
$val = ($this->isRepeatEntireEvent() || $this->isRepeatInstrument());
return $val;
function isFirstRepeatEvent() {
if ($_GET['instance'] == 1 && $GLOBALS['isRepeatingEvent']) {
return true;
}
return false;
}

/**
* Returns true if the event is a "Repeat Entire Event (repeat all instruments together)" and is not first instance.
* 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.
function isNthRepeatEvent() {
if ($_GET['instance'] > 1 && $GLOBALS['isRepeatingEvent']) {
return true;
}
return false;
}

/**
* Returns true if the event is a "Repeat Instrument (repeat independently of each other)".
* Returns true if the event is a "Repeat Instrument (repeat independently of each other)" and is the first instance.
*/
function isRepeatInstrument() {
return $GLOBALS['isRepeatingForm'];
function isFirstRepeatForm() {
return ($_GET['instance'] == 1 && $GLOBALS['isRepeatingForm']);
}

/**
* Returns true if the event is a "Repeat Instrument (repeat independently of each other)" and is NOT the first instance.
*/
function isNthRepeatForm() {
return ($_GET['instance'] > 1 && $GLOBALS['isRepeatingForm']);
}

/**
Expand Down Expand Up @@ -192,29 +208,27 @@ 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
// For the first instance of a repeating event/form, the previous event is the closest saved event/form prior to the current event/form.
// For the nth instance of a repeating event/form, the previous event is the current event.
foreach ($events as $event) {
// 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()) {
if (!in_array($source_form, $Proj->eventsForms[$event])) {
break;
}

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 = "";
// Only break for the first instance of an event/form.
// First instance events or forms need data from previous events/forms, whereas, nth repeating events/forms depend on data from the current event i.e., $_GET['event_id']
if ($event == $_GET['event_id'] && $this->isFirstRepeatEventOrForm()) {
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
}

// Set the previous event for first instance of a repeat event/form to the previous event.
// Set the previous event for the nth instance of a repeat event/form to the current event.
if ($this->isFirstRepeatEventOrForm()) {
$prev_event = $event;
} elseif ($this->isNthRepeatEventOrForm()) {
$prev_event = $_GET['event_id'];
$form_name = $source_form;
break;
}
}
}

if (!$prev_event) {
Expand All @@ -225,12 +239,20 @@ function setDefaultValues() {
// 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];

// The object returned by $instances = $data['repeat_instances'][$event] changes dependening on if the data is from an event or a form.
// For repeating events $instances, the key for the returned object is an empty string ("") i.e., ["": ...]
// For repeating forms $instances, the key for the returned object is the form name i.e., ["baseline_data": ...]
$instances = ($data['repeat_instances'][$prev_event][$source_form]) ?? ($data['repeat_instances'][$prev_event][""]);

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])) {
} elseif (isset($instances)) {
// 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][$form_name], -1)[0];
$default_value = $most_recent_instance[$source_field];
// $instances are out of order when form data is deleted.
// In that case, using array_slice($instances, -1)[0] is unreliable and can return the instance
$max = max(array_keys($instances));
$default_value = $instances[$max][$source_field];
}

// Handling checkboxes case.
Expand Down

0 comments on commit 79eeb8c

Please sign in to comment.