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 226

Instance Attribute Details

#addressString

Returns the member’s address.

Returns:

  • (String)

    the member’s address



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

attribute :id, :type => Integer

#address2String

Returns the member’s 2nd address.

Returns:

  • (String)

    the member’s 2nd address



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

attribute :id, :type => Integer

#ageString

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

Returns:

  • (String)

    the member’s current age (yy/mm)



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

attribute :id, :type => Integer

#custom1String

Returns the custom1 data for the member.

Returns:

  • (String)

    the custom1 data for the member



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

attribute :id, :type => Integer

#custom2String

Returns the custom2 data for the member.

Returns:

  • (String)

    the custom2 data for the member



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

attribute :id, :type => Integer

#custom3String

Returns the custom3 data for the member.

Returns:

  • (String)

    the custom3 data for the member



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

attribute :id, :type => Integer

#custom4String

Returns the custom4 data for the member.

Returns:

  • (String)

    the custom4 data for the member



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

attribute :id, :type => Integer

#custom5String

Returns the custom5 data for the member.

Returns:

  • (String)

    the custom5 data for the member



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

attribute :id, :type => Integer

#custom6String

Returns the custom6 data for the member.

Returns:

  • (String)

    the custom6 data for the member



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

attribute :id, :type => Integer

#custom7String

Returns the custom7 data for the member.

Returns:

  • (String)

    the custom7 data for the member



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

attribute :id, :type => Integer

#custom8String

Returns the custom8 data for the member.

Returns:

  • (String)

    the custom8 data for the member



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

attribute :id, :type => Integer

#custom9String

Returns the custom9 data for the member.

Returns:

  • (String)

    the custom9 data for the member



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

attribute :id, :type => Integer

#date_of_birthDate

Returns the member’s date of birth.

Returns:

  • (Date)

    the member’s date of birth



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

attribute :id, :type => Integer

#email1String

Returns the 1st email address for the member.

Returns:

  • (String)

    the 1st email address for the member



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

attribute :id, :type => Integer

#email2String

Returns the 2nd email address for the member.

Returns:

  • (String)

    the 2nd email address for the member



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

attribute :id, :type => Integer

#email3String

Returns the 3rd email address for the member.

Returns:

  • (String)

    the 3rd email address for the member



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

attribute :id, :type => Integer

#email4String

Returns the 4th email address for the member.

Returns:

  • (String)

    the 4th email address for the member



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

attribute :id, :type => Integer

#ethnicityString

Returns the member’s ethnicity.

Returns:

  • (String)

    the member’s ethnicity



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

attribute :id, :type => Integer

#first_nameString

Returns the member’s first name.

Returns:

  • (String)

    the member’s first name



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

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



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

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)



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

attribute :id, :type => Integer

#has_photoBoolean

Returns whether the scout has a photo in OSM.

Returns:

  • (Boolean)

    whether the scout has a photo in OSM



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

attribute :id, :type => Integer

#idFixnum

Returns the id for the member.

Returns:

  • (Fixnum)

    the id for the member



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

attribute :id, :type => Integer

#joinedDate

Returns when the member joined the section.

Returns:

  • (Date)

    when the member joined the section



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

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



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

attribute :id, :type => Integer

#joining_in_yearsFixnum

Returns ?.

Returns:

  • (Fixnum)

    ?



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

attribute :id, :type => Integer

#last_nameString

Returns the member’s last name.

Returns:

  • (String)

    the member’s last name



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

attribute :id, :type => Integer

#medicalString

Returns the member’s key medical details.

Returns:

  • (String)

    the member’s key medical details



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

attribute :id, :type => Integer

#notesString

Returns notes relating to the member.

Returns:

  • (String)

    notes relating to the member



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

attribute :id, :type => Integer

#parentsString

Returns the member’s parent’s names.

Returns:

  • (String)

    the member’s parent’s names



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

attribute :id, :type => Integer

#phone1String

Returns the 1st phone number for the member.

Returns:

  • (String)

    the 1st phone number for the member



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

attribute :id, :type => Integer

#phone2String

Returns the 2nd phone number for the member.

Returns:

  • (String)

    the 2nd phone number for the member



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

attribute :id, :type => Integer

#phone3String

Returns the 3rd phone number for the member.

