class Osm::Register

Public Class Methods

get_attendance(api, section, term=nil, options={}) click to toggle source

Get register attendance @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get the register for @param [Osm::Term, Fixnum, to_i, nil] term The term (or its ID) to get the register for, passing nil causes the current term to be used @!macro options_get @return [Array<Register::Attendance>] representing the attendance of each member

# File lib/osm/register.rb, line 47
def self.get_attendance(api, section, term=nil, options={})
  Osm::Model.require_ability_to(api, :read, :register, section, options)
  section_id = section.to_i
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['register_attendance', section_id, term_id]

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

  data = api.perform_query("users.php?action=register&sectionid=#{section_id}&termid=#{term_id}")
  dates_s = get_structure(api, section, term, options)
  dates_s = dates_s.map{ |f| f.id }
  dates_d = dates_s.map{ |d| Osm::parse_date(d) }

  to_return = []
  if data.is_a?(Hash) && data['items'].is_a?(Array)
    data = data['items']
    data.each do |item|
      if item.is_a?(Hash)
        unless item['scoutid'].to_i < 0  # It's a total row
          attendance = {}
          dates_d.each_with_index do |date, index|
            item_attendance = item[dates_s[index]]
            attendance[date] = :unadvised_absent
            attendance[date] = :yes if item_attendance.eql?('Yes')
            attendance[date] = :advised_absent if item_attendance.eql?('No')
          end
          to_return.push Osm::Register::Attendance.new(
            :member_id => Osm::to_i_or_nil(item['scoutid']),
            :grouping_id => Osm::to_i_or_nil(item ['patrolid']),
            :section_id => section_id,
            :first_name => item['firstname'],
            :last_name => item['lastname'],
            :total => item['total'].to_i,
            :attendance => attendance,
          )
        end
      end
    end
    Osm::Model.cache_write(api, cache_key, to_return)
  end
  return to_return
end
get_structure(api, section, term=nil, options={}) click to toggle source

Get register structure @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The section (or its ID) to get the structure for @param [Osm::Term, Fixnum, to_i, nil] term The term (or its ID) to get the structure for, passing nil causes the current term to be used @!macro options_get @return [Array<Osm::Register::Field>] representing the fields of the register

# File lib/osm/register.rb, line 11
def self.get_structure(api, section, term=nil, options={})
  Osm::Model.require_ability_to(api, :read, :register, section, options)
  section_id = section.to_i
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['register_structure', section_id, term_id]

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

  data = api.perform_query("users.php?action=registerStructure&sectionid=#{section_id}&termid=#{term_id}")

  structure = []
  if data.is_a?(Array)
    data = (data.size == 2) ? data[1] : []
    if data.is_a?(Hash) && data['rows'].is_a?(Array)
      data['rows'].each do |row|
        structure.push Field.new(
          :id => row['field'],
          :name => row['name'],
          :tooltip => row['tooltip'],
        )
      end
    end
  end

  Osm::Model.cache_write(api, cache_key, structure) unless structure.nil?
  return structure
end
update_attendance(data={}) click to toggle source

Update attendance for an evening in OSM @param [Hash] data @option data [Osm::Api] :api The api to use to make the request @option data [Osm::Section] :section the section to update the register for @option data [Osm::Term, to_i, nil] :term The term (or its ID) to get the register for, passing nil causes the current term to be used @option data [Osm::Evening, DateTime, Date] :evening the evening to update the register on @option data [Symbol] :attendance what to mark the attendance as, one of :yes, :unadvised_absent or :advised_absent @option data [Fixnum, Array<Fixnum>, Osm::Member, Array<Osm::Member>] :members the members (or their ids) to update @option data [Array<Hash>] :completed_badge_requirements (optional) the badge requirements to mark as completed, selected from the Hash returned by the get_badge_requirements_for_evening method @return [Boolean] whether the update succedded @raise [Osm::ArgumentIsInvalid] If data is not “Yes”, “No” or “Absent” @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing @raise [Osm::ArgumentIsInvalid] If data is missing

# 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