Method: Osm::Register.update_attendance

Defined in:
lib/osm/register.rb

.update_attendance(data = {}) ⇒ Boolean

Update attendance for an evening in OSM

Parameters:

  • data (Hash) (defaults to: {})

Options Hash (data):

  • :api (Osm::Api)

    The api to use to make the request

  • :section (Osm::Section)

    the section to update the register for

  • :term (Osm::Term, #to_i, nil)

    The term (or its ID) to get the register for, passing nil causes the current term to be used

  • :evening (Osm::Evening, DateTime, Date)

    the evening to update the register on

  • :attendance (Symbol)

    what to mark the attendance as, one of :yes, :unadvised_absent or :advised_absent

  • :members (Fixnum, Array<Fixnum>, Osm::Member, Array<Osm::Member>)

    the members (or their ids) to update

  • :completed_badge_requirements (Array<Hash>) — default: optional

    the badge requirements to mark as completed, selected from the Hash returned by the get_badge_requirements_for_evening method

Returns:

  • (Boolean)

    whether the update succedded

Raises:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/osm/register.rb', line 107

def self.update_attendance(data={})
  raise Osm::ArgumentIsInvalid, ':attendance is invalid' unless [:yes, :unadvised_absent, :advised_absent].include?(data[:attendance])
  raise Osm::ArgumentIsInvalid, ':section is missing' if data[:section].nil?
  raise Osm::ArgumentIsInvalid, ':evening is missing' if data[:evening].nil?
  raise Osm::ArgumentIsInvalid, ':members is missing' if data[:members].nil?
  raise Osm::ArgumentIsInvalid, ':api is missing' if data[:api].nil?
  api = data[:api]
  Osm::Model.require_ability_to(api, :write, :register, data[:section])

  term_id = data[:term].nil? ? Osm::Term.get_current_term_for_section(api, section).id : data[:term].to_i

  data[:members] = [*data[:members]].map{ |member| (member.is_a?(Fixnum) ? member : member.id).to_s } # Make sure it's an Array of Strings

  response = api.perform_query("users.php?action=registerUpdate&sectionid=#{data[:section].id}&termid=#{term_id}", {
    'scouts' => data[:members].inspect,
    'selectedDate' => data[:evening].strftime(Osm::OSM_DATE_FORMAT),
    'present' => {:yes => 'Yes', :unadvised_absent => nil, :advised_absent => 'No'}[data[:attendance]],
    'section' => data[:section].type,
    'sectionid' => data[:section].id,
    'completedBadges' => (data[:completed_badge_requirements] || []).to_json
  })

  # The cached attendance will be out of date - remove them
  Osm::Model.cache_delete(api, ['register_attendance', data[:section].id, term_id])

  return response.is_a?(Array)
end