| 1 | package CohortExplorer::Application::REDCap::Datasource; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = 0.14; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base qw(CohortExplorer::Datasource); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | #------- | ||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub authenticate { | ||||||||||||||||||||||||||||||
|
Subroutine
authenticate: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 12 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 13 | 0 | require HTTP::Cookies; | |||||||||||||||||||||||||||||||
| 14 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||
| 15 | 0 | my $cookie = HTTP::Cookies->new(); | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | # Authenticate using REDCap url | ||||||||||||||||||||||||||||||||
| 18 | # Default REDCap url is http://localhost:80 | ||||||||||||||||||||||||||||||||
| 19 | 0 | my $ua = LWP::UserAgent->new( timeout => 10, cookie_jar => $cookie, ssl_opts => { verify_hostname => 0 } ); | |||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | (my $url = $self->url || 'http://localhost:80/redcap' ) =~ s/\/$//; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->url || 'http://localhost:80/redcap'
MC/DC0%0/1
$self->url || 'http://localhost:80/redcap'
$self->url
Truth table0%0/2
A
A= $self->url
| |||||||||||||||||||||||||||||||||
| 21 | my $res = $ua->post( | ||||||||||||||||||||||||||||||||
| 22 | $url . '/index.php', | ||||||||||||||||||||||||||||||||
| 23 | [ | ||||||||||||||||||||||||||||||||
| 24 | username => $opts{username}, | ||||||||||||||||||||||||||||||||
| 25 | password => $opts{password} | ||||||||||||||||||||||||||||||||
| 26 | 0 | ] | |||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | 0 | my $code = $res->code; | |||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ( $cookie->as_string =~ /(?:PHPSESSID|authchallenge)=[^;]+;/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | return | ||||||||||||||||||||||||||||||||
| 32 | $self->dbh->selectrow_hashref( | ||||||||||||||||||||||||||||||||
| 33 | 0 | "SELECT rp.project_id, rur.data_export_tool, rur.group_id FROM redcap_user_rights AS rur INNER JOIN redcap_projects AS rp ON rur.project_id = rp.project_id WHERE rp.project_name = ? AND rur.data_export_tool != 0 AND rur.username = ? AND ( rp.project_id NOT IN ( SELECT project_id FROM redcap_external_links_exclude_projects ) AND ( rur.expiration <= CURDATE() OR rur.expiration IS NULL ) )", undef, $self->name, $opts{username} ); | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||||||
| 40 | 0 | die "Failed to connect to REDCap server using '$url' (" | |||||||||||||||||||||||||||||||
| 41 | . $res->status_line . ")\n"; | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub additional_params { | ||||||||||||||||||||||||||||||
|
Subroutine
additional_params: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 46 | 0 | my ( $self, $res, %opts ) = @_; | |||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 0 | my %param = %$res; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # Get static tables and dynamic_event_ids (i.e. comma separated event_ids of all repeating forms) | ||||||||||||||||||||||||||||||||
| 51 | 0 | my $rows = $self->dbh->selectall_arrayref( "SELECT arm_name, GROUP_CONCAT( DISTINCT IF( form_count = 1, form, NULL ) ) AS static_tables FROM ( SELECT GROUP_CONCAT( DISTINCT ref.form_name ) AS form, COUNT( ref.event_id ) AS form_count, arm_name FROM redcap_events_forms AS ref INNER JOIN redcap_events_metadata AS rem ON ref.event_id = rem.event_id INNER JOIN redcap_events_arms AS rea ON rea.arm_id = rem.arm_id WHERE rea.project_id = ? GROUP BY ref.form_name, rea.arm_id ORDER BY ref.event_id) AS t GROUP BY arm_name", undef, $param{project_id} ); | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | die "Datasource has multiple arms, please provide a single arm (i.e. arm_name) to query\n" if @$rows > 1 && !$self->arm_name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$rows > 1 and not $self->arm_name
MC/DC0%0/2
@$rows > 1 and not $self->arm_name
@$rows > 1
not $self->arm_name
Truth table0%0/3
A and not(B)
A= @$rows > 1B= not $self->arm_name
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( @$rows ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 0 | my $arm_index; | |||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( @$rows > 1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 0 | ($arm_index) = grep ( $self->arm_name eq $rows->[$_][0], 0..$#$rows ); | |||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | die "Arm name must be either " . join ( ' or ', map { "'$_->[0]'" } @$rows ) if !defined $arm_index; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | @param{ qw/type arm_name static_tables/ } = ( 'longitudinal', | ||||||||||||||||||||||||||||||
|
Condition0%0/6
$arm_index || 0
$$rows[$arm_index or 0][1] || ''
$arm_index || 0
MC/DC0%0/1
$arm_index || 0
$arm_index
MC/DC0%0/1
$$rows[$arm_index or 0][1] || ''
$$rows[$arm_index or 0][1]
MC/DC0%0/1
$arm_index || 0
$arm_index
Truth table0%0/2
A
A= $arm_index
Truth table0%0/2
A
A= $$rows[$arm_index or 0][1]
Truth table0%0/2
A
A= $arm_index
| |||||||||||||||||||||||||||||||||
| 62 | $rows->[$arm_index || 0 ][0], | ||||||||||||||||||||||||||||||||
| 63 | [ split ',', ( $rows->[$arm_index || 0][1] || '') ] | ||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||||||
| 67 | 0 | $param{type} = 'standard'; | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 0 | return \%param; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub entity_structure { | ||||||||||||||||||||||||||||||
|
Subroutine
entity_structure: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my %struct = ( | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 76 | -columns => [ | ||||||||||||||||||||||||||||||||
| 77 | entity_id => 'rd.record', | ||||||||||||||||||||||||||||||||
| 78 | variable => 'rd.field_name', | ||||||||||||||||||||||||||||||||
| 79 | value => 'rd.value', | ||||||||||||||||||||||||||||||||
| 80 | table => 'form_name' | ||||||||||||||||||||||||||||||||
| 81 | ], | ||||||||||||||||||||||||||||||||
| 82 | -from => [ | ||||||||||||||||||||||||||||||||
| 83 | -join => ( | ||||||||||||||||||||||||||||||||
| 84 | $self->type eq 'standard' | ||||||||||||||||||||||||||||||||
| 85 | ? qw/redcap_data|rd <=>{project_id=project_id} redcap_metadata|rm/ | ||||||||||||||||||||||||||||||||
| 86 | : qw/redcap_data|rd <=>{event_id=event_id} redcap_events_forms|ref <=>{event_id=event_id} redcap_events_metadata|rem <=>{arm_id=arm_id} redcap_events_arms|rea/ | ||||||||||||||||||||||||||||||||
| 87 | ) | ||||||||||||||||||||||||||||||||
| 88 | ], | ||||||||||||||||||||||||||||||||
| 89 | -where => { | ||||||||||||||||||||||||||||||||
| 90 | 'rd.project_id' => $self->project_id, | ||||||||||||||||||||||||||||||||
| 91 | 'rd.record' => ( $self->group_id ? { -in => \[ 'SELECT record FROM redcap_data WHERE project_id = ? AND field_name = ? AND | ||||||||||||||||||||||||||||||||
| 92 | value = ?', $self->project_id, '__GROUP_ID__', $self->group_id ] } : { 'like', '%' } ) | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | # Add visit column if the datasource is longitudinal | ||||||||||||||||||||||||||||||||
| 97 | # Visit number is determined using the init_event_id | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ( $self->type eq 'longitudinal' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 0 | push @{ $struct{-columns} }, visit => 'rem.descrip'; | |||||||||||||||||||||||||||||||
| 100 | 0 | $struct{-order_by} = 'rem.day_offset'; | |||||||||||||||||||||||||||||||
| 101 | 0 | $struct{-where}{'rea.arm_name'} = $self->arm_name; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | 0 | return \%struct; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub table_structure { | ||||||||||||||||||||||||||||||
|
Subroutine
table_structure: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 107 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 108 | 0 | my @cols = ( | |||||||||||||||||||||||||||||||
| 109 | arm => 'GROUP_CONCAT( DISTINCT rea.arm_name )', | ||||||||||||||||||||||||||||||||
| 110 | table => 'GROUP_CONCAT( DISTINCT rm.form_name )', | ||||||||||||||||||||||||||||||||
| 111 | label => 'GROUP_CONCAT( DISTINCT rm.form_menu_description )', | ||||||||||||||||||||||||||||||||
| 112 | variable_count => 'COUNT( DISTINCT rm.field_name )', | ||||||||||||||||||||||||||||||||
| 113 | event_count => 'COUNT( DISTINCT rem.day_offset )', | ||||||||||||||||||||||||||||||||
| 114 | event_description => "GROUP_CONCAT( DISTINCT CONCAT( rem.descrip, '(', rem.day_offset, ')' ) ORDER BY rem.day_offset SEPARATOR '\n ')" | ||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # If data_export_tool is != 1 remove variables tagged as identifiers | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ( $self->type eq 'longitudinal' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | return { | ||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | -columns => \@cols, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | -from => [ | ||||||||||||||||||||||||||||||||
| 123 | -join => | ||||||||||||||||||||||||||||||||
| 124 | qw/redcap_metadata|rm <=>{form_name=form_name} redcap_events_forms|ref <=>{event_id=event_id} redcap_events_metadata|rem <=>{arm_id=arm_id} redcap_events_arms|rea/ | ||||||||||||||||||||||||||||||||
| 125 | ], | ||||||||||||||||||||||||||||||||
| 126 | -order_by => [ qw/rm.form_name rem.day_offset/ ], | ||||||||||||||||||||||||||||||||
| 127 | -group_by => 'rm.form_name', | ||||||||||||||||||||||||||||||||
| 128 | -having => { 'variable_count' => { '>', 0 } }, | ||||||||||||||||||||||||||||||||
| 129 | -where => { | ||||||||||||||||||||||||||||||||
| 130 | 'rm.project_id' => $self->project_id, | ||||||||||||||||||||||||||||||||
| 131 | 'rea.arm_name' => $self->arm_name, | ||||||||||||||||||||||||||||||||
| 132 | 'rea.project_id' => { -ident => 'rm.project_id' }, | ||||||||||||||||||||||||||||||||
| 133 | 'rm.field_phi' => ( $self->data_export_tool == 2 | ||||||||||||||||||||||||||||||||
| 134 | ? { '=', undef } | ||||||||||||||||||||||||||||||||
| 135 | : [ { '=', undef }, { '!=', undef } ] | ||||||||||||||||||||||||||||||||
| 136 | ) | ||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | }; | ||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||||||
| 142 | return { | ||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | -columns => [ splice( @cols, 2, 6 ) ], | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | -from => 'redcap_metadata AS rm', | ||||||||||||||||||||||||||||||||
| 145 | -order_by => 'rm.field_order', | ||||||||||||||||||||||||||||||||
| 146 | -group_by => 'rm.form_name', | ||||||||||||||||||||||||||||||||
| 147 | -having => { 'variable_count' => { '>', 0 } }, | ||||||||||||||||||||||||||||||||
| 148 | -where => { | ||||||||||||||||||||||||||||||||
| 149 | 'rm.project_id' => $self->project_id, | ||||||||||||||||||||||||||||||||
| 150 | 'rm.field_phi' => ( $self->data_export_tool == 2 | ||||||||||||||||||||||||||||||||
| 151 | ? { '=', undef } | ||||||||||||||||||||||||||||||||
| 152 | : [ { '=', undef }, { '!=', undef } ] | ||||||||||||||||||||||||||||||||
| 153 | ) | ||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub variable_structure { | ||||||||||||||||||||||||||||||
|
Subroutine
variable_structure: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 160 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | return { | ||||||||||||||||||||||||||||||||
| 163 | -columns => [ | ||||||||||||||||||||||||||||||||
| 164 | variable => 'field_name', | ||||||||||||||||||||||||||||||||
| 165 | table => 'form_name', | ||||||||||||||||||||||||||||||||
| 166 | type => "IF( element_validation_type IS NULL, 'text', element_validation_type )", | ||||||||||||||||||||||||||||||||
| 167 | unit => 'field_units', | ||||||||||||||||||||||||||||||||
| 168 | category => "IF( element_enum like '%, %', REPLACE( element_enum, '\\\\n', '\n'), '' )", | ||||||||||||||||||||||||||||||||
| 169 | label => 'element_label' | ||||||||||||||||||||||||||||||||
| 170 | ], | ||||||||||||||||||||||||||||||||
| 171 | -from => 'redcap_metadata', | ||||||||||||||||||||||||||||||||
| 172 | -order_by => 'field_order', | ||||||||||||||||||||||||||||||||
| 173 | -where => { | ||||||||||||||||||||||||||||||||
| 174 | 'project_id' => $self->project_id, | ||||||||||||||||||||||||||||||||
| 175 | 'field_phi' => ( $self->data_export_tool == 2 | ||||||||||||||||||||||||||||||||
| 176 | ? { '=', undef } | ||||||||||||||||||||||||||||||||
| 177 | : [ { '=', undef }, { '!=', undef } ] | ||||||||||||||||||||||||||||||||
| 178 | ), | ||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | 'form_name' => { -in => [ keys %{$self->table_info} ] } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub datatype_map { | ||||||||||||||||||||||||||||||
|
Subroutine
datatype_map: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 186 | return { | ||||||||||||||||||||||||||||||||
| 187 | 0 | 'int' => 'signed', | |||||||||||||||||||||||||||||||
| 188 | 'float' => 'decimal', | ||||||||||||||||||||||||||||||||
| 189 | 'date_dmy' => 'date', | ||||||||||||||||||||||||||||||||
| 190 | 'date_mdy' => 'date', | ||||||||||||||||||||||||||||||||
| 191 | 'date_ymd' => 'date', | ||||||||||||||||||||||||||||||||
| 192 | 'datetime_dmy' => 'datetime', | ||||||||||||||||||||||||||||||||
| 193 | 'datetime_mdy' => 'datetime', | ||||||||||||||||||||||||||||||||
| 194 | 'datetime_ymd' => 'datetime', | ||||||||||||||||||||||||||||||||
| 195 | 'datetime_seconds_dmy' => 'datetime', | ||||||||||||||||||||||||||||||||
| 196 | 'datetime_seconds_mdy' => 'datetime', | ||||||||||||||||||||||||||||||||
| 197 | 'datetime_seconds_ymd' => 'datetime', | ||||||||||||||||||||||||||||||||
| 198 | 'number' => 'decimal', | ||||||||||||||||||||||||||||||||
| 199 | 'number_1dp' => 'decimal(10,1)', | ||||||||||||||||||||||||||||||||
| 200 | 'number_2dp' => 'decimal(10,2)', | ||||||||||||||||||||||||||||||||
| 201 | 'number_3dp' => 'decimal(10,3)', | ||||||||||||||||||||||||||||||||
| 202 | 'number_4dp' => 'decimal(10,4)', | ||||||||||||||||||||||||||||||||
| 203 | 'time' => 'time', | ||||||||||||||||||||||||||||||||
| 204 | 'time_mm_sec' => 'time' | ||||||||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | #------- | ||||||||||||||||||||||||||||||||
| 209 | 1 | 1; | |||||||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||||||