Class: Osm::Meeting

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

Defined Under Namespace

Classes: Activity, BadgeLink

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 Meeting


# File 'lib/osm/meeting.rb', line 61

Instance Attribute Details

#activitiesArray<Activity>


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#dateDate


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#finish_timeString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#gamesString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#idFixnum


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#leadersString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#notes_for_parentsString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#post_notesString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#pre_notesString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#section_idFixnum


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#start_timeString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

#titleString


34
# File 'lib/osm/meeting.rb', line 34

attribute :id, :type => Integer

Class Method Details

.create(api, parameters) ⇒ Osm::Meeting?

Create a meeting in OSM


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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 = {}) ⇒ Array<Osm::Meeting>

Get the programme for a given term

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
# 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

Instance Method Details

#<=>(another) ⇒ Object

Compare Meeting based on section_id, date, start_time then id


322
323
324
325
326
327
328
329
330
331
332
333
# 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 = '') ⇒ Boolean

Add an activity to this meeting in OSM


236
237
238
239
240
241
242
243
244
245
246
247
248
249
# 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) ⇒ Boolean

Delete meeting from OSM


254
255
256
257
258
259
260
261
262
263
264
# 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 = {}) ⇒ Array<Hash>?

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.

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# 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) ⇒ Boolean

Update an meeting in OSM

Raises:


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# 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