Jump to content

Module:ElementAchievementBadges

Documentation for this module may be created at Module:ElementAchievementBadges/doc

--[[
* Name: ElementAchievementBadges
* Author: Mark W. Datysgeld
* Description: Element module that renders achievement badges for Person templates and handles all badge-type achievements
* Notes: Creates blocks for Blueprint framework; loads achievement data using AchievementSystem; filters for badge-type achievements; renders badges as HTML spans with CSS classes; includes category links; protected execution with error handling
]]

local p = {}

p.elementName = "achievementBadges"

-- Load required modules
local ErrorHandling = require('Module:ErrorHandling')
local Achievements = require('Module:AchievementSystem')

-- Default configuration
p.defaultConfig = {
    containerClass = "achievement-badges-container",
    badgeClass = "achievement-badge",
    emptyText = ""  -- Empty by default when no badges exist
}

-- Create block function
function p.createBlock()
    return function(template, args)
        -- Protected execution wrapper
        local function execute()
            local frame = mw.getCurrentFrame()
            
            -- Get the current page ID
            local pageId = mw.title.getCurrentTitle().id
            if not pageId then
                -- Include debug comment when no page ID available
                return "<!-- Achievement Badges: No page ID available -->"
            end
            
            -- CRITICAL: WE MUST PRE-LOAD THE JSON DATA WITH THE FRAME BEFORE GETTING ANY ACHIEVEMENTS
            pcall(function()
                Achievements.loadData(frame)
                Achievements.loadTypes(frame)
            end)
            
            -- Get all badge-type achievements using the centralized function
            local badgeAchievements = Achievements.getBadgeAchievements(pageId, frame)
            
            -- Minimal debug info for troubleshooting
            local debugInfo = string.format(
                "<!-- Achievement Badges: pageId=%s, found=%s -->",
                pageId or "nil",
                #badgeAchievements
            )
            
            -- If no badges found, return empty or placeholder text
            if #badgeAchievements == 0 then
                -- No badges found - return empty string or configured empty text
                local emptyText = template.config.achievementBadges 
                    and template.config.achievementBadges.emptyText
                    or p.defaultConfig.emptyText
                
                return debugInfo .. emptyText
            end
            
            -- Configuration
            local containerClass = template.config.achievementBadges 
                and template.config.achievementBadges.containerClass
                or p.defaultConfig.containerClass
            
            local badgeClass = template.config.achievementBadges 
                and template.config.achievementBadges.badgeClass
                or p.defaultConfig.badgeClass
            
            -- Build the badges HTML
            local badgesHtml = {}
            
            -- Add a table row to contain the badges
            table.insert(badgesHtml, '|-\n| colspan="2" class="' .. containerClass .. '" | ')
            
            -- Add each badge
            for _, achievement in ipairs(badgeAchievements) do
                -- For each badge, create a span with appropriate classes
                local badgeType = achievement.type or ""
                if badgeType ~= "" then
                    -- Add CSS classes from achievement type (e.g., "badge-icann-fellow")
                    local badgeSpan = string.format(
                        '<span class="%s %s" title="%s"></span>',
                        badgeClass, badgeType, achievement.name or badgeType
                    )
                    table.insert(badgesHtml, badgeSpan)
                end
            end

            -- Get category links from the centralized function
            local categoryLinksString = Achievements.getCategoryLinks(badgeAchievements, frame)
            
            -- Return the complete HTML with debug info and category links
            return table.concat(badgesHtml) .. categoryLinksString .. debugInfo
        end
        
        -- Wrap with error handling
        if template._errorContext then
            return ErrorHandling.protect(
                template._errorContext,
                "ElementBlock_achievementBadges",
                execute,
                ""
            )
        else
            local ok, result = pcall(execute)
            return ok and result or "<!-- Achievement Badges: Protected execution failed -->"
        end
    end
end

return p