Skip to content

Commit f321d10

Browse files
authored
[Battery] Potential improvement for battery assignment (#7163)
The behaviour change in the battery was copied over from the assign_missing _instruments php script. This is not the ideal implementation but is sufficient to serve as a proposal up for discussion. Alternate solutions can include completely separate function to populate vs create a battery only allow to "fill in" battery if already existing instruments are surveys (not a prepopulated battery of instruments) behaviour could be added uniquely to next stage (although this limits the potential to allow users to have a button au run "assign missing instruments from the front end") ... other combinations... Fixes #7162
1 parent d549725 commit f321d10

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

php/libraries/NDB_BVL_Battery.class.inc

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,6 @@ class NDB_BVL_Battery
135135
);
136136
}
137137

138-
// assert that the current battery is empty
139-
if (count($currentTests) > 0) {
140-
throw new Exception(
141-
"Battery is not empty - will not clobber an existing battery."
142-
);
143-
}
144-
145138
// determine the list of instruments to register
146139
$neededTests = $this->lookupBattery(
147140
$this->age,
@@ -152,13 +145,34 @@ class NDB_BVL_Battery
152145
$firstVisit
153146
);
154147

148+
// assert that the current battery is empty
149+
if (count($currentTests) > 0) {
150+
$neededTests = $this->getBatteryDifference($currentTests, $neededTests);
151+
}
152+
155153
// loop over the list of instruments
156154
foreach ($neededTests AS $testName) {
157155
// add the instrument
158156
$this->addInstrument($testName);
159157
} // end looping over the list of instruments
160158
} // end createBattery()
161159

160+
/**
161+
* Computes the difference between the existing battery and the
162+
* theoretical battery and returns only the missing tests as an array
163+
*
164+
* @param array $currentBattery Existing battery at the specified timepoint
165+
* @param array $lookupBattery Theoretical battery from the test_battery table
166+
*
167+
* @return array
168+
*/
169+
function getBatteryDifference(
170+
array $currentBattery=[],
171+
array $lookupBattery=[]
172+
) : array {
173+
return array_unique(array_diff($lookupBattery, $currentBattery));
174+
}
175+
162176
/**
163177
* Adds the specified instrument to the current battery, so long
164178
* as it is not already there (singleton model)

0 commit comments

Comments
 (0)