Method: Osm::Event#update

Defined in:
lib/osm/event.rb

#update(api) ⇒ Boolean

Update event in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the update succedded (will return true if no updates needed to be made)



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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/osm/event.rb', line 269

def update(api)
  require_ability_to(api, :write, :events, section_id)
   updated = true

  # Update main attributes
  update_main_attributes = false
  %w{ id name location start finish cost cost_tbc notes confirm_by_date allow_changes reminders attendance_limit attendance_limit_includes_leaders allow_booking }.each do |a|
    if changed_attributes.include?(a)
      update_main_attributes = true
      break # no use checking the others
    end
  end
  if update_main_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',
    })
    updated &= data.is_a?(Hash) && (data['id'].to_i == id)
  end

  # Private notepad
  if changed_attributes.include?('notepad')
    data = api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
      'eventid' => id,
      'notepad' => notepad,
    })
    updated &= data.is_a?(Hash)
  end

  # MySCOUT notepad
  if changed_attributes.include?('public_notepad')
    data = api.perform_query("events.php?action=saveNotepad&sectionid=#{section_id}", {
      'eventid' => id,
      'pnnotepad' => public_notepad,
    })
    updated &= data.is_a?(Hash)
  end

  # Badges
  if changed_attributes.include?('badges')
    original_badges = @original_attributes['badges'] || []

    # Deleted badges
    badges_to_delete = []
    original_badges.each do |badge|
      unless badges.include?(badge)
        badges_to_delete.push badge
      end
    end
    badges_to_delete.each do |badge|
      data = api.perform_query("ext/badges/records/index.php?action=deleteBadgeLink&sectionid=#{section_id}", {
        'section' => badge.badge_section,
        'sectionid' => section_id,
        'type' => 'event',
        'id' => id,
        'badge_id' => badge.badge_id,
        'badge_version' => badge.badge_version,
        'column_id' => badge.requirement_id,
      })
      updated &= data.is_a?(Hash) && data['status']
    end

    # Added badges
    badges.each do |badge|
      unless original_badges.include?(badge)
        updated &= add_badge_link(api, badge)
      end
    end
  end # includes badges

  if updated
    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