Class: Osm::Member

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

Defined Under Namespace

Modules: EmailableContact, EnableableEmailableContact, EnableablePhoneableContact Classes: Contact, DoctorContact, EmergencyContact, MemberContact, PrimaryContact, SecondaryContact

Constant Summary collapse

GID_PRIMARY_CONTACT =

Constants for group id

1
GID_SECONDARY_CONTACT =
2
GID_EMERGENCY_CONTACT =
3
GID_DOCTOR_CONTACT =
4
GID_CUSTOM =
5
GID_MEMBER_CONTACT =
6
GID_FLOATING =
7
CID_FIRST_NAME =

Constants for column id

2
CID_LAST_NAME =
3
CID_ADDRESS_1 =
7
CID_ADDRESS_2 =
8
CID_ADDRESS_3 =
9
CID_ADDRESS_4 =
10
CID_POSTCODE =
11
CID_EMAIL_1 =
12
CID_RECIEVE_EMAIL_1 =
13
CID_EMAIL_2 =
14
CID_RECIEVE_EMAIL_2 =
15
CID_PHONE_1 =
18
CID_RECIEVE_PHONE_1 =
19
CID_PHONE_2 =
20
CID_RECIEVE_PHONE_2 =
21
CID_GENDER =
34
CID_SURGERY =
54
CORE_FIELD_IDS =
(1..21).to_a + [34, 54]

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


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

Instance Attribute Details

#additional_informationDirtyHashy


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

attribute :id, :type => Integer

#additional_information_labelsDirtyHashy


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

attribute :id, :type => Integer

#ageString


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

attribute :id, :type => Integer

#contactOsm::Member::MemberContact?


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

attribute :id, :type => Integer

#date_of_birthDate


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

attribute :id, :type => Integer

#doctorOsm::Member::DoctorContact?


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

attribute :id, :type => Integer

#emergency_contactOsm::Member::EmergencyContact?


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

attribute :id, :type => Integer

#finished_sectionDate


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

attribute :id, :type => Integer

#first_nameString


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

attribute :id, :type => Integer

#genderSymbol


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

attribute :id, :type => Integer

#grouping_idFixnum


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

attribute :id, :type => Integer

#grouping_labelString


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

attribute :id, :type => Integer

#grouping_leaderFixnum


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

attribute :id, :type => Integer

#grouping_leader_labelString


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

attribute :id, :type => Integer

#idFixnum


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

attribute :id, :type => Integer

#joined_movementDate


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

attribute :id, :type => Integer

#last_nameString


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

attribute :id, :type => Integer

#primary_contactOsm::Member::PrimaryContact?


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

attribute :id, :type => Integer

#secondary_contactOsm::Member::SecondaryContact?


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

attribute :id, :type => Integer

#section_idFixnum


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

attribute :id, :type => Integer

#started_sectionDate


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

attribute :id, :type => Integer

Class Method Details

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

