Class: Osm::Event

Inherits:
Model
  • Object
show all
Defined in:
lib/osm/event.rb

Defined Under Namespace

Classes: Attendance, Column

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Event



# File 'lib/osm/event.rb', line 83


Instance Attribute Details

#allow_bookingBoolean



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#allow_changesBoolean



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#archivedBoolean



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#attendance_limitFixnum



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#attendance_reminderFixnum



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#columnsArray<Osm::Event::Column>



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#confirm_by_dateDate



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#costString



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#finishDateTime



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#idFixnum



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#locationString



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#nameString



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#notepadString



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#notesString



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#public_notepadString



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#remindersBoolean



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#section_idFixnum



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

#startDateTime



45
# File 'lib/osm/event.rb', line 45

attribute :id, :type => Integer

Class Method Details

.create(api, parameters) ⇒ Osm::Event?

Create an event in OSM

Raises:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/osm/event.rb', line 152

def self.create(api, parameters)
  require_ability_to(api, :write, :events, parameters[:section_id])
  event = new(parameters)
  raise Osm::ObjectIsInvalid, 'event is invalid' unless event.valid?

  data = api.perform_query("events.php?action=addEvent&sectionid=#{event.section_id}", {
    'name' => event.name,
    'location' => event.location,
    'startdate' => event.start? ? event.start.strftime(Osm::OSM_DATE_FORMAT) : '',
    'enddate' => event.finish? ? event.finish.strftime(Osm::OSM_DATE_FORMAT) : '',
    'cost' => event.cost_tbc? ? '-1' : event.cost,
    'notes' => event.notes,
    'starttime' => event.start? ? event.start.strftime(Osm::OSM_TIME_FORMAT) : '',
    'endtime' => event.finish? ? event.finish.strftime(Osm::OSM_TIME_FORMAT) : '',
    'confdate' => event.confirm_by_date? ? event.confirm_by_date.strftime(Osm::OSM_DATE_FORMAT) : '',
    'allowChanges' => event.allow_changes ? 'true' : 'false',
    'disablereminders' => !event.reminders ? 'true' : 'false',
    'attendancelimit' => event.attendance_limit,
    'attendancereminder' => event.attendance_reminder,
    'limitincludesleaders' => event.attendance_limit_includes_leaders ? 'true' : 'false',
    'allowbooking' => event.allow_booking ? 'true' : 'false',
  })

  # The cached events for the section will be out of date - remove them
  cache_delete(api, ['events', event.section_id])
  cache_write(api, ['event', event.id], event)

  if (data.is_a?(Hash) && data.has_key?('id'))
    event.id = data['id'].to_i
    return event
  else
    return nil
  end
end

.get(api, section, event_id, options = {}) ⇒ Osm::Event?

Get an event



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/osm/event.rb', line 133

