diff --git a/SQL/0000-00-06-BiobankTables.sql b/SQL/0000-00-06-BiobankTables.sql index b7cd3b8bf25..311fc8b4d1b 100644 --- a/SQL/0000-00-06-BiobankTables.sql +++ b/SQL/0000-00-06-BiobankTables.sql @@ -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`) @@ -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); diff --git a/SQL/New_patches/2025-10-29_add-order-index-to-specimen-protocols.sql b/SQL/New_patches/2025-10-29_add-order-index-to-specimen-protocols.sql new file mode 100644 index 00000000000..29f1ecd91c3 --- /dev/null +++ b/SQL/New_patches/2025-10-29_add-order-index-to-specimen-protocols.sql @@ -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; diff --git a/modules/biobank/php/optionsendpoint.class.inc b/modules/biobank/php/optionsendpoint.class.inc index 7ee3daa5e10..908028daa46 100644 --- a/modules/biobank/php/optionsendpoint.class.inc +++ b/modules/biobank/php/optionsendpoint.class.inc @@ -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'] @@ -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 diff --git a/modules/biobank/php/specimendao.class.inc b/modules/biobank/php/specimendao.class.inc index f0f111a9701..ac4af15ac82 100644 --- a/modules/biobank/php/specimendao.class.inc +++ b/modules/biobank/php/specimendao.class.inc @@ -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, @@ -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 @@ -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]);