class Osm::Meeting

Public Class Methods

create(api, parameters) click to toggle source

Create a meeting in OSM @param [Osm::Api] api The api to use to make the request @return [Osm::Meeting, nil] the created meeting, nil if failed

# File lib/osm/meeting.rb, line 147
def self.create(api, parameters)
  require_ability_to(api, :write, :programme, parameters[:section_id])
  meeting = new(parameters)

  data = api.perform_query("programme.php?action=addActivityToProgramme", {
    'meetingdate' => meeting.date.strftime(Osm::OSM_DATE_FORMAT),
    'sectionid' => meeting.section_id,
    'activityid' => -1,
    'start' => meeting.date.strftime(Osm::OSM_DATE_FORMAT),
    'starttime' => meeting.start_time,
    'endtime' => meeting.finish_time,
    'title' => meeting.title,
  })

  # The cached programmes for the section will be out of date - remove them
  Osm::Term.get_for_section(api, meeting.section_id).each do |term|
    cache_delete(api, ['programme', meeting.section_id, term.id])
  end

  return data.is_a?(Hash) ? meeting : nil
end
get_for_section(api, section, term=nil, options={}) click to toggle source

Get the programme for a given term @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 programme for @param [Osm::term, Fixnum, nil] term The term (or its ID) to get the programme for, passing nil causes the current term to be used @!macro options_get @return [Array<Osm::Meeting>]