Get members for a section

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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


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

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

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

  result = Array.new

  api_response = api.perform_query('ext/members/contact/grid/?action=getMembers', {
    'section_id' => section.to_i,
    'term_id' => term.to_i,
  })

  data = api_response['data'].is_a?(Hash) ? api_response['data'].values : []
  structure = (api_response['meta'] || {})['structure'] || []
  structure = Hash[ structure.map{ |i| [i['group_id'].to_i, i ] } ] # Make a hash of identifier to group data hash

  custom_labels = {}
  key_key = 'column_id'   # the key in the data from OSM to use as the key in additional_information and labels hashes
  structure.each do |gid, group|
    columns = group['columns'] || []
    custom_labels[gid.to_i] = Hash[ columns.map.select{ |a| gid.eql?(GID_CUSTOM) || !CORE_FIELD_IDS.include?(a['column_id'].to_i) }.map{ |c| [c[key_key], c['label']] } ]
  end

  data.each do |item|
    item_data = Hash[ item['custom_data'].map{ |k,v| [k.to_i, v] } ]
    member_contact = item_data[GID_MEMBER_CONTACT].nil? ? nil : Hash[ item_data[GID_MEMBER_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ]
    member_custom = item_data[GID_MEMBER_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_MEMBER_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [k.to_i, v] } ]
    primary_contact = item_data[GID_PRIMARY_CONTACT].nil? ? nil : Hash[ item_data[GID_PRIMARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ]
    primary_custom = item_data[GID_PRIMARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_PRIMARY_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [k.to_i, v] } ]
    secondary_contact = item_data[GID_SECONDARY_CONTACT].nil? ? nil : Hash[ item_data[GID_SECONDARY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ]
    secondary_custom = item_data[GID_SECONDARY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_SECONDARY_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [k.to_i, v] } ]
    emergency_contact = item_data[GID_EMERGENCY_CONTACT].nil? ? nil : Hash[ item_data[GID_EMERGENCY_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ]
    emergency_custom = item_data[GID_EMERGENCY_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_EMERGENCY_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [k.to_i, v] } ]
    doctor_contact = item_data[GID_DOCTOR_CONTACT].nil? ? nil : Hash[ item_data[GID_DOCTOR_CONTACT].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ]
    doctor_custom = item_data[GID_DOCTOR_CONTACT].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_DOCTOR_CONTACT].select{ |k,v| !CORE_FIELD_IDS.include?(k.to_i) }.map{ |k,v| [k.to_i, v] } ]
    floating_data = item_data[GID_FLOATING].nil? ? {} : Hash[ item_data[GID_FLOATING].map{ |k,v| [k.to_i, v] }.select{ |k,v| CORE_FIELD_IDS.include?(k) } ]
    custom_data = item_data[GID_CUSTOM].nil? ? DirtyHashy.new : DirtyHashy[ item_data[GID_CUSTOM].map{ |k,v| [k.to_i, v] } ]

    result.push Osm::Member.new(
      :id => Osm::to_i_or_nil(item['member_id']),
      :section_id => Osm::to_i_or_nil(item['section_id']),
      :first_name => item['first_name'],
      :last_name => item['last_name'],
      :grouping_id => Osm::to_i_or_nil(item['patrol_id']),
      :grouping_label => item['patrol'],
      :grouping_leader => item['patrol_role_level'],
      :grouping_leader_label => item['patrol_role_level_label'],
      :age => item['age'],
      :date_of_birth => Osm::parse_date(item['date_of_birth'], :ignore_epoch => true),
      :started_section => Osm::parse_date(item['joined']),
      :finished_section => Osm::parse_date(item['end_date']),
      :joined_movement => Osm::parse_date(item['started']),
      :gender => {'male'=>:male, 'female'=>:female, 'other'=>:other, 'unspecified'=>:unspecified}[(floating_data[CID_GENDER] || '').downcase],
      :contact => member_contact.nil? ? nil : MemberContact.new(
        first_name: item['first_name'],
        last_name: item['last_name'],
        address_1: member_contact[CID_ADDRESS_1],
        address_2: member_contact[CID_ADDRESS_2],
        address_3: member_contact[CID_ADDRESS_3],
        address_4: member_contact[CID_ADDRESS_4],
        postcode: member_contact[CID_POSTCODE],
        phone_1: member_contact[CID_PHONE_1],
        phone_2: member_contact[CID_PHONE_2],
        email_1: member_contact[CID_EMAIL_1],
        email_2: member_contact[CID_EMAIL_2],
        receive_phone_1: member_contact[CID_RECIEVE_PHONE_1],
        receive_phone_2: member_contact[CID_RECIEVE_PHONE_2],
        receive_email_1: member_contact[CID_RECIEVE_EMAIL_1],
        receive_email_2: member_contact[CID_RECIEVE_EMAIL_2],
        additional_information: member_custom,
        additional_information_labels: custom_labels[GID_MEMBER_CONTACT],
      ),
      :primary_contact => primary_contact.nil? ? nil : PrimaryContact.new(
        first_name: primary_contact[CID_FIRST_NAME],
        last_name: primary_contact[CID_LAST_NAME],
        address_1: primary_contact[CID_ADDRESS_1],
        address_2: primary_contact[CID_ADDRESS_2],
        address_3: primary_contact[CID_ADDRESS_3],
        address_4: primary_contact[CID_ADDRESS_4],
        postcode: primary_contact[CID_POSTCODE],
        phone_1: primary_contact[CID_PHONE_1],
        phone_2: primary_contact[CID_PHONE_2],
        email_1: primary_contact[CID_EMAIL_1],
        email_2: primary_contact[CID_EMAIL_2],
        receive_phone_1: primary_contact[CID_RECIEVE_PHONE_1],
        receive_phone_2: primary_contact[CID_RECIEVE_PHONE_2],
        receive_email_1: primary_contact[CID_RECIEVE_EMAIL_1],
        receive_email_2: primary_contact[CID_RECIEVE_EMAIL_2],
        additional_information: primary_custom,
        additional_information_labels: custom_labels[GID_PRIMARY_CONTACT],
      ),
      :secondary_contact => secondary_contact.nil? ? nil : SecondaryContact.new(
        first_name: secondary_contact[CID_FIRST_NAME],
        last_name: secondary_contact[CID_LAST_NAME],
        address_1: secondary_contact[CID_ADDRESS_1],
        address_2: secondary_contact[CID_ADDRESS_2],
        address_3: secondary_contact[CID_ADDRESS_3],
        address_4: secondary_contact[CID_ADDRESS_4],
        postcode: secondary_contact[CID_POSTCODE],
        phone_1: secondary_contact[CID_PHONE_1],
        phone_2: secondary_contact[CID_PHONE_2],
        email_1: secondary_contact[CID_EMAIL_1],
        email_2: secondary_contact[CID_EMAIL_2],
        receive_phone_1: secondary_contact[CID_RECIEVE_PHONE_1],
        receive_phone_2: secondary_contact[CID_RECIEVE_PHONE_2],
        receive_email_1: secondary_contact[CID_RECIEVE_EMAIL_1],
        receive_email_2: secondary_contact[CID_RECIEVE_EMAIL_2],
        additional_information: secondary_custom,
        additional_information_labels: custom_labels[GID_SECONDARY_CONTACT],
      ),
      :emergency_contact => emergency_contact.nil? ? nil : EmergencyContact.new(
        first_name: emergency_contact[CID_FIRST_NAME],
        last_name: emergency_contact[CID_LAST_NAME],
        address_1: emergency_contact[CID_ADDRESS_1],
        address_2: emergency_contact[CID_ADDRESS_2],
        address_3: emergency_contact[CID_ADDRESS_3],
        address_4: emergency_contact[CID_ADDRESS_4],
        postcode: emergency_contact[CID_POSTCODE],
        phone_1: emergency_contact[CID_PHONE_1],
        phone_2: emergency_contact[CID_PHONE_2],
        email_1: emergency_contact[CID_EMAIL_1],
        email_2: emergency_contact[CID_EMAIL_2],
        additional_information: emergency_custom,
        additional_information_labels: custom_labels[GID_EMERGENCY_CONTACT],
      ),
      :doctor => doctor_contact.nil? ? nil : DoctorContact.new(
        first_name: doctor_contact[CID_FIRST_NAME],
        last_name: doctor_contact[CID_LAST_NAME],
        surgery: doctor_contact[CID_SURGERY],
        address_1: doctor_contact[CID_ADDRESS_1],
        address_2: doctor_contact[CID_ADDRESS_2],
        address_3: doctor_contact[CID_ADDRESS_3],
        address_4: doctor_contact[CID_ADDRESS_4],
        postcode: doctor_contact[CID_POSTCODE],
        phone_1: doctor_contact[CID_PHONE_1],
        phone_2: doctor_contact[CID_PHONE_2],
        additional_information: doctor_custom,
        additional_information_labels: custom_labels[GID_DOCTOR_CONTACT],
      ),
      additional_information: custom_data,
      additional_information_labels: custom_labels[GID_CUSTOM],
    )
  end

  cache_write(api, cache_key, result)
  return result
