Class: Osm::Register

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

Defined Under Namespace

Classes: Attendance, Field

Class Method Summary collapse

Class Method Details

.get_attendance(api, section, term = nil, options = {}) ⇒ Array<Register::Attendance>

Get register attendance

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the register for

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

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

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

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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 = {}) ⇒ Array<Osm::Register::Field>

Get register structure

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Section, Fixnum, #to_i)

    The section (or its ID) to get the structure for

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

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

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

Options Hash (options):

  • :no_cache (Boolean) — default: optional

    if true then the data will be retreived from OSM not the cache

Returns:


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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 = {}) ⇒ 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