# File lib/osm/meeting.rb, line 72
def self.get_for_section(api, section, term=nil, options={})
  require_ability_to(api, :read, :programme, 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 = ['programme', section_id, term_id]

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

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

  result = Array.new
  data = {'items'=>[],'activities'=>{}} if data.is_a? Array
  items = data['items'] || []
  activities = data['activities'] || {}
  badge_links = data['badgelinks'] || {}

  items.each do |item|
    attributes = {}
    attributes[:id] = Osm::to_i_or_nil(item['eveningid'])
    attributes[:section_id] = Osm::to_i_or_nil(item['sectionid'])
    attributes[:title] = item['title'] || 'Unnamed meeting'
    attributes[:notes_for_parents] = item['notesforparents'] || ''
    attributes[:games] = item['games'] || ''
    attributes[:pre_notes] = item['prenotes'] || ''
    attributes[:post_notes] = item['postnotes'] || ''
    attributes[:leaders] = item['leaders'] || ''
    attributes[:start_time] = item['starttime'].nil? ? nil : item['starttime'][0..4]
    attributes[:finish_time] = item['endtime'].nil? ? nil : item['endtime'][0..4]
    attributes[:date] = Osm::parse_date(item['meetingdate'])

    our_activities = activities[item['eveningid']]
    attributes[:activities] = Array.new
    unless our_activities.nil?
      our_activities.each do |activity_data|
        if activity_data.is_a?(Array)
          activity_data = activity_data.find{ |a| a.is_a?(Hash) && a.has_key?('activityid') }
        end
        attributes[:activities].push Osm::Meeting::Activity.new(
          :activity_id => Osm::to_i_or_nil(activity_data['activityid']),
          :title => activity_data['title'],
          :notes => activity_data['notes'],
        )
      end
    end

    our_badge_links = badge_links[item['eveningid']]
    attributes[:badge_links] = Array.new
    unless our_badge_links.nil?
      our_badge_links.each do |badge_data|
        attributes[:badge_links].push Osm::Meeting::BadgeLink.new(
          :badge_type => badge_data['badgetype'].to_sym,
          :badge_section => badge_data['section'].to_sym,
          :badge_name => badge_data['badgeLongName'],
          :badge_id => Osm::to_i_or_nil(badge_data['badge_id']),
          :badge_version => Osm::to_i_or_nil(badge_data['badge_version']),
          :requirement_id => Osm::to_i_or_nil(badge_data['column_id']),
          :requirement_label => badge_data['columnnameLongName'],
          :data => badge_data['data'],
        )
      end
    end

    result.push new(attributes)
  end

  cache_write(api, cache_key, result)
  return result
end

Public Instance Methods

<=>(another) click to toggle source

Compare Meeting based on section_id, date, start_time then id

# File lib/osm/meeting.rb, line 322
def <=>(another)
  result = self.section_id <=> another.try(:section_id)
  result = self.date <=> another.try(:date) if result == 0
  if result == 0
    my_start_time = self.start_time.split(':').map{ |i| i.to_i }
    another_start_time = another.start_time.split(':').map{ |i| i.to_i }
    result = my_start_time[0] <=> another_start_time[0] if result == 0
    result = compare = my_start_time[1] <=> another_start_time[1] if result == 0
  end  
  result = self.id <=> another.try(:id) if result == 0
  return result
end
add_activity(api, activity, notes='') click to toggle source

Add an activity to this meeting in OSM @param [Osm::Api] api The api to use to make the request @param [Osm::Activity] activity The Activity to add to the Meeting @param [String] notes The notes which should appear for this Activity on this Meeting @return [Boolean] Whether the activity ws successfully added

# File lib/osm/meeting.rb, line 236
def add_activity(api, activity, notes='')
  if activity.add_to_programme(api, section_id, date, notes)
    activities.push Osm::Meeting::Activity.new(:activity_id => activity.id, :notes => notes, :title => activity.title)

    # The cached programmes for the section will be out of date - remove them
    Osm::Term.get_for_section(api, section_id).each do |term|
      cache_delete(api, ['programme', section_id, term.id]) if term.contains_date?(date)
    end

    return true
  end

  return false
end
delete(api) click to toggle source

Delete meeting from OSM @param [Osm::Api] api The api to use to make the request @return [Boolean] true

# File lib/osm/meeting.rb, line 254
def delete(api)
  require_ability_to(api, :write, :programme, section_id)
  data = api.perform_query("programme.php?action=deleteEvening&eveningid=#{id}&sectionid=#{section_id}")

  # The cached programmes for the section will be out of date - remove them
  Osm::Term.get_for_section(api, section_id).each do |term|
    cache_delete(api, ['programme', section_id, term.id]) if term.contains_date?(date)
  end

  return true
end
get_badge_requirements(api, options={}) click to toggle source

Get the badge requirements met on a specific meeting Requires either write permission to badges (prefered as it's one OSM query) or read permission to programme. @param [Osm::Api] api The api to use to make the request @!macro options_get @return [Array<Hash>] hashes ready to pass into the update_register method @return [nil] if something went wrong

# File lib/osm/meeting.rb, line 274
def get_badge_requirements(api, options={})
  section = Osm::Section.get(api, section_id)
  cache_key = ['badge_requirements', section.id, id]
  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end
  badges = nil

  if has_permission?(api, :write, :badge, section_id, options)
    # We can shortcut and do it in one query
    badges = api.perform_query("users.php?action=getActivityRequirements&date=#{date.strftime(Osm::OSM_DATE_FORMAT)}&sectionid=#{section.id}&section=#{section.type}")

  else
    # We'll have to iterate through the activities
    require_ability_to(api, :read, :programme, section_id, options)

    links = badge_links
    activities.each do |activity|
      activity = Osm::Activity.get(api, activity.activity_id, nil, options)
      links += activity.badges
    end

    badges = []
    links.each do |badge|
      badges.push({
        "badge" => nil,#"activity_animalcarer",
        "badge_id" => badge.badge_id,
        "badge_version" => badge.badge_version,
        "column_id" => badge.requirement_id,
        "badgeName" => badge.badge_name,
        "badgetype" => badge.badge_type,
        "columngroup" => nil,#"A",
        "columnname" => nil,#"a",
        "data" => badge.data,
        "eveningid" => id,
        "meetingdate" => date,
        "name" => badge.requirement_label,
        "section" => badge.badge_section,
        "sectionid" => section_id
      })
    end
  end

  cache_write(api, cache_key, badges) unless badges.nil?
  return badges
end
update(api) click to toggle source

Update an meeting in OSM @param [Osm::Api] api The api to use to make the request @return [Boolean] if the operation suceeded or not @raise [Osm::ObjectIsInvalid] If the Meeting is invalid

# File lib/osm/meeting.rb, line 174
def update(api)
  raise Osm::ObjectIsInvalid, 'meeting is invalid' unless valid?
  require_ability_to(api, :write, :programme, section_id)

  activities_data = Array.new
  activities.each do |activity|
    this_activity = {
      'activityid' => activity.activity_id,
      'notes' => activity.notes,
    }
    activities_data.push this_activity
  end

  api_data = {
    'eveningid' => id,
    'sectionid' => section_id,
    'meetingdate' => date.strftime(Osm::OSM_DATE_FORMAT),
    'starttime' => start_time,
    'endtime' => finish_time,
    'title' => title,
    'notesforparents' => notes_for_parents,
    'prenotes' => pre_notes,
    'postnotes' => post_notes,
    'games' => games,
    'leaders' => leaders,
    'activity' => ActiveSupport::JSON.encode(activities_data),
    'badgelinks' => ActiveSupport::JSON.encode(badge_links.map{ |b|
      {
        'badge_id' => b.badge_id.to_s,
        'badge_version' => b.badge_version.to_s,
        'column_id' => b.requirement_id.to_s,
        'badge' => nil,
        'badgeLongName' => b.badge_name,
        'columnname' => nil,
        'columnnameLongName' => b.requirement_label,
        'data' => b.data,
        'section' => b.badge_section,
        'sectionLongName' => nil,
        'badgetype' => b.badge_type.to_s,
        'badgetypeLongName' => nil,
      }
    })
  }
  response = api.perform_query("programme.php?action=editEvening", api_data)

  if response.is_a?(Hash) && (response['result'] == 0)
    reset_changed_attributes
    # The cached programmes for the section will be out of date - remove them
    Osm::Term.get_for_section(api, section_id).each do |term|
      cache_delete(api, ['programme', section_id, term.id]) if term.contains_date?(date)
    end
    return true
  else
    return false
  end
end