end

Instance Method Details

#<=>(another) ⇒ Object

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


556
557
558
559
560
561
562
563
# File 'lib/osm/member.rb', line 556

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


419
420
421
# File 'lib/osm/member.rb', line 419

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

#age_yearsFixnum

Get the years element of this scout's age


413
414
415
# File 'lib/osm/member.rb', line 413

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

#all_emailsArray<String>

Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all phone numbers from all contacts for the member (not emergency or doctor) Get an array of enabled phone numbers from all contacts for the member (not emergency or doctor)


483
484
485
486
487
488
489
490
491
# File 'lib/osm/member.rb', line 483

[:all_emails, :all_emails_with_name, :enabled_emails, :enabled_emails_with_name, :all_phones, :enabled_phones].each do |meth|
  define_method meth do
    items = []
    [:contact, :primary_contact, :secondary_contact].each do |cont|
      items.push *send(cont).send(meth)
    end
    return items
  end
end

#all_emails_with_nameArray<String>

Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all phone numbers from all contacts for the member (not emergency or doctor) Get an array of enabled phone numbers from all contacts for the member (not emergency or doctor)


483
484
485
486
487
488
489
490
491
# File 'lib/osm/member.rb', line 483

[:all_emails, :all_emails_with_name, :enabled_emails, :enabled_emails_with_name, :all_phones, :enabled_phones].each do |meth|
  define_method meth do
    items = []
    [:contact, :primary_contact, :secondary_contact].each do |cont|
      items.push *send(cont).send(meth)
    end
    return items
  end
