Skip to content

Commit 3dc2ff4

Browse files
committed
Bug fix for instance filtering
Bug fix for filter logic handling when instance table of current form. Change in v1.10.0 where references in filter data on current form does not work - is not required - when instance table is of the same form the user is viewing.
1 parent 107897f commit 3dc2ff4

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

InstanceTable.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function redcap_module_ajax($action, $payload, $project_id, $record, $ins
113113
$filter = $payload["filter"];
114114
$hideChoiceValues = (bool)$payload["hide_vals"];
115115
$hideFormStatus = (bool)$payload["hide_form_status"];
116-
$data = $this->getInstanceData($record, $event, $form, $fields, $filter, !$hideFormStatus, $hideChoiceValues);
116+
$data = $this->getInstanceData($record, $event, $form, $fields, $filter, $this->instrument, !$hideFormStatus, $hideChoiceValues);
117117
return $data;
118118
}
119119
}
@@ -448,7 +448,7 @@ protected function makeHtmlTable($repeatingFormDetails, $canEdit) {
448448

449449
// if survey form get data on page load (as no add/edit and have no auth for an ajax call)
450450
if ($this->isSurvey) {
451-
$instanceData = $this->getInstanceData($this->record, $eventId, $formName, $varList, $filter, false, $hideChoiceValues);
451+
$instanceData = $this->getInstanceData($this->record, $eventId, $formName, $varList, $filter, $this->instrument, false, $hideChoiceValues);
452452
if (count($instanceData) > 0) {
453453
$html.='<tbody>';
454454
foreach ($instanceData as $rowValues) {
@@ -489,7 +489,7 @@ protected function makeHtmlTable($repeatingFormDetails, $canEdit) {
489489
return $html;
490490
}
491491

492-
public function getInstanceData($record, $event, $form, $fields, $filter, $includeFormStatus=true, $hideChoiceValues=false) {
492+
public function getInstanceData($record, $event, $form, $fields, $filter, $formViewContext, $includeFormStatus=true, $hideChoiceValues=false) {
493493
global $Proj, $lang, $user_rights;
494494
$this->Proj = $Proj;
495495
$this->lang = &$lang;
@@ -527,9 +527,10 @@ public function getInstanceData($record, $event, $form, $fields, $filter, $inclu
527527

528528
if ($includeFormStatus) { $fields[] = $form.'_complete'; }
529529

530-
if (!empty($filter)) {
531-
// if context for instance table is a repeating form/event then replace any references to fields in this context in the logic expression with their current values
532-
// this prevents [current-instance] being automatically added by LogicTester::preformatLogicEventInstanceSmartVariable()
530+
if ($form!=$formViewContext && !empty($filter)) {
531+
// if instance table of a repeating form/event is being viewed on a (different) repeating form/event
532+
// then replace any references to fields from the current view context in the logic expression with their current values
533+
// this prevents [current-instance] being automatically added by LogicTester::preformatLogicEventInstanceSmartVariables()
533534
// and enables the filter to pick up instances of another form with values that match something in the current repeating context
534535
$filter = $this->replaceRepeatingContextValuesInLogicWithValues($filter);
535536
}
@@ -736,7 +737,7 @@ function init() {
736737
taggedField.lengthChange = false;
737738
break;
738739
default:
739-
taggedField.lengthVal = lengthLbl = [taggedField.page_size];
740+
taggedField.lengthVal = [taggedField.page_size];
740741
taggedField.lengthChange = false;
741742
}
742743
var thisTbl;
@@ -1080,8 +1081,9 @@ public function redcap_every_page_before_render($project_id) {
10801081

10811082
/**
10821083
* replaceRepeatingContextValuesInLogicWithValues
1083-
* if context for instance table is a repeating form/event then replace any references to fields in this context in the logic expression with their current values
1084-
* this prevents [current-instance] being automatically added by LogicTester::preformatLogicEventInstanceSmartVariable()
1084+
* if instance table of a repeating form/event is being viewed on a (different) repeating form/event
1085+
* then replace any references to fields from the current view context in the logic expression with their current values
1086+
* this prevents [current-instance] being automatically added by LogicTester::preformatLogicEventInstanceSmartVariables()
10851087
* and enables the filter to pick up instances of another form with values that match something in the current repeating context
10861088
* @param string $filter
10871089
* @return string
@@ -1109,18 +1111,18 @@ protected function replaceRepeatingContextValuesInLogicWithValues(string $filter
11091111
$currentContextData = \REDCap::getData('array', $this->record, $repeatingFields, $this->event_id);
11101112

11111113
foreach ($repeatingFields as $rf) {
1112-
if (!str_contains($filter, "[$rf]")) continue; // field not used in logic
1114+
if (!str_contains($filter, "[$rf]")) continue; // field not used in logic - ignore
11131115
$currentContextValue = $currentContextData[$this->record]['repeat_instances'][$this->event_id][$rptFormKey][$this->repeat_instance][$rf] ?? '';
11141116
$replaceValue = ($currentContextValue!=='' && (starts_with($this->Proj->metadata[$rf]['text_validation_type'], 'integer') || starts_with($this->Proj->metadata[$rf]['text_validation_type'], 'number')))
11151117
? "$currentContextValue"
11161118
: "'$currentContextValue'";
11171119

11181120
$replacePatterns = array();
11191121
if (\REDCap::isLongitudinal()) {
1120-
$replacePatterns[] = "/\[$currentEventName\]\[$rf\](\[current-instance\])?[\s=<>!]/";
1121-
$replacePatterns[] = "/\[current-event\]\[$rf\](\[current-instance\])?[\s=<>!]/";
1122+
$replacePatterns[] = "/\[$currentEventName\]\[$rf\](?:\[current-instance\])?(?=[\s=<>!])/";
1123+
$replacePatterns[] = "/\[current-event\]\[$rf\](?:\[current-instance\])?(?=[\s=<>!])/";
11221124
} else {
1123-
$replacePatterns[] = "/\[$rf\](\[current-instance\])?[\s=<>!]/";
1125+
$replacePatterns[] = "/\[$rf\](?:\[current-instance\])?(?=[\s=<>!])/";
11241126
}
11251127

11261128
$filter = preg_replace($replacePatterns, $replaceValue, $filter);

0 commit comments

Comments
 (0)