Created
November 28, 2008 06:18
-
-
Save ahoward/29913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is a tricky method used to pull a report to review out of the pool of | |
# possible reports, it can return nothing when there are no reports or when | |
# there are simply no qualifying reports for the student to review. read | |
# *carefully* if you plan on modifying it | |
# | |
def find_lettuce_report_to_review options = {} | |
options.to_options! | |
student = options.delete(:student) or raise 'no student' | |
transaction do | |
pool = pool_for(student) | |
unit = self | |
report_table = Report.table_name | |
report_review_table = ReportReview.table_name | |
# find the list of any report the student has already reviewed - we'll | |
# use this to prevent a double review of any report | |
# | |
select = [:id, :report_id].join(', ') | |
conditions = { :user_id => student.id, :pool_id => pool.id } | |
reviews = ReportReview.find(:all, :select => select, :conditions => conditions, :include => :report) | |
# setup a list of previously reviewed report ids - we don't want to do | |
# these again.... | |
# | |
report_ids = reviews.map{|review| review.report.id}.compact | |
# now look for a report to review, we want one that is published, in the | |
# correct unit, in the correct pool, that is not written by the student | |
# and which has not already been review by the student. furthermore, we | |
# want to select one which is most likely to be needing a review, we do | |
# this be selecting the ones with the lowest review count and the from | |
# those selecting the ones which haven't had a reviewer look at them in | |
# a while (reviewed_at). | |
# | |
conditions = [ | |
" | |
#{ report_table }.is_published=? AND | |
#{ report_table }.unit_id=? AND | |
#{ report_table }.user_id!=? AND | |
#{ report_table }.pool_id=? | |
", | |
true, unit.id, student.id, pool.id | |
] | |
unless report_ids.empty? | |
condition_clause = conditions.first | |
list = report_ids.join(', ') | |
condition_clause << " AND #{ report_table }.id NOT IN(#{ list }) " | |
end | |
order = " | |
#{ report_table }.reviews_count, #{ report_table }.reviewed_at | |
" | |
Report.find(:first, | |
options.reverse_merge(:conditions => conditions, :order => order, :include => :questionnaire) | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment