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

Constructor Details

#initializeObject

Initialize a new Term


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

Instance Attribute Details

#badgesArray<Osm::Activity::Badge>


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

attribute :id, :type => Integer

#deletableBoolean


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

attribute :id, :type => Integer

#descriptionString


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

attribute :id, :type => Integer

#editableBoolean


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

attribute :id, :type => Integer

#filesArray<Osm::Activity::File>


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

attribute :id, :type => Integer

#group_idFixnum


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

attribute :id, :type => Integer

#idFixnum


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

attribute :id, :type => Integer

#instructionsString


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

attribute :id, :type => Integer

#locationSymbol


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

attribute :id, :type => Integer

#ratingFixnum


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

attribute :id, :type => Integer

#resourcesString


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

attribute :id, :type => Integer

#running_timeFixnum


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

attribute :id, :type => Integer

#sectionsArray<Symbol>


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

attribute :id, :type => Integer

#sharedFixnum


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

attribute :id, :type => Integer

#tagsArray<String>


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

attribute :id, :type => Integer

#titleString


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

attribute :id, :type => Integer

#usedFixnum


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

attribute :id, :type => Integer

#user_idFixnum


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

attribute :id, :type => Integer

#versionFixnum


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

attribute :id, :type => Integer

#versionsArray<Osm::Activity::Version>


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

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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


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
179
180
181
182
183
# File 'lib/osm/activity.rb', line 106

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

Instance Method Details

#<=>(another) ⇒ Object

Compare Activity based on id then version


278
279
280
281
282
# File 'lib/osm/activity.rb', line 278

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


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/osm/activity.rb', line 205

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

Raises:


194
195
196
197
# File 'lib/osm/activity.rb', line 194

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

Raises:


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/osm/activity.rb', line 231

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