def self.get(api, section, event_id, options={})
  require_ability_to(api, :read, :events, section, options)
  section_id = section.to_i
  event_id = event_id.to_i
  cache_key = ['event', event_id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  event_data = api.perform_query("events.php?action=getEvent&sectionid=#{section_id}&eventid=#{event_id}")
  return self.new_event_from_data(event_data)
end

.get_for_section(api, section, options = {}) ⇒ Array<Osm::Event>

Get events for a section

Options Hash (options):

  • :include_archived (Boolean) — default: optional

    if true then archived events will also be returned



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/osm/event.rb', line 94

def self.get_for_section(api, section, options={})
  require_ability_to(api, :read, :events, section, options)
  section_id = section.to_i
  cache_key = ['events', section_id]
  events = nil

  if !options[:no_cache] && cache_exist?(api, cache_key)
    ids = cache_read(api, cache_key)
    events = get_from_ids(api, ids, 'event', section, options, :get_for_section)
  end

  if events.nil?
    data = api.perform_query("events.php?action=getEvents&sectionid=#{section_id}&showArchived=true")
    events = Array.new
    ids = Array.new
    unless data['items'].nil?
      data['items'].map { |i| i['eventid'].to_i }.each do |event_id|
        event_data = api.perform_query("events.php?action=getEvent&sectionid=#{section_id}&eventid=#{event_id}")
        event = self.new_event_from_data(event_data)
        events.push event
        ids.push event.id
        cache_write(api, ['event', event.id], event)
      end
    end
    cache_write(api, cache_key, ids)
  end

  return events if options[:include_archived]
  return events.reject do |event|
    event.archived?
  end
end

Instance Method Details

#<=>(another) ⇒ Object

Compare Event based on start, name then id



366
367
368
369
370
371
372
# File 'lib/osm/event.rb', line 366

def <=>(another)
  return 0 if self.id == another.try(:id)
  result = self.start <=> another.try(:start)
  result = self.name <=> another.try(:name) if result == 0
  result = self.id <=> another.try(:id) if result == 0
  return result
end

#add_column(api, name, label = '', required = false) ⇒ Boolean

Add a column to the event in OSM

Raises:



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/osm/event.rb', line 311

def add_column(api, name, label='', required=false)
  require_ability_to(api, :write, :events, section_id)
  raise Osm::ArgumentIsInvalid, 'name is invalid' if name.blank?

  data = api.perform_query("events.php?action=addColumn&sectionid=#{section_id}&eventid=#{id}", {
    'columnName' => name,
    'parentLabel' => label,
    'parentRequire' => (required ? 1 : 0),
  })

  # The cached events for the section will be out of date - remove them
  cache_delete(api, ['events', section_id])
  cache_delete(api, ['event', id])
  cache_delete(api, ['event_attendance', id])

  self.columns = self.class.new_event_from_data(data).columns

  return data.is_a?(Hash) && (data['eventid'].to_i == id)
end

#cost_free?Boolean

Whether the cost is zero



361
362
363
# File 'lib/osm/event.rb', line 361

def cost_free?
  cost.eql?('0.00')
end

#cost_tbc?Boolean

Whether the cost is to be confirmed



355
356
357
# File 'lib/osm/event.rb', line 355

def cost_tbc?
  cost.eql?('TBC')
end

#delete(api) ⇒ Boolean

Delete event from OSM



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/osm/event.rb', line 237

def delete(api)
  require_ability_to(api, :write, :events, section_id)

  data = api.perform_query("events.php?action=deleteEvent&sectionid=#{section_id}&eventid=#{id}")

  if data.is_a?(Hash) && data['ok']
    cache_delete(api, ['event', id])
    return true
  end
  return false
end

#get_attendance(api, term = nil, options = {}) ⇒ Array<Osm::Event::Attendance>

Get event attendance

Options Hash (options):

  • :include_archived (Boolean) — default: optional

    if true then archived activities will also be returned



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/osm/event.rb', line 256

def get_attendance(api, term=nil, options={})
  require_ability_to(api, :read, :events, section_id, options)
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section_id).id : term.to_i
  cache_key = ['event_attendance', id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("events.php?action=getEventAttendance&eventid=#{id}&sectionid=#{section_id}&termid=#{term_id}")
  data = data['items'] || []

  payment_values = {
    'Manual' => :manual,
    'Automatic' => :automatic,
  }
  attending_values = {
    'Yes' => :yes,
    'No' => :no,
    'Invited' => :invited,
    'Show in My.SCOUT' => :shown,
    'Reserved' => :reserved,
  }

  attendance = []
  data.each_with_index do |item, index|
    attendance.push Osm::Event::Attendance.new(
      :event => self,
      :member_id => Osm::to_i_or_nil(item['scoutid']),
      :grouping_id => Osm::to_i_or_nil(item['patrolid'].eql?('') ? nil : item['patrolid']),
      :first_name => item['firstname'],
      :last_name => item['lastname'],
      :date_of_birth => item['dob'].nil? ? nil : Osm::parse_date(item['dob'], :ignore_epoch => true),
      :attending => attending_values[item['attending']],
      :payment_control => payment_values[item['payment']],
      :fields => item.select { |key, value| key.to_s.match(/\Af_\d+\Z/) }
                     .inject({}){ |h,(k,v)| h[k[2..-1].to_i] = v; h },
      :payments => item.select { |key, value| key.to_s.match(/\Ap\d+\Z/) }
                       .inject({}){ |h,(k,v)| h[k[1..-1].to_i] = v; h },
      :row => index,
    )
  end

  cache_write(api, cache_key, attendance)
  return attendance
end

#limited_attendance?Boolean

Whether thete is a limit on attendance for this event



333
334
335
# File 'lib/osm/event.rb', line 333

def limited_attendance?
  (attendance_limit != 0)
end

#spaces(api) ⇒ Fixnum?

Get the number of spaces left for the event



348
349
350
351
# File 'lib/osm/event.rb', line 348

def spaces(api)
  return nil unless limited_attendance?
  return attendance_limit - attendees(api)
end

#spaces?(api) ⇒ Boolean

Whether there are spaces left for the event



340
341
342
343
# File 'lib/osm/event.rb', line 340

def spaces?(api)
  return true unless limited_attendance?
  return attendance_limit > attendees(api)
end

#update(api) ⇒ Boolean

Update event in OSM



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/osm/event.rb', line 190

def update(api)
  require_ability_to(api, :write, :events, section_id)

  to_update = changed_attributes

  data = api.perform_query("events.php?action=addEvent&sectionid=#{section_id}", {
    'eventid' => id,
    'name' => name,
    'location' => location,
    'startdate' => start? ? start.strftime(Osm::OSM_DATE_FORMAT) : '',
    'enddate' => finish? ? finish.strftime(Osm::OSM_DATE_FORMAT) : '',
    'cost' => cost_tbc? ? '-1' : cost,
    'notes' => notes,
    'starttime' => start? ? start.strftime(Osm::OSM_TIME_FORMAT) : '',
    'endtime' => finish? ? finish.strftime(Osm::OSM_TIME_FORMAT) : '',
    'confdate' => confirm_by_date? ? confirm_by_date.strftime(Osm::OSM_DATE_FORMAT) : '',
    'allowChanges' => allow_changes ? 'true' : 'false',
    'disablereminders' => !reminders ? 'true' : 'false',
    'attendancelimit' => attendance_limit,
    'attendancereminder' => attendance_reminder,
    'limitincludesleaders' => attendance_limit_includes_leaders ? 'true' : 'false',
    'allowbooking' => allow_booking ? 'true' : 'false',
  })

  api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
    'eventid' => id,
    'notepad' => notepad,
  }) if to_update.include?('notepad')

  api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
    'eventid' => id,
    'pnnotepad' => public_notepad,
  }) if to_update.include?('public_notepad')

  if data.is_a?(Hash) && (data['id'].to_i == id)
    reset_changed_attributes
    # The cached event will be out of date - remove it
    cache_delete(api, ['event', id])
    return true
  else
    return false
  end
end