Guest User

Untitled

a guest
Feb 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. # this is a tricky method used to pull a report to review out of the pool of
  2. # possible reports, it can return nothing when there are no reports or when
  3. # there are simply no qualifying reports for the student to review. read
  4. # *carefully* if you plan on modifying it
  5. #
  6. def find_lettuce_report_to_review options = {}
  7. options.to_options!
  8. student = options.delete(:student) or raise 'no student'
  9.  
  10. transaction do
  11. pool = pool_for(student)
  12. unit = self
  13.  
  14. report_table = Report.table_name
  15. report_review_table = ReportReview.table_name
  16.  
  17. # find the list of any report the student has already reviewed - we'll
  18. # use this to prevent a double review of any report
  19. #
  20. select = [:id, :report_id].join(', ')
  21. conditions = { :user_id => student.id, :pool_id => pool.id }
  22. reviews = ReportReview.find(:all, :select => select, :conditions => conditions, :include => :report)
  23.  
  24. # setup a list of previously reviewed report ids - we don't want to do
  25. # these again....
  26. #
  27. report_ids = reviews.map{|review| review.report.id}.compact
  28.  
  29. # now look for a report to review, we want one that is published, in the
  30. # correct unit, in the correct pool, that is not written by the student
  31. # and which has not already been review by the student. furthermore, we
  32. # want to select one which is most likely to be needing a review, we do
  33. # this be selecting the ones with the lowest review count and the from
  34. # those selecting the ones which haven't had a reviewer look at them in
  35. # a while (reviewed_at).
  36. #
  37. conditions = [
  38. "
  39. #{ report_table }.is_published=? AND
  40. #{ report_table }.unit_id=? AND
  41. #{ report_table }.user_id!=? AND
  42. #{ report_table }.pool_id=?
  43. ",
  44. true, unit.id, student.id, pool.id
  45. ]
  46.  
  47. unless report_ids.empty?
  48. condition_clause = conditions.first
  49. list = report_ids.join(', ')
  50. condition_clause << " AND #{ report_table }.id NOT IN(#{ list }) "
  51. end
  52.  
  53. order = "
  54. #{ report_table }.reviews_count, #{ report_table }.reviewed_at
  55. "
  56.  
  57. Report.find(:first,
  58. options.reverse_merge(:conditions => conditions, :order => order, :include => :questionnaire)
  59. )
  60. end
  61. end
Add Comment
Please, Sign In to add comment