Sprankelprachtig aan/afmeldsysteem

activity.rb 10KB

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