Class: Osm::Badge::Data

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#<, #<=, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Instance Attribute Details

#awardedDate

Returns the last stage awarded.

Returns:

  • (Date)

    the last stage awarded


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#awarded_dateDate

Returns when the badge was awarded.

Returns:

  • (Date)

    when the badge was awarded


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#badgeOsm::Badge

Returns the badge that the data belongs to.

Returns:

  • (Osm::Badge)

    the badge that the data belongs to


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#dueFixnum

Returns whether this badge is due according to OSM, number indicates stage if appropriate.

Returns:

  • (Fixnum)

    whether this badge is due according to OSM, number indicates stage if appropriate


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#first_nameFixnum

Returns the member's first name.

Returns:

  • (Fixnum)

    the member's first name


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#last_nameFixnum

Returns Ithe member's last name.

Returns:

  • (Fixnum)

    Ithe member's last name


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#member_idFixnum

Returns ID of the member this data relates to.

Returns:

  • (Fixnum)

    ID of the member this data relates to


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#requirementsDirtyHashy

Returns the data for each badge requirement.

Returns:

  • (DirtyHashy)

    the data for each badge requirement


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

#section_idFixnum

Returns the ID of the section the member belongs to.

Returns:

  • (Fixnum)

    the ID of the section the member belongs to


533
# File 'lib/osm/badge.rb', line 533

attribute :member_id, :type => Integer

Instance Method Details

#<=>(another) ⇒ Object

Compare Badge::Data based on badge, section_id then member_id


847
848
849
850
851
852
# File 'lib/osm/badge.rb', line 847

def <=>(another)
  result = self.badge <=> another.try(:badge)
  result = self.section_id <=> another.try(:section_id) if result == 0
  result = self.member_id <=> another.try(:member_id) if result == 0
  return result
end

#earntFixnum

