Class: Osm::Member

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

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 Member

Parameters:

  • attributes (Hash)

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



# File 'lib/osm/member.rb', line 213

Instance Attribute Details

#addressString

Returns the member’s address.

Returns:

  • (String)

    the member’s address



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#address2String

Returns the member’s 2nd address.

Returns:

  • (String)

    the member’s 2nd address



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#ageString

Returns the member’s current age (yy/mm).

Returns:

  • (String)

    the member’s current age (yy/mm)



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom1String

Returns the custom1 data for the member.

Returns:

  • (String)

    the custom1 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom2String

Returns the custom2 data for the member.

Returns:

  • (String)

    the custom2 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom3String

Returns the custom3 data for the member.

Returns:

  • (String)

    the custom3 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom4String

Returns the custom4 data for the member.

Returns:

  • (String)

    the custom4 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom5String

Returns the custom5 data for the member.

Returns:

  • (String)

    the custom5 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom6String

Returns the custom6 data for the member.

Returns:

  • (String)

    the custom6 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom7String

Returns the custom7 data for the member.

Returns:

  • (String)

    the custom7 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom8String

Returns the custom8 data for the member.

Returns:

  • (String)

    the custom8 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#custom9String

Returns the custom9 data for the member.

Returns:

  • (String)

    the custom9 data for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#date_of_birthDate

Returns the member’s date of birth.

Returns:

  • (Date)

    the member’s date of birth



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#email1String

Returns the 1st email address for the member.

Returns:

  • (String)

    the 1st email address for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#email2String

Returns the 2nd email address for the member.

Returns:

  • (String)

    the 2nd email address for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#email3String

Returns the 3rd email address for the member.

Returns:

  • (String)

    the 3rd email address for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#email4String

Returns the 4th email address for the member.

Returns:

  • (String)

    the 4th email address for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#ethnicityString

Returns the member’s ethnicity.

Returns:

  • (String)

    the member’s ethnicity



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#first_nameString

Returns the member’s first name.

Returns:

  • (String)

    the member’s first name



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#grouping_idFixnum

Returns the grouping within the section that the member belongs to.

Returns:

  • (Fixnum)

    the grouping within the section that the member belongs to



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#grouping_leaderFixnum

Returns whether the member is the grouping leader (0=no, 1=seconder/APL, 2=sixer/PL).

Returns:

  • (Fixnum)

    whether the member is the grouping leader (0=no, 1=seconder/APL, 2=sixer/PL)



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#idFixnum

Returns the id for the member.

Returns:

  • (Fixnum)

    the id for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#joinedDate

Returns when the member joined the section.

Returns:

  • (Date)

    when the member joined the section



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#joined_yearsFixnum

Returns how many years the member has been in Scouting.

Returns:

  • (Fixnum)

    how many years the member has been in Scouting



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#joining_in_yearsFixnum

Returns ?.

Returns:

  • (Fixnum)

    ?



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#last_nameString

Returns the member’s last name.

Returns:

  • (String)

    the member’s last name



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#medicalString

Returns the member’s key medical details.

Returns:

  • (String)

    the member’s key medical details



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#notesString

Returns notes relating to the member.

Returns:

  • (String)

    notes relating to the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#parentsString

Returns the member’s parent’s names.

Returns:

  • (String)

    the member’s parent’s names



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#phone1String

Returns the 1st phone number for the member.

Returns:

  • (String)

    the 1st phone number for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#phone2String

Returns the 2nd phone number for the member.

Returns:

  • (String)

    the 2nd phone number for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#phone3String

Returns the 3rd phone number for the member.

Returns:

  • (String)

    the 3rd phone number for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#phone4String

Returns the 4th phone number for the member.

Returns:

  • (String)

    the 4th phone number for the member



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#religionString

Returns the member’s religion.

Returns:

  • (String)

    the member’s religion



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#schoolString

Returns the school the member attends.

Returns:

  • (String)

    the school the member attends



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#section_idFixnum

Returns the section the member belongs to.

Returns:

  • (Fixnum)

    the section the member belongs to



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#startedDate

Returns when the member started Scouting.

Returns:

  • (Date)

    when the member started Scouting



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#subsString

Returns details about the member’s subs.

Returns:

  • (String)

    details about the member’s subs



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

#typeString

Returns ?.

Returns:

  • (String)

    ?



84
# File 'lib/osm/member.rb', line 84

attribute :id, :type => Integer

Class Method Details

.get_for_section(api, section, term = nil, options = {}) ⇒ Array<Osm::Member>

