osm 1.2.15.dev.1 → 1.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGELOG.md +224 -221
- data/lib/osm/badge.rb +204 -102
- data/spec/osm/badge_spec.rb +217 -137
- data/version.rb +1 -1
- metadata +4 -4
data/lib/osm/badge.rb
CHANGED
@@ -2,6 +2,7 @@ module Osm
|
|
2
2
|
|
3
3
|
class Badge < Osm::Model
|
4
4
|
class Requirement; end # Ensure the constant exists for the validators
|
5
|
+
class RequirementModule; end # Ensure the constant exists for the validators
|
5
6
|
|
6
7
|
# @!attribute [rw] name
|
7
8
|
# @return [String] the name of the badge
|
@@ -9,6 +10,8 @@ module Osm
|
|
9
10
|
# @return [String] a description of the badge
|
10
11
|
# @!attribute [rw] requirements
|
11
12
|
# @return [Array<Osm::Badge::Requirement>] the requirements of the badge
|
13
|
+
# @!attribute [rw] modules
|
14
|
+
# @return [Array<Hash>] Details of the modules which make up the badge
|
12
15
|
# @!attribute [rw] id
|
13
16
|
# @return [Fixnum] the badge's id in OSM
|
14
17
|
# @!attribute [rw] version
|
@@ -25,6 +28,22 @@ module Osm
|
|
25
28
|
# @return [Fixnum] the OSM user who created this (version of the) badge
|
26
29
|
# @!attribute [rw] levels
|
27
30
|
# @return [Array<Fixnum>, nil] the levels available, nil if it's a single level badge
|
31
|
+
# @!attribute [rw] min_modules_required
|
32
|
+
# @return [Fixnum] the minimum number of modules which must be completed to earn the badge
|
33
|
+
# @!attribute [rw] min_requirements_required
|
34
|
+
# @return [Fixnum] the minimum number of requirements which must be completed to earn the badge
|
35
|
+
# @!attribute [rw] add_columns_to_module
|
36
|
+
# @return [Fixnum, nil] the module to add columns to for nights away type badges
|
37
|
+
# @!attribute [rw] level_requirement
|
38
|
+
# @return [Fixnum, nil] the column which stores the currently earnt level of nights away type badges
|
39
|
+
# @!attribute [rw] requires_modules
|
40
|
+
# @return [Array<Array<String>>, nil] the module letters required to gain the badge, at least one from each inner Array
|
41
|
+
# @!attribute [rw] other_requirements_required
|
42
|
+
# @return [Array<Hash>] the requirements (from other badges) required to complete this badge, {id: field ID, min: the minimum numerical value of the field's data}
|
43
|
+
# @!attribute [rw] badges_required
|
44
|
+
# @return [Array<Hash>] the other badges required to complete this badge, {id: The ID of the badge, version: The version of the badge}
|
45
|
+
# @!attribute [rw] show_level_letters
|
46
|
+
# @return [Boolean] Whether to show letters not numbers for the levels of a staged badge
|
28
47
|
|
29
48
|
attribute :name, :type => String
|
30
49
|
attribute :requirement_notes, :type => String
|
@@ -37,22 +56,36 @@ module Osm
|
|
37
56
|
attribute :sharing, :type => Object
|
38
57
|
attribute :user_id, :type => Integer
|
39
58
|
attribute :levels, :type => Object
|
40
|
-
attribute :
|
59
|
+
attribute :modules, :type => Object
|
60
|
+
attribute :min_modules_required, :type => Integer
|
61
|
+
attribute :min_requirements_required, :type => Integer
|
62
|
+
attribute :add_columns_to_module, :type => Integer
|
63
|
+
attribute :level_requirement, :type => Integer
|
64
|
+
attribute :requires_modules, :type => Object
|
65
|
+
attribute :other_requirements_required, :type => Object
|
66
|
+
attribute :badges_required, :type => Object
|
67
|
+
attribute :show_level_letters, :type => Boolean
|
41
68
|
|
42
69
|
if ActiveModel::VERSION::MAJOR < 4
|
43
|
-
attr_accessible :name, :requirement_notes, :requirements, :id, :version, :identifier, :group_name, :latest, :sharing, :user_id, :levels, :
|
70
|
+
attr_accessible :name, :requirement_notes, :requirements, :id, :version, :identifier, :group_name, :latest, :sharing, :user_id, :levels, :modules, :min_modules_required, :min_requirements_required, :add_columns_to_module, :level_requirement, :requires_modules, :other_requirements_required, :badges_required, :show_level_letters
|
44
71
|
end
|
45
72
|
|
46
73
|
validates_presence_of :name
|
47
74
|
validates_presence_of :requirement_notes
|
48
|
-
|
49
|
-
|
75
|
+
validates_numericality_of :id, :only_integer=>true, :greater_than_or_equal_to=>1
|
76
|
+
validates_numericality_of :version, :only_integer=>true, :greater_than_or_equal_to=>0
|
50
77
|
validates_presence_of :identifier
|
51
78
|
validates_inclusion_of :sharing, :in => [:draft, :private, :optin, :optin_locked, :default_locked]
|
52
79
|
validates_presence_of :user_id
|
53
80
|
validates :requirements, :array_of => {:item_type => Osm::Badge::Requirement, :item_valid => true}
|
81
|
+
validates :modules, :array_of => {:item_type => Osm::Badge::RequirementModule, :item_valid => true}
|
54
82
|
validates_inclusion_of :latest, :in => [true, false]
|
55
83
|
validates :levels, :array_of => {:item_type => Fixnum}, :allow_nil => true
|
84
|
+
validates_numericality_of :min_modules_required, :only_integer=>true, :greater_than_or_equal_to=>0
|
85
|
+
validates_numericality_of :min_requirements_required, :only_integer=>true, :greater_than_or_equal_to=>0
|
86
|
+
validates_numericality_of :add_columns_to_module, :only_integer=>true, :greater_than=>0, :allow_nil=>true
|
87
|
+
validates_numericality_of :level_requirement, :only_integer=>true, :greater_than=>0, :allow_nil=>true
|
88
|
+
validates_inclusion_of :show_level_letters, :in => [true, false]
|
56
89
|
|
57
90
|
|
58
91
|
# @!method initialize
|
@@ -107,31 +140,32 @@ module Osm
|
|
107
140
|
:sharing => badge_sharing_map[detail['sharing']],
|
108
141
|
:user_id => Osm.to_i_or_nil(detail['userid']),
|
109
142
|
:levels => config['levelslist'],
|
110
|
-
:
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
:show_letters => !!config['shownumbers'],
|
119
|
-
},
|
143
|
+
:min_modules_required => config['numModulesRequired'].to_i,
|
144
|
+
:min_requirements_required => config['minRequirementsCompleted'].to_i,
|
145
|
+
:add_columns_to_module => Osm.to_i_or_nil(config['addcolumns']),
|
146
|
+
:level_requirement => Osm.to_i_or_nil(config['levels_column_id']),
|
147
|
+
:requires_modules => config['requires'],
|
148
|
+
:other_requirements_required => (config['columnsRequired'] || []).map{ |i| {id: Osm.to_i_or_nil(i['id']), min: i['min'].to_i} },
|
149
|
+
:badges_required => (config['badgesRequired'] || []).map{ |i| {id: Osm.to_i_or_nil(i['id']), version: i['version'].to_i} },
|
150
|
+
:show_level_letters => !!config['shownumbers'],
|
120
151
|
)
|
121
152
|
|
153
|
+
modules = module_completion_data(api, badge, options)
|
154
|
+
badge.modules = modules
|
155
|
+
modules = Hash[*modules.map{|m| [m.letter, m]}.flatten]
|
156
|
+
|
122
157
|
requirements = []
|
123
158
|
((structure[1] || {})['rows'] || []).each do |r|
|
124
159
|
requirements.push Osm::Badge::Requirement.new(
|
125
160
|
:badge => badge,
|
126
161
|
:name => r['name'],
|
127
162
|
:description => r['tooltip'],
|
128
|
-
:
|
129
|
-
:
|
163
|
+
:mod => modules[r['module']],
|
164
|
+
:id => Osm::to_i_or_nil(r['field']),
|
130
165
|
:editable => r['editable'].to_s.eql?('true'),
|
131
166
|
)
|
132
167
|
end
|
133
168
|
badge.requirements = requirements
|
134
|
-
badge.completion_criteria[:modules] = module_completion_data(api, badge, options)
|
135
169
|
|
136
170
|
badges.push badge
|
137
171
|
end
|
@@ -247,30 +281,30 @@ module Osm
|
|
247
281
|
!levels.nil?
|
248
282
|
end
|
249
283
|
|
284
|
+
def add_columns?
|
285
|
+
!add_columns_to_module.nil?
|
286
|
+
end
|
287
|
+
|
250
288
|
def module_map
|
251
289
|
@module_map ||= Hash[
|
252
|
-
*
|
253
|
-
[m
|
290
|
+
*modules.map{ |m|
|
291
|
+
[m.id, m.letter, m.letter, m.id]
|
254
292
|
}.flatten
|
255
293
|
].except('z')
|
256
294
|
end
|
257
295
|
|
258
296
|
def needed_per_module
|
259
|
-
@needed_per_module ||= Hash[*
|
260
|
-
[m
|
297
|
+
@needed_per_module ||= Hash[*modules.map{ |m|
|
298
|
+
[m.id, m.min_required, m.letter, m.min_required]
|
261
299
|
}.flatten].except('z')
|
262
300
|
end
|
263
301
|
|
264
302
|
def module_letters
|
265
|
-
@module_letters ||=
|
303
|
+
@module_letters ||= modules.map{ |m| m.letter }.sort
|
266
304
|
end
|
267
305
|
|
268
306
|
def module_ids
|
269
|
-
@module_ids ||=
|
270
|
-
end
|
271
|
-
|
272
|
-
def modules
|
273
|
-
completion_criteria[:modules] || []
|
307
|
+
@module_ids ||= modules.map{ |m| m.id }.sort
|
274
308
|
end
|
275
309
|
|
276
310
|
|
@@ -303,6 +337,8 @@ module Osm
|
|
303
337
|
end
|
304
338
|
data = data[badge.version]
|
305
339
|
raise ArgumentError, "That badge does't exist (bad version)." if data.nil?
|
340
|
+
|
341
|
+
data.each{ |i| i.badge = badge }
|
306
342
|
return data
|
307
343
|
end
|
308
344
|
|
@@ -316,25 +352,26 @@ module Osm
|
|
316
352
|
osm_data = api.perform_query('ext/badges/records/?action=_getModuleDetails')
|
317
353
|
osm_data = (osm_data || {})['items'] || []
|
318
354
|
osm_data.map! do |i|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
355
|
+
[
|
356
|
+
Osm.to_i_or_nil(i['badge_id']),
|
357
|
+
Osm.to_i_or_nil(i['badge_version']),
|
358
|
+
Osm::Badge::RequirementModule.new({
|
359
|
+
id: Osm.to_i_or_nil(i['module_id']),
|
360
|
+
letter: i['module_letter'],
|
361
|
+
min_required: i['num_required'].to_i,
|
362
|
+
custom_columns: i['custom_columns'].to_i,
|
363
|
+
completed_into_column: i['completed_into_column_id'].to_i.eql?(0) ? nil : i['completed_into_column_id'].to_i,
|
364
|
+
numeric_into_column: i['numeric_into_column_id'].to_i.eql?(0) ? nil : i['numeric_into_column_id'].to_i,
|
365
|
+
add_column_id_to_numeric: i['add_column_id_to_numeric'].to_i.eql?(0) ? nil : i['add_column_id_to_numeric'].to_i,
|
366
|
+
})
|
367
|
+
]
|
330
368
|
end
|
331
369
|
|
332
370
|
data = {}
|
333
|
-
osm_data.each do |
|
334
|
-
id, version = i.values_at(:badge_id, :badge_version)
|
371
|
+
osm_data.each do |id, version, m|
|
335
372
|
data[id] ||= []
|
336
373
|
data[id][version] ||= []
|
337
|
-
data[id][version].push
|
374
|
+
data[id][version].push m
|
338
375
|
end
|
339
376
|
|
340
377
|
cache_write(api, cache_key, data, {expires_in: 864000}) # Expire in 24 hours as this data changes really slowly
|
@@ -365,50 +402,114 @@ module Osm
|
|
365
402
|
# @!attribute [rw] badge
|
366
403
|
# @return [Osm::Badge] the badge the requirement belongs to
|
367
404
|
# @!attribute [rw] name
|
368
|
-
# @return [String] the name of the badge
|
405
|
+
# @return [String] the name of the badge requirement
|
369
406
|
# @!attribute [rw] description
|
370
|
-
# @return [String] a description of the badge
|
371
|
-
# @!attribute [rw]
|
372
|
-
# @return [Fixnum] the
|
407
|
+
# @return [String] a description of the badge requirement
|
408
|
+
# @!attribute [rw] id
|
409
|
+
# @return [Fixnum] the id for the requirement (passed to OSM)
|
410
|
+
# @!attribute [rw] mod
|
411
|
+
# @return [Osm::Badge::RequirementModule] the module the requirement belongs to
|
373
412
|
# @!attribute [rw] editable
|
374
413
|
# @return [Boolean]
|
375
414
|
|
376
415
|
attribute :badge, :type => Object
|
377
416
|
attribute :name, :type => String
|
378
417
|
attribute :description, :type => String
|
379
|
-
attribute :
|
380
|
-
attribute :
|
418
|
+
attribute :mod, :type => Object
|
419
|
+
attribute :id, :type => Integer
|
381
420
|
attribute :editable, :type => Boolean
|
382
421
|
|
383
422
|
if ActiveModel::VERSION::MAJOR < 4
|
384
|
-
attr_accessible :name, :description, :
|
423
|
+
attr_accessible :name, :description, :id, :editable, :badge, :mod
|
385
424
|
end
|
386
425
|
|
387
426
|
validates_presence_of :name
|
388
427
|
validates_presence_of :description
|
389
|
-
validates_presence_of :
|
390
|
-
|
428
|
+
validates_presence_of :mod
|
429
|
+
validates_numericality_of :id, :only_integer=>true, :greater_than=>0
|
391
430
|
validates_presence_of :badge
|
392
431
|
validates_inclusion_of :editable, :in => [true, false]
|
393
432
|
|
394
433
|
# @!method initialize
|
395
|
-
# Initialize a new Badge
|
434
|
+
# Initialize a new Badge::Requirement
|
396
435
|
# @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
397
436
|
|
398
|
-
# Compare Badge::Requirement based on badge then
|
437
|
+
# Compare Badge::Requirement based on badge then requirement
|
399
438
|
def <=>(another)
|
400
439
|
result = self.badge <=> another.try(:badge)
|
401
|
-
result = self.
|
440
|
+
result = self.id <=> another.try(:id) if result == 0
|
402
441
|
return result
|
403
442
|
end
|
404
443
|
|
405
444
|
def inspect
|
406
|
-
Osm.inspect_instance(self,
|
445
|
+
Osm.inspect_instance(self, {:replace_with => {'badge' => :identifier}})
|
407
446
|
end
|
408
447
|
|
409
448
|
end # Class Requirement
|
410
449
|
|
411
450
|
|
451
|
+
class RequirementModule
|
452
|
+
include ActiveModel::MassAssignmentSecurity if ActiveModel::VERSION::MAJOR < 4
|
453
|
+
include ActiveAttr::Model
|
454
|
+
|
455
|
+
# @!attribute [rw] badge
|
456
|
+
# @return [Osm::Badge] the badge the requirement module belongs to
|
457
|
+
# @!attribute [rw] letter
|
458
|
+
# @return [String] the letter of the module
|
459
|
+
# @!attribute [rw] id
|
460
|
+
# @return [Fixnum] the id for the module
|
461
|
+
# @!attribute [rw] min_required
|
462
|
+
# @return [Fixnum] the minimum number of requirements which must be met to achieve this module
|
463
|
+
# @!attribute [rw] custom_columns
|
464
|
+
# @return [Fixnum, nil] ?
|
465
|
+
# @!attribute [rw] completed_into_column
|
466
|
+
# @return [Fixnum, nil] ?
|
467
|
+
# @!attribute [rw] numeric_into_column
|
468
|
+
# @return [Fixnum, nil] ?
|
469
|
+
# @!attribute [rw] add_column_id_to_numeric
|
470
|
+
# @return [Fixnum, nil] ?
|
471
|
+
|
472
|
+
attribute :badge, :type => Object
|
473
|
+
attribute :letter, :type => String
|
474
|
+
attribute :id, :type => Integer
|
475
|
+
attribute :min_required, :type => Integer
|
476
|
+
attribute :custom_columns, :type => Integer
|
477
|
+
attribute :completed_into_column, :type => Integer
|
478
|
+
attribute :numeric_into_column, :type => Integer
|
479
|
+
attribute :add_column_id_to_numeric, :type => Integer
|
480
|
+
|
481
|
+
if ActiveModel::VERSION::MAJOR < 4
|
482
|
+
attr_accessible :badge, :letter, :id, :min_required, :custom_columns, :completed_into_column, :numeric_into_column, :add_column_id_to_numeric
|
483
|
+
end
|
484
|
+
|
485
|
+
validates_presence_of :badge
|
486
|
+
validates_presence_of :letter
|
487
|
+
validates_numericality_of :id, :only_integer=>true, :greater_than=>0
|
488
|
+
validates_numericality_of :min_required, :only_integer=>true, :greater_than_or_equal_to=>0
|
489
|
+
validates_numericality_of :custom_columns, :only_integer=>true, :greater_than_or_equal_to=>0, :allow_nil=>true
|
490
|
+
validates_numericality_of :completed_into_column, :only_integer=>true, :greater_than=>0, :allow_nil=>true
|
491
|
+
validates_numericality_of :numeric_into_column, :only_integer=>true, :greater_than=>0, :allow_nil=>true
|
492
|
+
validates_numericality_of :add_column_id_to_numeric, :only_integer=>true, :greater_than=>0, :allow_nil=>true
|
493
|
+
|
494
|
+
# @!method initialize
|
495
|
+
# Initialize a new Badge::RequirementModule
|
496
|
+
# @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
497
|
+
|
498
|
+
# Compare Badge::RequirementModule based on badge then letter
|
499
|
+
def <=>(another)
|
500
|
+
result = self.badge <=> another.try(:badge)
|
501
|
+
result = self.letter <=> another.try(:letter) if result == 0
|
502
|
+
result = self.id <=> another.try(:id) if result == 0
|
503
|
+
return result
|
504
|
+
end
|
505
|
+
|
506
|
+
def inspect
|
507
|
+
Osm.inspect_instance(self, {:replace_with => {'badge' => :identifier}})
|
508
|
+
end
|
509
|
+
|
510
|
+
end # Class RequirementModule
|
511
|
+
|
512
|
+
|
412
513
|
class Data < Osm::Model
|
413
514
|
# @!attribute [rw] member_id
|
414
515
|
# @return [Fixnum] ID of the member this data relates to
|
@@ -454,7 +555,7 @@ module Osm
|
|
454
555
|
|
455
556
|
|
456
557
|
# @!method initialize
|
457
|
-
# Initialize a new Badge
|
558
|
+
# Initialize a new Badge::Data
|
458
559
|
# @param [Hash] attributes The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)
|
459
560
|
# Override initialize to set @orig_attributes
|
460
561
|
old_initialize = instance_method(:initialize)
|
@@ -471,38 +572,38 @@ module Osm
|
|
471
572
|
def total_gained
|
472
573
|
count = 0
|
473
574
|
badge.requirements.each do |requirement|
|
474
|
-
next unless requirement_met?(requirement.
|
575
|
+
next unless requirement_met?(requirement.id)
|
475
576
|
count += 1
|
476
577
|
end
|
477
578
|
return count
|
478
579
|
end
|
479
580
|
|
480
|
-
# Get the
|
481
|
-
# @return [
|
581
|
+
# Get the letters of modules gained
|
582
|
+
# @return [Array<Stirng>]
|
482
583
|
def modules_gained
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
next if gained < needed[module_id]
|
489
|
-
module_letter = badge.module_map[module_id]
|
490
|
-
modules.push module_letter unless module_letter >= 'y'
|
584
|
+
g_i_m = gained_in_modules
|
585
|
+
gained = []
|
586
|
+
badge.modules.each do |mod|
|
587
|
+
next if g_i_m[mod.id] < mod.min_required
|
588
|
+
gained.push mod.letter
|
491
589
|
end
|
492
|
-
|
493
|
-
return modules
|
590
|
+
gained
|
494
591
|
end
|
495
592
|
|
496
593
|
# Get the number of requirements gained in each module
|
497
594
|
# @return [Hash]
|
498
595
|
def gained_in_modules
|
499
596
|
count = {}
|
597
|
+
badge.modules.each do |mod|
|
598
|
+
count[mod.id] ||= 0
|
599
|
+
count[mod.letter] ||= 0
|
600
|
+
end
|
500
601
|
badge.requirements.each do |requirement|
|
501
|
-
|
502
|
-
|
503
|
-
count[requirement.
|
602
|
+
next unless requirement_met?(requirement.id)
|
603
|
+
count[requirement.mod.id] += 1
|
604
|
+
count[requirement.mod.letter] += 1
|
504
605
|
end
|
505
|
-
|
606
|
+
count
|
506
607
|
end
|
507
608
|
|
508
609
|
|
@@ -515,31 +616,32 @@ module Osm
|
|
515
616
|
return false if (due.eql?(1) && awarded.eql?(1))
|
516
617
|
return true if (due.eql?(1) && awarded.eql?(0))
|
517
618
|
|
518
|
-
|
519
|
-
|
520
|
-
if criteria[:min_modules_required] > 0
|
521
|
-
earnt &= (modules_gained.size >= criteria[:min_modules_required])
|
619
|
+
if badge.min_modules_required > 0
|
620
|
+
return false unless modules_gained.size >= badge.min_modules_required
|
522
621
|
end
|
523
|
-
if
|
524
|
-
|
622
|
+
if badge.min_requirements_required > 0
|
623
|
+
return false unless total_gained >= badge.min_requirements_required
|
525
624
|
end
|
526
|
-
if
|
625
|
+
if badge.requires_modules
|
527
626
|
# [['a'], ['b', 'c']] = a and (b or c)
|
528
|
-
requires =
|
627
|
+
requires = badge.requires_modules.clone
|
529
628
|
modules = modules_gained
|
530
629
|
requires.map!{ |a| a.map{ |b| modules.include?(b) } } # Replace letters with true/false
|
531
630
|
requires.map!{ |a| a.include?(true) } # Replace each combination with true/false
|
532
|
-
|
533
|
-
end
|
534
|
-
criteria[:badges_required].each do |b|
|
535
|
-
# {:id => ###, :version => #}
|
536
|
-
#TODO
|
631
|
+
return false if requires.include?(false) # Only earnt if all combinations are met
|
537
632
|
end
|
538
|
-
|
633
|
+
badge.other_requirements_required.each do |c|
|
539
634
|
# {:id => ###, :min => #}
|
635
|
+
if requirements.has_key?(c[:id]) # Only check it if the data is in the requirements Hash
|
636
|
+
return false unless requirement_met?(c[:id])
|
637
|
+
return false if requirements[c[:id]].to_i < c[:min]
|
638
|
+
end
|
639
|
+
end
|
640
|
+
badge.badges_required.each do |b|
|
641
|
+
# {:id => ###, :version => #}
|
540
642
|
#TODO
|
541
643
|
end
|
542
|
-
return
|
644
|
+
return true
|
543
645
|
end
|
544
646
|
end
|
545
647
|
|
@@ -552,8 +654,8 @@ module Osm
|
|
552
654
|
return earnt? ? 1 : 0
|
553
655
|
end
|
554
656
|
|
555
|
-
levels_column = badge.
|
556
|
-
unless badge.
|
657
|
+
levels_column = badge.level_requirement
|
658
|
+
unless badge.show_level_letters # It's a hikes, nights type badge
|
557
659
|
badge.levels.reverse_each do |level|
|
558
660
|
return level if requirements[levels_column].to_i >= level
|
559
661
|
end
|
@@ -588,9 +690,9 @@ module Osm
|
|
588
690
|
unless badge.has_levels?
|
589
691
|
return started? ? 1 : 0
|
590
692
|
end
|
591
|
-
unless badge.
|
693
|
+
unless badge.show_level_letters
|
592
694
|
# Nights, Hikes or Water
|
593
|
-
done = requirements[badge.
|
695
|
+
done = requirements[badge.level_requirement].to_i
|
594
696
|
levels = badge.levels # e.g. [0,1,2,3,4,5,10]
|
595
697
|
return 0 if levels.include?(done) # Has achieved a level (and not started next )
|
596
698
|
return 0 if done >= levels[-1] # No more levels to do
|
@@ -607,8 +709,8 @@ module Osm
|
|
607
709
|
return 0 if due == top_level || awarded == top_level # No more levels to do
|
608
710
|
((due + 1)..top_level).reverse_each do |level|
|
609
711
|
badge.requirements.each do |requirement|
|
610
|
-
next unless requirement.
|
611
|
-
return level if requirement_met?(requirement.
|
712
|
+
next unless requirement.mod.letter.eql?(letters[level - 1]) # Not interested in other levels
|
713
|
+
return level if requirement_met?(requirement.id)
|
612
714
|
end
|
613
715
|
end
|
614
716
|
return 0 # No levels started
|
@@ -700,20 +802,20 @@ module Osm
|
|
700
802
|
|
701
803
|
# Update requirements that changed
|
702
804
|
requirements_updated = true
|
703
|
-
|
704
|
-
requirements.changes.each do |
|
705
|
-
if
|
805
|
+
editable_requirements = badge.requirements.select{ |r| r.editable }.map{ |r| r.id }
|
806
|
+
requirements.changes.each do |requirement, (was,now)|
|
807
|
+
if editable_requirements.include?(requirement)
|
706
808
|
result = api.perform_query("ext/badges/records/?action=updateSingleRecord", {
|
707
809
|
'scoutid' => member_id,
|
708
810
|
'section_id' => section_id,
|
709
811
|
'badge_id' => badge.id,
|
710
812
|
'badge_version' => badge.version,
|
711
|
-
'field' =>
|
813
|
+
'field' => requirement,
|
712
814
|
'value' => now
|
713
815
|
})
|
714
816
|
requirements_updated = false unless result.is_a?(Hash) &&
|
715
817
|
(result['scoutid'].to_i == member_id) &&
|
716
|
-
(result[
|
818
|
+
(result[requirement.to_s] == now)
|
717
819
|
end
|
718
820
|
end
|
719
821
|
|
@@ -750,14 +852,14 @@ module Osm
|
|
750
852
|
end
|
751
853
|
|
752
854
|
def inspect
|
753
|
-
Osm.inspect_instance(self,
|
855
|
+
Osm.inspect_instance(self, {:replace_with => {'badge' => :name}})
|
754
856
|
end
|
755
857
|
|
756
858
|
# Work out if the requirmeent has been met
|
757
|
-
# @param [Fixnum, #to_i]
|
859
|
+
# @param [Fixnum, #to_i] requirement_id The id of the requirement to evaluate (e.g. "12", "xSomething", "Yes" or "")
|
758
860
|
# @return [Boolean] whether the requirmeent has been met
|
759
|
-
def requirement_met?(
|
760
|
-
data = requirements[
|
861
|
+
def requirement_met?(requirement_id)
|
862
|
+
data = requirements[requirement_id.to_i].to_s
|
761
863
|
return false if data == '0'
|
762
864
|
!(data.blank? || data[0].downcase.eql?('x'))
|
763
865
|
end
|