Method: Osm::Badge.get_badges_for_section
- Defined in:
- lib/osm/badge.rb
.get_badges_for_section(api, section, section_type = nil, options = {}) ⇒ Array<Osm::Badge>
Get badges
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 |
# File 'lib/osm/badge.rb', line 101 def self.get_badges_for_section(api, section, section_type=nil, ={}) raise Error, 'This method must be called on one of the subclasses (CoreBadge, ChallengeBadge, StagedBadge or ActivityBadge)' if type.nil? require_ability_to(api, :read, :badge, section, ) section = Osm::Section.get(api, section, ) unless section.is_a?(Osm::Section) section_type ||= section.type cache_key = ['badges', section_type, type] if ![:no_cache] && Osm::Model.cache_exist?(api, cache_key) return cache_read(api, cache_key) end term_id = Osm::Term.get_current_term_for_section(api, section, ).to_i badges = [] badge_sharing_map = { 'draft' => :draft, 'private' => :private, 'optin' => :optin, 'optin-locked' => :optin_locked, 'default-locked' => :default_locked } data = api.perform_query("ext/badges/records/?action=getBadgeStructureByType§ion=#{section_type}&type_id=#{type_id}&term_id=#{term_id}§ion_id=#{section.id}") badge_order = data["badgeOrder"].to_s.split(',') structures = data["structure"] || {} details = data["details"] || {} badge_order.each do |b| structure = structures[b] detail = details[b] config = ActiveSupport::JSON.decode(detail['config'] || '{}') badge = new( :id => detail['badge_id'], :version => detail['badge_version'], :identifier => detail['badge_identifier'], :name => detail['name'], :requirement_notes => detail['description'], :group_name => detail['group_name'], :latest => detail['latest'].to_i.eql?(1), :sharing => badge_sharing_map[detail['sharing']], :user_id => Osm.to_i_or_nil(detail['userid']), :levels => config['levelslist'], :min_modules_required => config['numModulesRequired'].to_i, :min_requirements_required => config['minRequirementsCompleted'].to_i, :add_columns_to_module => Osm.to_i_or_nil(config['addcolumns']), :level_requirement => Osm.to_i_or_nil(config['levels_column_id']), :requires_modules => config['requires'], :other_requirements_required => (config['columnsRequired'] || []).map{ |i| {id: Osm.to_i_or_nil(i['id']), min: i['min'].to_i} }, :badges_required => (config['badgesRequired'] || []).map{ |i| {id: Osm.to_i_or_nil(i['id']), version: i['version'].to_i} }, :show_level_letters => !!config['shownumbers'], ) modules = module_completion_data(api, badge, ) badge.modules = modules modules = Hash[*modules.map{|m| [m.letter, m]}.flatten] requirements = [] ((structure[1] || {})['rows'] || []).each do |r| requirements.push Osm::Badge::Requirement.new( :badge => badge, :name => r['name'], :description => r['tooltip'], :mod => modules[r['module']], :id => Osm::to_i_or_nil(r['field']), :editable => r['editable'].to_s.eql?('true'), ) end badge.requirements = requirements badges.push badge end cache_write(api, cache_key, badges) return badges end |