Sprankelprachtig aan/afmeldsysteem

activity.rb 9.3KB

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