diff --git a/app/datatables/protocols_datatable.rb b/app/datatables/protocols_datatable.rb index 711b40eff..81c5cc7ec 100644 --- a/app/datatables/protocols_datatable.rb +++ b/app/datatables/protocols_datatable.rb @@ -177,16 +177,14 @@ class ProtocolsDatatable < CustomDatatable .joins("LEFT OUTER JOIN protocols protocol_versions " \ "ON protocol_versions.protocol_type = #{Protocol.protocol_types[:in_repository_published_version]} " \ "AND protocol_versions.parent_id = protocols.parent_id") - .joins("LEFT OUTER JOIN protocols self_linked_task_protocols " \ - "ON self_linked_task_protocols.protocol_type = #{Protocol.protocol_types[:linked]} " \ - "AND self_linked_task_protocols.parent_id = protocols.id") - .joins("LEFT OUTER JOIN protocols parent_linked_task_protocols " \ - "ON parent_linked_task_protocols.protocol_type = #{Protocol.protocol_types[:linked]} " \ - "AND parent_linked_task_protocols.parent_id = protocols.parent_id") - .joins("LEFT OUTER JOIN protocols version_linked_task_protocols " \ - "ON version_linked_task_protocols.protocol_type = #{Protocol.protocol_types[:linked]} " \ - "AND version_linked_task_protocols.parent_id = protocol_versions.id " \ - "AND version_linked_task_protocols.parent_id != protocols.id") + .joins("LEFT OUTER JOIN protocols protocol_originals " \ + "ON protocol_originals.protocol_type = #{Protocol.protocol_types[:in_repository_published_original]} " \ + "AND protocol_originals.id = protocols.parent_id OR " \ + "(protocols.id = protocol_originals.id AND protocols.parent_id IS NULL)") + .joins("LEFT OUTER JOIN protocols linked_task_protocols " \ + "ON linked_task_protocols.protocol_type = #{Protocol.protocol_types[:linked]} " \ + "AND (linked_task_protocols.parent_id = protocol_versions.id OR " \ + "linked_task_protocols.parent_id = protocol_originals.id)") .joins('LEFT OUTER JOIN "protocol_protocol_keywords" ' \ 'ON "protocol_protocol_keywords"."protocol_id" = "protocols"."id"') .joins('LEFT OUTER JOIN "protocol_keywords" ' \ @@ -206,9 +204,7 @@ class ProtocolsDatatable < CustomDatatable "CASE WHEN protocols.protocol_type = #{Protocol.protocol_types[:in_repository_draft]} " \ "THEN 0 ELSE COUNT(DISTINCT(\"protocol_versions\".\"id\")) + 1 " \ "END AS nr_of_versions", - '(COUNT(DISTINCT("self_linked_task_protocols"."id")) + ' \ - 'COUNT(DISTINCT("parent_linked_task_protocols"."id")) + ' \ - 'COUNT(DISTINCT("version_linked_task_protocols"."id"))) AS nr_of_linked_tasks', + 'COUNT(DISTINCT("linked_task_protocols"."id")) AS nr_of_linked_tasks', 'COUNT(DISTINCT("all_user_assignments"."id")) AS "nr_of_assigned_users"', 'MAX("users"."full_name") AS "full_username_str"', # "Hack" to get single username 'MAX("archived_users"."full_name") AS "archived_full_username_str"'