Class: EffortLog
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EffortLog
- Defined in:
- app/models/effort_log.rb
Class Method Summary collapse
- .find_effort_logs(current_user) ⇒ Object
- .latest_for_user(user_id, week_number, year) ⇒ Object
- .log_effort_week?(year, week_number) ⇒ Boolean
- .users_with_effort_against_unregistered_courses ⇒ Object
Instance Method Summary collapse
- #determine_total_effort ⇒ Object
- #editable_by(current_user) ⇒ Object
- #existing_effort_log_line_item_attributes=(effort_log_line_item_attributes) ⇒ Object
- #new_effort_log_line_item_attributes=(line_item_attributes) ⇒ Object
- #save_effort_log_line_items ⇒ Object
- #validate_effort_against_registered_courses ⇒ Object
Class Method Details
.find_effort_logs(current_user) ⇒ Object
11 12 13 |
# File 'app/models/effort_log.rb', line 11 def self.find_effort_logs(current_user) where("user_id = '#{current_user.id}'").order("year DESC, week_number DESC") end |
.latest_for_user(user_id, week_number, year) ⇒ Object
110 111 112 |
# File 'app/models/effort_log.rb', line 110 def self.latest_for_user(user_id, week_number, year) EffortLog.where("user_id = ? and week_number = ? and year = ?", user_id, week_number, year).first end |
.log_effort_week?(year, week_number) ⇒ Boolean
102 103 104 105 106 107 108 |
# File 'app/models/effort_log.rb', line 102 def self.log_effort_week?(year, week_number) if AcademicCalendar.spring_break(year).include?(week_number) return false else return AcademicCalendar.week_during_semester?(year, week_number) end end |
.users_with_effort_against_unregistered_courses ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/effort_log.rb', line 48 def self.users_with_effort_against_unregistered_courses cweek = Date.today.cweek cyear = Date.today.cwyear sql_effort_logs_this_week = "SELECT e.* FROM effort_logs e,users u where e.week_number=#{cweek} and e.year=#{cyear} and u.id=e.user_id and u.is_student IS TRUE" effort_logs_this_week = EffortLog.find_by_sql(sql_effort_logs_this_week) @error_effort_logs_users = {} effort_logs_this_week.each do |effort_log| courses_in_error = effort_log.validate_effort_against_registered_courses() if (courses_in_error!="") @error_effort_logs_users[effort_log.user] = courses_in_error end end @error_effort_logs_users end |
Instance Method Details
#determine_total_effort ⇒ Object
67 68 69 70 71 72 73 |
# File 'app/models/effort_log.rb', line 67 def determine_total_effort self.sum = 0 self.effort_log_line_items.each do |line| line.determine_total_effort self.sum = self.sum + line.sum end end |
#editable_by(current_user) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/models/effort_log.rb', line 15 def editable_by(current_user) if (current_user && current_user.is_admin?) return true end if (current_user && current_user.id == user_id) a = Date.today b = Date.commercial(self.year, self.week_number, 1) c = (Date.commercial(self.year, self.week_number, 7) + 1.day) if (Date.today >= Date.commercial(self.year, self.week_number, 1) && Date.today <= (Date.commercial(self.year, self.week_number, 7) + 1.day)) return true end end return false end |
#existing_effort_log_line_item_attributes=(effort_log_line_item_attributes) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/effort_log.rb', line 85 def existing_effort_log_line_item_attributes=(effort_log_line_item_attributes) effort_log_line_items.reject(&:new_record?).each do |effort_log_line_item| attributes = effort_log_line_item_attributes[effort_log_line_item.id.to_s] if attributes effort_log_line_item.attributes = attributes else effort_log_line_items.delete(effort_log_line_item) end end end |
#new_effort_log_line_item_attributes=(line_item_attributes) ⇒ Object
76 77 78 79 80 |
# File 'app/models/effort_log.rb', line 76 def new_effort_log_line_item_attributes=(line_item_attributes) line_item_attributes.each do |attributes| effort_log_line_items.build(attributes) end end |
#save_effort_log_line_items ⇒ Object
96 97 98 99 100 |
# File 'app/models/effort_log.rb', line 96 def save_effort_log_line_items effort_log_line_items.each do |effort_log_line_item| effort_log_line_item.save(false) end end |
#validate_effort_against_registered_courses ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/models/effort_log.rb', line 30 def validate_effort_against_registered_courses course_error_msg = "" registered_courses = self.user.registered_for_these_courses_during_current_semester() unregistered_courses = {} self.effort_log_line_items.each do |log_line_item| if (log_line_item.sum != 0) if (log_line_item.course.nil?) unregistered_courses["No course selected"] = 1 elsif (!registered_courses.include?(log_line_item.course)) unregistered_courses[log_line_item.course.name] = 1 end end end course_error_msg = unregistered_courses.keys.join("<br/>") unless unregistered_courses.empty? return course_error_msg end |