Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SQL/0000-00-06-BiobankTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ CREATE TABLE `biobank_specimen_protocol_attribute_rel` (
`SpecimenAttributeID` int(10) unsigned NOT NULL,
`Required` tinyint(1) DEFAULT NULL,
`showInDataTable` tinyint(1) DEFAULT NULL,
`OrderIndex` INT UNSIGNED NOT NULL,
PRIMARY KEY (`SpecimenProtocolID`,`SpecimenAttributeID`),
UNIQUE KEY `UK_SpecimenProtocolId_OrderIndex` (`SpecimenProtocolID`, `OrderIndex`),
KEY `FK_biobank_specimen_protocol_attribute_rel_SpecimenAttributeID` (`SpecimenAttributeID`),
CONSTRAINT `FK_biobank_specimen_protocol_attribute__rel_SpecimenProtocolID` FOREIGN KEY (`SpecimenProtocolID`) REFERENCES `biobank_specimen_protocol` (`SpecimenProtocolID`),
CONSTRAINT `FK_biobank_specimen_protocol_attribute_rel_SpecimenAttributeID` FOREIGN KEY (`SpecimenAttributeID`) REFERENCES `biobank_specimen_attribute` (`SpecimenAttributeID`)
Expand Down Expand Up @@ -399,6 +401,7 @@ VALUES (1,'Blood Collection',1,1),
INSERT INTO `biobank_specimen_type_unit_rel` VALUES (1,1),(2,2);
INSERT INTO `biobank_specimen_type_parent` VALUES (2,1);
INSERT INTO `biobank_specimen_type_container_type_rel` VALUES (1,1),(2,1);
INSERT INTO `shipment_type` VALUES (1, 'Gel Pack Container'),(2, 'Insulated Foam Box'),(3, 'Dry Shipper');

-- Insert ConfigSettings for label printing endpoint
INSERT INTO ConfigSettings (Name, Description, Visible, AllowMultiple, Label, OrderNumber) VALUES ('biobank', 'Settings related to the biobank module', 1, 0, 'Biobank', 16);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Step 1: Add the column allowing NULL (temporarily)
-- This allows the operation to succeed on a table that already has existing rows.
ALTER TABLE biobank_specimen_protocol_attribute_rel
ADD COLUMN OrderIndex INT UNSIGNED NULL;

-- Step 2: Populate the existing rows with unique, non-NULL OrderIndex values.
-- This uses a variable-based method to assign a sequential number (0, 1, 2, ...)
-- to each row within the same SpecimenProtocolID group.

SET @r := -1;
SET @g := 0;

-- A temporary table/derived table is used to calculate the unique index for each group
UPDATE biobank_specimen_protocol_attribute_rel AS t1
INNER JOIN (
SELECT
t2.SpecimenProtocolID,
t2.SpecimenAttributeID,
@r := CASE WHEN @g = t2.SpecimenProtocolID THEN @r + 1 ELSE 0 END AS new_OrderIndex,
@g := t2.SpecimenProtocolID AS current_group
FROM
biobank_specimen_protocol_attribute_rel AS t2
ORDER BY
t2.SpecimenProtocolID, t2.SpecimenAttributeID

) AS ranked_data
ON t1.SpecimenProtocolID = ranked_data.SpecimenProtocolID
AND t1.SpecimenAttributeID = ranked_data.SpecimenAttributeID
SET t1.OrderIndex = ranked_data.new_OrderIndex;

-- Step 3: Enforce the constraints (NOT NULL and UNIQUE).
-- Now that every row has a valid, unique value, these operations will succeed.

-- 3a. Add the UNIQUE Constraint
ALTER TABLE biobank_specimen_protocol_attribute_rel
ADD CONSTRAINT UK_SpecimenProtocolId_OrderIndex
UNIQUE (SpecimenProtocolID, OrderIndex);

-- 3b. Change the Column to NOT NULL
ALTER TABLE biobank_specimen_protocol_attribute_rel
MODIFY COLUMN OrderIndex INT UNSIGNED NOT NULL;
39 changes: 26 additions & 13 deletions modules/biobank/php/optionsendpoint.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,25 @@ class OptionsEndpoint extends \NDB_Page implements RequestHandlerInterface
// TODO: This should eventually be replaced by a call directly to a
// Candidate endpoint or Candidate controller that will be able to
// provide Candidate Objects.
$query = "SELECT c.CandID as id,
PSCID as pscid,
Sex as sex,
GROUP_CONCAT(DISTINCT DiagnosisID) as diagnosisIds,
GROUP_CONCAT(DISTINCT s.ID) as sessionIds
FROM candidate c
LEFT JOIN session s USING (CandID)
LEFT JOIN candidate_diagnosis_rel USING (CandID)
WHERE s.CenterID IN ($userCenters)
AND s.ProjectID IN ($userProjects)
GROUP BY
CandID";
$query = "
SELECT
c.CandID as id,
PSCID as pscid,
Sex as sex,
GROUP_CONCAT(DISTINCT DxEvolutionID) as diagnosisIds,
GROUP_CONCAT(DISTINCT s.ID) as sessionIds
FROM
candidate c
LEFT JOIN session s
ON s.CandidateID=c.ID
LEFT JOIN candidate_diagnosis_evolution_rel cder
ON cder.CandidateID=c.ID
WHERE
s.CenterID IN ($userCenters)
AND s.ProjectID IN ($userProjects)
GROUP BY
CandID
";
$candidates = $db->pselectWithIndexKey($query, [], 'id');
foreach ($candidates as $id => $candidate) {
$candidates[$id]['diagnosisIds'] = $candidate['diagnosisIds']
Expand All @@ -163,7 +170,13 @@ class OptionsEndpoint extends \NDB_Page implements RequestHandlerInterface
// XXX: This should eventually be replaced by a call directly to a
// Candidate endpoint or Candidate controller that will be able to
// provide Diagnosis Options.
$query = 'SELECT DiagnosisID as id, Name as label FROM diagnosis';
$query = '
SELECT
DxEvolutionID as id,
Name as label
FROM
diagnosis_evolution
';
$diagnoses = $db->pselectWithIndexKey($query, [], 'id');

$sessionQuery = "SELECT
Expand Down
6 changes: 3 additions & 3 deletions modules/biobank/php/specimendao.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SpecimenDAO extends \LORIS\Data\ProvisionerInstance
as ParentSpecimenIDs,
GROUP_CONCAT(DISTINCT bc2.Barcode)
as ParentSpecimenBarcodes,
s.CandID as CandidateID,
s.CandidateID,
c.PSCID as CandidatePSCID,
bs.SessionID,
s.CenterID as SessionCenterID,
Expand Down Expand Up @@ -154,7 +154,7 @@ class SpecimenDAO extends \LORIS\Data\ProvisionerInstance
LEFT JOIN session s
ON bs.SessionID=s.ID
LEFT JOIN candidate c
ON s.CandID=c.CandID
ON s.CandidateID=c.ID
LEFT JOIN biobank_specimen_pool_rel bspor
ON bs.SpecimenID=bspor.SpecimenID
LEFT JOIN biobank_specimen_collection bsc
Expand Down Expand Up @@ -561,7 +561,7 @@ class SpecimenDAO extends \LORIS\Data\ProvisionerInstance
SELECT IFNULL(MAX(bs.SampleNumber), 0) AS max_sample_number
FROM biobank_specimen bs
JOIN session s ON bs.SessionID = s.ID
WHERE s.CandID = :candId
WHERE s.CandidateID = :candId
";

$sampleNumber = $this->db->pselectOneInt($query, ['candId' => $candId]);
Expand Down
Loading