Sprankelprachtig aan/afmeldsysteem

activity.rb 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. has_many :subgroups,
  46. dependent: :destroy
  47. validates :name, presence: true
  48. validates :start, presence: true
  49. validate :deadline_before_start, unless: "self.deadline.blank?"
  50. validate :end_after_start, unless: "self.end.blank?"
  51. validate :reminder_before_deadline, unless: "self.reminder_at.blank?"
  52. after_create :create_missing_participants!
  53. after_create :copy_default_subgroups!
  54. after_commit :schedule_reminder, if: Proc.new {|a| a.previous_changes["reminder_at"] }
  55. # Get all people (not participants) that are organizers. Does not include
  56. # group leaders, although they may modify the activity as well.
  57. def organizers
  58. self.participants.includes(:person).where(is_organizer: true)
  59. end
  60. # Determine whether the passed Person participates in the activity.
  61. def is_participant?(person)
  62. Participant.exists?(
  63. activity_id: self.id,
  64. person_id: person.id
  65. )
  66. end
  67. # Determine whether the passed Person is an organizer for the activity.
  68. def is_organizer?(person)
  69. Participant.exists?(
  70. person_id: person.id,
  71. activity_id: self.id,
  72. is_organizer: true
  73. )
  74. end
  75. # Query the database to determine the amount of participants that are present/absent/unknown
  76. def state_counts
  77. self.participants.group(:attending).count
  78. end
  79. # Return participants attending, absent, unknown
  80. def human_state_counts
  81. c = self.state_counts
  82. p = c[true]
  83. a = c[false]
  84. u = c[nil]
  85. return "#{p or 0}, #{a or 0}, #{u or 0}"
  86. end
  87. # Determine whether the passed Person may change this activity.
  88. def may_change?(person)
  89. person.is_admin ||
  90. self.is_organizer?(person) ||
  91. self.group.is_leader?(person)
  92. end
  93. # Create Participants for all People that
  94. # 1. are members of the group
  95. # 2. do not have Participants (and thus, no way to confirm) yet
  96. def create_missing_participants!
  97. people = self.group.people
  98. if not self.participants.empty?
  99. people = people.where('people.id NOT IN (?)', self.people.ids)
  100. end
  101. people.each do |p|
  102. Participant.create(
  103. activity: self,
  104. person: p,
  105. )
  106. end
  107. end
  108. # Create Subgroups from the defaults set using DefaultSubgroups
  109. def copy_default_subgroups!
  110. defaults = self.group.default_subgroups
  111. defaults.each do |dsg|
  112. sg = Subgroup.new(activity: self)
  113. sg.name = dsg.name
  114. sg.is_assignable = dsg.is_assignable
  115. sg.save! # Should never fail, as DSG and SG have identical validation, and names cannot clash.
  116. end
  117. end
  118. # Create multiple Activities from data in a CSV file, assign to a group, return.
  119. def self.from_csv(content, group)
  120. reader = CSV.parse(content, {headers: true, skip_blanks: true})
  121. result = []
  122. reader.each do |row|
  123. a = Activity.new
  124. a.group = group
  125. a.name = row['name']
  126. a.description = row['description']
  127. a.location = row['location']
  128. sd = Date.strptime(row['start_date'])
  129. st = Time.strptime(row['start_time'], '%H:%M')
  130. a.start = Time.zone.local(sd.year, sd.month, sd.day, st.hour, st.min)
  131. if not row['end_date'].blank?
  132. ed = Date.strptime(row['end_date'])
  133. et = Time.strptime(row['end_time'], '%H:%M')
  134. a.end = Time.zone.local(ed.year, ed.month, ed.day, et.hour, et.min)
  135. end
  136. dd = Date.strptime(row['deadline_date'])
  137. dt = Time.strptime(row['deadline_time'], '%H:%M')
  138. a.deadline = Time.zone.local(dd.year, dd.month, dd.day, dt.hour, dt.min)
  139. result << a
  140. end
  141. result
  142. end
  143. # Send a reminder to all participants who haven't responded, and set their
  144. # response to 'attending'.
  145. def send_reminder
  146. # Sanity check that the reminder date didn't change while queued.
  147. return unless !self.reminder_done && self.reminder_at
  148. return if self.reminder_at > Time.zone.now
  149. participants = self.participants.where(attending: nil)
  150. participants.each { |p| p.send_reminder }
  151. self.reminder_done = true
  152. self.save
  153. end
  154. def schedule_reminder
  155. return if self.reminder_at.nil? || self.reminder_done
  156. self.delay(run_at: self.reminder_at).send_reminder
  157. end
  158. private
  159. # Assert that the deadline for participants to change the deadline, if any,
  160. # is set before the event starts.
  161. def deadline_before_start
  162. if self.deadline > self.start
  163. errors.add(:deadline, I18n.t('activities.errors.must_be_before_start'))
  164. end
  165. end
  166. # Assert that the activity's end, if any, occurs after the event's start.
  167. def end_after_start
  168. if self.end < self.start
  169. errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
  170. end
  171. end
  172. # Assert that the reminder for non-response is sent while participants still
  173. # can change their response.
  174. def reminder_before_deadline
  175. if self.reminder_at > self.deadline
  176. errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline'))
  177. end
  178. end
  179. end