Sprankelprachtig aan/afmeldsysteem

activity.rb 10KB

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