Get members for a section

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

  • term (Osm::Term, Fixnum, #to_i, nil) (defaults to: nil)

    The term (or its ID) to get the members 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:



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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/osm/member.rb', line 150

def self.get_for_section(api, section, term=nil, options={})
  require_ability_to(api, :read, :member, section, options)
  section = Osm::Section.get(api, section) if section.is_a?(Fixnum)
  term = -1 if section.waiting?
  term_id = term.nil? ? Osm::Term.get_current_term_for_section(api, section).id : term.to_i
  cache_key = ['members', section.id, term_id]

  if !options[:no_cache] && cache_exist?(api, cache_key)
    return cache_read(api, cache_key)
  end

  data = api.perform_query("users.php?action=getUserDetails&sectionid=#{section.id}&termid=#{term_id}")

  result = Array.new
  data['items'].each do |item|
    result.push Osm::Member.new(
      :section_id => section.id,
      :id => Osm::to_i_or_nil(item['scoutid']),
      :type => item['type'],
      :first_name => item['firstname'],
      :last_name => item['lastname'],
      :email1 => item['email1'],
      :email2 => item['email2'],
      :email3 => item['email3'],
      :email4 => item['email4'],
      :phone1 => item['phone1'],
      :phone2 => item['phone2'],
      :phone3 => item['phone3'],
      :phone4 => item['phone4'],
      :address => item['address'],
      :address2 => item['address2'],
      :date_of_birth => Osm::parse_date(item['dob'], :ignore_epoch => true),
      :started => Osm::parse_date(item['started']),
      :joining_in_years => item['joining_in_yrs'].to_i,
      :parents => item['parents'],
      :notes => item['notes'],
      :medical => item['medical'],
      :religion => item['religion'],
      :school => item['school'],
      :ethnicity => item['ethnicity'],
      :subs => item['subs'],
      :custom1 => item['custom1'],
      :custom2 => item['custom2'],
      :custom3 => item['custom3'],
      :custom4 => item['custom4'],
      :custom5 => item['custom5'],
      :custom6 => item['custom6'],
      :custom7 => item['custom7'],
      :custom8 => item['custom8'],
      :custom9 => item['custom9'],
      :grouping_id => Osm::to_i_or_nil(item['patrolid']),
      :grouping_leader => Osm::to_i_or_nil(item['patrolleader']),
      :joined => Osm::parse_date(item['joined']),
      :age => item['age'].gsub(' ', ''),
      :joined_years => item['yrs'].to_i,
    )
  end

  cache_write(api, cache_key, result)
  return result
end

Instance Method Details

#<=>(another) ⇒ Object

Compare Activity based on section_id, grouping_id, grouping_leader (descending), last_name then first_name



393
394
395
396
397
398
399
400
# File 'lib/osm/member.rb', line 393

def <=>(another)
  result = self.section_id <=> another.try(:section_id)
  result = self.grouping_id <=> another.try(:grouping_id) if result == 0
  result = -(self.grouping_leader <=> another.try(:grouping_leader)) if result == 0
  result = self.last_name <=> another.try(:last_name) if result == 0
  result = self.first_name <=> another.try(:last_name) if result == 0
  return result
end

#age_monthsFixnum

Get the months element of this scout’s age

Returns:

  • (Fixnum)

    the number of months since this scout’s last birthday



359
360
361
# File 'lib/osm/member.rb', line 359

def age_months
  return age[-2..-1].to_i
end

#age_yearsFixnum

Get the years element of this scout’s age

Returns:

  • (Fixnum)

    the number of years this scout has been alive



353
354
355
# File 'lib/osm/member.rb', line 353

def age_years
  return age[0..1].to_i
end

#create(api) ⇒ Boolan

Create the user in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolan)

    whether the member was successfully added or not

Raises:



223
224
225
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/osm/member.rb', line 223

def create(api)
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
  require_ability_to(api, :write, :member, section_id)
  raise Osm::Error, 'the member already exists in OSM' unless id.nil?

  data = api.perform_query("users.php?action=newMember", {
    'firstname' => first_name,
    'lastname' => last_name,
    'dob' => date_of_birth.strftime(Osm::OSM_DATE_FORMAT),
    'started' => started.strftime(Osm::OSM_DATE_FORMAT),
    'startedsection' => joined.strftime(Osm::OSM_DATE_FORMAT),
    'patrolid' => grouping_id,
    'patrolleader' => grouping_leader,
    'sectionid' => section_id,
    'email1' => email1,
    'email2' => email2,
    'email3' => email3,
    'email4' => email4,
    'phone1' => phone1,
    'phone2' => phone2,
    'phone3' => phone3,
    'phone4' => phone4,
    'address' => address,
    'address2' => address2,
    'parents' => parents,
    'notes' => notes,
    'medical' => medical,
    'religion' => religion,
    'school' => school,
    'ethnicity' => ethnicity,
    'subs' => subs,
    'custom1' => custom1,
    'custom2' => custom2,
    'custom3' => custom3,
    'custom4' => custom4,
    'custom5' => custom5,
    'custom6' => custom6,
    'custom7' => custom7,
    'custom8' => custom8,
    'custom9' => custom9,
  })

  if (data.is_a?(Hash) && (data['result'] == 'ok') && (data['scoutid'].to_i > 0))
    self.id = data['scoutid'].to_i
    # The cached members for the section will be out of date - remove them
    Osm::Term.get_for_section(api, section_id).each do |term|
      cache_delete(api, ['members', section_id, term.id])
    end
    return true
  else
    return false
  end
