Pre Cursor to this question.... I did not write this code, I have been asked to adapt the code. The original coder is no longer available to assist.


I have added a column to the database so that I can tell if the item is "random" or not. I need to change this code so that the stats_controller,model, and views include or exclude Random,Non-Random or all items. From the looks of this I have a Model called stats_cache that searches the DB and caches the information, the Controller then allows for the filtering. I have updated the view for the option to select All/random/non-random.

The Database has a table called prelimForm and column called qa_non_random which is automatically 0 unless told otherwise (1).

the code added to the views is
Code:
<tr>
<th><label for="">Random vs Non-Random</label></th>
<td>
<select name="report[qa_non_random]">
<option value="all">All</option>
<option value="0">Random</option>
<option value="1">Non-Random</option>
</select>			
</td>
</tr>

Code for the controller is (I've added hash mark to disable the code until I can get it working):

Code:
def quality_report
    @modalities = Modality.find(:all)
    @totals = [0,0,0,0,0,0,0]
    @total_2b_plus = 0
    
    conditions = "1=1 "
    conditions << "AND radiologist_id = #{params[:report][:radiologist_id]} " unless params[:report][:radiologist_id] == "all"
    conditions << "AND facility_id = #{params[:report][:facility_id]} " unless params[:report][:facility_id] == '0'
    #conditions << "AND qa_non_random = #{params[:report][:qa_non_random]} " unless params[:report][:random] == "all"
        
    start_date  = params[:report][:start_date].empty? ? Time.at(0) : Date.parse( params[:report][:start_date] )
    end_date    = params[:report][:end_date].empty? ? Time.now : Date.parse( params[:report][:end_date] )
    
    conditions << "AND report_time BETWEEN '#{start_date.strftime('%Y-%m-%d')}' AND '#{end_date.strftime('%Y-%m-%d')}' "
    
    @radiologist = User.find(params[:report][:radiologist_id]) unless params[:report][:radiologist_id] == "all"
    @facility = params[:report][:facility_id] == '0' ? "all facilities" : PatientFacility.find(params[:report][:facility_id]).facilityName
    #@qa_non_random = params[:report][:qa_non_random] unless params[:report][:qa_non_random] == "all"
    @dates = "#{start_date.strftime("%m/%d/%Y")} to #{end_date.strftime("%m/%d/%Y")}"
    @stats = calc_stats(conditions)
    @qa_data_total = 0
    @stats.each { |s| @qa_data_total += s[0] unless s.nil? }
  end
and the model

Code:
class StatsCache < ActiveRecord::Base
  serialize :contents
  
  public
  def self.refresh
    StatsCache.proc_facilities_stats
  end
  
  public
  def self.proc_facilities_stats
    facilities_stats = StatsCache.find_by_name('Facilities')
    #facilities_stats.save if facilities_stats.nil?
    
    stats = Hash.new
    facilities = PatientFacility.find(:all)
    modalities = Modality.find(:all)
  
    db = ActiveRecord::Base.connection()
    for facility in facilities
      stats[facility.facilityName] = { :total => 0 }
      for modality in modalities
  		  sql = "
  		  SELECT count(f.id) as total_reports
  		  FROM prelimForm pf
    		LEFT JOIN findings f ON pf.id = f.prelimFormID
    		WHERE pf.patientFacilityID = #{facility.id}
    		AND f.modalityID = #{modality.id}
    		AND pf.deleted = 'N' AND pf.readTime <> ''"
    		
    		stats[facility.facilityName][modality.name] = db.select_value(sql).to_i
    		stats[facility.facilityName][:total] += stats[facility.facilityName][modality.name]
      end
      
      stats.delete(facility.facilityName) if stats[facility.facilityName][:total] == 0
    end
    
    facilities_stats.update_attributes!(:contents => stats)
  end
  
  def self.proc_qa_stats
    facilities = PatientFacility.find(:all)
    radiologists = User.radiologists
    
    conditions = ["deleted = 'N' AND interpreted_at <> 0 AND interpreted_at <> prelimForm.submitTime AND read_at <> 0"]
    
    
    for rad in radiologists
      sql = "
      SELECT
      
      count(pf.*) as total_studies FROM prelimForm pf,

      INNER JOIN findings f ON pf.id = f.prelimFormID
      WHERE deleted = 'N' AND qa_status = 3
      AND pf.radiologist_id = #{rad.id}
      AND pf.patientFacilityID = #{facility.id}
      "
    
    
    
    end
    
    
    
    
    PrelimForm.find_by_sql(sql)      
  end
  

end
Any help would be appreciated