Returns:

  • (String)

    the 3rd phone number for the member



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

attribute :id, :type => Integer

#phone4String

Returns the 4th phone number for the member.

Returns:

  • (String)

    the 4th phone number for the member



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

attribute :id, :type => Integer

#religionString

Returns the member’s religion.

Returns:

  • (String)

    the member’s religion



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

attribute :id, :type => Integer

#schoolString

Returns the school the member attends.

Returns:

  • (String)

    the school the member attends



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

attribute :id, :type => Integer

#section_idFixnum

Returns the section the member belongs to.

Returns:

  • (Fixnum)

    the section the member belongs to



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

attribute :id, :type => Integer

#startedDate

Returns when the member started Scouting.

Returns:

  • (Date)

    when the member started Scouting



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

attribute :id, :type => Integer

#subsString

Returns details about the member’s subs.

Returns:

  • (String)

    details about the member’s subs



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

attribute :id, :type => Integer

#typeString

Returns ?.

Returns:

  • (String)

    ?



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

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:



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
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/osm/member.rb', line 157

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}")
  summary_data = api.perform_query("ext/members/contact/?action=getListOfMembers&sort=patrolid&sectionid=#{section.id}&termid=#{term_id}&section=#{section.type}") || {}

  summary_data = summary_data['items'] || []
  summary_data = Hash[summary_data.map { |i| [i['scoutid'].to_i, i] }]

  result = Array.new
  data['items'].each do |item|
    id = Osm::to_i_or_nil(item['scoutid'])
    result.push Osm::Member.new(
      :section_id => section.id,
      :id => id,
      :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,
      :has_photo => summary_data[id]['pic']
    )
  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



459
460
461
462
463
464
465
466
# File 'lib/osm/member.rb', line 459

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



372
373
374
# File 'lib/osm/member.rb', line 372

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



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

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:



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
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/osm/member.rb', line 236

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_photo(api, black_and_white = false, options = {}) ⇒ Object

Get the member’s photo

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • black_and_white (Boolean) (defaults to: false)

    Whether you want the photo in blank and white

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

  • the photo of the member

Raises:

  • (Osm:Error)

    if the member has no photo or doesn’t exist in OSM



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/osm/member.rb', line 420

def get_photo(api, black_and_white=false, options={})
  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::Error, "the member doesn't have a photo in OSM" unless has_photo

  cache_key = ['member_photo', self.id, black_and_white]

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

  url = "ext/members/contact/images/member.php?sectionid=#{section_id}&scoutid=#{self.id}&bw=#{black_and_white}"
  image = api.perform_query(url)

  cache_write(api, cache_key, image) unless image.nil?
  return image
end

#leader?Boolean

Check if the member is in the leaders grouping

Returns:

  • (Boolean)


385
386
387
# File 'lib/osm/member.rb', line 385

def leader?
  grouping_id.eql?(-2)
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, :notice, :details, :census or :giftaid)

  • item_id (#to_i) (defaults to: nil)

    Allows you to link to a specfic item (only for :events)

Returns:

  • (String)

    the URL for this member’s My.SCOUT

Raises:



447
448
449
450
451
452
453
454
455
456
# File 'lib/osm/member.rb', line 447

def myscout_link(api, link_to=:badges, item_id=nil)
  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, :details, :census, :giftaid].include?(link_to)

  link = "#{api.base_url}/parents/#{link_to}.php?sc=#{self.id}&se=#{section_id}&c=#{myscout_link_key(api)}"
  link += "&e=#{item_id.to_i}" if item_id && link_to.eql?(:events)
  return link
end

Get the Key to use in My.SCOUT links for this member

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (String)

    the key

Raises:

  • (Osm::ObjectIsInvalid)

    If the Member is invalid

  • (Osm::Error)

    if the member does not already exist in OSM or the member’s My.SCOUT key could not be retrieved from OSM



400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/osm/member.rb', line 400

def myscout_link_key(api)
  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?

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



379
380
381
# File 'lib/osm/member.rb', line 379

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:



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
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/osm/member.rb', line 294

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

#youth?Boolean

Check if the member is in a non-leaders grouping

Returns:

  • (Boolean)


391
392
393
# File 'lib/osm/member.rb', line 391

def youth?
  grouping_id > 0
end