Class: Osm::Activity

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

Defined Under Namespace

Classes: Badge, File, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Instance Attribute Details

#badgesArray<Osm::Activity::Badge>

Returns Array<Osm::Activity::Badge>.

Returns:



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#deletableBoolean

Returns Wether the current API user can delete this activity.

Returns:

  • (Boolean)

    Wether the current API user can delete this activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#descriptionString

Returns the description of the activity.

Returns:

  • (String)

    the description of the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#editableBoolean

Returns Wether the current API user can edit this activity.

Returns:

  • (Boolean)

    Wether the current API user can edit this activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#filesArray<Osm::Activity::File>

Returns Array<Osm::Activity::File>.

Returns:



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#group_idFixnum

Returns the group_id.

Returns:

  • (Fixnum)

    the group_id



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#idFixnum

Returns the id for the activity.

Returns:

  • (Fixnum)

    the id for the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#instructionsString

Returns instructions for doing the activity.

Returns:

  • (String)

    instructions for doing the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#locationSymbol

Returns :indoors, :outdoors or :both.

Returns:

  • (Symbol)

    :indoors, :outdoors or :both



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#ratingFixnum

Returns ?.

Returns:

  • (Fixnum)

    ?



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#resourcesString

Returns resources required to do the activity.

Returns:

  • (String)

    resources required to do the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#running_timeFixnum

Returns duration of the activity in minutes.

Returns:

  • (Fixnum)

    duration of the activity in minutes



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#sectionsArray<Symbol>

Returns the sections the activity is appropriate for.

Returns:

  • (Array<Symbol>)

    the sections the activity is appropriate for



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#sharedFixnum

Returns 2 - Public, 0 - Private.

Returns:

  • (Fixnum)

    2 - Public, 0 - Private



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#tagsArray<String>

Returns the tags attached to the activity.

Returns:

  • (Array<String>)

    the tags attached to the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#titleString

Returns the activity’s title.

Returns:

  • (String)

    the activity’s title



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#usedFixnum

Returns How many times this activity has been used (total accross all of OSM).

Returns:

  • (Fixnum)

    How many times this activity has been used (total accross all of OSM)



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#user_idFixnum

Returns the user_id of the creator of the activity.

Returns:

  • (Fixnum)

    the user_id of the creator of the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#versionFixnum

Returns the version of the activity.

Returns:

  • (Fixnum)

    the version of the activity



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

#versionsArray<Osm::Activity::Version>

Returns:



49
# File 'lib/osm/activity.rb', line 49

attribute :id, :type => Integer

Class Method Details

.get(api, activity_id, version = nil, options = {}) ⇒ Osm::Activity

Get activity details

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • activity_id (Fixnum)

    The activity ID

  • version (Fixnum) (defaults to: nil)

    The version of the activity to retreive, if nil the latest version will be assumed

  • 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:



103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/osm/activity.rb', line 103

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) ? Osm::make_array_of_symbols(data['sections']) : []
  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(
      :activity_id => Osm::to_i_or_nil(badge_data['activityid']),
      :section_type => badge_data['section'].to_sym,
      :type => badge_data['badgetype'].to_sym,
      :badge => badge_data['badge'],
      :requirement => badge_data['columnname'],
      :label => badge_data['label']
    )
  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

Instance Method Details

#<=>(another) ⇒ Object

Compare Activity based on id then version



257
258
259
260
261
# File 'lib/osm/activity.rb', line 257

def <=>(another)
  result = self.id <=> another.try(:id)
  result = self.version <=> another.try(:version) if result == 0
  return result
end

#add_to_programme(api, section, date, notes = "") ⇒ Boolean

Add this activity to the programme in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

    The Section (or it’s ID) to add the Activity to

  • date (Date, DateTime)

    The date of the Evening to add the Activity to (OSM will create the Evening if it doesn’t already exist)

  • notes (String) (defaults to: "")

    The notes which should appear for this Activity on this Evening

Returns:

  • (Boolean)

    Whether the activity was successfully added



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/osm/activity.rb', line 200

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

Get the link to display this activity in OSM

Returns:

  • (String)

    the link for this member’s My.SCOUT

Raises:



189
190
191
192
# File 'lib/osm/activity.rb', line 189

def osm_link
  raise Osm::ObjectIsInvalid, 'activity is invalid' unless valid?
  return "https://www.onlinescoutmanager.co.uk/?l=p#{self.id}"
end

#update(api, section, secret_update = false) ⇒ Boolean

Update this activity in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

    The Section (or it’s ID)

  • secret_update (Boolean) (defaults to: false)

    Whether this is a secret update

Returns:

  • (Boolean)

    Whether the activity was successfully added

Raises:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/osm/activity.rb', line 226

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| {'activityid'=>b.activity_id.to_s, 'section'=>b.section_type, 'badgetype'=>b.type, 'badge'=>b.badge, 'columnname'=>b.requirement, 'label'=>b.label}}.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