class Osm::Activity

Constants

SORT_BY

Public Class Methods

get(api, activity_id, version=nil, options={}) click to toggle source

Get activity details @param [Osm::Api] api The api to use to make the request @param [Fixnum] activity_id The activity ID @param [Fixnum] version The version of the activity to retreive, if nil the latest version will be assumed @!macro options_get @return [Osm::Activity]

# File lib/osm/activity.rb, line 108
def self.get(api, activity_id, version=nil, options={})
  cache_key = ['activity', activity_id]

  if !options[:no_cache] && cache_exist?(api, [*cache_key, version])
    activity = cache_read(api, [*cache_key, version])
    if (activity.shared == 2) || (activity.user_id == api.user_id) ||  # Shared or owned by this user
    Osm::Section.get_all(api).map{ |s| s.group_id }.uniq.include?(activity.group_id)  # user belomngs to the group owning the activity
      return activity
    else
      return nil
    end
  end

  data = nil
  if version.nil?
    data = api.perform_query("programme.php?action=getActivity&id=#{activity_id}")
  else
    data = api.perform_query("programme.php?action=getActivity&id=#{activity_id}&version=#{version}")
  end

  attributes = {}
  attributes[:id] = Osm::to_i_or_nil(data['details']['activityid'])
  attributes[:version] = data['details']['version'].to_i
  attributes[:group_id] = Osm::to_i_or_nil(data['details']['groupid'])
  attributes[:user_id] = Osm::to_i_or_nil(data['details']['userid'])
  attributes[:title] = data['details']['title']
  attributes[:description] = data['details']['description']
  attributes[:resources] = data['details']['resources']
  attributes[:instructions] = data['details']['instructions']
  attributes[:running_time] = Osm::to_i_or_nil(data['details']['runningtime'])
  attributes[:location] = data['details']['location'].to_sym
  attributes[:shared] = Osm::to_i_or_nil(data['details']['shared'])
  attributes[:rating] = data['details']['rating'].to_i
  attributes[:editable] = data['editable']
  attributes[:deletable] = data['deletable'] ? true : false
  attributes[:used] = data['used'].to_i
  attributes[:sections] = data['sections'].is_a?(Array) ? data['sections'].map(&:to_sym) : []
  attributes[:tags] = data['tags'].is_a?(Array) ? data['tags'] : []
  attributes[:versions] = []
  attributes[:files] = []
  attributes[:badges] = []

  # Populate Arrays
  (data['files'].is_a?(Array) ? data['files'] : []).each do |file_data|
    attributes[:files].push File.new(
      :id => Osm::to_i_or_nil(file_data['fileid']),
      :activity_id => Osm::to_i_or_nil(file_data['activityid']),
      :file_name => file_data['filename'],
      :name => file_data['name']
    )
  end
  (data['badges'].is_a?(Array) ? data['badges'] : []).each do |badge_data|
    attributes[:badges].push Badge.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
  (data['versions'].is_a?(Array) ? data['versions'] : []).each do |version_data|
    attributes[:versions].push Version.new(
      :version => Osm::to_i_or_nil(version_data['value']),
      :created_by => Osm::to_i_or_nil(version_data['userid']),
      :created_by_name => version_data['firstname'],
      :label => version_data['label']
    )
  end

  activity = Osm::Activity.new(attributes)

  cache_write(api, [*cache_key, nil], activity) if version.nil?
  cache_write(api, [*cache_key, version], activity)
  return activity
end

Public Instance Methods

add_to_programme(api, section, date, notes="") click to toggle source

Add this activity to the programme in OSM @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The Section (or it's ID) to add the Activity to @param [Date, DateTime] date The date of the Evening to add the Activity to (OSM will create the Evening if it doesn't already exist) @param [String] notes The notes which should appear for this Activity on this Evening @return [Boolean] Whether the activity was successfully added

# File lib/osm/activity.rb, line 207
def add_to_programme(api, section, date, notes="")
  require_ability_to(api, :write, :programme, section)

  data = api.perform_query("programme.php?action=addActivityToProgramme", {
    'meetingdate' => date.strftime(Osm::OSM_DATE_FORMAT),
    'activityid' => id,
    'sectionid' => section.to_i,
    'notes' => notes,
  })

  if (data == {'result'=>0})
    # The cached activity will be out of date - remove it
    cache_delete(api, ['activity', self.id])
    return true
  else
    return false
  end
end
update(api, section, secret_update=false) click to toggle source

Update this activity in OSM @param [Osm::Api] api The api to use to make the request @param [Osm::Section, Fixnum, to_i] section The Section (or it's ID) @param [Boolean] secret_update Whether this is a secret update @return [Boolean] Whether the activity was successfully added @raise [Osm::ObjectIsInvalid] If the Activity is invalid @raise [Osm::Forbidden] If the Activity is not editable

# File lib/osm/activity.rb, line 233
def update(api, section, secret_update=false)
  raise Osm::ObjectIsInvalid, 'activity is invalid' unless valid?
  raise Osm::Forbidden, "You are not allowed to update this activity" unless self.editable

  data = api.perform_query("programme.php?action=update", {
    'title' => title,
    'description' => description,
    'resources' => resources,
    'instructions' => instructions,
    'id' => id,
    'files' => files.map{|f| f.id }.join(','),
    'time' => running_time.to_s,
    'location' => location,
    'sections' => sections.to_json,
    'tags' => tags.to_json,
    'links' => badges.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,
        'sections' => sections.map{ |s| s.to_s },
        'badgetype' => b.badge_type.to_s,
        'badgetypeLongName' => nil,
      }
    }.to_json,
    'shared' => shared,
    'sectionid' => section.to_i,
    'secretEdit' => secret_update,
  })

  if (data == {'result'=>true})
    # The cached activity will be out of date - remove it
    cache_delete(api, ['activity', self.id])
    return true
  else
    return false
  end
end