Skip to content

Commit f815a14

Browse files
committed
Fixed the diagnosis by students admin report. The dx_by_students routine was not
querying the correct table in the database.
1 parent 9a3c510 commit f815a14

File tree

1 file changed

+84
-45
lines changed

1 file changed

+84
-45
lines changed

app/models/reports.rb

Lines changed: 84 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -106,57 +106,96 @@ def self.site_encounters_by_care_settings
106106
# Individual diagnoses that each student has performed
107107
#-------------------------------------------
108108
def self.dx_by_students
109-
dxcats = DiagnosisCategory.all
110109

111-
template = %Q{sum(if(dx.category_id=%s,1,0)) as '%s'}
110+
dxcats = DiagnosisCategory.all
111+
fieldNames = Array.new
112+
dxcats.each do |aField|
113+
ActiveRecord::Base.logger.debug "aField: #{aField}"
114+
fieldNames << aField.name
115+
end
112116

113-
partialSqlStatement = dxcats.map { |dxc|
114-
sprintf template, dxc["id"], dxc["name"]
115-
}.join(",\n")
116-
117-
118-
sql = <<-EOF
119-
select
120-
u.firstname as 'FirstName',
121-
u.lastname as 'LastName',
122-
u.email as 'Email',
123-
#{partialSqlStatement}
124-
from encounters e
125-
join users u on e.created_by = u.id
126-
join encounter_dx edx on edx.encounter_id = e.id
127-
join dx on dx.id = edx.dx_id
128-
where e.clerkship_id = 1
129-
group by e.created_by;
130-
EOF
117+
sql = <<-EOF
118+
select FirstName, LastName, Email,
119+
if (dxcatid = 1, dxcatcount,0) as '#{dxcats[0].name}',
120+
if (dxcatid = 2, dxcatcount,0) as '#{dxcats[1].name}',
121+
if (dxcatid = 3, dxcatcount,0) as '#{dxcats[2].name}',
122+
if (dxcatid = 4, dxcatcount,0) as '#{dxcats[3].name}',
123+
if (dxcatid = 5, dxcatcount,0) as '#{dxcats[4].name}',
124+
if (dxcatid = 6, dxcatcount,0) as '#{dxcats[5].name}',
125+
if (dxcatid = 7, dxcatcount,0) as '#{dxcats[6].name}',
126+
if (dxcatid = 8, dxcatcount,0) as '#{dxcats[7].name}',
127+
if (dxcatid = 9, dxcatcount,0) as '#{dxcats[8].name}',
128+
if (dxcatid = 10, dxcatcount,0) as '#{dxcats[9].name}',
129+
if (dxcatid = 11, dxcatcount,0) as '#{dxcats[10].name}',
130+
if (dxcatid = 12, dxcatcount,0) as '#{dxcats[11].name}',
131+
if (dxcatid = 13, dxcatcount,0) as '#{dxcats[12].name}',
132+
if (dxcatid = 14, dxcatcount,0) as '#{dxcats[13].name}',
133+
if (dxcatid = 15, dxcatcount,0) as '#{dxcats[14].name}'
134+
from (select u.firstname as 'FirstName',
135+
u.lastname as 'LastName',
136+
u.email as 'Email',
137+
edx.created_by, dc.id as dxcatid, count(*) as dxcatcount
138+
from encounter_dx edx
139+
join users u on edx.created_by = u.id
140+
join dx d on edx.dx_id = d.id
141+
join dx_categories dc on d.category_id = dc.id
142+
group by edx.created_by,dc.id) as derivedTable
143+
EOF
131144

132-
# Calling ActiveRecord::Base.connection.execute(sql) in order to get the fields of each record returned
133-
# in the correct order. Using the select_all method returns a hash of the rows but they are not ordered
134-
# correctly
135-
sqlResult = ActiveRecord::Base.connection.execute sql
145+
# Calling ActiveRecord::Base.connection.execute(sql) in order to get the fields of each record returned
146+
# in the correct order. Using the select_all method returns a hash of the rows but they are not ordered
147+
# correctly
148+
sqlResult = ActiveRecord::Base.connection.execute sql
136149

137-
# this returns a Mysql::Result object.
138-
# first check whether something was returned
139-
if sqlResult != nil
140-
# first extract the field names
141-
fieldObjectArray = sqlResult.fetch_fields()
150+
# this returns a Mysql::Result object.
151+
# first check whether something was returned
152+
if sqlResult != nil then
153+
# first extract the field names
154+
fieldObjectArray = sqlResult.fetch_fields()
142155

143-
fieldNames = Array.new
144-
fieldObjectArray.each do |aField|
145-
fieldNames << aField.name
146-
end
156+
fieldNames = Array.new
157+
fieldObjectArray.each do |aField|
158+
fieldNames << aField.name
159+
end
147160

148-
# now extract the rows of data
149-
rows = Array.new
150-
sqlResult.each do |row|
151-
rows << row
152-
end
153-
sqlResult.free;
154-
end
155-
156-
fieldNamesAndDataArray = Array.new
157-
fieldNamesAndDataArray << fieldNames << rows
158-
return fieldNamesAndDataArray
159-
end
161+
# now extract the rows of data
162+
rows = Array.new
163+
sqlResult.each do |row|
164+
rows << row
165+
end
166+
sqlResult.free;
167+
168+
currentrow = ["", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
169+
nextrow = ["", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
170+
buckets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
171+
172+
outputrows = []
173+
rows.each do |row|
174+
currentrow = nextrow
175+
nextrow = row
176+
# check for a new student and check for first iteration where currentrow[2] == ""
177+
if ((currentrow[2] != nextrow[2]) and (currentrow[2] != ""))
178+
currentrow[3..18] = buckets
179+
outputrows << currentrow
180+
buckets = nextrow[3..18]
181+
else
182+
for index in (0..14)
183+
buckets[index] = buckets[index].to_i + nextrow[index+3].to_i
184+
end # for loop
185+
end # if currentrow[2] != nextrow[2]
186+
end # rows.each
187+
188+
# will alway have to write out the last record after exiting the loop
189+
currentrow = nextrow
190+
currentrow[3..18] = buckets
191+
outputrows << currentrow
192+
end # if sql_result
193+
194+
195+
fieldNamesAndDataArray = Array.new
196+
fieldNamesAndDataArray << fieldNames << outputrows
197+
return fieldNamesAndDataArray
198+
end
160199

161200
#-------------------------------------------
162201
# diagnoses by Category

0 commit comments

Comments
 (0)