end

Get the My.SCOUT link for this member

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • link_to (Symbol) (defaults to: :badges)

    The page in My.SCOUT to link to (:payments, :events, :programme, :badges or :notice)

Returns:

  • (String)

    the link for this member’s My.SCOUT

Raises:



377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/osm/member.rb', line 377

def myscout_link(api, link_to=:badges)
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
  require_ability_to(api, :read, :member, section_id)
  raise Osm::Error, 'the member does not already exist in OSM' if id.nil?
  raise Osm::ArgumentIsInvalid, 'link_to is invalid' unless [:payments, :events, :programme, :badges, :notice].include?(link_to)

  if @myscout_link_key.nil?
    data = api.perform_query("api.php?action=getMyScoutKey&sectionid=#{section_id}&scoutid=#{self.id}")
    raise Osm::Error, 'Could not retrieve the key for the link from OSM' unless data['ok']
    @myscout_link_key = data['key']
  end

  return "https://www.onlinescoutmanager.co.uk/parents/#{link_to}.php?sc=#{self.id}&se=#{section_id}&c=#{@myscout_link_key}"
end

#name(seperator = ' ') ⇒ String

Get the full name

Parameters:

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

    What to split the scout’s first name and last name with

Returns:

  • (String)

    this scout’s full name seperated by the optional seperator



366
367
368
# File 'lib/osm/member.rb', line 366

def name(seperator=' ')
  return "#{first_name}#{seperator.to_s}#{last_name}"
end

#update(api) ⇒ Boolan

Update the member in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolan)

    whether the member was successfully updated or not

Raises:



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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/osm/member.rb', line 281

def update(api)
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
  require_ability_to(api, :write, :member, section_id)

  to_update = changed_attributes
  values = {}
  values['firstname']      = first_name if to_update.include?('first_name')
  values['lastname']       = last_name  if to_update.include?('last_name')
  values['dob']            = date_of_birth.strftime(Osm::OSM_DATE_FORMAT) if to_update.include?('date_of_birth')
  values['started']        = started.strftime(Osm::OSM_DATE_FORMAT) if to_update.include?('started')
  values['startedsection'] = joined.strftime(Osm::OSM_DATE_FORMAT) if to_update.include?('joined')
  values['email1']         = email1     if to_update.include?('email1')
  values['email2']         = email2     if to_update.include?('email2')
  values['email3']         = email3     if to_update.include?('email3')
  values['email4']         = email4     if to_update.include?('email4')
  values['phone1']         = phone1     if to_update.include?('phone1')
  values['phone2']         = phone2     if to_update.include?('phone2')
  values['phone3']         = phone3     if to_update.include?('phone3')
  values['phone4']         = phone4     if to_update.include?('phone3')
  values['address']        = address    if to_update.include?('address')
  values['address2']       = address2   if to_update.include?('address2')
  values['parents']        = parents    if to_update.include?('parents')
  values['notes']          = notes      if to_update.include?('notes')
  values['medical']        = medical    if to_update.include?('medical')
  values['religion']       = religion   if to_update.include?('religion')
  values['school']         = school     if to_update.include?('school')
  values['ethnicity']      = ethnicity  if to_update.include?('ethnicity')
  values['subs']           = subs       if to_update.include?('subs')
  values['custom1']        = custom1    if to_update.include?('custom1')
  values['custom2']        = custom2    if to_update.include?('custom2')
  values['custom3']        = custom3    if to_update.include?('custom3')
  values['custom4']        = custom4    if to_update.include?('custom4')
  values['custom5']        = custom5    if to_update.include?('custom5')
  values['custom6']        = custom6    if to_update.include?('custom6')
  values['custom7']        = custom7    if to_update.include?('custom7')
  values['custom8']        = custom8    if to_update.include?('custom8')
  values['custom9']        = custom9    if to_update.include?('custom9')

  result = true
  values.each do |column, value|
    data = api.perform_query("users.php?action=updateMember&dateFormat=generic", {
      'scoutid' => self.id,
      'column' => column,
      'value' => value,
      'sectionid' => section_id,
    })
    result &= (data[column] == value.to_s)
  end

  if to_update.include?('grouping_id') || to_update.include?('grouping_leader')
    data = api.perform_query("users.php?action=updateMemberPatrol", {
      'scoutid' => self.id,
      'patrolid' => grouping_id,
      'pl' => grouping_leader,
      'sectionid' => section_id,
    })
    result &= ((data['patrolid'].to_i == grouping_id) && (data['patrolleader'].to_i == grouping_leader))
  end

  if result
    reset_changed_attributes
    # The cached columns for the flexi record will be out of date - remove them
    Osm::Term.get_for_section(api, section_id).each do |term|
      Osm::Model.cache_delete(api, ['members', section_id, term.id])
    end
  end

  return result
end