Sprankelprachtig aan/afmeldsysteem

activity.rb 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #
  42. # @!attribute subgroup_division_enabled
  43. # @return [Boolean]
  44. # whether automatic subgroup division on the deadline is enabled.
  45. #
  46. # @!attribute subgroup_division_done
  47. # @return [Boolean]
  48. # whether subgroup division has been performed.
  49. belongs_to :group
  50. has_many :participants,
  51. dependent: :destroy
  52. has_many :people, through: :participants
  53. has_many :subgroups,
  54. dependent: :destroy
  55. validates :name, presence: true
  56. validates :start, presence: true
  57. validate :deadline_before_start, unless: "self.deadline.blank?"
  58. validate :end_after_start, unless: "self.end.blank?"
  59. validate :reminder_before_deadline, unless: "self.reminder_at.blank?"
  60. validate :subgroups_for_division_present, on: :update
  61. after_create :create_missing_participants!
  62. after_create :copy_default_subgroups!
  63. after_commit :schedule_reminder,
  64. if: Proc.new { |a| a.previous_changes["reminder_at"] }
  65. after_commit :schedule_subgroup_division,
  66. if: Proc.new { |a| (a.previous_changes['deadline'] ||
  67. a.previous_changes['subgroup_division_enabled']) &&
  68. !a.subgroup_division_done &&
  69. a.subgroup_division_enabled }
  70. # Get all people (not participants) that are organizers. Does not include
  71. # group leaders, although they may modify the activity as well.
  72. def organizers
  73. self.participants.includes(:person).where(is_organizer: true)
  74. end
  75. # Determine whether the passed Person participates in the activity.
  76. def is_participant?(person)
  77. Participant.exists?(
  78. activity_id: self.id,
  79. person_id: person.id
  80. )
  81. end
  82. # Determine whether the passed Person is an organizer for the activity.
  83. def is_organizer?(person)
  84. Participant.exists?(
  85. person_id: person.id,
  86. activity_id: self.id,
  87. is_organizer: true
  88. )
  89. end
  90. # Query the database to determine the amount of participants that are present/absent/unknown
  91. def state_counts
  92. self.participants.group(:attending).count
  93. end
  94. # Return participants attending, absent, unknown
  95. def human_state_counts
  96. c = self.state_counts
  97. p = c[true]
  98. a = c[false]
  99. u = c[nil]
  100. return "#{p or 0}, #{a or 0}, #{u or 0}"
  101. end
  102. # Determine whether the passed Person may change this activity.
  103. def may_change?(person)
  104. person.is_admin ||
  105. self.is_organizer?(person) ||
  106. self.group.is_leader?(person)
  107. end
  108. # Create Participants for all People that
  109. # 1. are members of the group
  110. # 2. do not have Participants (and thus, no way to confirm) yet
  111. def create_missing_participants!
  112. people = self.group.people
  113. if not self.participants.empty?
  114. people = people.where('people.id NOT IN (?)', self.people.ids)
  115. end
  116. people.each do |p|
  117. Participant.create(
  118. activity: self,
  119. person: p,
  120. )
  121. end
  122. end
  123. # Create Subgroups from the defaults set using DefaultSubgroups
  124. def copy_default_subgroups!
  125. defaults = self.group.default_subgroups
  126. # If there are no subgroups, there cannot be subgroup division.
  127. self.update_attribute(:subgroup_division_enabled, false) if defaults.none?
  128. defaults.each do |dsg|
  129. sg = Subgroup.new(activity: self)
  130. sg.name = dsg.name
  131. sg.is_assignable = dsg.is_assignable
  132. sg.save! # Should never fail, as DSG and SG have identical validation, and names cannot clash.
  133. end
  134. end
  135. # Create multiple Activities from data in a CSV file, assign to a group, return.
  136. def self.from_csv(content, group)
  137. reader = CSV.parse(content, {headers: true, skip_blanks: true})
  138. result = []
  139. reader.each do |row|
  140. a = Activity.new
  141. a.group = group
  142. a.name = row['name']
  143. a.description = row['description']
  144. a.location = row['location']
  145. sd = Date.strptime(row['start_date'])
  146. st = Time.strptime(row['start_time'], '%H:%M')
  147. a.start = Time.zone.local(sd.year, sd.month, sd.day, st.hour, st.min)
  148. if not row['end_date'].blank?
  149. ed = Date.strptime(row['end_date'])
  150. et = Time.strptime(row['end_time'], '%H:%M')
  151. a.end = Time.zone.local(ed.year, ed.month, ed.day, et.hour, et.min)
  152. end
  153. dd = Date.strptime(row['deadline_date'])
  154. dt = Time.strptime(row['deadline_time'], '%H:%M')
  155. a.deadline = Time.zone.local(dd.year, dd.month, dd.day, dt.hour, dt.min)
  156. result << a
  157. end
  158. result
  159. end
  160. # Send a reminder to all participants who haven't responded, and set their
  161. # response to 'attending'.
  162. def send_reminder
  163. # Sanity check that the reminder date didn't change while queued.
  164. return unless !self.reminder_done && self.reminder_at
  165. return if self.reminder_at > Time.zone.now
  166. participants = self.participants.where(attending: nil)
  167. participants.each { |p| p.send_reminder }
  168. self.reminder_done = true
  169. self.save
  170. end
  171. def schedule_reminder
  172. return if self.reminder_at.nil? || self.reminder_done
  173. self.delay(run_at: self.reminder_at).send_reminder
  174. end
  175. def schedule_subgroup_division
  176. return if self.deadline.nil? || self.subgroup_division_done
  177. self.delay(run_at: self.deadline).assign_subgroups!
  178. end
  179. # Assign a subgroup to all attending participants without one.
  180. def assign_subgroups!
  181. # Sanity check: we need subgroups to divide into.
  182. return unless self.subgroups.any?
  183. # Get participants in random order
  184. ps = self
  185. .participants
  186. .where(attending: true)
  187. .where(subgroup: nil)
  188. .to_a
  189. ps.shuffle!
  190. # Get groups, link to participant count
  191. groups = self
  192. .subgroups
  193. .where(is_assignable: true)
  194. .to_a
  195. .map { |sg| [sg.participants.count, sg] }
  196. ps.each do |p|
  197. # Sort groups so the group with the least participants gets the following participant
  198. groups.sort!
  199. # Assign participant to group with least members
  200. p.subgroup = groups.first.second
  201. p.save
  202. # Update the group's position in the list, will sort when next participant is processed.
  203. groups.first[0] += 1
  204. end
  205. end
  206. private
  207. # Assert that the deadline for participants to change the deadline, if any,
  208. # is set before the event starts.
  209. def deadline_before_start
  210. if self.deadline > self.start
  211. errors.add(:deadline, I18n.t('activities.errors.must_be_before_start'))
  212. end
  213. end
  214. # Assert that the activity's end, if any, occurs after the event's start.
  215. def end_after_start
  216. if self.end < self.start
  217. errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
  218. end
  219. end
  220. # Assert that the reminder for non-response is sent while participants still
  221. # can change their response.
  222. def reminder_before_deadline
  223. if self.reminder_at > self.deadline
  224. errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline'))
  225. end
  226. end
  227. end