Class: Osm::Meeting

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

Defined Under Namespace

Classes: Activity

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

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



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

Instance Attribute Details

#activitiesArray<Activity>

Returns list of activities being done during the meeting.

Returns:

  • (Array<Activity>)

    list of activities being done during the meeting



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

attribute :id, :type => Integer

#dateDate

Returns the date of the meeting.

Returns:

  • (Date)

    the date of the meeting



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

attribute :id, :type => Integer

#finish_timeString

Returns the end time (hh:mm).

Returns:

  • (String)

    the end time (hh:mm)



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

attribute :id, :type => Integer

#gamesString

Returns games to be played during the meeting.

Returns:

  • (String)

    games to be played during the meeting



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

attribute :id, :type => Integer

#idFixnum

Returns the id of the meeting.

Returns:

  • (Fixnum)

    the id of the meeting



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

attribute :id, :type => Integer

#leadersString

Returns the leaders present at the meeting.

Returns:

  • (String)

    the leaders present at the meeting



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

attribute :id, :type => Integer

#notes_for_parentsString

Returns notes to be shared with parents.

Returns:

  • (String)

    notes to be shared with parents



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

attribute :id, :type => Integer

#post_notesString

Returns notes for the end of the meeting.

Returns:

  • (String)

    notes for the end of the meeting



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

attribute :id, :type => Integer

#pre_notesString

Returns notes for the start of the meeting.

Returns:

  • (String)

    notes for the start of the meeting



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

attribute :id, :type => Integer

#section_idFixnum

Returns the section the meeting belongs to.

Returns:

  • (Fixnum)

    the section the meeting belongs to



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

attribute :id, :type => Integer

#start_timeString

Returns the start time (hh:mm).

Returns:

  • (String)

    the start time (hh:mm)



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

attribute :id, :type => Integer

#titleString

Returns the title of the meeting.

Returns:

  • (String)

    the title of the meeting



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

attribute :id, :type => Integer

Class Method Details

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

Create a meeting in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Osm::Meeting, nil)

    the created meeting, nil if failed



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/osm/meeting.rb', line 120

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

    The section (or its ID) to get the programme for

  • term (Osm::term, Fixnum, nil) (defaults to: nil)

    The term (or its ID) to get the programme for, passing nil causes the current term to be used

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



66
67
68
69
70
71
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
# File 'lib/osm/meeting.rb', line 66

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'] || {}

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



245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/osm/meeting.rb', line 245

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • activity (Osm::Activity)

    The Activity to add to the Meeting

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

    The notes which should appear for this Activity on this Meeting

Returns:

  • (Boolean)

    Whether the activity ws successfully added



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/osm/meeting.rb', line 194

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    true



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/osm/meeting.rb', line 212

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

Parameters:

  • api (Osm::Api)

    The api to use to make the request

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

  • (Array<Hash>)

    hashes ready to pass into the update_register method



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/osm/meeting.rb', line 229

def get_badge_requirements(api, options={})
  require_ability_to(api, :read, :programme, section_id, 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

  data = api.perform_query("users.php?action=getActivityRequirements&date=#{date.strftime(Osm::OSM_DATE_FORMAT)}&sectionid=#{section.id}&section=#{section.type}")

  cache_write(api, cache_key, data)
  return data
end

#update(api) ⇒ Boolean

Update an meeting in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    if the operation suceeded or not

Raises:



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
184
185
186
187
# File 'lib/osm/meeting.rb', line 147

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
  activities_data = ActiveSupport::JSON.encode(activities_data)

  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' => activities_data,
  }
  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