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