end

#all_phonesArray<String>

Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all phone numbers from all contacts for the member (not emergency or doctor) Get an array of enabled phone numbers from all contacts for the member (not emergency or doctor)


483
484
485
486
487
488
489
490
491
# File 'lib/osm/member.rb', line 483

[:all_emails, :all_emails_with_name, :enabled_emails, :enabled_emails_with_name, :all_phones, :enabled_phones].each do |meth|
  define_method meth do
    items = []
    [:contact, :primary_contact, :secondary_contact].each do |cont|
      items.push *send(cont).send(meth)
    end
    return items
  end
end

#create(api) ⇒ Boolan?

Create the user in OSM

Raises:


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
# File 'lib/osm/member.rb', line 305

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

  data = api.perform_query("users.php?action=newMember", {
    'firstname' => first_name,
    'lastname' => last_name,
    'dob' => date_of_birth.strftime(Osm::OSM_DATE_FORMAT),
    'started' => joined_movement.strftime(Osm::OSM_DATE_FORMAT),
    'startedsection' => started_section.strftime(Osm::OSM_DATE_FORMAT),
    'sectionid' => section_id,
  })

  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    # Now it's created we need to give OSM the rest of the data

    updated = update(api, true)
    return updated ? true : nil
  else
    return false
  end
end

#current?(date = Date.today) ⇒ Boolean

Check if this is a current member of the section they were retrieved for


457
458
459
460
461
462
463
# File 'lib/osm/member.rb', line 457

def current?(date=Date.today)
  if finished_section.nil?
    return (started_section <= date)
  else
    return (started_section <= date) && (finished_section >= date)
  end
end

#enabled_emailsArray<String>

Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all phone numbers from all contacts for the member (not emergency or doctor) Get an array of enabled phone numbers from all contacts for the member (not emergency or doctor)


483
484
485
486
487
488
489
490
491
# File 'lib/osm/member.rb', line 483

[:all_emails, :all_emails_with_name, :enabled_emails, :enabled_emails_with_name, :all_phones, :enabled_phones].each do |meth|
  define_method meth do
    items = []
    [:contact, :primary_contact, :secondary_contact].each do |cont|
      items.push *send(cont).send(meth)
    end
    return items
  end
end

#enabled_emails_with_nameArray<String>

Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all phone numbers from all contacts for the member (not emergency or doctor) Get an array of enabled phone numbers from all contacts for the member (not emergency or doctor)


483
484
485
486
487
488
489
490
491
# File 'lib/osm/member.rb', line 483

[:all_emails, :all_emails_with_name, :enabled_emails, :enabled_emails_with_name, :all_phones, :enabled_phones].each do |meth|
  define_method meth do
    items = []
    [:contact, :primary_contact, :secondary_contact].each do |cont|
      items.push *send(cont).send(meth)
    end
    return items
  end
end

#enabled_phonesArray<String>

Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all email addresses from all contacts for the member (not emergency or doctor) Get an array of all email addresses from all contacts for the member in a format which includes the contact's name (not emergency or doctor) Get an array of all phone numbers from all contacts for the member (not emergency or doctor) Get an array of enabled phone numbers from all contacts for the member (not emergency or doctor)


483
484
485
486
487
488
489
490
491
# File 'lib/osm/member.rb', line 483

[:all_emails, :all_emails_with_name, :enabled_emails, :enabled_emails_with_name, :all_phones, :enabled_phones].each do |meth|
  define_method meth do
    items = []
    [:contact, :primary_contact, :secondary_contact].each do |cont|
      items.push *send(cont).send(meth)
    end
    return items
  end
