Sprankelprachtig aan/afmeldsysteem

activity.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # An Activity represents a single continuous event that the members of a group may attend.
  2. # An Activity belongs to a group, and has many participants.
  3. class Activity < ApplicationRecord
  4. # @!attribute name
  5. # @return [String]
  6. # a short name for the activity.
  7. #
  8. # @!attribute description
  9. # @return [String]
  10. # a short text describing the activity. This text is always visible to
  11. # all users.
  12. #
  13. # @!attribute location
  14. # @return [String]
  15. # a short text describing where the activity will take place. Always
  16. # visible to all participants.
  17. #
  18. # @!attribute start
  19. # @return [TimeWithZone]
  20. # when the activity starts.
  21. #
  22. # @!attribute end
  23. # @return [TimeWithZone]
  24. # when the activity ends.
  25. #
  26. # @!attribute deadline
  27. # @return [TimeWithZone]
  28. # when the normal participants (everyone who isn't an organizer or group
  29. # leader) may not change their own attendance anymore. Disabled if set to
  30. # nil.
  31. #
  32. # @!attribute reminder_at
  33. # @return [TimeWithZone]
  34. # when all participants which haven't responded yet (attending is nil)
  35. # will be automatically set to 'present' and emailed. Must be before the
  36. # deadline, disabled if nil.
  37. #
  38. # @!attribute reminder_done
  39. # @return [Boolean]
  40. # whether or not sending the reminder has finished.
  41. belongs_to :group
  42. has_many :participants,
  43. dependent: :destroy
  44. has_many :people, through: :participants
  45. validates :name, presence: true
  46. validates :start, presence: true
  47. validate :deadline_before_start, unless: "self.deadline.blank?"
  48. validate :end_after_start, unless: "self.end.blank?"
  49. validate :reminder_before_deadline, unless: "self.reminder_at.blank?"
  50. after_create :create_missing_participants!
  51. after_commit :schedule_reminder, if: Proc.new {|a| a.previous_changes["reminder_at"] }
  52. # Get all people (not participants) that are organizers. Does not include
  53. # group leaders, although they may modify the activity as well.
  54. def organizers
  55. self.participants.includes(:person).where(is_organizer: true)
  56. end
  57. # Determine whether the passed Person participates in the activity.
  58. def is_participant?(person)
  59. Participant.exists?(
  60. activity_id: self.id,
  61. person_id: person.id
  62. )
  63. end
  64. # Determine whether the passed Person is an organizer for the activity.
  65. def is_organizer?(person)
  66. Participant.exists?(
  67. person_id: person.id,
  68. activity_id: self.id,
  69. is_organizer: true
  70. )
  71. end
  72. # Query the database to determine the amount of participants that are present/absent/unknown
  73. def state_counts
  74. self.participants.group(:attending).count
  75. end
  76. # Return participants attending, absent, unknown
  77. def human_state_counts
  78. c = self.state_counts
  79. p = c[true]
  80. a = c[false]
  81. u = c[nil]
  82. return "#{p or 0}, #{a or 0}, #{u or 0}"
  83. end
  84. # Determine whether the passed Person may change this activity.
  85. def may_change?(person)
  86. person.is_admin ||
  87. self.is_organizer?(person) ||
  88. self.group.is_leader?(person)
  89. end
  90. # Create Participants for all People that
  91. # 1. are members of the group
  92. # 2. do not have Participants (and thus, no way to confirm) yet
  93. def create_missing_participants!
  94. people = self.group.people
  95. if not self.participants.empty?
  96. people = people.where('people.id NOT IN (?)', self.people.ids)
  97. end
  98. people.each do |p|
  99. Participant.create(
  100. activity: self,
  101. person: p,
  102. )
  103. end
  104. end
  105. # Create multiple Activities from data in a CSV file, assign to a group, return.
  106. def self.from_csv(content, group)
  107. reader = CSV.parse(content, {headers: true, skip_blanks: true})
  108. result = []
  109. reader.each do |row|
  110. a = Activity.new
  111. a.group = group
  112. a.name = row['name']
  113. a.description = row['description']
  114. a.location = row['location']
  115. sd = Date.strptime(row['start_date'])
  116. st = Time.strptime(row['start_time'], '%H:%M')
  117. a.start = Time.zone.local(sd.year, sd.month, sd.day, st.hour, st.min)
  118. if not row['end_date'].blank?
  119. ed = Date.strptime(row['end_date'])
  120. et = Time.strptime(row['end_time'], '%H:%M')
  121. a.end = Time.zone.local(ed.year, ed.month, ed.day, et.hour, et.min)
  122. end
  123. dd = Date.strptime(row['deadline_date'])
  124. dt = Time.strptime(row['deadline_time'], '%H:%M')
  125. a.deadline = Time.zone.local(dd.year, dd.month, dd.day, dt.hour, dt.min)
  126. result << a
  127. end
  128. result
  129. end
  130. # Send a reminder to all participants who haven't responded, and set their
  131. # response to 'attending'.
  132. def send_reminder
  133. # Sanity check that the reminder date didn't change while queued.
  134. return unless !self.reminder_done && self.reminder_at
  135. return if self.reminder_at > Time.zone.now
  136. participants = self.participants.where(attending: nil)
  137. participants.each { |p| p.send_reminder }
  138. self.reminder_done = true
  139. self.save
  140. end
  141. def schedule_reminder
  142. return if self.reminder_at.nil? || self.reminder_done
  143. self.delay(run_at: self.reminder_at).send_reminder
  144. end
  145. private
  146. # Assert that the deadline for participants to change the deadline, if any,
  147. # is set before the event starts.
  148. def deadline_before_start
  149. if self.deadline > self.start
  150. errors.add(:deadline, I18n.t('activities.errors.must_be_before_start'))
  151. end
  152. end
  153. # Assert that the activity's end, if any, occurs after the event's start.
  154. def end_after_start
  155. if self.end < self.start
  156. errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
  157. end
  158. end
  159. # Assert that the reminder for non-response is sent while participants still
  160. # can change their response.
  161. def reminder_before_deadline
  162. if self.reminder_at > self.deadline
  163. errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline'))
  164. end
  165. end
  166. end