Get what stage which has most recently been earnt (using #earnt? will tell you if it's still due (not yet awarded))

Returns:

  • (Fixnum)

    the stage which has most recently been due


652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/osm/badge.rb', line 652

def earnt
  unless badge.has_levels?
    return earnt? ? 1 : 0
  end

  levels_column = badge.level_requirement
  unless badge.show_level_letters # It's a hikes, nights type badge
    badge.levels.reverse_each do |level|
      return level if requirements[levels_column].to_i >= level
    end
  else # It's an activity type badge
    modules = modules_gained
    letters = ('a'..'z').to_a
    (awarded..badge.levels.last).reverse_each do |level|
      return level if modules.include?(letters[level - 1])
    end
  end
  return 0
end

#earnt?Boolean

Check if this badge has been earnt

Returns:

  • (Boolean)

    whether the badge has been earnt (ignores other badge's and their requirements which might be needed)


612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/osm/badge.rb', line 612

def earnt?
  if badge.has_levels?
    return earnt > awarded
  else
    return false if (due.eql?(1) && awarded.eql?(1))
    return true if (due.eql?(1) && awarded.eql?(0))

    if badge.min_modules_required > 0
      return false unless modules_gained.size >= badge.min_modules_required
    end
    if badge.min_requirements_required > 0
      return false unless total_gained >= badge.min_requirements_required
    end
    if badge.requires_modules
      # [['a'], ['b', 'c']] = a and (b or c)
      requires = badge.requires_modules.clone
      modules = modules_gained
      requires.map!{ |a| a.map{ |b| modules.include?(b) } } # Replace letters with true/false
      requires.map!{ |a| a.include?(true) } # Replace each combination with true/false
      return false if requires.include?(false) # Only earnt if all combinations are met
    end
    badge.other_requirements_required.each do |c|
      # {:id => ###, :min => #}
      if requirements.has_key?(c[:id]) # Only check it if the data is in the requirements Hash
        return false unless requirement_met?(c[:id])
        return false if requirements[c[:id]].to_i < c[:min]
      end
    end
    badge.badges_required.each do |b|
      # {:id => ###, :version => #}
      #TODO
    end
    return true
  end
end

#gained_in_modulesHash

Get the number of requirements gained in each module

Returns:

  • (Hash)

595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/osm/badge.rb', line 595

def gained_in_modules
  count = {}
  badge.modules.each do |mod|
    count[mod.id] ||= 0
    count[mod.letter] ||= 0
  end
  badge.requirements.each do |requirement|
    next unless requirement_met?(requirement.id)
    count[requirement.mod.id] += 1
    count[requirement.mod.letter] += 1
  end
  count
end

#inspectObject


854
855
856
# File 'lib/osm/badge.rb', line 854

def inspect
  Osm.inspect_instance(self, {:replace_with => {'badge' => :name}})
end

#mark_awarded(api, date = Date.today, level = due) ⇒ Boolean

Mark the badge as awarded in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • date (Date) (defaults to: Date.today)

    The date to mark the badge as awarded

  • level (Fixnum) (defaults to: due)

    The level of the badge to award (1 for non-staged badges), setting the level to 0 unawards the badge

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:

  • (ArgumentError)

726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/osm/badge.rb', line 726

def mark_awarded(api, date=Date.today, level=due)
  raise ArgumentError, 'date is not a Date' unless date.is_a?(Date)
  raise ArgumentError, 'level can not be negative' if level < 0
  section = Osm::Section.get(api, section_id)
  require_ability_to(api, :write, :badge, section)

  date_formatted = date.strftime(Osm::OSM_DATE_FORMAT)
  entries = [{
    'badge_id' => badge.id.to_s,
    'badge_version' => badge.version.to_s,
    'scout_id' => member_id.to_s,
    'level' => level.to_s
  }]

  result = api.perform_query("ext/badges/records/?action=awardBadge", {
    'date' => date_formatted,
    'sectionid' => section_id,
    'entries' => entries.to_json
  })
  updated = result.is_a?(Hash) &&
            (result['scoutid'].to_i == member_id) &&
            (result['awarded'].to_i == level) &&
            (result['awardeddate'] == date_formatted)

  if updated
    awarded = level
    awarded_date = date
  end
  return updated
end

#mark_due(api, level = completed) ⇒ Boolean

Mark the badge as due in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • level (Fixnum) (defaults to: completed)

    The level of the badge to award (1 for non-staged badges), setting the level to 0 unawards the badge

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:

  • (ArgumentError)

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/osm/badge.rb', line 769

def mark_due(api, level=completed)
  raise ArgumentError, 'level can not be negative' if level < 0
  section = Osm::Section.get(api, section_id)
  require_ability_to(api, :write, :badge, section)

  result = api.perform_query("ext/badges/records/?action=overrideCompletion", {
    'section_id' => section.id,
    'badge_id' => badge.id,
    'badge_version' => badge.version,
    'scoutid' => member_id,
    'level' => level
  })
  updated = result.is_a?(Hash) &&
            (result['scoutid'].to_i == member_id) &&
            (result['completed'].to_i == level)
  return updated
end

#mark_not_awarded(api) ⇒ Boolean

Mark the badge as not awarded in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the data was updated in OSM


760
761
762
# File 'lib/osm/badge.rb', line 760

def mark_not_awarded(api)
  mark_awarded(api, Date.today, 0)
end

#mark_not_due(api) ⇒ Boolean

Mark the badge as not due in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the data was updated in OSM


790
791
792
# File 'lib/osm/badge.rb', line 790

def mark_not_due(api)
  mark_due(api, 0)
end

#modules_gainedArray<Stirng>

Get the letters of modules gained

Returns:

  • (Array<Stirng>)

583
584
585
586
587
588
589
590
591
# File 'lib/osm/badge.rb', line 583

def modules_gained
  g_i_m = gained_in_modules
  gained = []
  badge.modules.each do |mod|
    next if g_i_m[mod.id] < mod.min_required
    gained.push mod.letter
  end
  gained
end

#requirement_met?(requirement_id) ⇒ Boolean

Work out if the requirmeent has been met

Parameters:

  • requirement_id (Fixnum, #to_i)

    The id of the requirement to evaluate (e.g. “12”, “xSomething”, “Yes” or “”)

Returns:

  • (Boolean)

    whether the requirmeent has been met


861
862
863
864
865
# File 'lib/osm/badge.rb', line 861

def requirement_met?(requirement_id)
  data = requirements[requirement_id.to_i].to_s
  return false if data == '0'
  !(data.blank? || data[0].downcase.eql?('x'))
end

#startedFixnum

Get which stage has been started

Returns:

  • (Fixnum)

    which stage of the badge has been started by the member (lowest)


689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/osm/badge.rb', line 689

def started
  unless badge.has_levels?
    return started? ? 1 : 0
  end
  unless badge.show_level_letters
    # Nights, Hikes or Water
    done = requirements[badge.level_requirement].to_i
    levels = badge.levels                    # e.g. [0,1,2,3,4,5,10]
    return 0 if levels.include?(done)        # Has achieved a level (and not started next )
    return 0 if done >= levels[-1]           # No more levels to do
    (1..(levels.size-1)).to_a.reverse_each do |i|  # indexes from last to 2nd
      this_level = levels[i]
      previous_level = levels[i-1]
      return this_level if (done < this_level && done > previous_level) # this_level has been started (and not finished)
    end
    return 0 # No reason we should ever get here
  else
    # 'Normal' staged
    letters = ('a'..'z').to_a
    top_level = badge.levels[-1]
    return 0 if due == top_level || awarded == top_level # No more levels to do
    ((due + 1)..top_level).reverse_each do |level|
      badge.requirements.each do |requirement|
        next unless requirement.mod.letter.eql?(letters[level - 1]) # Not interested in other levels
        return level if requirement_met?(requirement.id)
      end
    end
    return 0 # No levels started
  end
end

#started?Boolean

Check if this badge has been started

Returns:

  • (Boolean)

    whether the badge has been started by the member (always false if the badge has been completed)


675
676
677
678
679
680
681
682
683
684
# File 'lib/osm/badge.rb', line 675

def started?
  if badge.has_levels?
    return (started > due)
  end
  return false if due?
  requirements.each do |key, value|
    return true if requirement_met?(key)
  end
  return false
end

#total_gainedFixnum

Get the total number of gained requirements

Returns:

  • (Fixnum)

    the total number of requirements considered gained


572
573
574
575
576
577
578
579
# File 'lib/osm/badge.rb', line 572

def total_gained
  count = 0
  badge.requirements.each do |requirement|
    next unless requirement_met?(requirement.id)
    count += 1
  end
  return count
end

#update(api) ⇒ Boolean

Update data in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

Returns:

  • (Boolean)

    whether the data was updated in OSM

Raises:


798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/osm/badge.rb', line 798

def update(api)
  raise Osm::ObjectIsInvalid, 'data is invalid' unless valid?
  section = Osm::Section.get(api, section_id)
  require_ability_to(api, :write, :badge, section)

  # Update requirements that changed
  requirements_updated = true
  editable_requirements = badge.requirements.select{ |r| r.editable }.map{ |r| r.id }
  requirements.changes.each do |requirement, (was,now)|
    if editable_requirements.include?(requirement)
      result = api.perform_query("ext/badges/records/?action=updateSingleRecord", {
        'scoutid' => member_id,
        'section_id' => section_id,
        'badge_id' => badge.id,
        'badge_version' => badge.version,
        'field' => requirement,
        'value' => now
      })
      requirements_updated = false unless result.is_a?(Hash) &&
                             (result['scoutid'].to_i == member_id) &&
                             (result[requirement.to_s] == now)
    end
  end

  if requirements_updated
    requirements.clean_up!
  end

  # Update due if it changed
  due_updated = true
  if changed_attributes.include?('due')
    due_updated = mark_due(api, due)
  end

  # Update awarded if it changed 
  awarded_updated = true
  if changed_attributes.include?('awarded') || changed_attributes.include?('awarded_date')
    awarded_updated = mark_awarded(api, awarded_date, awarded)
  end

  # reset changed attributes if everything was updated ok
  if due_updated && awarded_updated
    reset_changed_attributes
  end

  return requirements_updated && due_updated && awarded_updated
end