#TODO create two html tables to separate CR from the rest.
require 'rubygems'
require 'activerecord' # gem install activerecord
# Activerecord is only required because of how easy it makes date manipulation.
require 'report' # should be in same folder
require 'timestamp' # should be in same folder
require 'pp' #pretty print, useful for debugging
# In Ruby, variables that begin with an upper-case letter are constants.
# This reminds the programmer not to mess with the content without good reason.
# Ruby is flexible and WILL let you change the value - so be responsible.
# Don't drink and derive.
Folders_FullPath = ['//pluto/transcript/radiology/dictamatic_preview',
'//pluto/transcript/radiology/teleradiology_preview/qa']
Monday = Date.today.monday
Timestamp = get_timestamp
#This mymod / modvar combo is used for basic Don't Repeat Yourself coding.
#Look for it when I iterate through the previews, around line 77.
mymod = {:cr => "", :non_cr => ""}
modvar = ""
myshade = {:shade_cr => false, :shade_non_cr => false}
shadevar = ""
file = %Q{<HTML>
<HEAD>
<TITLE>Dictamatic_Preview sorted by date, then last name</TITLE>
</HEAD>
<BODY>
Report generated on <b>#{Time.now}</b><br/>
Bolded lines mean a report has been in the folder more than 18 hours.<br/>
}
Folders_FullPath.each do |folder|
previews = []
# go to folder
Dir.chdir folder
# get list of files - 'glob' is actually a filter, * means 'get everything'.
Dir.glob('*') do |item|
# We don't want directories or temp Word files.
next if File.directory? item
next if item[0..0] == "~"
begin
previews << (Report.new item)
rescue
puts "#{item}"
puts "This thing made it crash."
System.END 0
end
end
#Sorting in ascending order.
previews.sort! do |file1,file2|
# Clever hack for dual sort - by date then last name.
comp = file1.date <=> file2.date
comp.zero? ? (file1.last <=> file2.last) : comp
end
total_exams_in_folder = previews.size
exams_before_this_past_mon = 0
# Getting total exams in Dictamatic_Preview from before Monday.
previews.each { |i| exams_before_this_past_mon += 1 if i.date < Monday }
folder_name = folder.split('/')[-1].upcase
file << %Q{<b><center>EXAMS IN #{folder_name}: #{total_exams_in_folder}<br/>
Exams from before #{Monday}: #{exams_before_this_past_mon}</center></b><br/>
}
modvar[:non_cr] = %Q{<b><center>MODALITIES OTHER THAN CR</center></b>
<table>\n<tr><th>Name</th><th>Date</th><th>Age</th><th>Procedure</th></tr>\n}
modvar[:cr] = %Q{<b><center>CR ONLY</center></b>
<table>\n<tr><th>Name</th><th>Date</th><th>Age</th><th>Procedure</th></tr>\n}
previews.each do |i|
bold = i.age[0..1].to_i > 18 and i.age[-2..-1] == "hr"
# The famous ternary operator. I set usevar to :cr or :non_cr.
modvar = i.modality == 'CR' ? 'cr'.to_sym : 'non_cr'.to_sym
shadevar = modvar == 'cr'.to_sym ? 'shade_cr'.to_sym : 'shade_non_cr'.to_sym
#Maybe I'm taking it a little far.
#This is borderline unreadable. If modvar is :cr, then I set the shadevar
#to one thing, otherwise I set it to another thing.
name = "#{i.last}, #{i.first}"
mymod[modvar] << (myshade[shadevar] ? "<tr bgcolor=#DDDDDD>" : "<tr>")
# Don't you just love the ternary operator? Way to condense code!
myshade[shadevar] = !myshade[shadevar]
mymod[modvar] << bold ? "<td><b>#{name}</b></td>" : "<td>#{name}</td>"
mymod[modvar] << bold ? "<td><b>#{i.date}</b></td>" : "<td>#{i.date}</td>"
mymod[modvar] << bold ? "<td><b>#{i.age}</b></td>" : "<td>#{i.age}</td>"
mymod[modvar] << bold ? "<td><b>#{i.proc}</b></td>" : "<td>#{i.proc}</td>"
mymod[modvar] << "</tr>\n"
end
mymod[modvar] << "</table>"
mymod.each_value { |v| file << v << "\n" }
end
file << %Q{
</BODY>
</HTML>}
# I am opening a file and using it as the object of a block of code.
# The file is closed automatically when the block ends.
File.open("c:/reports/Dictamatic_Preview/#{Timestamp}.html", 'w') do |f|
f << file
end