|
@@ -87,17 +87,17 @@ class Activity < ApplicationRecord
|
87
|
87
|
# Get all people (not participants) that are organizers. Does not include
|
88
|
88
|
# group leaders, although they may modify the activity as well.
|
89
|
89
|
def organizers
|
90
|
|
- self.participants.includes(:person).where(is_organizer: true)
|
|
90
|
+ participants.includes(:person).where(is_organizer: true)
|
91
|
91
|
end
|
92
|
92
|
|
93
|
93
|
def organizer_names
|
94
|
|
- self.organizers.map { |o| o.person.full_name }
|
|
94
|
+ organizers.map { |o| o.person.full_name }
|
95
|
95
|
end
|
96
|
96
|
|
97
|
97
|
# Determine whether the passed Person participates in the activity.
|
98
|
98
|
def participant?(person)
|
99
|
99
|
Participant.exists?(
|
100
|
|
- activity_id: self.id,
|
|
100
|
+ activity_id: id,
|
101
|
101
|
person_id: person.id
|
102
|
102
|
)
|
103
|
103
|
end
|
|
@@ -106,19 +106,19 @@ class Activity < ApplicationRecord
|
106
|
106
|
def organizer?(person)
|
107
|
107
|
Participant.exists?(
|
108
|
108
|
person_id: person.id,
|
109
|
|
- activity_id: self.id,
|
|
109
|
+ activity_id: id,
|
110
|
110
|
is_organizer: true
|
111
|
111
|
)
|
112
|
112
|
end
|
113
|
113
|
|
114
|
114
|
# Query the database to determine the amount of participants that are present/absent/unknown
|
115
|
115
|
def state_counts
|
116
|
|
- self.participants.group(:attending).count
|
|
116
|
+ participants.group(:attending).count
|
117
|
117
|
end
|
118
|
118
|
|
119
|
119
|
# Return participants attending, absent, unknown
|
120
|
120
|
def human_state_counts
|
121
|
|
- c = self.state_counts
|
|
121
|
+ c = state_counts
|
122
|
122
|
p = c[true]
|
123
|
123
|
a = c[false]
|
124
|
124
|
u = c[nil]
|
|
@@ -128,16 +128,16 @@ class Activity < ApplicationRecord
|
128
|
128
|
# Determine whether the passed Person may change this activity.
|
129
|
129
|
def may_change?(person)
|
130
|
130
|
person.is_admin ||
|
131
|
|
- self.organizer?(person) ||
|
132
|
|
- self.group.leader?(person)
|
|
131
|
+ organizer?(person) ||
|
|
132
|
+ group.leader?(person)
|
133
|
133
|
end
|
134
|
134
|
|
135
|
135
|
# Create Participants for all People that
|
136
|
136
|
# 1. are members of the group
|
137
|
137
|
# 2. do not have Participants (and thus, no way to confirm) yet
|
138
|
138
|
def create_missing_participants!
|
139
|
|
- people = self.group.people
|
140
|
|
- people = people.where('people.id NOT IN (?)', self.people.ids) unless self.participants.empty?
|
|
139
|
+ people = group.people
|
|
140
|
+ people = people.where('people.id NOT IN (?)', self.people.ids) unless participants.empty?
|
141
|
141
|
|
142
|
142
|
people.each do |p|
|
143
|
143
|
Participant.create(
|
|
@@ -149,10 +149,10 @@ class Activity < ApplicationRecord
|
149
|
149
|
|
150
|
150
|
# Create Subgroups from the defaults set using DefaultSubgroups
|
151
|
151
|
def copy_default_subgroups!
|
152
|
|
- defaults = self.group.default_subgroups
|
|
152
|
+ defaults = group.default_subgroups
|
153
|
153
|
|
154
|
154
|
# If there are no subgroups, there cannot be subgroup division.
|
155
|
|
- self.update_attribute(:subgroup_division_enabled, false) if defaults.none?
|
|
155
|
+ update_attribute(:subgroup_division_enabled, false) if defaults.none?
|
156
|
156
|
|
157
|
157
|
defaults.each do |dsg|
|
158
|
158
|
sg = Subgroup.new(activity: self)
|
|
@@ -210,48 +210,48 @@ class Activity < ApplicationRecord
|
210
|
210
|
# response to 'attending'.
|
211
|
211
|
def send_reminder
|
212
|
212
|
# Sanity check that the reminder date didn't change while queued.
|
213
|
|
- return unless !self.reminder_done && self.reminder_at
|
214
|
|
- return if self.reminder_at > Time.zone.now
|
|
213
|
+ return unless !reminder_done && reminder_at
|
|
214
|
+ return if reminder_at > Time.zone.now
|
215
|
215
|
|
216
|
216
|
participants = self.participants.where(attending: nil)
|
217
|
217
|
participants.each(&:send_reminder)
|
218
|
218
|
|
219
|
219
|
self.reminder_done = true
|
220
|
|
- self.save
|
|
220
|
+ save
|
221
|
221
|
end
|
222
|
222
|
|
223
|
223
|
def schedule_reminder
|
224
|
|
- return if self.reminder_at.nil? || self.reminder_done
|
|
224
|
+ return if reminder_at.nil? || reminder_done
|
225
|
225
|
|
226
|
|
- self.delay(run_at: self.reminder_at).send_reminder
|
|
226
|
+ delay(run_at: reminder_at).send_reminder
|
227
|
227
|
end
|
228
|
228
|
|
229
|
229
|
def schedule_subgroup_division
|
230
|
|
- return if self.deadline.nil? || self.subgroup_division_done
|
|
230
|
+ return if deadline.nil? || subgroup_division_done
|
231
|
231
|
|
232
|
|
- self.delay(run_at: self.deadline).assign_subgroups!(mail: true)
|
|
232
|
+ delay(run_at: deadline).assign_subgroups!(mail: true)
|
233
|
233
|
end
|
234
|
234
|
|
235
|
235
|
# Assign a subgroup to all attending participants without one.
|
236
|
236
|
def assign_subgroups!(mail = false)
|
237
|
237
|
# Sanity check: we need subgroups to divide into.
|
238
|
|
- return unless self.subgroups.any?
|
|
238
|
+ return unless subgroups.any?
|
239
|
239
|
|
240
|
240
|
# Get participants in random order
|
241
|
|
- ps = self
|
242
|
|
- .participants
|
243
|
|
- .where(attending: true)
|
244
|
|
- .where(subgroup: nil)
|
245
|
|
- .to_a
|
|
241
|
+ ps =
|
|
242
|
+ participants
|
|
243
|
+ .where(attending: true)
|
|
244
|
+ .where(subgroup: nil)
|
|
245
|
+ .to_a
|
246
|
246
|
|
247
|
247
|
ps.shuffle!
|
248
|
248
|
|
249
|
249
|
# Get groups, link to participant count
|
250
|
|
- groups = self
|
251
|
|
- .subgroups
|
252
|
|
- .where(is_assignable: true)
|
253
|
|
- .to_a
|
254
|
|
- .map { |sg| [sg.participants.count, sg] }
|
|
250
|
+ groups =
|
|
251
|
+ subgroups
|
|
252
|
+ .where(is_assignable: true)
|
|
253
|
+ .to_a
|
|
254
|
+ .map { |sg| [sg.participants.count, sg] }
|
255
|
255
|
|
256
|
256
|
ps.each do |p|
|
257
|
257
|
# Sort groups so the group with the least participants gets the following participant
|
|
@@ -265,21 +265,21 @@ class Activity < ApplicationRecord
|
265
|
265
|
groups.first[0] += 1
|
266
|
266
|
end
|
267
|
267
|
|
268
|
|
- self.notify_subgroups! if mail
|
|
268
|
+ notify_subgroups! if mail
|
269
|
269
|
end
|
270
|
270
|
|
271
|
271
|
def clear_subgroups!(only_assignable = true)
|
272
|
|
- sgs = self
|
273
|
|
- .subgroups
|
|
272
|
+ sgs =
|
|
273
|
+ subgroups
|
274
|
274
|
|
275
|
275
|
if only_assignable
|
276
|
276
|
sgs = sgs
|
277
|
277
|
.where(is_assignable: true)
|
278
|
278
|
end
|
279
|
279
|
|
280
|
|
- ps = self
|
281
|
|
- .participants
|
282
|
|
- .where(subgroup: sgs)
|
|
280
|
+ ps =
|
|
281
|
+ participants
|
|
282
|
+ .where(subgroup: sgs)
|
283
|
283
|
|
284
|
284
|
ps.each do |p|
|
285
|
285
|
p.subgroup = nil
|
|
@@ -289,10 +289,10 @@ class Activity < ApplicationRecord
|
289
|
289
|
|
290
|
290
|
# Notify participants of the current subgroups, if any.
|
291
|
291
|
def notify_subgroups!
|
292
|
|
- ps = self
|
293
|
|
- .participants
|
294
|
|
- .joins(:person)
|
295
|
|
- .where.not(subgroup: nil)
|
|
292
|
+ ps =
|
|
293
|
+ participants
|
|
294
|
+ .joins(:person)
|
|
295
|
+ .where.not(subgroup: nil)
|
296
|
296
|
|
297
|
297
|
ps.each(&:send_subgroup_notification)
|
298
|
298
|
end
|
|
@@ -302,22 +302,22 @@ class Activity < ApplicationRecord
|
302
|
302
|
# Assert that the deadline for participants to change the deadline, if any,
|
303
|
303
|
# is set before the event starts.
|
304
|
304
|
def deadline_before_start
|
305
|
|
- errors.add(:deadline, I18n.t('activities.errors.must_be_before_start')) if self.deadline > self.start
|
|
305
|
+ errors.add(:deadline, I18n.t('activities.errors.must_be_before_start')) if deadline > start
|
306
|
306
|
end
|
307
|
307
|
|
308
|
308
|
# Assert that the activity's end, if any, occurs after the event's start.
|
309
|
309
|
def end_after_start
|
310
|
|
- errors.add(:end, I18n.t('activities.errors.must_be_after_start')) if self.end < self.start
|
|
310
|
+ errors.add(:end, I18n.t('activities.errors.must_be_after_start')) if self.end < start
|
311
|
311
|
end
|
312
|
312
|
|
313
|
313
|
# Assert that the reminder for non-response is sent while participants still
|
314
|
314
|
# can change their response.
|
315
|
315
|
def reminder_before_deadline
|
316
|
|
- errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline')) if self.reminder_at > self.deadline
|
|
316
|
+ errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline')) if reminder_at > deadline
|
317
|
317
|
end
|
318
|
318
|
|
319
|
319
|
# Assert that there is at least one divisible subgroup.
|
320
|
320
|
def subgroups_for_division_present
|
321
|
|
- errors.add(:subgroup_division_enabled, I18n.t('activities.errors.cannot_divide_without_subgroups')) if self.subgroups.where(is_assignable: true).none? && subgroup_division_enabled?
|
|
321
|
+ errors.add(:subgroup_division_enabled, I18n.t('activities.errors.cannot_divide_without_subgroups')) if subgroups.where(is_assignable: true).none? && subgroup_division_enabled?
|
322
|
322
|
end
|
323
|
323
|
end
|