end

#female?Boolean

Check if the member is male


450
451
452
# File 'lib/osm/member.rb', line 450

def female?
  gender == :female
end

#get_photo(api, black_and_white = !current?,, options = {}) ⇒ Object

Get the member's photo

Options Hash (options):

  • :no_cache (Boolean) — default: optional

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

Raises:

  • (Osm:Error)

    if the member doesn't exist in OSM


518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/osm/member.rb', line 518

def get_photo(api, black_and_white=!current?, 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?

  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


432
433
434
# File 'lib/osm/member.rb', line 432

def leader?
  grouping_id.eql?(-2)
end

#male?Boolean

Check if the member is male


444
445
446
# File 'lib/osm/member.rb', line 444

def male?
  gender == :male
end

Get the My.SCOUT link for this member

Raises:


544
545
546
547
548
549
550
551
552
553
# File 'lib/osm/member.rb', line 544

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

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


498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/osm/member.rb', line 498

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


426
427
428
# File 'lib/osm/member.rb', line 426

def name(seperator=' ')
  return [first_name, last_name].select{ |i| !i.blank? }.join(seperator)
end

#update(api, force = false) ⇒ Boolean

Update the member in OSM

Raises:


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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/osm/member.rb', line 338

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

  updated = true

  # Do core attributes
  attribute_map = [
    ['first_name', 'firstname', first_name],
    ['last_name', 'lastname', last_name],
    ['grouping_id', 'patrolid', grouping_id],
    ['grouping_leader', 'patrolleader', grouping_leader],
    ['date_of_birth', 'dob', date_of_birth.strftime(Osm::OSM_DATE_FORMAT)],
    ['started_section', 'startedsection', started_section.strftime(Osm::OSM_DATE_FORMAT)],
    ['joined_movement', 'started', joined_movement.strftime(Osm::OSM_DATE_FORMAT)],
  ] # our name => OSM name
  attribute_map.select{ |attr,col,val| force || changed_attributes.include?(attr) }.each do |attr,col,val|
    data = api.perform_query("ext/members/contact/?action=update", {
      'scoutid' => self.id,
      'column' => col,
      'value' => val,
      'sectionid' => section_id,
    })
    updated = updated && data.is_a?(Hash) && data['ok'].eql?(true)
  end # each attr to update

  # Do 'floating' attributes
  if force || changed_attributes.include?('gender')
    new_value = {male: 'Male', female: 'Female', other: 'Other'}[gender] || 'Unspecified'
    data = api.perform_query("ext/customdata/?action=updateColumn&section_id=#{section_id}", {
      'associated_id' => self.id,
      'associated_type' => 'member',
      'value' => new_value,
      'column_id' => CID_GENDER,
      'group_id' => GID_FLOATING,
      'context' => 'members',
    })
    updated = updated && data.is_a?(Hash) && data['data'].is_a?(Hash) && data['data']['value'].eql?(new_value)
  end

  # Do custom attributes
  additional_information.keys.select{ |a| force || additional_information.changes.keys.include?(a) }.each do |attr|
    new_value = additional_information[attr]
    data = api.perform_query("ext/customdata/?action=updateColumn&section_id=#{section_id}", {
      'associated_id' => self.id,
      'associated_type' => 'member',
      'value' => new_value,
      'column_id' => attr,
      'group_id' => GID_CUSTOM,
      'context' => 'members',
    })
    updated = updated && data.is_a?(Hash) && data['data'].is_a?(Hash) && data['data']['value'].eql?(new_value)
  end # each attr to update

  # Do contacts
  updated = (contact.nil? || contact.update(api, self, force)) && updated
  updated = (primary_contact.nil? || primary_contact.update(api, self, force)) && updated
  updated = (secondary_contact.nil? || secondary_contact.update(api, self, force)) && updated
  updated = (emergency_contact.nil? ||emergency_contact.update(api, self, force)) && updated
  updated = (doctor.nil? || doctor.update(api, self, force)) && updated

  # Finish off
  if updated
    reset_changed_attributes
    additional_information.clean_up!    # The cached columns for the members 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 updated
end

#youth?Boolean

Check if the member is in a non-leaders grouping


438
439
440
# File 'lib/osm/member.rb', line 438

def youth?
  grouping_id > 0
end