From f65e9555d74af958ec65094edc4b95281c32612d Mon Sep 17 00:00:00 2001 From: Mia Petkova Date: Thu, 20 Oct 2011 13:31:31 -0400 Subject: [PATCH 1/4] Changed TestBattery to accept CenterID as part of battery --- php/libraries/NDB_BVL_Battery.class.inc | 15 +++-- php/libraries/NDB_Form_next_stage.class.inc | 2 +- tools/assign_missing_instruments.php | 75 ++++++++++++--------- 3 files changed, 56 insertions(+), 36 deletions(-) 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 +?> From e453185b74d3a26eddd7d13b5bd8c1fbc2e7de3b Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Thu, 3 Nov 2011 15:12:11 -0400 Subject: [PATCH 2/4] Updated add_timepoint_instruments which uses proper CenterID --- tools/add_timepoint_instruments.php | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tools/add_timepoint_instruments.php diff --git a/tools/add_timepoint_instruments.php b/tools/add_timepoint_instruments.php new file mode 100644 index 00000000000..0f9d91a81a7 --- /dev/null +++ b/tools/add_timepoint_instruments.php @@ -0,0 +1,44 @@ +makeCommandLine(); +$client->initialize($configFile); + +$db =& Database::singleton(); +if(PEAR::isError($db)) { + fwrite(STDERR, "Could not connect to database: ".$db->getMessage()); + return false; +} + +$visit_ages = array( + 'v06' => 180, + 'v12' => 365, + 'v18' => 545, + 'v24' => 730 +); + +if($argc != 3) { + print "Usage: $argv[0] \n"; + exit(-1); +} +$vl = $argv[2]; +$CandID= $argv[1]; + +$fake_age = $visit_ages[$vl]; + +$session= $DB->pselectRow("SELECT ID, SubprojectID, CenterID FROM session WHERE CandID=:CandID AND Visit_label=:vl", array(':CandID' => $CandID, 'vl' => $vl)); +$SessionID = $session['ID']; +$Subproj = $session['SubprojectID']; +$Site = $session['CenterID'] +print_r($rows); +$rows = $DB->pselect("SELECT tb.Test_name FROM test_battery tb WHERE ((tb.AgeMinDays < :fake_age AND tb.AgeMaxDays > :fake_age) OR tb.Visit_label=:vl) AND tb.SubprojectID=:subproj AND NOT EXISTS (SELECT 'x' FROM flag f WHERE f.Test_name=tb.Test_name AND f.SessionID=:SessionID) AND (tb.CenterID=:site OR tb.CenterID IS NULL)", array(':fake_age' => $fake_age, ':vl' => $vl, ':subproj' => $Subproj, ':SessionID' => $SessionID, ':site' => $Site)); + +foreach($rows as $instrument) { + `php fix_timepoint_date_problems.php add_instrument $CandID $SessionID $instrument[Test_name]`; +} From c6235e0b46cbc71ca078dfe37c76070303bcca03 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Wed, 11 Jan 2012 10:26:42 -0500 Subject: [PATCH 3/4] Revert add of incorrectly added file This reverts commit e453185b74d3a26eddd7d13b5bd8c1fbc2e7de3b. --- tools/add_timepoint_instruments.php | 44 ----------------------------- 1 file changed, 44 deletions(-) delete mode 100644 tools/add_timepoint_instruments.php diff --git a/tools/add_timepoint_instruments.php b/tools/add_timepoint_instruments.php deleted file mode 100644 index 0f9d91a81a7..00000000000 --- a/tools/add_timepoint_instruments.php +++ /dev/null @@ -1,44 +0,0 @@ -makeCommandLine(); -$client->initialize($configFile); - -$db =& Database::singleton(); -if(PEAR::isError($db)) { - fwrite(STDERR, "Could not connect to database: ".$db->getMessage()); - return false; -} - -$visit_ages = array( - 'v06' => 180, - 'v12' => 365, - 'v18' => 545, - 'v24' => 730 -); - -if($argc != 3) { - print "Usage: $argv[0] \n"; - exit(-1); -} -$vl = $argv[2]; -$CandID= $argv[1]; - -$fake_age = $visit_ages[$vl]; - -$session= $DB->pselectRow("SELECT ID, SubprojectID, CenterID FROM session WHERE CandID=:CandID AND Visit_label=:vl", array(':CandID' => $CandID, 'vl' => $vl)); -$SessionID = $session['ID']; -$Subproj = $session['SubprojectID']; -$Site = $session['CenterID'] -print_r($rows); -$rows = $DB->pselect("SELECT tb.Test_name FROM test_battery tb WHERE ((tb.AgeMinDays < :fake_age AND tb.AgeMaxDays > :fake_age) OR tb.Visit_label=:vl) AND tb.SubprojectID=:subproj AND NOT EXISTS (SELECT 'x' FROM flag f WHERE f.Test_name=tb.Test_name AND f.SessionID=:SessionID) AND (tb.CenterID=:site OR tb.CenterID IS NULL)", array(':fake_age' => $fake_age, ':vl' => $vl, ':subproj' => $Subproj, ':SessionID' => $SessionID, ':site' => $Site)); - -foreach($rows as $instrument) { - `php fix_timepoint_date_problems.php add_instrument $CandID $SessionID $instrument[Test_name]`; -} From 36466286b26a86f0c46a8f7eb8eda90c6b1f5bf6 Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Wed, 11 Jan 2012 10:30:55 -0500 Subject: [PATCH 4/4] Update table schema for CenterID in test battery --- SQL/0000-00-00-schema.sql | 1 + SQL/2012-01-11-TestBatteryCenterID.sql | 1 + 2 files changed, 2 insertions(+) create mode 100644 SQL/2012-01-11-TestBatteryCenterID.sql 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;