Sprankelprachtig aan/afmeldsysteem

activity.rb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. belongs_to :group
  32. has_many :participants,
  33. dependent: :destroy
  34. has_many :people, through: :participants
  35. validates :name, presence: true
  36. validates :start, presence: true
  37. validate :deadline_before_start, unless: "self.deadline.blank?"
  38. validate :end_after_start, unless: "self.end.blank?"
  39. after_create :create_missing_participants!
  40. # Get all people (not participants) that are organizers. Does not include
  41. # group leaders, although they may modify the activity as well.
  42. def organizers
  43. self.participants.includes(:person).where(is_organizer: true)
  44. end
  45. # Determine whether the passed Person participates in the activity.
  46. def is_participant?(person)
  47. Participant.exists?(
  48. activity_id: self.id,
  49. person_id: person.id
  50. )
  51. end
  52. # Determine whether the passed Person is an organizer for the activity.
  53. def is_organizer?(person)
  54. Participant.exists?(
  55. person_id: person.id,
  56. activity_id: self.id,
  57. is_organizer: true
  58. )
  59. end
  60. # Query the database to determine the amount of participants that are present/absent/unknown
  61. def state_counts
  62. self.participants.group(:attending).count
  63. end
  64. # Return participants attending, absent, unknown
  65. def human_state_counts
  66. c = self.state_counts
  67. p = c[true]
  68. a = c[false]
  69. u = c[nil]
  70. return "#{p or 0}, #{a or 0}, #{u or 0}"
  71. end
  72. # Determine whether the passed Person may change this activity.
  73. def may_change?(person)
  74. person.is_admin ||
  75. self.is_organizer?(person) ||
  76. self.group.is_leader?(person)
  77. end
  78. # Create Participants for all People that
  79. # 1. are members of the group
  80. # 2. do not have Participants (and thus, no way to confirm) yet
  81. def create_missing_participants!
  82. people = self.group.people
  83. if not self.participants.empty?
  84. people = people.where('people.id NOT IN (?)', self.people.ids)
  85. end
  86. people.each do |p|
  87. Participant.create(
  88. activity: self,
  89. person: p,
  90. )
  91. end
  92. end
  93. # Create multiple Activities from data in a CSV file, assign to a group, return.
  94. def self.from_csv(content, group)
  95. reader = CSV.parse(content, {headers: true, skip_blanks: true})
  96. result = []
  97. reader.each do |row|
  98. a = Activity.new
  99. a.group = group
  100. a.name = row['name']
  101. a.description = row['description']
  102. a.location = row['location']
  103. sd = Date.strptime(row['start_date'])
  104. st = Time.strptime(row['start_time'], '%H:%M')
  105. a.start = DateTime.new(sd.year, sd.month, sd.day, st.hour, st.min)
  106. if not row['end_date'].blank?
  107. ed = Date.strptime(row['end_date'])
  108. et = Time.strptime(row['end_time'], '%H:%M')
  109. a.end = DateTime.new(ed.year, ed.month, ed.day, et.hour, et.min)
  110. end
  111. dd = Date.strptime(row['deadline_date'])
  112. dt = Time.strptime(row['deadline_time'], '%H:%M')
  113. a.deadline = DateTime.new(dd.year, dd.month, dd.day, dt.hour, dt.min)
  114. result << a
  115. end
  116. result
  117. end
  118. private
  119. # Assert that the deadline for participants to change the deadline, if any,
  120. # is set before the event starts.
  121. def deadline_before_start
  122. if self.deadline > self.start
  123. errors.add(:deadline, I18n.t('activities.errors.must_be_before_start'))
  124. end
  125. end
  126. # Assert that the activity's end, if any, occurs after the event's start.
  127. def end_after_start
  128. if self.end < self.start
  129. errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
  130. end
  131. end
  132. end