diff --git a/SQL/0000-00-00-schema.sql b/SQL/0000-00-00-schema.sql index bc6fe9da221..9c1bc8a25f1 100644 --- a/SQL/0000-00-00-schema.sql +++ b/SQL/0000-00-00-schema.sql @@ -1251,6 +1251,7 @@ CREATE TABLE `test_battery` ( `Stage` varchar(255) default NULL, `SubprojectID` int(11) default NULL, `Visit_label` varchar(255) default NULL, + `CenterID` int(11) default NULL, PRIMARY KEY (`ID`), KEY `age_test` (`AgeMinDays`,`AgeMaxDays`,`Test_name`), KEY `FK_test_battery_1` (`Test_name`), diff --git a/SQL/2012-01-11-TestBatteryCenterID.sql b/SQL/2012-01-11-TestBatteryCenterID.sql new file mode 100644 index 00000000000..2f4445b0fe4 --- /dev/null +++ b/SQL/2012-01-11-TestBatteryCenterID.sql @@ -0,0 +1 @@ +ALTER TABLE test_battery ADD COLUMN `CenterID` int(11) default NULL; diff --git a/php/libraries/NDB_BVL_Battery.class.inc b/php/libraries/NDB_BVL_Battery.class.inc index ed7c490ff05..501d2c9c0ed 100644 --- a/php/libraries/NDB_BVL_Battery.class.inc +++ b/php/libraries/NDB_BVL_Battery.class.inc @@ -100,14 +100,14 @@ class NDB_BVL_Battery extends PEAR * @return void * @access public */ - function createBattery($SubprojectID, $stage=null,$visit_label=null) + function createBattery($SubprojectID, $stage=null, $visit_label=null, $center_ID=null) { // assert that a battery has already been selected $error = $this->_assertBatterySelected(); if($this->isError($error)) return $error; - // get the current battery - $currentTests = $this->getBattery($stage, $SubprojectID,$visit_label); + // get the current battery (center id is irrelevant here, as is stage probably) + $currentTests = $this->getBattery($stage, $SubprojectID, $visit_label); if($this->isError($currentTests)) { return $this->raiseError("Could not get the current list of instruments, thus unable to assert that the battery is empty"); } @@ -118,7 +118,7 @@ class NDB_BVL_Battery extends PEAR } // determine the list of instruments to register - $neededTests = $this->lookupBattery($this->age, $SubprojectID, $stage,$visit_label); + $neededTests = $this->lookupBattery($this->age, $SubprojectID, $stage, $visit_label, $center_ID); if($this->isError($neededTests)) { return $this->raiseError("Could not lookup the battery for age $this->age and subprojectID ".$this->getSubprojectID()); } @@ -356,7 +356,7 @@ class NDB_BVL_Battery extends PEAR * @return array an array of instrument names * @access public */ - function lookupBattery($age, $SubprojectID, $stage=null, $visit_label=null) + function lookupBattery($age, $SubprojectID, $stage=null, $visit_label=null, $center_ID=null) { if(empty($this->age)) $this->age = $age; // The query to lookup the battery uses the min/max age ranges if they are not 0 @@ -374,6 +374,11 @@ class NDB_BVL_Battery extends PEAR $query .= " AND b.Stage='$stage'"; } + if(!is_null($center_ID)) { // return all instruments that match this center or all centers (null) + $query .= " AND (b.CenterID='$center_ID' OR b.CenterID IS NULL)"; + } else { + $query .= " AND b.CenterID IS NULL"; // Unless the center is specified & matched, don't return site-restricted instruments + } // get the list of instruments $rows = array(); diff --git a/php/libraries/NDB_Form_next_stage.class.inc b/php/libraries/NDB_Form_next_stage.class.inc index c92d58da0e4..95d97932968 100644 --- a/php/libraries/NDB_Form_next_stage.class.inc +++ b/php/libraries/NDB_Form_next_stage.class.inc @@ -53,7 +53,7 @@ class NDB_Form_next_stage extends NDB_Form $battery->selectBattery($timePoint->getData('SessionID')); // add instruments to the time point (lower case stage) - $battery->createBattery($timePoint->getSubprojectID(), $newStage, $timePoint->getVisitLabel()); + $battery->createBattery($timePoint->getSubprojectID(), $newStage, $timePoint->getVisitLabel(), $timePoint->getCenterID()); //------------------------------------------------------------ diff --git a/tools/assign_missing_instruments.php b/tools/assign_missing_instruments.php index d480154bbd2..e0e166b1c82 100644 --- a/tools/assign_missing_instruments.php +++ b/tools/assign_missing_instruments.php @@ -36,39 +36,54 @@ $query="SELECT ID, subprojectID from session"; if(!empty($argv[1]) && $argv[1]!="confirm"){ $query.=" WHERE visit_label='$argv[1]'"; +} else { + $visit_labels = $DB->pselect("SELECT DISTINCT Visit_label FROM session WHERE Active='Y' AND Cancelled='N' AND Visit_label NOT LIKE '%phantom%' AND Visit_label NOT LIKE 'Vsup%'", array()); } -$DB->select($query, $results); -foreach($results AS $result){ - // create a new battery object && new battery - $battery =& new NDB_BVL_Battery; - - // select a specific time point (sessionID) for the battery - $battery->selectBattery($result['ID']); - $timePoint =& TimePoint::singleton($result['ID']); - - //To assign missing instruments to all sessions, sent to DCC or not. - $defined_battery=$battery->lookupBattery($battery->age, $result['subprojectID'], $timePoint->getCurrentStage()); - $actual_battery=$battery->getBattery($timePoint->getCurrentStage()); - - //To assign missing instruments only for sessions in the Visit stage... -// $defined_battery=$battery->lookupBattery($battery->age, $result['subprojectID'], 'Visit'); -// $actual_battery=$battery->getBattery('Visit'); - - $diff=array_diff($defined_battery, $actual_battery); - if(!empty($diff)){ - echo "\n CandID: ".$timePoint->getCandID()." Visit Label: ".$timePoint->getVisitLabel()."\nMissing Instruments:\n"; - print_r($diff); - } - if($argv[1]=="confirm" || $argv[2]=="confirm"){ - foreach($diff AS $test_name){ - $battery->addInstrument($test_name); - } + +function PopulateVisitLabel($result, $visit_label) { + global $argv; + // create a new battery object && new battery + $battery =& new NDB_BVL_Battery; + + // select a specific time point (sessionID) for the battery + $battery->selectBattery($result['ID']); + $timePoint =& TimePoint::singleton($result['ID']); + + //To assign missing instruments to all sessions, sent to DCC or not. + $defined_battery=$battery->lookupBattery($battery->age, $result['subprojectID'], $timePoint->getCurrentStage(), $visit_label, $timePoint->getCenterID()); + $actual_battery=$battery->getBattery($timePoint->getCurrentStage(), $result['subprojectID']); + + $diff=array_diff($defined_battery, $actual_battery); + if(!empty($diff)){ + echo "\n CandID: ".$timePoint->getCandID()." Visit Label: ".$timePoint->getVisitLabel()."\nMissing Instruments:\n"; + print_r($diff); + } + if($argv[1]=="confirm" || $argv[2]=="confirm"){ + foreach($diff AS $test_name){ + $battery->addInstrument($test_name); } - - unset($battery); - unset($timePoint); + } + + unset($battery); + unset($timePoint); + } + +if(isset($visit_label)) { + $query="SELECT s.ID, s.subprojectID from session s LEFT JOIN candidate c USING (CandID) WHERE s.Active='Y' AND s.Cancelled='N' AND c.Active='Y' AND s.Cancelled='N' AND s.visit_label='$argv[1]'"; + $DB->select($query, $results); + foreach($results AS $result){ + PopulateVisitLabel($result, $visit_label); + } +} else if (isset($visit_labels)) { + $query="SELECT s.ID, s.subprojectID, s.Visit_label from session s LEFT JOIN candidate c USING (CandID) WHERE s.Active='Y' AND s.Cancelled='N' AND c.Active='Y' AND s.Cancelled='N' AND s.Visit_label NOT LIKE 'Vsup%'"; + $DB->select($query, $results); + foreach($results AS $result) { + PopulateVisitLabel($result, $result['Visit_label']); + } +} + if($argv[1]!="confirm" && $argv[2]!="confirm"){ echo "\n\nRun this tool again with the argument 'confirm' to perform the changes\n\n"; } -?> \ No newline at end of file +?>